blob: dda5cfe772f7fe857540d7a3bacba928e3f14f40 [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 Cheng23cc8702006-03-27 08:10:26 +000094 MVT::ValueType VT = NotZero.getValueType();
95 if (VT== MVT::f64) {
96 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
97 (uint64_t)-1)
98 return false;
99 } else {
100 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
101 (uint32_t)-1)
102 return false;
103 }
Evan Chenga8df1662006-03-27 06:58:47 +0000104 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000105 return false;
106
107 // Okay, we have at least one ~0 value, check to see if the rest match or are
108 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000109 for (++i; i != e; ++i)
110 if (N->getOperand(i) != NotZero &&
111 N->getOperand(i).getOpcode() != ISD::UNDEF)
112 return false;
113 return true;
114}
115
116
Evan Cheng4a147842006-03-26 09:50:58 +0000117/// isBuildVectorAllZeros - Return true if the specified node is a
118/// BUILD_VECTOR where all of the elements are 0 or undef.
119bool ISD::isBuildVectorAllZeros(const SDNode *N) {
120 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121
122 unsigned i = 0, e = N->getNumOperands();
123
124 // Skip over all of the undef values.
125 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
126 ++i;
127
128 // Do not accept an all-undef vector.
129 if (i == e) return false;
130
131 // Do not accept build_vectors that aren't all constants or which have non-~0
132 // elements.
133 SDOperand Zero = N->getOperand(i);
134 if (isa<ConstantSDNode>(Zero)) {
135 if (!cast<ConstantSDNode>(Zero)->isNullValue())
136 return false;
137 } else if (isa<ConstantFPSDNode>(Zero)) {
138 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
139 return false;
140 } else
141 return false;
142
143 // Okay, we have at least one ~0 value, check to see if the rest match or are
144 // undefs.
145 for (++i; i != e; ++i)
146 if (N->getOperand(i) != Zero &&
147 N->getOperand(i).getOpcode() != ISD::UNDEF)
148 return false;
149 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000150}
151
Chris Lattnerc3aae252005-01-07 07:46:32 +0000152/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
153/// when given the operation for (X op Y).
154ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
155 // To perform this operation, we just need to swap the L and G bits of the
156 // operation.
157 unsigned OldL = (Operation >> 2) & 1;
158 unsigned OldG = (Operation >> 1) & 1;
159 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
160 (OldL << 1) | // New G bit
161 (OldG << 2)); // New L bit.
162}
163
164/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
165/// 'op' is a valid SetCC operation.
166ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
167 unsigned Operation = Op;
168 if (isInteger)
169 Operation ^= 7; // Flip L, G, E bits, but not U.
170 else
171 Operation ^= 15; // Flip all of the condition bits.
172 if (Operation > ISD::SETTRUE2)
173 Operation &= ~8; // Don't let N and U bits get set.
174 return ISD::CondCode(Operation);
175}
176
177
178/// isSignedOp - For an integer comparison, return 1 if the comparison is a
179/// signed operation and 2 if the result is an unsigned comparison. Return zero
180/// if the operation does not depend on the sign of the input (setne and seteq).
181static int isSignedOp(ISD::CondCode Opcode) {
182 switch (Opcode) {
183 default: assert(0 && "Illegal integer setcc operation!");
184 case ISD::SETEQ:
185 case ISD::SETNE: return 0;
186 case ISD::SETLT:
187 case ISD::SETLE:
188 case ISD::SETGT:
189 case ISD::SETGE: return 1;
190 case ISD::SETULT:
191 case ISD::SETULE:
192 case ISD::SETUGT:
193 case ISD::SETUGE: return 2;
194 }
195}
196
197/// getSetCCOrOperation - Return the result of a logical OR between different
198/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
199/// returns SETCC_INVALID if it is not possible to represent the resultant
200/// comparison.
201ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
202 bool isInteger) {
203 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
204 // Cannot fold a signed integer setcc with an unsigned integer setcc.
205 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209 // If the N and U bits get set then the resultant comparison DOES suddenly
210 // care about orderedness, and is true when ordered.
211 if (Op > ISD::SETTRUE2)
212 Op &= ~16; // Clear the N bit.
213 return ISD::CondCode(Op);
214}
215
216/// getSetCCAndOperation - Return the result of a logical AND between different
217/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
218/// function returns zero if it is not possible to represent the resultant
219/// comparison.
220ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
221 bool isInteger) {
222 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
223 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000224 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000225
226 // Combine all of the condition bits.
227 return ISD::CondCode(Op1 & Op2);
228}
229
Chris Lattnerb48da392005-01-23 04:39:44 +0000230const TargetMachine &SelectionDAG::getTarget() const {
231 return TLI.getTargetMachine();
232}
233
Jim Laskey58b968b2005-08-17 20:08:02 +0000234//===----------------------------------------------------------------------===//
235// SelectionDAG Class
236//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000237
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000238/// RemoveDeadNodes - This method deletes all unreachable nodes in the
239/// SelectionDAG, including nodes (like loads) that have uses of their token
240/// chain but no other uses and no side effect. If a node is passed in as an
241/// argument, it is used as the seed for node deletion.
242void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000243 // Create a dummy node (which is not added to allnodes), that adds a reference
244 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000245 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000246
Chris Lattnerf469cb62005-11-08 18:52:27 +0000247 bool MadeChange = false;
248
Chris Lattner8b8749f2005-08-17 19:00:20 +0000249 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000250 if (N && N->use_empty()) {
251 DestroyDeadNode(N);
252 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000253 }
254
Chris Lattnerde202b32005-11-09 23:47:37 +0000255 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
256 if (I->use_empty() && I->getOpcode() != 65535) {
257 // Node is dead, recursively delete newly dead uses.
258 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000259 MadeChange = true;
260 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000261
262 // Walk the nodes list, removing the nodes we've marked as dead.
263 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000264 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
265 SDNode *N = I++;
266 if (N->use_empty())
267 AllNodes.erase(N);
268 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000269 }
270
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000272 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273}
274
Chris Lattnerf469cb62005-11-08 18:52:27 +0000275/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
276/// graph. If it is the last user of any of its operands, recursively process
277/// them the same way.
278///
279void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280 // Okay, we really are going to delete this node. First take this out of the
281 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000282 RemoveNodeFromCSEMaps(N);
283
284 // Next, brutally remove the operand list. This is safe to do, as there are
285 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000286 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
287 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000288 O->removeUser(N);
289
290 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000291 if (O->use_empty())
292 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000293 }
Chris Lattner65113b22005-11-08 22:07:03 +0000294 delete[] N->OperandList;
295 N->OperandList = 0;
296 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000297
298 // Mark the node as dead.
299 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000300}
301
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000302void SelectionDAG::DeleteNode(SDNode *N) {
303 assert(N->use_empty() && "Cannot delete a node that is not dead!");
304
305 // First take this out of the appropriate CSE map.
306 RemoveNodeFromCSEMaps(N);
307
Chris Lattner1e111c72005-09-07 05:37:01 +0000308 // Finally, remove uses due to operands of this node, remove from the
309 // AllNodes list, and delete the node.
310 DeleteNodeNotInCSEMaps(N);
311}
312
313void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
314
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000315 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000316 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000317
318 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000319 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
320 I->Val->removeUser(N);
321 delete[] N->OperandList;
322 N->OperandList = 0;
323 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000324
325 delete N;
326}
327
Chris Lattner149c58c2005-08-16 18:17:10 +0000328/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
329/// correspond to it. This is useful when we're about to delete or repurpose
330/// the node. We don't want future request for structurally identical nodes
331/// to return N anymore.
332void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000334 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000335 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000336 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000337 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
338 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000340 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 Erased = TargetConstants.erase(std::make_pair(
342 cast<ConstantSDNode>(N)->getValue(),
343 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000344 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000345 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000346 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000347 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000348 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000349 }
Chris Lattner3181a772006-01-29 06:26:56 +0000350 case ISD::TargetConstantFP: {
351 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
352 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
353 break;
354 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000355 case ISD::STRING:
356 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
357 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000358 case ISD::CONDCODE:
359 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
360 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000361 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000362 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
363 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000364 case ISD::GlobalAddress: {
365 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
366 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
367 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000369 }
370 case ISD::TargetGlobalAddress: {
371 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
372 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
373 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000374 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000375 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000377 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000378 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000379 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000380 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000381 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000382 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000383 Erased = ConstantPoolIndices.
384 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000385 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
386 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000387 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000388 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000389 Erased = TargetConstantPoolIndices.
390 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000391 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
392 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000393 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000394 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000395 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000396 break;
397 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000398 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000399 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000400 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000401 Erased =
402 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000403 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000404 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000405 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000406 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
407 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000408 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000409 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
410 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000411 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000412 case ISD::SRCVALUE: {
413 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000414 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000415 break;
416 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000417 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000418 Erased = Loads.erase(std::make_pair(N->getOperand(1),
419 std::make_pair(N->getOperand(0),
420 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000421 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000422 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000423 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000424 if (N->getNumOperands() == 0) {
425 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
426 N->getValueType(0)));
427 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000428 Erased =
429 UnaryOps.erase(std::make_pair(N->getOpcode(),
430 std::make_pair(N->getOperand(0),
431 N->getValueType(0))));
432 } else if (N->getNumOperands() == 2) {
433 Erased =
434 BinaryOps.erase(std::make_pair(N->getOpcode(),
435 std::make_pair(N->getOperand(0),
436 N->getOperand(1))));
437 } else {
438 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
439 Erased =
440 OneResultNodes.erase(std::make_pair(N->getOpcode(),
441 std::make_pair(N->getValueType(0),
442 Ops)));
443 }
Chris Lattner385328c2005-05-14 07:42:29 +0000444 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000445 // Remove the node from the ArbitraryNodes map.
446 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
447 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000448 Erased =
449 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
450 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000451 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000452 break;
453 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000454#ifndef NDEBUG
455 // Verify that the node was actually in one of the CSE maps, unless it has a
456 // flag result (which cannot be CSE'd) or is one of the special cases that are
457 // not subject to CSE.
458 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000459 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000460 N->dump();
461 assert(0 && "Node is not in map!");
462 }
463#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000464}
465
Chris Lattner8b8749f2005-08-17 19:00:20 +0000466/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
467/// has been taken out and modified in some way. If the specified node already
468/// exists in the CSE maps, do not modify the maps, but return the existing node
469/// instead. If it doesn't exist, add it and return null.
470///
471SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
472 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000473 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000474 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000475
Chris Lattner9f8cc692005-12-19 22:21:21 +0000476 // Check that remaining values produced are not flags.
477 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
478 if (N->getValueType(i) == MVT::Flag)
479 return 0; // Never CSE anything that produces a flag.
480
Chris Lattnerfe14b342005-12-01 23:14:50 +0000481 if (N->getNumValues() == 1) {
482 if (N->getNumOperands() == 1) {
483 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
484 std::make_pair(N->getOperand(0),
485 N->getValueType(0)))];
486 if (U) return U;
487 U = N;
488 } else if (N->getNumOperands() == 2) {
489 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
490 std::make_pair(N->getOperand(0),
491 N->getOperand(1)))];
492 if (B) return B;
493 B = N;
494 } else {
495 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
496 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000497 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000498 if (ORN) return ORN;
499 ORN = N;
500 }
501 } else {
502 if (N->getOpcode() == ISD::LOAD) {
503 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
504 std::make_pair(N->getOperand(0),
505 N->getValueType(0)))];
506 if (L) return L;
507 L = N;
508 } else {
509 // Remove the node from the ArbitraryNodes map.
510 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
511 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
512 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
513 std::make_pair(RV, Ops))];
514 if (AN) return AN;
515 AN = N;
516 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000517 }
518 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000519}
520
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000521/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
522/// were replaced with those specified. If this node is never memoized,
523/// return null, otherwise return a pointer to the slot it would take. If a
524/// node already exists with these operands, the slot will be non-null.
525SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000526 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000527 return 0; // Never add these nodes.
528
529 // Check that remaining values produced are not flags.
530 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
531 if (N->getValueType(i) == MVT::Flag)
532 return 0; // Never CSE anything that produces a flag.
533
534 if (N->getNumValues() == 1) {
535 return &UnaryOps[std::make_pair(N->getOpcode(),
536 std::make_pair(Op, N->getValueType(0)))];
537 } else {
538 // Remove the node from the ArbitraryNodes map.
539 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
540 std::vector<SDOperand> Ops;
541 Ops.push_back(Op);
542 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
543 std::make_pair(RV, Ops))];
544 }
545 return 0;
546}
547
548/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
549/// were replaced with those specified. If this node is never memoized,
550/// return null, otherwise return a pointer to the slot it would take. If a
551/// node already exists with these operands, the slot will be non-null.
552SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
553 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000554 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000555 return 0; // Never add these nodes.
556
557 // Check that remaining values produced are not flags.
558 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
559 if (N->getValueType(i) == MVT::Flag)
560 return 0; // Never CSE anything that produces a flag.
561
562 if (N->getNumValues() == 1) {
563 return &BinaryOps[std::make_pair(N->getOpcode(),
564 std::make_pair(Op1, Op2))];
565 } else {
566 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
567 std::vector<SDOperand> Ops;
568 Ops.push_back(Op1);
569 Ops.push_back(Op2);
570 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
571 std::make_pair(RV, Ops))];
572 }
573 return 0;
574}
575
576
577/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
578/// were replaced with those specified. If this node is never memoized,
579/// return null, otherwise return a pointer to the slot it would take. If a
580/// node already exists with these operands, the slot will be non-null.
581SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
582 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000583 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000584 return 0; // Never add these nodes.
585
586 // Check that remaining values produced are not flags.
587 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
588 if (N->getValueType(i) == MVT::Flag)
589 return 0; // Never CSE anything that produces a flag.
590
591 if (N->getNumValues() == 1) {
592 if (N->getNumOperands() == 1) {
593 return &UnaryOps[std::make_pair(N->getOpcode(),
594 std::make_pair(Ops[0],
595 N->getValueType(0)))];
596 } else if (N->getNumOperands() == 2) {
597 return &BinaryOps[std::make_pair(N->getOpcode(),
598 std::make_pair(Ops[0], Ops[1]))];
599 } else {
600 return &OneResultNodes[std::make_pair(N->getOpcode(),
601 std::make_pair(N->getValueType(0),
602 Ops))];
603 }
604 } else {
605 if (N->getOpcode() == ISD::LOAD) {
606 return &Loads[std::make_pair(Ops[1],
607 std::make_pair(Ops[0], N->getValueType(0)))];
608 } else {
609 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
610 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
611 std::make_pair(RV, Ops))];
612 }
613 }
614 return 0;
615}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000616
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000617
Chris Lattner78ec3112003-08-11 14:57:33 +0000618SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000619 while (!AllNodes.empty()) {
620 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000621 delete [] N->OperandList;
622 N->OperandList = 0;
623 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000624 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000625 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000626}
627
Chris Lattner0f2287b2005-04-13 02:38:18 +0000628SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000629 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000630 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000631 return getNode(ISD::AND, Op.getValueType(), Op,
632 getConstant(Imm, Op.getValueType()));
633}
634
Chris Lattnerc3aae252005-01-07 07:46:32 +0000635SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
636 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000637 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
638
Chris Lattnerc3aae252005-01-07 07:46:32 +0000639 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000640 if (VT != MVT::i64)
641 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000642
Chris Lattnerc3aae252005-01-07 07:46:32 +0000643 SDNode *&N = Constants[std::make_pair(Val, VT)];
644 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000645 N = new ConstantSDNode(false, Val, VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
648}
649
Chris Lattner36ce6912005-11-29 06:21:05 +0000650SDOperand SelectionDAG::getString(const std::string &Val) {
651 StringSDNode *&N = StringNodes[Val];
652 if (!N) {
653 N = new StringSDNode(Val);
654 AllNodes.push_back(N);
655 }
656 return SDOperand(N, 0);
657}
658
Chris Lattner37bfbb42005-08-17 00:34:06 +0000659SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
660 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
661 // Mask out any bits that are not valid for this constant.
662 if (VT != MVT::i64)
663 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
664
665 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
666 if (N) return SDOperand(N, 0);
667 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000668 AllNodes.push_back(N);
669 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000670}
671
Chris Lattnerc3aae252005-01-07 07:46:32 +0000672SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
673 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
674 if (VT == MVT::f32)
675 Val = (float)Val; // Mask out extra precision.
676
Chris Lattnerd8658612005-02-17 20:17:32 +0000677 // Do the map lookup using the actual bit pattern for the floating point
678 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
679 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000680 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000681 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000682 N = new ConstantFPSDNode(false, Val, VT);
683 AllNodes.push_back(N);
684 return SDOperand(N, 0);
685}
686
687SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
688 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
689 if (VT == MVT::f32)
690 Val = (float)Val; // Mask out extra precision.
691
692 // Do the map lookup using the actual bit pattern for the floating point
693 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
694 // we don't have issues with SNANs.
695 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
696 if (N) return SDOperand(N, 0);
697 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
700}
701
Chris Lattnerc3aae252005-01-07 07:46:32 +0000702SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000703 MVT::ValueType VT, int offset) {
704 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000706 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
709}
710
711SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000712 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000713 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000714 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000715 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000716 AllNodes.push_back(N);
717 return SDOperand(N, 0);
718}
719
720SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
721 SDNode *&N = FrameIndices[FI];
722 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000723 N = new FrameIndexSDNode(FI, VT, false);
724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
726}
727
728SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
729 SDNode *&N = TargetFrameIndices[FI];
730 if (N) return SDOperand(N, 0);
731 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
734}
735
Evan Chengb8973bd2006-01-31 22:23:14 +0000736SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000737 unsigned Alignment, int Offset) {
738 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
739 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000741 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000742 AllNodes.push_back(N);
743 return SDOperand(N, 0);
744}
745
Evan Chengb8973bd2006-01-31 22:23:14 +0000746SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000747 unsigned Alignment, int Offset) {
748 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
749 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000750 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000751 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752 AllNodes.push_back(N);
753 return SDOperand(N, 0);
754}
755
756SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
757 SDNode *&N = BBNodes[MBB];
758 if (N) return SDOperand(N, 0);
759 N = new BasicBlockSDNode(MBB);
760 AllNodes.push_back(N);
761 return SDOperand(N, 0);
762}
763
Chris Lattner15e4b012005-07-10 00:07:11 +0000764SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
765 if ((unsigned)VT >= ValueTypeNodes.size())
766 ValueTypeNodes.resize(VT+1);
767 if (ValueTypeNodes[VT] == 0) {
768 ValueTypeNodes[VT] = new VTSDNode(VT);
769 AllNodes.push_back(ValueTypeNodes[VT]);
770 }
771
772 return SDOperand(ValueTypeNodes[VT], 0);
773}
774
Chris Lattnerc3aae252005-01-07 07:46:32 +0000775SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
776 SDNode *&N = ExternalSymbols[Sym];
777 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000778 N = new ExternalSymbolSDNode(false, Sym, VT);
779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
781}
782
Chris Lattner809ec112006-01-28 10:09:25 +0000783SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
784 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000785 SDNode *&N = TargetExternalSymbols[Sym];
786 if (N) return SDOperand(N, 0);
787 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000788 AllNodes.push_back(N);
789 return SDOperand(N, 0);
790}
791
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000792SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
793 if ((unsigned)Cond >= CondCodeNodes.size())
794 CondCodeNodes.resize(Cond+1);
795
Chris Lattner079a27a2005-08-09 20:40:02 +0000796 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000797 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000798 AllNodes.push_back(CondCodeNodes[Cond]);
799 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000800 return SDOperand(CondCodeNodes[Cond], 0);
801}
802
Chris Lattner0fdd7682005-08-30 22:38:38 +0000803SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
804 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
805 if (!Reg) {
806 Reg = new RegisterSDNode(RegNo, VT);
807 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000808 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000809 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000810}
811
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000812SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
813 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814 // These setcc operations always fold.
815 switch (Cond) {
816 default: break;
817 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000818 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000820 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 }
822
Chris Lattner67255a12005-04-07 18:14:58 +0000823 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
824 uint64_t C2 = N2C->getValue();
825 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
826 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000827
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828 // Sign extend the operands if required
829 if (ISD::isSignedIntSetCC(Cond)) {
830 C1 = N1C->getSignExtended();
831 C2 = N2C->getSignExtended();
832 }
833
834 switch (Cond) {
835 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000836 case ISD::SETEQ: return getConstant(C1 == C2, VT);
837 case ISD::SETNE: return getConstant(C1 != C2, VT);
838 case ISD::SETULT: return getConstant(C1 < C2, VT);
839 case ISD::SETUGT: return getConstant(C1 > C2, VT);
840 case ISD::SETULE: return getConstant(C1 <= C2, VT);
841 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
842 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
843 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
844 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
845 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000846 }
Chris Lattner24673922005-04-07 18:58:54 +0000847 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000848 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000849 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
850 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
851
852 // If the comparison constant has bits in the upper part, the
853 // zero-extended value could never match.
854 if (C2 & (~0ULL << InSize)) {
855 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
856 switch (Cond) {
857 case ISD::SETUGT:
858 case ISD::SETUGE:
859 case ISD::SETEQ: return getConstant(0, VT);
860 case ISD::SETULT:
861 case ISD::SETULE:
862 case ISD::SETNE: return getConstant(1, VT);
863 case ISD::SETGT:
864 case ISD::SETGE:
865 // True if the sign bit of C2 is set.
866 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
867 case ISD::SETLT:
868 case ISD::SETLE:
869 // True if the sign bit of C2 isn't set.
870 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
871 default:
872 break;
873 }
874 }
875
876 // Otherwise, we can perform the comparison with the low bits.
877 switch (Cond) {
878 case ISD::SETEQ:
879 case ISD::SETNE:
880 case ISD::SETUGT:
881 case ISD::SETUGE:
882 case ISD::SETULT:
883 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000884 return getSetCC(VT, N1.getOperand(0),
885 getConstant(C2, N1.getOperand(0).getValueType()),
886 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000887 default:
888 break; // todo, be more careful with signed comparisons
889 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000890 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
891 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
892 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
893 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
894 MVT::ValueType ExtDstTy = N1.getValueType();
895 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000896
Chris Lattner7b2880c2005-08-24 22:44:39 +0000897 // If the extended part has any inconsistent bits, it cannot ever
898 // compare equal. In other words, they have to be all ones or all
899 // zeros.
900 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000901 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000902 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
903 return getConstant(Cond == ISD::SETNE, VT);
904
905 // Otherwise, make this a use of a zext.
906 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000907 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000908 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000909 }
910
Chris Lattner67255a12005-04-07 18:14:58 +0000911 uint64_t MinVal, MaxVal;
912 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
913 if (ISD::isSignedIntSetCC(Cond)) {
914 MinVal = 1ULL << (OperandBitSize-1);
915 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
916 MaxVal = ~0ULL >> (65-OperandBitSize);
917 else
918 MaxVal = 0;
919 } else {
920 MinVal = 0;
921 MaxVal = ~0ULL >> (64-OperandBitSize);
922 }
923
924 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
925 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
926 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
927 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000928 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
929 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000930 }
931
932 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
933 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
934 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000935 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
936 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000937 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000938
Nate Begeman72ea2812005-04-14 08:56:52 +0000939 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
940 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000941
Nate Begeman72ea2812005-04-14 08:56:52 +0000942 // Canonicalize setgt X, Min --> setne X, Min
943 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000944 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000945
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000946 // If we have setult X, 1, turn it into seteq X, 0
947 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000948 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
949 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000950 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000951 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000952 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
953 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000954
955 // If we have "setcc X, C1", check to see if we can shrink the immediate
956 // by changing cc.
957
958 // SETUGT X, SINTMAX -> SETLT X, 0
959 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
960 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000961 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000962
963 // FIXME: Implement the rest of these.
964
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000965
966 // Fold bit comparisons when we can.
967 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
968 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
969 if (ConstantSDNode *AndRHS =
970 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
971 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
972 // Perform the xform if the AND RHS is a single bit.
973 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
974 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000975 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000976 TLI.getShiftAmountTy()));
977 }
978 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
979 // (X & 8) == 8 --> (X & 8) >> 3
980 // Perform the xform if C2 is a single bit.
981 if ((C2 & (C2-1)) == 0) {
982 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000983 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000984 }
985 }
986 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 }
Chris Lattner67255a12005-04-07 18:14:58 +0000988 } else if (isa<ConstantSDNode>(N1.Val)) {
989 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000990 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000991 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000992
993 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
994 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
995 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000996
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 switch (Cond) {
998 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000999 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1000 case ISD::SETNE: return getConstant(C1 != C2, VT);
1001 case ISD::SETLT: return getConstant(C1 < C2, VT);
1002 case ISD::SETGT: return getConstant(C1 > C2, VT);
1003 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1004 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 }
1006 } else {
1007 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001008 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001009 }
1010
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001011 // Could not fold it.
1012 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013}
1014
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001016///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001018 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1019 if (!N) {
1020 N = new SDNode(Opcode, VT);
1021 AllNodes.push_back(N);
1022 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 return SDOperand(N, 0);
1024}
1025
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1027 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001028 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001029 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1031 uint64_t Val = C->getValue();
1032 switch (Opcode) {
1033 default: break;
1034 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001035 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1037 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001038 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1039 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001040 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001041 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001042 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001043 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001044 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001045 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001046 case ISD::BSWAP:
1047 switch(VT) {
1048 default: assert(0 && "Invalid bswap!"); break;
1049 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1050 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1051 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1052 }
1053 break;
1054 case ISD::CTPOP:
1055 switch(VT) {
1056 default: assert(0 && "Invalid ctpop!"); break;
1057 case MVT::i1: return getConstant(Val != 0, VT);
1058 case MVT::i8:
1059 Tmp1 = (unsigned)Val & 0xFF;
1060 return getConstant(CountPopulation_32(Tmp1), VT);
1061 case MVT::i16:
1062 Tmp1 = (unsigned)Val & 0xFFFF;
1063 return getConstant(CountPopulation_32(Tmp1), VT);
1064 case MVT::i32:
1065 return getConstant(CountPopulation_32((unsigned)Val), VT);
1066 case MVT::i64:
1067 return getConstant(CountPopulation_64(Val), VT);
1068 }
1069 case ISD::CTLZ:
1070 switch(VT) {
1071 default: assert(0 && "Invalid ctlz!"); break;
1072 case MVT::i1: return getConstant(Val == 0, VT);
1073 case MVT::i8:
1074 Tmp1 = (unsigned)Val & 0xFF;
1075 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1076 case MVT::i16:
1077 Tmp1 = (unsigned)Val & 0xFFFF;
1078 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1079 case MVT::i32:
1080 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1081 case MVT::i64:
1082 return getConstant(CountLeadingZeros_64(Val), VT);
1083 }
1084 case ISD::CTTZ:
1085 switch(VT) {
1086 default: assert(0 && "Invalid cttz!"); break;
1087 case MVT::i1: return getConstant(Val == 0, VT);
1088 case MVT::i8:
1089 Tmp1 = (unsigned)Val | 0x100;
1090 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1091 case MVT::i16:
1092 Tmp1 = (unsigned)Val | 0x10000;
1093 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1094 case MVT::i32:
1095 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1096 case MVT::i64:
1097 return getConstant(CountTrailingZeros_64(Val), VT);
1098 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 }
1100 }
1101
Chris Lattner94683772005-12-23 05:30:37 +00001102 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1104 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001105 case ISD::FNEG:
1106 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001107 case ISD::FABS:
1108 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001109 case ISD::FP_ROUND:
1110 case ISD::FP_EXTEND:
1111 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001112 case ISD::FP_TO_SINT:
1113 return getConstant((int64_t)C->getValue(), VT);
1114 case ISD::FP_TO_UINT:
1115 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001116 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001117 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001118 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001119 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001120 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001121 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 }
1123
1124 unsigned OpOpcode = Operand.Val->getOpcode();
1125 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001126 case ISD::TokenFactor:
1127 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128 case ISD::SIGN_EXTEND:
1129 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001130 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1132 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1133 break;
1134 case ISD::ZERO_EXTEND:
1135 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001136 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001137 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001138 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001139 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001140 case ISD::ANY_EXTEND:
1141 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001142 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001143 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1144 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1145 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1146 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 case ISD::TRUNCATE:
1148 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001149 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001150 if (OpOpcode == ISD::TRUNCATE)
1151 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001152 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1153 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001154 // If the source is smaller than the dest, we still need an extend.
1155 if (Operand.Val->getOperand(0).getValueType() < VT)
1156 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1157 else if (Operand.Val->getOperand(0).getValueType() > VT)
1158 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1159 else
1160 return Operand.Val->getOperand(0);
1161 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 break;
Chris Lattner35481892005-12-23 00:16:34 +00001163 case ISD::BIT_CONVERT:
1164 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001165 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001166 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001167 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001168 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1169 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001170 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001171 case ISD::SCALAR_TO_VECTOR:
1172 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1173 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1174 "Illegal SCALAR_TO_VECTOR node!");
1175 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001176 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001177 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1178 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001179 Operand.Val->getOperand(0));
1180 if (OpOpcode == ISD::FNEG) // --X -> X
1181 return Operand.Val->getOperand(0);
1182 break;
1183 case ISD::FABS:
1184 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1185 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1186 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001187 }
1188
Chris Lattner43247a12005-08-25 19:12:10 +00001189 SDNode *N;
1190 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1191 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001192 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001193 E = N = new SDNode(Opcode, Operand);
1194 } else {
1195 N = new SDNode(Opcode, Operand);
1196 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197 N->setValueTypes(VT);
1198 AllNodes.push_back(N);
1199 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001200}
1201
Chris Lattner36019aa2005-04-18 03:48:41 +00001202
1203
Chris Lattnerc3aae252005-01-07 07:46:32 +00001204SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1205 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001206#ifndef NDEBUG
1207 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001208 case ISD::TokenFactor:
1209 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1210 N2.getValueType() == MVT::Other && "Invalid token factor!");
1211 break;
Chris Lattner76365122005-01-16 02:23:22 +00001212 case ISD::AND:
1213 case ISD::OR:
1214 case ISD::XOR:
1215 case ISD::UDIV:
1216 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001217 case ISD::MULHU:
1218 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001219 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1220 // fall through
1221 case ISD::ADD:
1222 case ISD::SUB:
1223 case ISD::MUL:
1224 case ISD::SDIV:
1225 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001226 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1227 // fall through.
1228 case ISD::FADD:
1229 case ISD::FSUB:
1230 case ISD::FMUL:
1231 case ISD::FDIV:
1232 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001233 assert(N1.getValueType() == N2.getValueType() &&
1234 N1.getValueType() == VT && "Binary operator types must match!");
1235 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001236 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1237 assert(N1.getValueType() == VT &&
1238 MVT::isFloatingPoint(N1.getValueType()) &&
1239 MVT::isFloatingPoint(N2.getValueType()) &&
1240 "Invalid FCOPYSIGN!");
1241 break;
Chris Lattner76365122005-01-16 02:23:22 +00001242 case ISD::SHL:
1243 case ISD::SRA:
1244 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001245 case ISD::ROTL:
1246 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001247 assert(VT == N1.getValueType() &&
1248 "Shift operators return type must be the same as their first arg");
1249 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001250 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001251 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001252 case ISD::FP_ROUND_INREG: {
1253 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1254 assert(VT == N1.getValueType() && "Not an inreg round!");
1255 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1256 "Cannot FP_ROUND_INREG integer types");
1257 assert(EVT <= VT && "Not rounding down!");
1258 break;
1259 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001260 case ISD::AssertSext:
1261 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 case ISD::SIGN_EXTEND_INREG: {
1263 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1264 assert(VT == N1.getValueType() && "Not an inreg extend!");
1265 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1266 "Cannot *_EXTEND_INREG FP types");
1267 assert(EVT <= VT && "Not extending!");
1268 }
1269
Chris Lattner76365122005-01-16 02:23:22 +00001270 default: break;
1271 }
1272#endif
1273
Chris Lattnerc3aae252005-01-07 07:46:32 +00001274 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1275 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1276 if (N1C) {
1277 if (N2C) {
1278 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1279 switch (Opcode) {
1280 case ISD::ADD: return getConstant(C1 + C2, VT);
1281 case ISD::SUB: return getConstant(C1 - C2, VT);
1282 case ISD::MUL: return getConstant(C1 * C2, VT);
1283 case ISD::UDIV:
1284 if (C2) return getConstant(C1 / C2, VT);
1285 break;
1286 case ISD::UREM :
1287 if (C2) return getConstant(C1 % C2, VT);
1288 break;
1289 case ISD::SDIV :
1290 if (C2) return getConstant(N1C->getSignExtended() /
1291 N2C->getSignExtended(), VT);
1292 break;
1293 case ISD::SREM :
1294 if (C2) return getConstant(N1C->getSignExtended() %
1295 N2C->getSignExtended(), VT);
1296 break;
1297 case ISD::AND : return getConstant(C1 & C2, VT);
1298 case ISD::OR : return getConstant(C1 | C2, VT);
1299 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001300 case ISD::SHL : return getConstant(C1 << C2, VT);
1301 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001302 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001303 case ISD::ROTL :
1304 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1305 VT);
1306 case ISD::ROTR :
1307 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1308 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309 default: break;
1310 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 } else { // Cannonicalize constant to RHS if commutative
1312 if (isCommutativeBinOp(Opcode)) {
1313 std::swap(N1C, N2C);
1314 std::swap(N1, N2);
1315 }
1316 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 }
1318
1319 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1320 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001321 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001322 if (N2CFP) {
1323 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1324 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001325 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1326 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1327 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1328 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001329 if (C2) return getConstantFP(C1 / C2, VT);
1330 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001331 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 if (C2) return getConstantFP(fmod(C1, C2), VT);
1333 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001334 case ISD::FCOPYSIGN: {
1335 union {
1336 double F;
1337 uint64_t I;
1338 } u1;
1339 union {
1340 double F;
1341 int64_t I;
1342 } u2;
1343 u1.F = C1;
1344 u2.F = C2;
1345 if (u2.I < 0) // Sign bit of RHS set?
1346 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1347 else
1348 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1349 return getConstantFP(u1.F, VT);
1350 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001351 default: break;
1352 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001353 } else { // Cannonicalize constant to RHS if commutative
1354 if (isCommutativeBinOp(Opcode)) {
1355 std::swap(N1CFP, N2CFP);
1356 std::swap(N1, N2);
1357 }
1358 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001359 }
1360
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 // Finally, fold operations that do not require constants.
1362 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001363 case ISD::FP_ROUND_INREG:
1364 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1365 break;
1366 case ISD::SIGN_EXTEND_INREG: {
1367 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1368 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001369 break;
1370 }
1371
Nate Begemaneea805e2005-04-13 21:23:31 +00001372 // FIXME: figure out how to safely handle things like
1373 // int foo(int x) { return 1 << (x & 255); }
1374 // int bar() { return foo(256); }
1375#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001376 case ISD::SHL:
1377 case ISD::SRL:
1378 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001379 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001380 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001381 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001382 else if (N2.getOpcode() == ISD::AND)
1383 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1384 // If the and is only masking out bits that cannot effect the shift,
1385 // eliminate the and.
1386 unsigned NumBits = MVT::getSizeInBits(VT);
1387 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1388 return getNode(Opcode, VT, N1, N2.getOperand(0));
1389 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001390 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001391#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001392 }
1393
Chris Lattner27e9b412005-05-11 18:57:39 +00001394 // Memoize this node if possible.
1395 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001396 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001397 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1398 if (BON) return SDOperand(BON, 0);
1399
1400 BON = N = new SDNode(Opcode, N1, N2);
1401 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001402 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001403 }
1404
Chris Lattner3e011362005-05-14 07:45:46 +00001405 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001406 AllNodes.push_back(N);
1407 return SDOperand(N, 0);
1408}
1409
Chris Lattnerc3aae252005-01-07 07:46:32 +00001410SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1411 SDOperand N1, SDOperand N2, SDOperand N3) {
1412 // Perform various simplifications.
1413 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1414 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1415 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1416 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001417 case ISD::SETCC: {
1418 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001419 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001420 if (Simp.Val) return Simp;
1421 break;
1422 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001423 case ISD::SELECT:
1424 if (N1C)
1425 if (N1C->getValue())
1426 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001427 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001428 return N3; // select false, X, Y -> Y
1429
1430 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001431 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001432 case ISD::BRCOND:
1433 if (N2C)
1434 if (N2C->getValue()) // Unconditional branch
1435 return getNode(ISD::BR, MVT::Other, N1, N3);
1436 else
1437 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001438 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001439 case ISD::VECTOR_SHUFFLE:
1440 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1441 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1442 N3.getOpcode() == ISD::BUILD_VECTOR &&
1443 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1444 "Illegal VECTOR_SHUFFLE node!");
1445 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001446 }
1447
Chris Lattner385328c2005-05-14 07:42:29 +00001448 std::vector<SDOperand> Ops;
1449 Ops.reserve(3);
1450 Ops.push_back(N1);
1451 Ops.push_back(N2);
1452 Ops.push_back(N3);
1453
Chris Lattner43247a12005-08-25 19:12:10 +00001454 // Memoize node if it doesn't produce a flag.
1455 SDNode *N;
1456 if (VT != MVT::Flag) {
1457 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1458 if (E) return SDOperand(E, 0);
1459 E = N = new SDNode(Opcode, N1, N2, N3);
1460 } else {
1461 N = new SDNode(Opcode, N1, N2, N3);
1462 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001463 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 AllNodes.push_back(N);
1465 return SDOperand(N, 0);
1466}
1467
1468SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001469 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001470 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001471 std::vector<SDOperand> Ops;
1472 Ops.reserve(4);
1473 Ops.push_back(N1);
1474 Ops.push_back(N2);
1475 Ops.push_back(N3);
1476 Ops.push_back(N4);
1477 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001478}
1479
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001480SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1481 SDOperand N1, SDOperand N2, SDOperand N3,
1482 SDOperand N4, SDOperand N5) {
1483 std::vector<SDOperand> Ops;
1484 Ops.reserve(5);
1485 Ops.push_back(N1);
1486 Ops.push_back(N2);
1487 Ops.push_back(N3);
1488 Ops.push_back(N4);
1489 Ops.push_back(N5);
1490 return getNode(Opcode, VT, Ops);
1491}
1492
Evan Cheng7038daf2005-12-10 00:37:58 +00001493SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1494 SDOperand Chain, SDOperand Ptr,
1495 SDOperand SV) {
1496 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1497 if (N) return SDOperand(N, 0);
1498 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1499
1500 // Loads have a token chain.
1501 setNodeValueTypes(N, VT, MVT::Other);
1502 AllNodes.push_back(N);
1503 return SDOperand(N, 0);
1504}
1505
1506SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1507 SDOperand Chain, SDOperand Ptr,
1508 SDOperand SV) {
1509 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1510 if (N) return SDOperand(N, 0);
1511 std::vector<SDOperand> Ops;
1512 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001513 Ops.push_back(Chain);
1514 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001515 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001516 Ops.push_back(getConstant(Count, MVT::i32));
1517 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001518 std::vector<MVT::ValueType> VTs;
1519 VTs.reserve(2);
1520 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1521 return getNode(ISD::VLOAD, VTs, Ops);
1522}
1523
1524SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1525 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1526 MVT::ValueType EVT) {
1527 std::vector<SDOperand> Ops;
1528 Ops.reserve(4);
1529 Ops.push_back(Chain);
1530 Ops.push_back(Ptr);
1531 Ops.push_back(SV);
1532 Ops.push_back(getValueType(EVT));
1533 std::vector<MVT::ValueType> VTs;
1534 VTs.reserve(2);
1535 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1536 return getNode(Opcode, VTs, Ops);
1537}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001538
Chris Lattner0437cdd2005-05-09 04:14:13 +00001539SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001540 assert((!V || isa<PointerType>(V->getType())) &&
1541 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001542 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1543 if (N) return SDOperand(N, 0);
1544
1545 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001546 AllNodes.push_back(N);
1547 return SDOperand(N, 0);
1548}
1549
Nate Begemanacc398c2006-01-25 18:21:52 +00001550SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1551 SDOperand Chain, SDOperand Ptr,
1552 SDOperand SV) {
1553 std::vector<SDOperand> Ops;
1554 Ops.reserve(3);
1555 Ops.push_back(Chain);
1556 Ops.push_back(Ptr);
1557 Ops.push_back(SV);
1558 std::vector<MVT::ValueType> VTs;
1559 VTs.reserve(2);
1560 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1561 return getNode(ISD::VAARG, VTs, Ops);
1562}
1563
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001564SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001565 std::vector<SDOperand> &Ops) {
1566 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001568 case 1: return getNode(Opcode, VT, Ops[0]);
1569 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1570 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001571 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001573
Chris Lattner89c34632005-05-14 06:20:26 +00001574 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001575 switch (Opcode) {
1576 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001577 case ISD::TRUNCSTORE: {
1578 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1579 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1580#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1581 // If this is a truncating store of a constant, convert to the desired type
1582 // and store it instead.
1583 if (isa<Constant>(Ops[0])) {
1584 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1585 if (isa<Constant>(Op))
1586 N1 = Op;
1587 }
1588 // Also for ConstantFP?
1589#endif
1590 if (Ops[0].getValueType() == EVT) // Normal store?
1591 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1592 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1593 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1594 "Can't do FP-INT conversion!");
1595 break;
1596 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001597 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001598 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001599 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1600 "LHS and RHS of condition must have same type!");
1601 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1602 "True and False arms of SelectCC must have same type!");
1603 assert(Ops[2].getValueType() == VT &&
1604 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001605 break;
1606 }
1607 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001608 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001609 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1610 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001611 break;
1612 }
Chris Lattneref847df2005-04-09 03:27:28 +00001613 }
1614
Chris Lattner385328c2005-05-14 07:42:29 +00001615 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001616 SDNode *N;
1617 if (VT != MVT::Flag) {
1618 SDNode *&E =
1619 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1620 if (E) return SDOperand(E, 0);
1621 E = N = new SDNode(Opcode, Ops);
1622 } else {
1623 N = new SDNode(Opcode, Ops);
1624 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001625 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001626 AllNodes.push_back(N);
1627 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001628}
1629
Chris Lattner89c34632005-05-14 06:20:26 +00001630SDOperand SelectionDAG::getNode(unsigned Opcode,
1631 std::vector<MVT::ValueType> &ResultTys,
1632 std::vector<SDOperand> &Ops) {
1633 if (ResultTys.size() == 1)
1634 return getNode(Opcode, ResultTys[0], Ops);
1635
Chris Lattner5f056bf2005-07-10 01:55:33 +00001636 switch (Opcode) {
1637 case ISD::EXTLOAD:
1638 case ISD::SEXTLOAD:
1639 case ISD::ZEXTLOAD: {
1640 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1641 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1642 // If they are asking for an extending load from/to the same thing, return a
1643 // normal load.
1644 if (ResultTys[0] == EVT)
1645 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001646 if (MVT::isVector(ResultTys[0])) {
1647 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1648 "Invalid vector extload!");
1649 } else {
1650 assert(EVT < ResultTys[0] &&
1651 "Should only be an extending load, not truncating!");
1652 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001653 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001654 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001655 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1656 "Cannot convert from FP to Int or Int -> FP!");
1657 break;
1658 }
1659
Chris Lattnere89083a2005-05-14 07:25:05 +00001660 // FIXME: figure out how to safely handle things like
1661 // int foo(int x) { return 1 << (x & 255); }
1662 // int bar() { return foo(256); }
1663#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001664 case ISD::SRA_PARTS:
1665 case ISD::SRL_PARTS:
1666 case ISD::SHL_PARTS:
1667 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001668 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001669 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1670 else if (N3.getOpcode() == ISD::AND)
1671 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1672 // If the and is only masking out bits that cannot effect the shift,
1673 // eliminate the and.
1674 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1675 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1676 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1677 }
1678 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001679#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001680 }
Chris Lattner89c34632005-05-14 06:20:26 +00001681
Chris Lattner43247a12005-08-25 19:12:10 +00001682 // Memoize the node unless it returns a flag.
1683 SDNode *N;
1684 if (ResultTys.back() != MVT::Flag) {
1685 SDNode *&E =
1686 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1687 if (E) return SDOperand(E, 0);
1688 E = N = new SDNode(Opcode, Ops);
1689 } else {
1690 N = new SDNode(Opcode, Ops);
1691 }
Chris Lattnera3255112005-11-08 23:30:28 +00001692 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001693 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001694 return SDOperand(N, 0);
1695}
1696
Chris Lattnera3255112005-11-08 23:30:28 +00001697void SelectionDAG::setNodeValueTypes(SDNode *N,
1698 std::vector<MVT::ValueType> &RetVals) {
1699 switch (RetVals.size()) {
1700 case 0: return;
1701 case 1: N->setValueTypes(RetVals[0]); return;
1702 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1703 default: break;
1704 }
1705
1706 std::list<std::vector<MVT::ValueType> >::iterator I =
1707 std::find(VTList.begin(), VTList.end(), RetVals);
1708 if (I == VTList.end()) {
1709 VTList.push_front(RetVals);
1710 I = VTList.begin();
1711 }
1712
1713 N->setValueTypes(&(*I)[0], I->size());
1714}
1715
1716void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1717 MVT::ValueType VT2) {
1718 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1719 E = VTList.end(); I != E; ++I) {
1720 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1721 N->setValueTypes(&(*I)[0], 2);
1722 return;
1723 }
1724 }
1725 std::vector<MVT::ValueType> V;
1726 V.push_back(VT1);
1727 V.push_back(VT2);
1728 VTList.push_front(V);
1729 N->setValueTypes(&(*VTList.begin())[0], 2);
1730}
1731
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001732/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1733/// specified operands. If the resultant node already exists in the DAG,
1734/// this does not modify the specified node, instead it returns the node that
1735/// already exists. If the resultant node does not exist in the DAG, the
1736/// input node is returned. As a degenerate case, if you specify the same
1737/// input operands as the node already has, the input node is returned.
1738SDOperand SelectionDAG::
1739UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1740 SDNode *N = InN.Val;
1741 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1742
1743 // Check to see if there is no change.
1744 if (Op == N->getOperand(0)) return InN;
1745
1746 // See if the modified node already exists.
1747 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1748 if (NewSlot && *NewSlot)
1749 return SDOperand(*NewSlot, InN.ResNo);
1750
1751 // Nope it doesn't. Remove the node from it's current place in the maps.
1752 if (NewSlot)
1753 RemoveNodeFromCSEMaps(N);
1754
1755 // Now we update the operands.
1756 N->OperandList[0].Val->removeUser(N);
1757 Op.Val->addUser(N);
1758 N->OperandList[0] = Op;
1759
1760 // If this gets put into a CSE map, add it.
1761 if (NewSlot) *NewSlot = N;
1762 return InN;
1763}
1764
1765SDOperand SelectionDAG::
1766UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1767 SDNode *N = InN.Val;
1768 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1769
1770 // Check to see if there is no change.
1771 bool AnyChange = false;
1772 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1773 return InN; // No operands changed, just return the input node.
1774
1775 // See if the modified node already exists.
1776 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1777 if (NewSlot && *NewSlot)
1778 return SDOperand(*NewSlot, InN.ResNo);
1779
1780 // Nope it doesn't. Remove the node from it's current place in the maps.
1781 if (NewSlot)
1782 RemoveNodeFromCSEMaps(N);
1783
1784 // Now we update the operands.
1785 if (N->OperandList[0] != Op1) {
1786 N->OperandList[0].Val->removeUser(N);
1787 Op1.Val->addUser(N);
1788 N->OperandList[0] = Op1;
1789 }
1790 if (N->OperandList[1] != Op2) {
1791 N->OperandList[1].Val->removeUser(N);
1792 Op2.Val->addUser(N);
1793 N->OperandList[1] = Op2;
1794 }
1795
1796 // If this gets put into a CSE map, add it.
1797 if (NewSlot) *NewSlot = N;
1798 return InN;
1799}
1800
1801SDOperand SelectionDAG::
1802UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1803 std::vector<SDOperand> Ops;
1804 Ops.push_back(Op1);
1805 Ops.push_back(Op2);
1806 Ops.push_back(Op3);
1807 return UpdateNodeOperands(N, Ops);
1808}
1809
1810SDOperand SelectionDAG::
1811UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1812 SDOperand Op3, SDOperand Op4) {
1813 std::vector<SDOperand> Ops;
1814 Ops.push_back(Op1);
1815 Ops.push_back(Op2);
1816 Ops.push_back(Op3);
1817 Ops.push_back(Op4);
1818 return UpdateNodeOperands(N, Ops);
1819}
1820
1821SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001822UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1823 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1824 std::vector<SDOperand> Ops;
1825 Ops.push_back(Op1);
1826 Ops.push_back(Op2);
1827 Ops.push_back(Op3);
1828 Ops.push_back(Op4);
1829 Ops.push_back(Op5);
1830 return UpdateNodeOperands(N, Ops);
1831}
1832
1833
1834SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001835UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1836 SDNode *N = InN.Val;
1837 assert(N->getNumOperands() == Ops.size() &&
1838 "Update with wrong number of operands");
1839
1840 // Check to see if there is no change.
1841 unsigned NumOps = Ops.size();
1842 bool AnyChange = false;
1843 for (unsigned i = 0; i != NumOps; ++i) {
1844 if (Ops[i] != N->getOperand(i)) {
1845 AnyChange = true;
1846 break;
1847 }
1848 }
1849
1850 // No operands changed, just return the input node.
1851 if (!AnyChange) return InN;
1852
1853 // See if the modified node already exists.
1854 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1855 if (NewSlot && *NewSlot)
1856 return SDOperand(*NewSlot, InN.ResNo);
1857
1858 // Nope it doesn't. Remove the node from it's current place in the maps.
1859 if (NewSlot)
1860 RemoveNodeFromCSEMaps(N);
1861
1862 // Now we update the operands.
1863 for (unsigned i = 0; i != NumOps; ++i) {
1864 if (N->OperandList[i] != Ops[i]) {
1865 N->OperandList[i].Val->removeUser(N);
1866 Ops[i].Val->addUser(N);
1867 N->OperandList[i] = Ops[i];
1868 }
1869 }
1870
1871 // If this gets put into a CSE map, add it.
1872 if (NewSlot) *NewSlot = N;
1873 return InN;
1874}
1875
1876
1877
Chris Lattner149c58c2005-08-16 18:17:10 +00001878
1879/// SelectNodeTo - These are used for target selectors to *mutate* the
1880/// specified node to have the specified return type, Target opcode, and
1881/// operands. Note that target opcodes are stored as
1882/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001883///
1884/// Note that SelectNodeTo returns the resultant node. If there is already a
1885/// node of the specified opcode and operands, it returns that node instead of
1886/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001887SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1888 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001889 // If an identical node already exists, use it.
1890 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1891 if (ON) return SDOperand(ON, 0);
1892
Chris Lattner7651fa42005-08-24 23:00:29 +00001893 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001894
Chris Lattner7651fa42005-08-24 23:00:29 +00001895 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1896 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001897
1898 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001899 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001900}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001901
Chris Lattnereb19e402005-11-30 22:45:14 +00001902SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1903 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001904 // If an identical node already exists, use it.
1905 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1906 std::make_pair(Op1, VT))];
1907 if (ON) return SDOperand(ON, 0);
1908
Chris Lattner149c58c2005-08-16 18:17:10 +00001909 RemoveNodeFromCSEMaps(N);
1910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1911 N->setValueTypes(VT);
1912 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001913
1914 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001915 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001916}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001917
Chris Lattnereb19e402005-11-30 22:45:14 +00001918SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1919 MVT::ValueType VT, SDOperand Op1,
1920 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001921 // If an identical node already exists, use it.
1922 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1923 std::make_pair(Op1, Op2))];
1924 if (ON) return SDOperand(ON, 0);
1925
Chris Lattner149c58c2005-08-16 18:17:10 +00001926 RemoveNodeFromCSEMaps(N);
1927 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1928 N->setValueTypes(VT);
1929 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001930
1931 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001932 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001933}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001934
Chris Lattnereb19e402005-11-30 22:45:14 +00001935SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1936 MVT::ValueType VT, SDOperand Op1,
1937 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001938 // If an identical node already exists, use it.
1939 std::vector<SDOperand> OpList;
1940 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1941 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1942 std::make_pair(VT, OpList))];
1943 if (ON) return SDOperand(ON, 0);
1944
Chris Lattner149c58c2005-08-16 18:17:10 +00001945 RemoveNodeFromCSEMaps(N);
1946 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1947 N->setValueTypes(VT);
1948 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001949
1950 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001951 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001952}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001953
Chris Lattnereb19e402005-11-30 22:45:14 +00001954SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1955 MVT::ValueType VT, SDOperand Op1,
1956 SDOperand Op2, SDOperand Op3,
1957 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001958 // If an identical node already exists, use it.
1959 std::vector<SDOperand> OpList;
1960 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1961 OpList.push_back(Op4);
1962 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1963 std::make_pair(VT, OpList))];
1964 if (ON) return SDOperand(ON, 0);
1965
Nate Begeman294a0a12005-08-18 07:30:15 +00001966 RemoveNodeFromCSEMaps(N);
1967 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1968 N->setValueTypes(VT);
1969 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001970
1971 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001972 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001973}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001974
Chris Lattnereb19e402005-11-30 22:45:14 +00001975SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1976 MVT::ValueType VT, SDOperand Op1,
1977 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1978 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001979 // If an identical node already exists, use it.
1980 std::vector<SDOperand> OpList;
1981 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1982 OpList.push_back(Op4); OpList.push_back(Op5);
1983 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1984 std::make_pair(VT, OpList))];
1985 if (ON) return SDOperand(ON, 0);
1986
Chris Lattner6b09a292005-08-21 18:49:33 +00001987 RemoveNodeFromCSEMaps(N);
1988 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1989 N->setValueTypes(VT);
1990 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001991
1992 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001993 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001994}
Chris Lattner149c58c2005-08-16 18:17:10 +00001995
Chris Lattnereb19e402005-11-30 22:45:14 +00001996SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1997 MVT::ValueType VT, SDOperand Op1,
1998 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1999 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002000 // If an identical node already exists, use it.
2001 std::vector<SDOperand> OpList;
2002 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2003 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2004 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2005 std::make_pair(VT, OpList))];
2006 if (ON) return SDOperand(ON, 0);
2007
Evan Cheng61ca74b2005-11-30 02:04:11 +00002008 RemoveNodeFromCSEMaps(N);
2009 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2010 N->setValueTypes(VT);
2011 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002012
2013 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002014 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002015}
2016
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002017SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2018 MVT::ValueType VT, SDOperand Op1,
2019 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2020 SDOperand Op5, SDOperand Op6,
2021 SDOperand Op7) {
2022 // If an identical node already exists, use it.
2023 std::vector<SDOperand> OpList;
2024 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2025 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2026 OpList.push_back(Op7);
2027 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2028 std::make_pair(VT, OpList))];
2029 if (ON) return SDOperand(ON, 0);
2030
2031 RemoveNodeFromCSEMaps(N);
2032 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2033 N->setValueTypes(VT);
2034 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2035
2036 ON = N; // Memoize the new node.
2037 return SDOperand(N, 0);
2038}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002039SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2040 MVT::ValueType VT, SDOperand Op1,
2041 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2042 SDOperand Op5, SDOperand Op6,
2043 SDOperand Op7, SDOperand Op8) {
2044 // If an identical node already exists, use it.
2045 std::vector<SDOperand> OpList;
2046 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2047 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2048 OpList.push_back(Op7); OpList.push_back(Op8);
2049 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2050 std::make_pair(VT, OpList))];
2051 if (ON) return SDOperand(ON, 0);
2052
2053 RemoveNodeFromCSEMaps(N);
2054 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2055 N->setValueTypes(VT);
2056 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2057
2058 ON = N; // Memoize the new node.
2059 return SDOperand(N, 0);
2060}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002061
Chris Lattnereb19e402005-11-30 22:45:14 +00002062SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2063 MVT::ValueType VT1, MVT::ValueType VT2,
2064 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002065 // If an identical node already exists, use it.
2066 std::vector<SDOperand> OpList;
2067 OpList.push_back(Op1); OpList.push_back(Op2);
2068 std::vector<MVT::ValueType> VTList;
2069 VTList.push_back(VT1); VTList.push_back(VT2);
2070 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2071 std::make_pair(VTList, OpList))];
2072 if (ON) return SDOperand(ON, 0);
2073
Chris Lattner0fb094f2005-11-19 01:44:53 +00002074 RemoveNodeFromCSEMaps(N);
2075 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2076 setNodeValueTypes(N, VT1, VT2);
2077 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002078
2079 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002080 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002081}
2082
Chris Lattnereb19e402005-11-30 22:45:14 +00002083SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2084 MVT::ValueType VT1, MVT::ValueType VT2,
2085 SDOperand Op1, SDOperand Op2,
2086 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002087 // If an identical node already exists, use it.
2088 std::vector<SDOperand> OpList;
2089 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2090 std::vector<MVT::ValueType> VTList;
2091 VTList.push_back(VT1); VTList.push_back(VT2);
2092 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2093 std::make_pair(VTList, OpList))];
2094 if (ON) return SDOperand(ON, 0);
2095
Chris Lattner0fb094f2005-11-19 01:44:53 +00002096 RemoveNodeFromCSEMaps(N);
2097 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2098 setNodeValueTypes(N, VT1, VT2);
2099 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002100
2101 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002102 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002103}
2104
Chris Lattnereb19e402005-11-30 22:45:14 +00002105SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2106 MVT::ValueType VT1, MVT::ValueType VT2,
2107 SDOperand Op1, SDOperand Op2,
2108 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002109 // If an identical node already exists, use it.
2110 std::vector<SDOperand> OpList;
2111 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2112 OpList.push_back(Op4);
2113 std::vector<MVT::ValueType> VTList;
2114 VTList.push_back(VT1); VTList.push_back(VT2);
2115 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2116 std::make_pair(VTList, OpList))];
2117 if (ON) return SDOperand(ON, 0);
2118
Chris Lattner0fb094f2005-11-19 01:44:53 +00002119 RemoveNodeFromCSEMaps(N);
2120 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2121 setNodeValueTypes(N, VT1, VT2);
2122 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002123
2124 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002125 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002126}
2127
Chris Lattnereb19e402005-11-30 22:45:14 +00002128SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2129 MVT::ValueType VT1, MVT::ValueType VT2,
2130 SDOperand Op1, SDOperand Op2,
2131 SDOperand Op3, SDOperand Op4,
2132 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002133 // If an identical node already exists, use it.
2134 std::vector<SDOperand> OpList;
2135 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2136 OpList.push_back(Op4); OpList.push_back(Op5);
2137 std::vector<MVT::ValueType> VTList;
2138 VTList.push_back(VT1); VTList.push_back(VT2);
2139 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2140 std::make_pair(VTList, OpList))];
2141 if (ON) return SDOperand(ON, 0);
2142
Chris Lattner0fb094f2005-11-19 01:44:53 +00002143 RemoveNodeFromCSEMaps(N);
2144 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2145 setNodeValueTypes(N, VT1, VT2);
2146 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002147
2148 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002149 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002150}
2151
Evan Cheng6ae46c42006-02-09 07:15:23 +00002152/// getTargetNode - These are used for target selectors to create a new node
2153/// with specified return type(s), target opcode, and operands.
2154///
2155/// Note that getTargetNode returns the resultant node. If there is already a
2156/// node of the specified opcode and operands, it returns that node instead of
2157/// the current one.
2158SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2159 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2160}
2161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2162 SDOperand Op1) {
2163 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2164}
2165SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2166 SDOperand Op1, SDOperand Op2) {
2167 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2168}
2169SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2170 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2171 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2172}
2173SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2174 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2175 SDOperand Op4) {
2176 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2177}
2178SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2179 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2180 SDOperand Op4, SDOperand Op5) {
2181 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2182}
2183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2184 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2185 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2186 std::vector<SDOperand> Ops;
2187 Ops.reserve(6);
2188 Ops.push_back(Op1);
2189 Ops.push_back(Op2);
2190 Ops.push_back(Op3);
2191 Ops.push_back(Op4);
2192 Ops.push_back(Op5);
2193 Ops.push_back(Op6);
2194 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2195}
2196SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2197 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2198 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2199 SDOperand Op7) {
2200 std::vector<SDOperand> Ops;
2201 Ops.reserve(7);
2202 Ops.push_back(Op1);
2203 Ops.push_back(Op2);
2204 Ops.push_back(Op3);
2205 Ops.push_back(Op4);
2206 Ops.push_back(Op5);
2207 Ops.push_back(Op6);
2208 Ops.push_back(Op7);
2209 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2210}
2211SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2212 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2213 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2214 SDOperand Op7, SDOperand Op8) {
2215 std::vector<SDOperand> Ops;
2216 Ops.reserve(8);
2217 Ops.push_back(Op1);
2218 Ops.push_back(Op2);
2219 Ops.push_back(Op3);
2220 Ops.push_back(Op4);
2221 Ops.push_back(Op5);
2222 Ops.push_back(Op6);
2223 Ops.push_back(Op7);
2224 Ops.push_back(Op8);
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2228 std::vector<SDOperand> &Ops) {
2229 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2230}
2231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2232 MVT::ValueType VT2, SDOperand Op1) {
2233 std::vector<MVT::ValueType> ResultTys;
2234 ResultTys.push_back(VT1);
2235 ResultTys.push_back(VT2);
2236 std::vector<SDOperand> Ops;
2237 Ops.push_back(Op1);
2238 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2239}
2240SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2241 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2242 std::vector<MVT::ValueType> ResultTys;
2243 ResultTys.push_back(VT1);
2244 ResultTys.push_back(VT2);
2245 std::vector<SDOperand> Ops;
2246 Ops.push_back(Op1);
2247 Ops.push_back(Op2);
2248 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2249}
2250SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2251 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2252 SDOperand Op3) {
2253 std::vector<MVT::ValueType> ResultTys;
2254 ResultTys.push_back(VT1);
2255 ResultTys.push_back(VT2);
2256 std::vector<SDOperand> Ops;
2257 Ops.push_back(Op1);
2258 Ops.push_back(Op2);
2259 Ops.push_back(Op3);
2260 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2261}
2262SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2263 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2264 SDOperand Op3, SDOperand Op4) {
2265 std::vector<MVT::ValueType> ResultTys;
2266 ResultTys.push_back(VT1);
2267 ResultTys.push_back(VT2);
2268 std::vector<SDOperand> Ops;
2269 Ops.push_back(Op1);
2270 Ops.push_back(Op2);
2271 Ops.push_back(Op3);
2272 Ops.push_back(Op4);
2273 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2274}
2275SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2276 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2277 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2278 std::vector<MVT::ValueType> ResultTys;
2279 ResultTys.push_back(VT1);
2280 ResultTys.push_back(VT2);
2281 std::vector<SDOperand> Ops;
2282 Ops.push_back(Op1);
2283 Ops.push_back(Op2);
2284 Ops.push_back(Op3);
2285 Ops.push_back(Op4);
2286 Ops.push_back(Op5);
2287 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2288}
2289SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2290 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2291 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2292 SDOperand Op6) {
2293 std::vector<MVT::ValueType> ResultTys;
2294 ResultTys.push_back(VT1);
2295 ResultTys.push_back(VT2);
2296 std::vector<SDOperand> Ops;
2297 Ops.push_back(Op1);
2298 Ops.push_back(Op2);
2299 Ops.push_back(Op3);
2300 Ops.push_back(Op4);
2301 Ops.push_back(Op5);
2302 Ops.push_back(Op6);
2303 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2304}
2305SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2306 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2307 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2308 SDOperand Op6, SDOperand Op7) {
2309 std::vector<MVT::ValueType> ResultTys;
2310 ResultTys.push_back(VT1);
2311 ResultTys.push_back(VT2);
2312 std::vector<SDOperand> Ops;
2313 Ops.push_back(Op1);
2314 Ops.push_back(Op2);
2315 Ops.push_back(Op3);
2316 Ops.push_back(Op4);
2317 Ops.push_back(Op5);
2318 Ops.push_back(Op6);
2319 Ops.push_back(Op7);
2320 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2321}
2322SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2323 MVT::ValueType VT2, MVT::ValueType VT3,
2324 SDOperand Op1, SDOperand Op2) {
2325 std::vector<MVT::ValueType> ResultTys;
2326 ResultTys.push_back(VT1);
2327 ResultTys.push_back(VT2);
2328 ResultTys.push_back(VT3);
2329 std::vector<SDOperand> Ops;
2330 Ops.push_back(Op1);
2331 Ops.push_back(Op2);
2332 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2333}
2334SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2335 MVT::ValueType VT2, MVT::ValueType VT3,
2336 SDOperand Op1, SDOperand Op2,
2337 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2338 std::vector<MVT::ValueType> ResultTys;
2339 ResultTys.push_back(VT1);
2340 ResultTys.push_back(VT2);
2341 ResultTys.push_back(VT3);
2342 std::vector<SDOperand> Ops;
2343 Ops.push_back(Op1);
2344 Ops.push_back(Op2);
2345 Ops.push_back(Op3);
2346 Ops.push_back(Op4);
2347 Ops.push_back(Op5);
2348 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2349}
2350SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2351 MVT::ValueType VT2, MVT::ValueType VT3,
2352 SDOperand Op1, SDOperand Op2,
2353 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2354 SDOperand Op6) {
2355 std::vector<MVT::ValueType> ResultTys;
2356 ResultTys.push_back(VT1);
2357 ResultTys.push_back(VT2);
2358 ResultTys.push_back(VT3);
2359 std::vector<SDOperand> Ops;
2360 Ops.push_back(Op1);
2361 Ops.push_back(Op2);
2362 Ops.push_back(Op3);
2363 Ops.push_back(Op4);
2364 Ops.push_back(Op5);
2365 Ops.push_back(Op6);
2366 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2367}
2368SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2369 MVT::ValueType VT2, MVT::ValueType VT3,
2370 SDOperand Op1, SDOperand Op2,
2371 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2372 SDOperand Op6, SDOperand Op7) {
2373 std::vector<MVT::ValueType> ResultTys;
2374 ResultTys.push_back(VT1);
2375 ResultTys.push_back(VT2);
2376 ResultTys.push_back(VT3);
2377 std::vector<SDOperand> Ops;
2378 Ops.push_back(Op1);
2379 Ops.push_back(Op2);
2380 Ops.push_back(Op3);
2381 Ops.push_back(Op4);
2382 Ops.push_back(Op5);
2383 Ops.push_back(Op6);
2384 Ops.push_back(Op7);
2385 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2386}
2387SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2388 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2389 std::vector<MVT::ValueType> ResultTys;
2390 ResultTys.push_back(VT1);
2391 ResultTys.push_back(VT2);
2392 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2393}
2394
Chris Lattner0fb094f2005-11-19 01:44:53 +00002395// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002396/// This can cause recursive merging of nodes in the DAG.
2397///
Chris Lattner8b52f212005-08-26 18:36:28 +00002398/// This version assumes From/To have a single result value.
2399///
Chris Lattner1e111c72005-09-07 05:37:01 +00002400void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2401 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002402 SDNode *From = FromN.Val, *To = ToN.Val;
2403 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2404 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002405 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002406
Chris Lattner8b8749f2005-08-17 19:00:20 +00002407 while (!From->use_empty()) {
2408 // Process users until they are all gone.
2409 SDNode *U = *From->use_begin();
2410
2411 // This node is about to morph, remove its old self from the CSE maps.
2412 RemoveNodeFromCSEMaps(U);
2413
Chris Lattner65113b22005-11-08 22:07:03 +00002414 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2415 I != E; ++I)
2416 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002417 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002418 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002419 To->addUser(U);
2420 }
2421
2422 // Now that we have modified U, add it back to the CSE maps. If it already
2423 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002424 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2425 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002426 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002427 if (Deleted) Deleted->push_back(U);
2428 DeleteNodeNotInCSEMaps(U);
2429 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002430 }
2431}
2432
2433/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2434/// This can cause recursive merging of nodes in the DAG.
2435///
2436/// This version assumes From/To have matching types and numbers of result
2437/// values.
2438///
Chris Lattner1e111c72005-09-07 05:37:01 +00002439void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2440 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002441 assert(From != To && "Cannot replace uses of with self");
2442 assert(From->getNumValues() == To->getNumValues() &&
2443 "Cannot use this version of ReplaceAllUsesWith!");
2444 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002445 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002446 return;
2447 }
2448
2449 while (!From->use_empty()) {
2450 // Process users until they are all gone.
2451 SDNode *U = *From->use_begin();
2452
2453 // This node is about to morph, remove its old self from the CSE maps.
2454 RemoveNodeFromCSEMaps(U);
2455
Chris Lattner65113b22005-11-08 22:07:03 +00002456 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2457 I != E; ++I)
2458 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002459 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002460 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002461 To->addUser(U);
2462 }
2463
2464 // Now that we have modified U, add it back to the CSE maps. If it already
2465 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002466 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2467 ReplaceAllUsesWith(U, Existing, Deleted);
2468 // U is now dead.
2469 if (Deleted) Deleted->push_back(U);
2470 DeleteNodeNotInCSEMaps(U);
2471 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002472 }
2473}
2474
Chris Lattner8b52f212005-08-26 18:36:28 +00002475/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2476/// This can cause recursive merging of nodes in the DAG.
2477///
2478/// This version can replace From with any result values. To must match the
2479/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002480void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002481 const std::vector<SDOperand> &To,
2482 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002483 assert(From->getNumValues() == To.size() &&
2484 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002485 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002486 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002487 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002488 return;
2489 }
2490
2491 while (!From->use_empty()) {
2492 // Process users until they are all gone.
2493 SDNode *U = *From->use_begin();
2494
2495 // This node is about to morph, remove its old self from the CSE maps.
2496 RemoveNodeFromCSEMaps(U);
2497
Chris Lattner65113b22005-11-08 22:07:03 +00002498 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2499 I != E; ++I)
2500 if (I->Val == From) {
2501 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002502 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002503 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002504 ToOp.Val->addUser(U);
2505 }
2506
2507 // Now that we have modified U, add it back to the CSE maps. If it already
2508 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002509 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2510 ReplaceAllUsesWith(U, Existing, Deleted);
2511 // U is now dead.
2512 if (Deleted) Deleted->push_back(U);
2513 DeleteNodeNotInCSEMaps(U);
2514 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002515 }
2516}
2517
Chris Lattner012f2412006-02-17 21:58:01 +00002518/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2519/// uses of other values produced by From.Val alone. The Deleted vector is
2520/// handled the same was as for ReplaceAllUsesWith.
2521void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2522 std::vector<SDNode*> &Deleted) {
2523 assert(From != To && "Cannot replace a value with itself");
2524 // Handle the simple, trivial, case efficiently.
2525 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2526 ReplaceAllUsesWith(From, To, &Deleted);
2527 return;
2528 }
2529
2530 // Get all of the users in a nice, deterministically ordered, uniqued set.
2531 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2532
2533 while (!Users.empty()) {
2534 // We know that this user uses some value of From. If it is the right
2535 // value, update it.
2536 SDNode *User = Users.back();
2537 Users.pop_back();
2538
2539 for (SDOperand *Op = User->OperandList,
2540 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2541 if (*Op == From) {
2542 // Okay, we know this user needs to be updated. Remove its old self
2543 // from the CSE maps.
2544 RemoveNodeFromCSEMaps(User);
2545
2546 // Update all operands that match "From".
2547 for (; Op != E; ++Op) {
2548 if (*Op == From) {
2549 From.Val->removeUser(User);
2550 *Op = To;
2551 To.Val->addUser(User);
2552 }
2553 }
2554
2555 // Now that we have modified User, add it back to the CSE maps. If it
2556 // already exists there, recursively merge the results together.
2557 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2558 unsigned NumDeleted = Deleted.size();
2559 ReplaceAllUsesWith(User, Existing, &Deleted);
2560
2561 // User is now dead.
2562 Deleted.push_back(User);
2563 DeleteNodeNotInCSEMaps(User);
2564
2565 // We have to be careful here, because ReplaceAllUsesWith could have
2566 // deleted a user of From, which means there may be dangling pointers
2567 // in the "Users" setvector. Scan over the deleted node pointers and
2568 // remove them from the setvector.
2569 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2570 Users.remove(Deleted[i]);
2571 }
2572 break; // Exit the operand scanning loop.
2573 }
2574 }
2575 }
2576}
2577
Chris Lattner7b2880c2005-08-24 22:44:39 +00002578
Jim Laskey58b968b2005-08-17 20:08:02 +00002579//===----------------------------------------------------------------------===//
2580// SDNode Class
2581//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002582
Chris Lattnera3255112005-11-08 23:30:28 +00002583
2584/// getValueTypeList - Return a pointer to the specified value type.
2585///
2586MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2587 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2588 VTs[VT] = VT;
2589 return &VTs[VT];
2590}
2591
Chris Lattner5c884562005-01-12 18:37:47 +00002592/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2593/// indicated value. This method ignores uses of other values defined by this
2594/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002595bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002596 assert(Value < getNumValues() && "Bad value!");
2597
2598 // If there is only one value, this is easy.
2599 if (getNumValues() == 1)
2600 return use_size() == NUses;
2601 if (Uses.size() < NUses) return false;
2602
Evan Cheng4ee62112006-02-05 06:29:23 +00002603 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002604
2605 std::set<SDNode*> UsersHandled;
2606
Evan Cheng4ee62112006-02-05 06:29:23 +00002607 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002608 UI != E; ++UI) {
2609 SDNode *User = *UI;
2610 if (User->getNumOperands() == 1 ||
2611 UsersHandled.insert(User).second) // First time we've seen this?
2612 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2613 if (User->getOperand(i) == TheValue) {
2614 if (NUses == 0)
2615 return false; // too many uses
2616 --NUses;
2617 }
2618 }
2619
2620 // Found exactly the right number of uses?
2621 return NUses == 0;
2622}
2623
2624
Evan Cheng4ee62112006-02-05 06:29:23 +00002625// isOnlyUse - Return true if this node is the only use of N.
2626bool SDNode::isOnlyUse(SDNode *N) const {
2627 bool Seen = false;
2628 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2629 SDNode *User = *I;
2630 if (User == this)
2631 Seen = true;
2632 else
2633 return false;
2634 }
2635
2636 return Seen;
2637}
2638
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002639// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002640bool SDOperand::isOperand(SDNode *N) const {
2641 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2642 if (*this == N->getOperand(i))
2643 return true;
2644 return false;
2645}
2646
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002647bool SDNode::isOperand(SDNode *N) const {
2648 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2649 if (this == N->OperandList[i].Val)
2650 return true;
2651 return false;
2652}
Evan Cheng4ee62112006-02-05 06:29:23 +00002653
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002654const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002655 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002656 default:
2657 if (getOpcode() < ISD::BUILTIN_OP_END)
2658 return "<<Unknown DAG Node>>";
2659 else {
Evan Cheng72261582005-12-20 06:22:03 +00002660 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002661 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002662 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2663 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002664
Evan Cheng72261582005-12-20 06:22:03 +00002665 TargetLowering &TLI = G->getTargetLoweringInfo();
2666 const char *Name =
2667 TLI.getTargetNodeName(getOpcode());
2668 if (Name) return Name;
2669 }
2670
2671 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002672 }
2673
Andrew Lenharth95762122005-03-31 21:24:06 +00002674 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002675 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002676 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002677 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002678 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002679 case ISD::AssertSext: return "AssertSext";
2680 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002681
2682 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002683 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002684 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002685 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002686
2687 case ISD::Constant: return "Constant";
2688 case ISD::ConstantFP: return "ConstantFP";
2689 case ISD::GlobalAddress: return "GlobalAddress";
2690 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002691 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002692 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002693 case ISD::INTRINSIC_WO_CHAIN: {
2694 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2695 return Intrinsic::getName((Intrinsic::ID)IID);
2696 }
2697 case ISD::INTRINSIC_VOID:
2698 case ISD::INTRINSIC_W_CHAIN: {
2699 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002700 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002701 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002702
Chris Lattnerb2827b02006-03-19 00:52:58 +00002703 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002704 case ISD::TargetConstant: return "TargetConstant";
2705 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002706 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2707 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002708 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002709 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002710
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002711 case ISD::CopyToReg: return "CopyToReg";
2712 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002713 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002714 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002715 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002716 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002717
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002718 // Unary operators
2719 case ISD::FABS: return "fabs";
2720 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002721 case ISD::FSQRT: return "fsqrt";
2722 case ISD::FSIN: return "fsin";
2723 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002724
2725 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002726 case ISD::ADD: return "add";
2727 case ISD::SUB: return "sub";
2728 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002729 case ISD::MULHU: return "mulhu";
2730 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002731 case ISD::SDIV: return "sdiv";
2732 case ISD::UDIV: return "udiv";
2733 case ISD::SREM: return "srem";
2734 case ISD::UREM: return "urem";
2735 case ISD::AND: return "and";
2736 case ISD::OR: return "or";
2737 case ISD::XOR: return "xor";
2738 case ISD::SHL: return "shl";
2739 case ISD::SRA: return "sra";
2740 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002741 case ISD::ROTL: return "rotl";
2742 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002743 case ISD::FADD: return "fadd";
2744 case ISD::FSUB: return "fsub";
2745 case ISD::FMUL: return "fmul";
2746 case ISD::FDIV: return "fdiv";
2747 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002748 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002749 case ISD::VADD: return "vadd";
2750 case ISD::VSUB: return "vsub";
2751 case ISD::VMUL: return "vmul";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002752
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002753 case ISD::SETCC: return "setcc";
2754 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002755 case ISD::SELECT_CC: return "select_cc";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002756 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2757 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002758 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2759 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerce872152006-03-19 06:31:19 +00002760 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerfb194b92006-03-19 23:56:04 +00002761 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2762 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattner354cde92006-03-28 19:54:42 +00002763 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
Chris Lattnere25ca692006-03-22 20:09:35 +00002764 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002765 case ISD::ADDC: return "addc";
2766 case ISD::ADDE: return "adde";
2767 case ISD::SUBC: return "subc";
2768 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002769 case ISD::SHL_PARTS: return "shl_parts";
2770 case ISD::SRA_PARTS: return "sra_parts";
2771 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002772
Chris Lattner7f644642005-04-28 21:44:03 +00002773 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002774 case ISD::SIGN_EXTEND: return "sign_extend";
2775 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002776 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002777 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002778 case ISD::TRUNCATE: return "truncate";
2779 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002780 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002781 case ISD::FP_EXTEND: return "fp_extend";
2782
2783 case ISD::SINT_TO_FP: return "sint_to_fp";
2784 case ISD::UINT_TO_FP: return "uint_to_fp";
2785 case ISD::FP_TO_SINT: return "fp_to_sint";
2786 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002787 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002788
2789 // Control flow instructions
2790 case ISD::BR: return "br";
2791 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002792 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002793 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002794 case ISD::CALLSEQ_START: return "callseq_start";
2795 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002796
2797 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002798 case ISD::LOAD: return "load";
2799 case ISD::STORE: return "store";
2800 case ISD::VLOAD: return "vload";
2801 case ISD::EXTLOAD: return "extload";
2802 case ISD::SEXTLOAD: return "sextload";
2803 case ISD::ZEXTLOAD: return "zextload";
2804 case ISD::TRUNCSTORE: return "truncstore";
2805 case ISD::VAARG: return "vaarg";
2806 case ISD::VACOPY: return "vacopy";
2807 case ISD::VAEND: return "vaend";
2808 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002809 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002810 case ISD::EXTRACT_ELEMENT: return "extract_element";
2811 case ISD::BUILD_PAIR: return "build_pair";
2812 case ISD::STACKSAVE: return "stacksave";
2813 case ISD::STACKRESTORE: return "stackrestore";
2814
2815 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002816 case ISD::MEMSET: return "memset";
2817 case ISD::MEMCPY: return "memcpy";
2818 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002819
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002820 // Bit manipulation
2821 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002822 case ISD::CTPOP: return "ctpop";
2823 case ISD::CTTZ: return "cttz";
2824 case ISD::CTLZ: return "ctlz";
2825
Chris Lattner36ce6912005-11-29 06:21:05 +00002826 // Debug info
2827 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002828 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002829 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002830
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002831 case ISD::CONDCODE:
2832 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002833 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002834 case ISD::SETOEQ: return "setoeq";
2835 case ISD::SETOGT: return "setogt";
2836 case ISD::SETOGE: return "setoge";
2837 case ISD::SETOLT: return "setolt";
2838 case ISD::SETOLE: return "setole";
2839 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002840
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002841 case ISD::SETO: return "seto";
2842 case ISD::SETUO: return "setuo";
2843 case ISD::SETUEQ: return "setue";
2844 case ISD::SETUGT: return "setugt";
2845 case ISD::SETUGE: return "setuge";
2846 case ISD::SETULT: return "setult";
2847 case ISD::SETULE: return "setule";
2848 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002849
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002850 case ISD::SETEQ: return "seteq";
2851 case ISD::SETGT: return "setgt";
2852 case ISD::SETGE: return "setge";
2853 case ISD::SETLT: return "setlt";
2854 case ISD::SETLE: return "setle";
2855 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002856 }
2857 }
2858}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002859
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002860void SDNode::dump() const { dump(0); }
2861void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002862 std::cerr << (void*)this << ": ";
2863
2864 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2865 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002866 if (getValueType(i) == MVT::Other)
2867 std::cerr << "ch";
2868 else
2869 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002870 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002871 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002872
2873 std::cerr << " ";
2874 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2875 if (i) std::cerr << ", ";
2876 std::cerr << (void*)getOperand(i).Val;
2877 if (unsigned RN = getOperand(i).ResNo)
2878 std::cerr << ":" << RN;
2879 }
2880
2881 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2882 std::cerr << "<" << CSDN->getValue() << ">";
2883 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2884 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002885 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002886 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002887 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002888 std::cerr << "<";
2889 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002890 if (offset > 0)
2891 std::cerr << " + " << offset;
2892 else
2893 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002894 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002895 std::cerr << "<" << FIDN->getIndex() << ">";
2896 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002897 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002898 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002899 if (offset > 0)
2900 std::cerr << " + " << offset;
2901 else
2902 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002903 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002904 std::cerr << "<";
2905 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2906 if (LBB)
2907 std::cerr << LBB->getName() << " ";
2908 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002909 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002910 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002911 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2912 } else {
2913 std::cerr << " #" << R->getReg();
2914 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002915 } else if (const ExternalSymbolSDNode *ES =
2916 dyn_cast<ExternalSymbolSDNode>(this)) {
2917 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002918 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2919 if (M->getValue())
2920 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2921 else
2922 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002923 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2924 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002925 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002926}
2927
Chris Lattnerde202b32005-11-09 23:47:37 +00002928static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002929 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2930 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002931 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002932 else
2933 std::cerr << "\n" << std::string(indent+2, ' ')
2934 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002935
Chris Lattnerea946cd2005-01-09 20:38:33 +00002936
2937 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002938 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002939}
2940
Chris Lattnerc3aae252005-01-07 07:46:32 +00002941void SelectionDAG::dump() const {
2942 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002943 std::vector<const SDNode*> Nodes;
2944 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2945 I != E; ++I)
2946 Nodes.push_back(I);
2947
Chris Lattner49d24712005-01-09 20:26:36 +00002948 std::sort(Nodes.begin(), Nodes.end());
2949
2950 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002951 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002952 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002953 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002954
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002955 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002956
Chris Lattnerc3aae252005-01-07 07:46:32 +00002957 std::cerr << "\n\n";
2958}
2959
Evan Chengfae9f1c2006-02-09 22:11:03 +00002960/// InsertISelMapEntry - A helper function to insert a key / element pair
2961/// into a SDOperand to SDOperand map. This is added to avoid the map
2962/// insertion operator from being inlined.
2963void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2964 SDNode *Key, unsigned KeyResNo,
2965 SDNode *Element, unsigned ElementResNo) {
2966 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2967 SDOperand(Element, ElementResNo)));
2968}