blob: 9c8b13029f6736dd9750f6688ca00891ee967f50 [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)) {
94 if (!cast<ConstantFPSDNode>(NotZero)->isExactlyValue(-1))
95 return false;
96 } else
Chris Lattner61d43992006-03-25 22:57:01 +000097 return false;
98
99 // Okay, we have at least one ~0 value, check to see if the rest match or are
100 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000101 for (++i; i != e; ++i)
102 if (N->getOperand(i) != NotZero &&
103 N->getOperand(i).getOpcode() != ISD::UNDEF)
104 return false;
105 return true;
106}
107
108
Evan Cheng4a147842006-03-26 09:50:58 +0000109/// isBuildVectorAllZeros - Return true if the specified node is a
110/// BUILD_VECTOR where all of the elements are 0 or undef.
111bool ISD::isBuildVectorAllZeros(const SDNode *N) {
112 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000113
114 unsigned i = 0, e = N->getNumOperands();
115
116 // Skip over all of the undef values.
117 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
118 ++i;
119
120 // Do not accept an all-undef vector.
121 if (i == e) return false;
122
123 // Do not accept build_vectors that aren't all constants or which have non-~0
124 // elements.
125 SDOperand Zero = N->getOperand(i);
126 if (isa<ConstantSDNode>(Zero)) {
127 if (!cast<ConstantSDNode>(Zero)->isNullValue())
128 return false;
129 } else if (isa<ConstantFPSDNode>(Zero)) {
130 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
131 return false;
132 } else
133 return false;
134
135 // Okay, we have at least one ~0 value, check to see if the rest match or are
136 // undefs.
137 for (++i; i != e; ++i)
138 if (N->getOperand(i) != Zero &&
139 N->getOperand(i).getOpcode() != ISD::UNDEF)
140 return false;
141 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000142}
143
Chris Lattnerc3aae252005-01-07 07:46:32 +0000144/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
145/// when given the operation for (X op Y).
146ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
147 // To perform this operation, we just need to swap the L and G bits of the
148 // operation.
149 unsigned OldL = (Operation >> 2) & 1;
150 unsigned OldG = (Operation >> 1) & 1;
151 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
152 (OldL << 1) | // New G bit
153 (OldG << 2)); // New L bit.
154}
155
156/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
157/// 'op' is a valid SetCC operation.
158ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
159 unsigned Operation = Op;
160 if (isInteger)
161 Operation ^= 7; // Flip L, G, E bits, but not U.
162 else
163 Operation ^= 15; // Flip all of the condition bits.
164 if (Operation > ISD::SETTRUE2)
165 Operation &= ~8; // Don't let N and U bits get set.
166 return ISD::CondCode(Operation);
167}
168
169
170/// isSignedOp - For an integer comparison, return 1 if the comparison is a
171/// signed operation and 2 if the result is an unsigned comparison. Return zero
172/// if the operation does not depend on the sign of the input (setne and seteq).
173static int isSignedOp(ISD::CondCode Opcode) {
174 switch (Opcode) {
175 default: assert(0 && "Illegal integer setcc operation!");
176 case ISD::SETEQ:
177 case ISD::SETNE: return 0;
178 case ISD::SETLT:
179 case ISD::SETLE:
180 case ISD::SETGT:
181 case ISD::SETGE: return 1;
182 case ISD::SETULT:
183 case ISD::SETULE:
184 case ISD::SETUGT:
185 case ISD::SETUGE: return 2;
186 }
187}
188
189/// getSetCCOrOperation - Return the result of a logical OR between different
190/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
191/// returns SETCC_INVALID if it is not possible to represent the resultant
192/// comparison.
193ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
194 bool isInteger) {
195 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
196 // Cannot fold a signed integer setcc with an unsigned integer setcc.
197 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000198
Chris Lattnerc3aae252005-01-07 07:46:32 +0000199 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000200
Chris Lattnerc3aae252005-01-07 07:46:32 +0000201 // If the N and U bits get set then the resultant comparison DOES suddenly
202 // care about orderedness, and is true when ordered.
203 if (Op > ISD::SETTRUE2)
204 Op &= ~16; // Clear the N bit.
205 return ISD::CondCode(Op);
206}
207
208/// getSetCCAndOperation - Return the result of a logical AND between different
209/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
210/// function returns zero if it is not possible to represent the resultant
211/// comparison.
212ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
213 bool isInteger) {
214 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
215 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000217
218 // Combine all of the condition bits.
219 return ISD::CondCode(Op1 & Op2);
220}
221
Chris Lattnerb48da392005-01-23 04:39:44 +0000222const TargetMachine &SelectionDAG::getTarget() const {
223 return TLI.getTargetMachine();
224}
225
Jim Laskey58b968b2005-08-17 20:08:02 +0000226//===----------------------------------------------------------------------===//
227// SelectionDAG Class
228//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000229
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000230/// RemoveDeadNodes - This method deletes all unreachable nodes in the
231/// SelectionDAG, including nodes (like loads) that have uses of their token
232/// chain but no other uses and no side effect. If a node is passed in as an
233/// argument, it is used as the seed for node deletion.
234void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000235 // Create a dummy node (which is not added to allnodes), that adds a reference
236 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000237 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000238
Chris Lattnerf469cb62005-11-08 18:52:27 +0000239 bool MadeChange = false;
240
Chris Lattner8b8749f2005-08-17 19:00:20 +0000241 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000242 if (N && N->use_empty()) {
243 DestroyDeadNode(N);
244 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000245 }
246
Chris Lattnerde202b32005-11-09 23:47:37 +0000247 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
248 if (I->use_empty() && I->getOpcode() != 65535) {
249 // Node is dead, recursively delete newly dead uses.
250 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000251 MadeChange = true;
252 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000253
254 // Walk the nodes list, removing the nodes we've marked as dead.
255 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000256 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
257 SDNode *N = I++;
258 if (N->use_empty())
259 AllNodes.erase(N);
260 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000261 }
262
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000264 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265}
266
Chris Lattnerf469cb62005-11-08 18:52:27 +0000267/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
268/// graph. If it is the last user of any of its operands, recursively process
269/// them the same way.
270///
271void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272 // Okay, we really are going to delete this node. First take this out of the
273 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000274 RemoveNodeFromCSEMaps(N);
275
276 // Next, brutally remove the operand list. This is safe to do, as there are
277 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000278 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
279 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000280 O->removeUser(N);
281
282 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000283 if (O->use_empty())
284 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000285 }
Chris Lattner65113b22005-11-08 22:07:03 +0000286 delete[] N->OperandList;
287 N->OperandList = 0;
288 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000289
290 // Mark the node as dead.
291 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000292}
293
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000294void SelectionDAG::DeleteNode(SDNode *N) {
295 assert(N->use_empty() && "Cannot delete a node that is not dead!");
296
297 // First take this out of the appropriate CSE map.
298 RemoveNodeFromCSEMaps(N);
299
Chris Lattner1e111c72005-09-07 05:37:01 +0000300 // Finally, remove uses due to operands of this node, remove from the
301 // AllNodes list, and delete the node.
302 DeleteNodeNotInCSEMaps(N);
303}
304
305void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
306
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000307 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000308 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000309
310 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000311 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
312 I->Val->removeUser(N);
313 delete[] N->OperandList;
314 N->OperandList = 0;
315 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000316
317 delete N;
318}
319
Chris Lattner149c58c2005-08-16 18:17:10 +0000320/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
321/// correspond to it. This is useful when we're about to delete or repurpose
322/// the node. We don't want future request for structurally identical nodes
323/// to return N anymore.
324void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000325 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000326 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000327 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000328 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
330 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000331 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000332 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 Erased = TargetConstants.erase(std::make_pair(
334 cast<ConstantSDNode>(N)->getValue(),
335 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000336 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000337 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000338 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000339 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000340 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000341 }
Chris Lattner3181a772006-01-29 06:26:56 +0000342 case ISD::TargetConstantFP: {
343 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
344 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
345 break;
346 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000347 case ISD::STRING:
348 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
349 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000350 case ISD::CONDCODE:
351 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
352 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000353 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000354 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
355 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000356 case ISD::GlobalAddress: {
357 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
358 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
359 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000360 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000361 }
362 case ISD::TargetGlobalAddress: {
363 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
364 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
365 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000366 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000367 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000371 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000373 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000374 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000375 Erased = ConstantPoolIndices.
376 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000377 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
378 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000379 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000380 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000381 Erased = TargetConstantPoolIndices.
382 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000383 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
384 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000385 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000386 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000387 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000388 break;
389 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000390 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000391 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000392 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000393 Erased =
394 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000395 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000396 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000397 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000398 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
399 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000400 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000401 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
402 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000403 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000404 case ISD::SRCVALUE: {
405 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000406 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000407 break;
408 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000410 Erased = Loads.erase(std::make_pair(N->getOperand(1),
411 std::make_pair(N->getOperand(0),
412 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000413 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000415 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000416 if (N->getNumOperands() == 0) {
417 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
418 N->getValueType(0)));
419 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000420 Erased =
421 UnaryOps.erase(std::make_pair(N->getOpcode(),
422 std::make_pair(N->getOperand(0),
423 N->getValueType(0))));
424 } else if (N->getNumOperands() == 2) {
425 Erased =
426 BinaryOps.erase(std::make_pair(N->getOpcode(),
427 std::make_pair(N->getOperand(0),
428 N->getOperand(1))));
429 } else {
430 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
431 Erased =
432 OneResultNodes.erase(std::make_pair(N->getOpcode(),
433 std::make_pair(N->getValueType(0),
434 Ops)));
435 }
Chris Lattner385328c2005-05-14 07:42:29 +0000436 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000437 // Remove the node from the ArbitraryNodes map.
438 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
439 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000440 Erased =
441 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
442 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000443 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444 break;
445 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000446#ifndef NDEBUG
447 // Verify that the node was actually in one of the CSE maps, unless it has a
448 // flag result (which cannot be CSE'd) or is one of the special cases that are
449 // not subject to CSE.
450 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000451 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000452 N->dump();
453 assert(0 && "Node is not in map!");
454 }
455#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000456}
457
Chris Lattner8b8749f2005-08-17 19:00:20 +0000458/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
459/// has been taken out and modified in some way. If the specified node already
460/// exists in the CSE maps, do not modify the maps, but return the existing node
461/// instead. If it doesn't exist, add it and return null.
462///
463SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
464 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000465 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000466 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000467
Chris Lattner9f8cc692005-12-19 22:21:21 +0000468 // Check that remaining values produced are not flags.
469 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
470 if (N->getValueType(i) == MVT::Flag)
471 return 0; // Never CSE anything that produces a flag.
472
Chris Lattnerfe14b342005-12-01 23:14:50 +0000473 if (N->getNumValues() == 1) {
474 if (N->getNumOperands() == 1) {
475 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
476 std::make_pair(N->getOperand(0),
477 N->getValueType(0)))];
478 if (U) return U;
479 U = N;
480 } else if (N->getNumOperands() == 2) {
481 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
482 std::make_pair(N->getOperand(0),
483 N->getOperand(1)))];
484 if (B) return B;
485 B = N;
486 } else {
487 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
488 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000489 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000490 if (ORN) return ORN;
491 ORN = N;
492 }
493 } else {
494 if (N->getOpcode() == ISD::LOAD) {
495 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
496 std::make_pair(N->getOperand(0),
497 N->getValueType(0)))];
498 if (L) return L;
499 L = N;
500 } else {
501 // Remove the node from the ArbitraryNodes map.
502 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
503 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
504 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
505 std::make_pair(RV, Ops))];
506 if (AN) return AN;
507 AN = N;
508 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000509 }
510 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000511}
512
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000513/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
514/// were replaced with those specified. If this node is never memoized,
515/// return null, otherwise return a pointer to the slot it would take. If a
516/// node already exists with these operands, the slot will be non-null.
517SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000518 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000519 return 0; // Never add these nodes.
520
521 // Check that remaining values produced are not flags.
522 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
523 if (N->getValueType(i) == MVT::Flag)
524 return 0; // Never CSE anything that produces a flag.
525
526 if (N->getNumValues() == 1) {
527 return &UnaryOps[std::make_pair(N->getOpcode(),
528 std::make_pair(Op, N->getValueType(0)))];
529 } else {
530 // Remove the node from the ArbitraryNodes map.
531 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
532 std::vector<SDOperand> Ops;
533 Ops.push_back(Op);
534 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
535 std::make_pair(RV, Ops))];
536 }
537 return 0;
538}
539
540/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
541/// were replaced with those specified. If this node is never memoized,
542/// return null, otherwise return a pointer to the slot it would take. If a
543/// node already exists with these operands, the slot will be non-null.
544SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
545 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000546 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000547 return 0; // Never add these nodes.
548
549 // Check that remaining values produced are not flags.
550 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
551 if (N->getValueType(i) == MVT::Flag)
552 return 0; // Never CSE anything that produces a flag.
553
554 if (N->getNumValues() == 1) {
555 return &BinaryOps[std::make_pair(N->getOpcode(),
556 std::make_pair(Op1, Op2))];
557 } else {
558 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
559 std::vector<SDOperand> Ops;
560 Ops.push_back(Op1);
561 Ops.push_back(Op2);
562 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
563 std::make_pair(RV, Ops))];
564 }
565 return 0;
566}
567
568
569/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
570/// were replaced with those specified. If this node is never memoized,
571/// return null, otherwise return a pointer to the slot it would take. If a
572/// node already exists with these operands, the slot will be non-null.
573SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
574 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000575 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000576 return 0; // Never add these nodes.
577
578 // Check that remaining values produced are not flags.
579 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
580 if (N->getValueType(i) == MVT::Flag)
581 return 0; // Never CSE anything that produces a flag.
582
583 if (N->getNumValues() == 1) {
584 if (N->getNumOperands() == 1) {
585 return &UnaryOps[std::make_pair(N->getOpcode(),
586 std::make_pair(Ops[0],
587 N->getValueType(0)))];
588 } else if (N->getNumOperands() == 2) {
589 return &BinaryOps[std::make_pair(N->getOpcode(),
590 std::make_pair(Ops[0], Ops[1]))];
591 } else {
592 return &OneResultNodes[std::make_pair(N->getOpcode(),
593 std::make_pair(N->getValueType(0),
594 Ops))];
595 }
596 } else {
597 if (N->getOpcode() == ISD::LOAD) {
598 return &Loads[std::make_pair(Ops[1],
599 std::make_pair(Ops[0], N->getValueType(0)))];
600 } else {
601 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
602 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
603 std::make_pair(RV, Ops))];
604 }
605 }
606 return 0;
607}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000608
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000609
Chris Lattner78ec3112003-08-11 14:57:33 +0000610SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000611 while (!AllNodes.empty()) {
612 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000613 delete [] N->OperandList;
614 N->OperandList = 0;
615 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000616 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000617 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000618}
619
Chris Lattner0f2287b2005-04-13 02:38:18 +0000620SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000621 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000622 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000623 return getNode(ISD::AND, Op.getValueType(), Op,
624 getConstant(Imm, Op.getValueType()));
625}
626
Chris Lattnerc3aae252005-01-07 07:46:32 +0000627SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
628 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
629 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000630 if (VT != MVT::i64)
631 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000632
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633 SDNode *&N = Constants[std::make_pair(Val, VT)];
634 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000635 N = new ConstantSDNode(false, Val, VT);
636 AllNodes.push_back(N);
637 return SDOperand(N, 0);
638}
639
Chris Lattner36ce6912005-11-29 06:21:05 +0000640SDOperand SelectionDAG::getString(const std::string &Val) {
641 StringSDNode *&N = StringNodes[Val];
642 if (!N) {
643 N = new StringSDNode(Val);
644 AllNodes.push_back(N);
645 }
646 return SDOperand(N, 0);
647}
648
Chris Lattner37bfbb42005-08-17 00:34:06 +0000649SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
650 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
651 // Mask out any bits that are not valid for this constant.
652 if (VT != MVT::i64)
653 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
654
655 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
656 if (N) return SDOperand(N, 0);
657 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658 AllNodes.push_back(N);
659 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000660}
661
Chris Lattnerc3aae252005-01-07 07:46:32 +0000662SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
663 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
664 if (VT == MVT::f32)
665 Val = (float)Val; // Mask out extra precision.
666
Chris Lattnerd8658612005-02-17 20:17:32 +0000667 // Do the map lookup using the actual bit pattern for the floating point
668 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
669 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000670 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000671 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000672 N = new ConstantFPSDNode(false, Val, VT);
673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
675}
676
677SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
678 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
679 if (VT == MVT::f32)
680 Val = (float)Val; // Mask out extra precision.
681
682 // Do the map lookup using the actual bit pattern for the floating point
683 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
684 // we don't have issues with SNANs.
685 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
686 if (N) return SDOperand(N, 0);
687 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000688 AllNodes.push_back(N);
689 return SDOperand(N, 0);
690}
691
Chris Lattnerc3aae252005-01-07 07:46:32 +0000692SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000693 MVT::ValueType VT, int offset) {
694 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000695 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000696 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
699}
700
701SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000702 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000703 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000704 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000705 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
708}
709
710SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
711 SDNode *&N = FrameIndices[FI];
712 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000713 N = new FrameIndexSDNode(FI, VT, false);
714 AllNodes.push_back(N);
715 return SDOperand(N, 0);
716}
717
718SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
719 SDNode *&N = TargetFrameIndices[FI];
720 if (N) return SDOperand(N, 0);
721 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000722 AllNodes.push_back(N);
723 return SDOperand(N, 0);
724}
725
Evan Chengb8973bd2006-01-31 22:23:14 +0000726SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000727 unsigned Alignment, int Offset) {
728 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
729 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000730 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000731 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
734}
735
Evan Chengb8973bd2006-01-31 22:23:14 +0000736SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000737 unsigned Alignment, int Offset) {
738 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
739 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000740 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000741 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000742 AllNodes.push_back(N);
743 return SDOperand(N, 0);
744}
745
746SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
747 SDNode *&N = BBNodes[MBB];
748 if (N) return SDOperand(N, 0);
749 N = new BasicBlockSDNode(MBB);
750 AllNodes.push_back(N);
751 return SDOperand(N, 0);
752}
753
Chris Lattner15e4b012005-07-10 00:07:11 +0000754SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
755 if ((unsigned)VT >= ValueTypeNodes.size())
756 ValueTypeNodes.resize(VT+1);
757 if (ValueTypeNodes[VT] == 0) {
758 ValueTypeNodes[VT] = new VTSDNode(VT);
759 AllNodes.push_back(ValueTypeNodes[VT]);
760 }
761
762 return SDOperand(ValueTypeNodes[VT], 0);
763}
764
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
766 SDNode *&N = ExternalSymbols[Sym];
767 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000768 N = new ExternalSymbolSDNode(false, Sym, VT);
769 AllNodes.push_back(N);
770 return SDOperand(N, 0);
771}
772
Chris Lattner809ec112006-01-28 10:09:25 +0000773SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
774 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000775 SDNode *&N = TargetExternalSymbols[Sym];
776 if (N) return SDOperand(N, 0);
777 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000778 AllNodes.push_back(N);
779 return SDOperand(N, 0);
780}
781
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000782SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
783 if ((unsigned)Cond >= CondCodeNodes.size())
784 CondCodeNodes.resize(Cond+1);
785
Chris Lattner079a27a2005-08-09 20:40:02 +0000786 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000787 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000788 AllNodes.push_back(CondCodeNodes[Cond]);
789 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000790 return SDOperand(CondCodeNodes[Cond], 0);
791}
792
Chris Lattner0fdd7682005-08-30 22:38:38 +0000793SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
794 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
795 if (!Reg) {
796 Reg = new RegisterSDNode(RegNo, VT);
797 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000798 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000799 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000800}
801
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000802SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
803 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000804 // These setcc operations always fold.
805 switch (Cond) {
806 default: break;
807 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000808 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000809 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000810 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000811 }
812
Chris Lattner67255a12005-04-07 18:14:58 +0000813 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
814 uint64_t C2 = N2C->getValue();
815 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
816 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818 // Sign extend the operands if required
819 if (ISD::isSignedIntSetCC(Cond)) {
820 C1 = N1C->getSignExtended();
821 C2 = N2C->getSignExtended();
822 }
823
824 switch (Cond) {
825 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000826 case ISD::SETEQ: return getConstant(C1 == C2, VT);
827 case ISD::SETNE: return getConstant(C1 != C2, VT);
828 case ISD::SETULT: return getConstant(C1 < C2, VT);
829 case ISD::SETUGT: return getConstant(C1 > C2, VT);
830 case ISD::SETULE: return getConstant(C1 <= C2, VT);
831 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
832 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
833 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
834 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
835 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836 }
Chris Lattner24673922005-04-07 18:58:54 +0000837 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000838 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000839 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
840 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
841
842 // If the comparison constant has bits in the upper part, the
843 // zero-extended value could never match.
844 if (C2 & (~0ULL << InSize)) {
845 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
846 switch (Cond) {
847 case ISD::SETUGT:
848 case ISD::SETUGE:
849 case ISD::SETEQ: return getConstant(0, VT);
850 case ISD::SETULT:
851 case ISD::SETULE:
852 case ISD::SETNE: return getConstant(1, VT);
853 case ISD::SETGT:
854 case ISD::SETGE:
855 // True if the sign bit of C2 is set.
856 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
857 case ISD::SETLT:
858 case ISD::SETLE:
859 // True if the sign bit of C2 isn't set.
860 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
861 default:
862 break;
863 }
864 }
865
866 // Otherwise, we can perform the comparison with the low bits.
867 switch (Cond) {
868 case ISD::SETEQ:
869 case ISD::SETNE:
870 case ISD::SETUGT:
871 case ISD::SETUGE:
872 case ISD::SETULT:
873 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000874 return getSetCC(VT, N1.getOperand(0),
875 getConstant(C2, N1.getOperand(0).getValueType()),
876 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000877 default:
878 break; // todo, be more careful with signed comparisons
879 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000880 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
881 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
882 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
883 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
884 MVT::ValueType ExtDstTy = N1.getValueType();
885 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000886
Chris Lattner7b2880c2005-08-24 22:44:39 +0000887 // If the extended part has any inconsistent bits, it cannot ever
888 // compare equal. In other words, they have to be all ones or all
889 // zeros.
890 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000891 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000892 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
893 return getConstant(Cond == ISD::SETNE, VT);
894
895 // Otherwise, make this a use of a zext.
896 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000897 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000898 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000899 }
900
Chris Lattner67255a12005-04-07 18:14:58 +0000901 uint64_t MinVal, MaxVal;
902 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
903 if (ISD::isSignedIntSetCC(Cond)) {
904 MinVal = 1ULL << (OperandBitSize-1);
905 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
906 MaxVal = ~0ULL >> (65-OperandBitSize);
907 else
908 MaxVal = 0;
909 } else {
910 MinVal = 0;
911 MaxVal = ~0ULL >> (64-OperandBitSize);
912 }
913
914 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
915 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
916 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
917 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000918 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
919 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000920 }
921
922 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
923 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
924 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000925 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
926 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000927 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000928
Nate Begeman72ea2812005-04-14 08:56:52 +0000929 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
930 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000931
Nate Begeman72ea2812005-04-14 08:56:52 +0000932 // Canonicalize setgt X, Min --> setne X, Min
933 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000934 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000935
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000936 // If we have setult X, 1, turn it into seteq X, 0
937 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000938 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
939 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000940 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000941 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000942 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
943 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000944
945 // If we have "setcc X, C1", check to see if we can shrink the immediate
946 // by changing cc.
947
948 // SETUGT X, SINTMAX -> SETLT X, 0
949 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
950 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000951 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000952
953 // FIXME: Implement the rest of these.
954
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000955
956 // Fold bit comparisons when we can.
957 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
958 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
959 if (ConstantSDNode *AndRHS =
960 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
961 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
962 // Perform the xform if the AND RHS is a single bit.
963 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
964 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000965 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000966 TLI.getShiftAmountTy()));
967 }
968 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
969 // (X & 8) == 8 --> (X & 8) >> 3
970 // Perform the xform if C2 is a single bit.
971 if ((C2 & (C2-1)) == 0) {
972 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000973 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000974 }
975 }
976 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 }
Chris Lattner67255a12005-04-07 18:14:58 +0000978 } else if (isa<ConstantSDNode>(N1.Val)) {
979 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000980 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000981 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982
983 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
984 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
985 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000986
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 switch (Cond) {
988 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000989 case ISD::SETEQ: return getConstant(C1 == C2, VT);
990 case ISD::SETNE: return getConstant(C1 != C2, VT);
991 case ISD::SETLT: return getConstant(C1 < C2, VT);
992 case ISD::SETGT: return getConstant(C1 > C2, VT);
993 case ISD::SETLE: return getConstant(C1 <= C2, VT);
994 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 }
996 } else {
997 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000998 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 }
1000
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001001 // Could not fold it.
1002 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001003}
1004
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001006///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001007SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001008 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1009 if (!N) {
1010 N = new SDNode(Opcode, VT);
1011 AllNodes.push_back(N);
1012 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 return SDOperand(N, 0);
1014}
1015
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1017 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001018 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001019 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1021 uint64_t Val = C->getValue();
1022 switch (Opcode) {
1023 default: break;
1024 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001025 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1027 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001028 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1029 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001030 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001031 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001032 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001033 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001034 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001035 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001036 case ISD::BSWAP:
1037 switch(VT) {
1038 default: assert(0 && "Invalid bswap!"); break;
1039 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1040 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1041 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1042 }
1043 break;
1044 case ISD::CTPOP:
1045 switch(VT) {
1046 default: assert(0 && "Invalid ctpop!"); break;
1047 case MVT::i1: return getConstant(Val != 0, VT);
1048 case MVT::i8:
1049 Tmp1 = (unsigned)Val & 0xFF;
1050 return getConstant(CountPopulation_32(Tmp1), VT);
1051 case MVT::i16:
1052 Tmp1 = (unsigned)Val & 0xFFFF;
1053 return getConstant(CountPopulation_32(Tmp1), VT);
1054 case MVT::i32:
1055 return getConstant(CountPopulation_32((unsigned)Val), VT);
1056 case MVT::i64:
1057 return getConstant(CountPopulation_64(Val), VT);
1058 }
1059 case ISD::CTLZ:
1060 switch(VT) {
1061 default: assert(0 && "Invalid ctlz!"); break;
1062 case MVT::i1: return getConstant(Val == 0, VT);
1063 case MVT::i8:
1064 Tmp1 = (unsigned)Val & 0xFF;
1065 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1066 case MVT::i16:
1067 Tmp1 = (unsigned)Val & 0xFFFF;
1068 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1069 case MVT::i32:
1070 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1071 case MVT::i64:
1072 return getConstant(CountLeadingZeros_64(Val), VT);
1073 }
1074 case ISD::CTTZ:
1075 switch(VT) {
1076 default: assert(0 && "Invalid cttz!"); break;
1077 case MVT::i1: return getConstant(Val == 0, VT);
1078 case MVT::i8:
1079 Tmp1 = (unsigned)Val | 0x100;
1080 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1081 case MVT::i16:
1082 Tmp1 = (unsigned)Val | 0x10000;
1083 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1084 case MVT::i32:
1085 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1086 case MVT::i64:
1087 return getConstant(CountTrailingZeros_64(Val), VT);
1088 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089 }
1090 }
1091
Chris Lattner94683772005-12-23 05:30:37 +00001092 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1094 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001095 case ISD::FNEG:
1096 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001097 case ISD::FABS:
1098 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 case ISD::FP_ROUND:
1100 case ISD::FP_EXTEND:
1101 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001102 case ISD::FP_TO_SINT:
1103 return getConstant((int64_t)C->getValue(), VT);
1104 case ISD::FP_TO_UINT:
1105 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001106 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001107 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001108 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001109 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001110 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001111 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 }
1113
1114 unsigned OpOpcode = Operand.Val->getOpcode();
1115 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001116 case ISD::TokenFactor:
1117 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001118 case ISD::SIGN_EXTEND:
1119 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001120 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001121 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1122 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1123 break;
1124 case ISD::ZERO_EXTEND:
1125 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001126 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001127 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001128 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001130 case ISD::ANY_EXTEND:
1131 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001132 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001133 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1134 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1135 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1136 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 case ISD::TRUNCATE:
1138 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001139 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 if (OpOpcode == ISD::TRUNCATE)
1141 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001142 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1143 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001144 // If the source is smaller than the dest, we still need an extend.
1145 if (Operand.Val->getOperand(0).getValueType() < VT)
1146 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1147 else if (Operand.Val->getOperand(0).getValueType() > VT)
1148 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1149 else
1150 return Operand.Val->getOperand(0);
1151 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152 break;
Chris Lattner35481892005-12-23 00:16:34 +00001153 case ISD::BIT_CONVERT:
1154 // Basic sanity checking.
Chris Lattner1c6191f2006-03-21 19:20:37 +00001155 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1156 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001157 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001158 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001159 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1160 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001161 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001162 case ISD::SCALAR_TO_VECTOR:
1163 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1164 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1165 "Illegal SCALAR_TO_VECTOR node!");
1166 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001167 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001168 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1169 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001170 Operand.Val->getOperand(0));
1171 if (OpOpcode == ISD::FNEG) // --X -> X
1172 return Operand.Val->getOperand(0);
1173 break;
1174 case ISD::FABS:
1175 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1176 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1177 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001178 }
1179
Chris Lattner43247a12005-08-25 19:12:10 +00001180 SDNode *N;
1181 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1182 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001183 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001184 E = N = new SDNode(Opcode, Operand);
1185 } else {
1186 N = new SDNode(Opcode, Operand);
1187 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001188 N->setValueTypes(VT);
1189 AllNodes.push_back(N);
1190 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001191}
1192
Chris Lattner36019aa2005-04-18 03:48:41 +00001193
1194
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1196 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001197#ifndef NDEBUG
1198 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001199 case ISD::TokenFactor:
1200 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1201 N2.getValueType() == MVT::Other && "Invalid token factor!");
1202 break;
Chris Lattner76365122005-01-16 02:23:22 +00001203 case ISD::AND:
1204 case ISD::OR:
1205 case ISD::XOR:
1206 case ISD::UDIV:
1207 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001208 case ISD::MULHU:
1209 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001210 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1211 // fall through
1212 case ISD::ADD:
1213 case ISD::SUB:
1214 case ISD::MUL:
1215 case ISD::SDIV:
1216 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001217 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1218 // fall through.
1219 case ISD::FADD:
1220 case ISD::FSUB:
1221 case ISD::FMUL:
1222 case ISD::FDIV:
1223 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001224 assert(N1.getValueType() == N2.getValueType() &&
1225 N1.getValueType() == VT && "Binary operator types must match!");
1226 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001227 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1228 assert(N1.getValueType() == VT &&
1229 MVT::isFloatingPoint(N1.getValueType()) &&
1230 MVT::isFloatingPoint(N2.getValueType()) &&
1231 "Invalid FCOPYSIGN!");
1232 break;
Chris Lattner76365122005-01-16 02:23:22 +00001233 case ISD::SHL:
1234 case ISD::SRA:
1235 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001236 case ISD::ROTL:
1237 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001238 assert(VT == N1.getValueType() &&
1239 "Shift operators return type must be the same as their first arg");
1240 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001241 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001242 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001243 case ISD::FP_ROUND_INREG: {
1244 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1245 assert(VT == N1.getValueType() && "Not an inreg round!");
1246 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1247 "Cannot FP_ROUND_INREG integer types");
1248 assert(EVT <= VT && "Not rounding down!");
1249 break;
1250 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001251 case ISD::AssertSext:
1252 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001253 case ISD::SIGN_EXTEND_INREG: {
1254 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1255 assert(VT == N1.getValueType() && "Not an inreg extend!");
1256 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1257 "Cannot *_EXTEND_INREG FP types");
1258 assert(EVT <= VT && "Not extending!");
1259 }
1260
Chris Lattner76365122005-01-16 02:23:22 +00001261 default: break;
1262 }
1263#endif
1264
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1266 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1267 if (N1C) {
1268 if (N2C) {
1269 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1270 switch (Opcode) {
1271 case ISD::ADD: return getConstant(C1 + C2, VT);
1272 case ISD::SUB: return getConstant(C1 - C2, VT);
1273 case ISD::MUL: return getConstant(C1 * C2, VT);
1274 case ISD::UDIV:
1275 if (C2) return getConstant(C1 / C2, VT);
1276 break;
1277 case ISD::UREM :
1278 if (C2) return getConstant(C1 % C2, VT);
1279 break;
1280 case ISD::SDIV :
1281 if (C2) return getConstant(N1C->getSignExtended() /
1282 N2C->getSignExtended(), VT);
1283 break;
1284 case ISD::SREM :
1285 if (C2) return getConstant(N1C->getSignExtended() %
1286 N2C->getSignExtended(), VT);
1287 break;
1288 case ISD::AND : return getConstant(C1 & C2, VT);
1289 case ISD::OR : return getConstant(C1 | C2, VT);
1290 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001291 case ISD::SHL : return getConstant(C1 << C2, VT);
1292 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001293 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001294 case ISD::ROTL :
1295 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1296 VT);
1297 case ISD::ROTR :
1298 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1299 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001300 default: break;
1301 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001302 } else { // Cannonicalize constant to RHS if commutative
1303 if (isCommutativeBinOp(Opcode)) {
1304 std::swap(N1C, N2C);
1305 std::swap(N1, N2);
1306 }
1307 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001308 }
1309
1310 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1311 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001312 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001313 if (N2CFP) {
1314 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1315 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001316 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1317 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1318 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1319 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320 if (C2) return getConstantFP(C1 / C2, VT);
1321 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001322 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001323 if (C2) return getConstantFP(fmod(C1, C2), VT);
1324 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001325 case ISD::FCOPYSIGN: {
1326 union {
1327 double F;
1328 uint64_t I;
1329 } u1;
1330 union {
1331 double F;
1332 int64_t I;
1333 } u2;
1334 u1.F = C1;
1335 u2.F = C2;
1336 if (u2.I < 0) // Sign bit of RHS set?
1337 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1338 else
1339 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1340 return getConstantFP(u1.F, VT);
1341 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 default: break;
1343 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001344 } else { // Cannonicalize constant to RHS if commutative
1345 if (isCommutativeBinOp(Opcode)) {
1346 std::swap(N1CFP, N2CFP);
1347 std::swap(N1, N2);
1348 }
1349 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001350 }
1351
Chris Lattnerc3aae252005-01-07 07:46:32 +00001352 // Finally, fold operations that do not require constants.
1353 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001354 case ISD::FP_ROUND_INREG:
1355 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1356 break;
1357 case ISD::SIGN_EXTEND_INREG: {
1358 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1359 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001360 break;
1361 }
1362
Nate Begemaneea805e2005-04-13 21:23:31 +00001363 // FIXME: figure out how to safely handle things like
1364 // int foo(int x) { return 1 << (x & 255); }
1365 // int bar() { return foo(256); }
1366#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001367 case ISD::SHL:
1368 case ISD::SRL:
1369 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001370 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001371 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001372 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001373 else if (N2.getOpcode() == ISD::AND)
1374 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1375 // If the and is only masking out bits that cannot effect the shift,
1376 // eliminate the and.
1377 unsigned NumBits = MVT::getSizeInBits(VT);
1378 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1379 return getNode(Opcode, VT, N1, N2.getOperand(0));
1380 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001381 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001382#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001383 }
1384
Chris Lattner27e9b412005-05-11 18:57:39 +00001385 // Memoize this node if possible.
1386 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001387 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001388 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1389 if (BON) return SDOperand(BON, 0);
1390
1391 BON = N = new SDNode(Opcode, N1, N2);
1392 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001393 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001394 }
1395
Chris Lattner3e011362005-05-14 07:45:46 +00001396 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001397 AllNodes.push_back(N);
1398 return SDOperand(N, 0);
1399}
1400
Chris Lattnerc3aae252005-01-07 07:46:32 +00001401SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1402 SDOperand N1, SDOperand N2, SDOperand N3) {
1403 // Perform various simplifications.
1404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1405 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1406 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1407 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001408 case ISD::SETCC: {
1409 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001410 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001411 if (Simp.Val) return Simp;
1412 break;
1413 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414 case ISD::SELECT:
1415 if (N1C)
1416 if (N1C->getValue())
1417 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001418 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001419 return N3; // select false, X, Y -> Y
1420
1421 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001422 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001423 case ISD::BRCOND:
1424 if (N2C)
1425 if (N2C->getValue()) // Unconditional branch
1426 return getNode(ISD::BR, MVT::Other, N1, N3);
1427 else
1428 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001429 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001430 case ISD::VECTOR_SHUFFLE:
1431 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1432 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1433 N3.getOpcode() == ISD::BUILD_VECTOR &&
1434 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1435 "Illegal VECTOR_SHUFFLE node!");
1436 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001437 }
1438
Chris Lattner385328c2005-05-14 07:42:29 +00001439 std::vector<SDOperand> Ops;
1440 Ops.reserve(3);
1441 Ops.push_back(N1);
1442 Ops.push_back(N2);
1443 Ops.push_back(N3);
1444
Chris Lattner43247a12005-08-25 19:12:10 +00001445 // Memoize node if it doesn't produce a flag.
1446 SDNode *N;
1447 if (VT != MVT::Flag) {
1448 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1449 if (E) return SDOperand(E, 0);
1450 E = N = new SDNode(Opcode, N1, N2, N3);
1451 } else {
1452 N = new SDNode(Opcode, N1, N2, N3);
1453 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001454 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 AllNodes.push_back(N);
1456 return SDOperand(N, 0);
1457}
1458
1459SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001460 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001461 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001462 std::vector<SDOperand> Ops;
1463 Ops.reserve(4);
1464 Ops.push_back(N1);
1465 Ops.push_back(N2);
1466 Ops.push_back(N3);
1467 Ops.push_back(N4);
1468 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001469}
1470
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001471SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1472 SDOperand N1, SDOperand N2, SDOperand N3,
1473 SDOperand N4, SDOperand N5) {
1474 std::vector<SDOperand> Ops;
1475 Ops.reserve(5);
1476 Ops.push_back(N1);
1477 Ops.push_back(N2);
1478 Ops.push_back(N3);
1479 Ops.push_back(N4);
1480 Ops.push_back(N5);
1481 return getNode(Opcode, VT, Ops);
1482}
1483
Evan Cheng7038daf2005-12-10 00:37:58 +00001484SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1485 SDOperand Chain, SDOperand Ptr,
1486 SDOperand SV) {
1487 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1488 if (N) return SDOperand(N, 0);
1489 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1490
1491 // Loads have a token chain.
1492 setNodeValueTypes(N, VT, MVT::Other);
1493 AllNodes.push_back(N);
1494 return SDOperand(N, 0);
1495}
1496
1497SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1498 SDOperand Chain, SDOperand Ptr,
1499 SDOperand SV) {
1500 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1501 if (N) return SDOperand(N, 0);
1502 std::vector<SDOperand> Ops;
1503 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001504 Ops.push_back(Chain);
1505 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001506 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001507 Ops.push_back(getConstant(Count, MVT::i32));
1508 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001509 std::vector<MVT::ValueType> VTs;
1510 VTs.reserve(2);
1511 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1512 return getNode(ISD::VLOAD, VTs, Ops);
1513}
1514
1515SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1516 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1517 MVT::ValueType EVT) {
1518 std::vector<SDOperand> Ops;
1519 Ops.reserve(4);
1520 Ops.push_back(Chain);
1521 Ops.push_back(Ptr);
1522 Ops.push_back(SV);
1523 Ops.push_back(getValueType(EVT));
1524 std::vector<MVT::ValueType> VTs;
1525 VTs.reserve(2);
1526 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1527 return getNode(Opcode, VTs, Ops);
1528}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001529
Chris Lattner0437cdd2005-05-09 04:14:13 +00001530SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001531 assert((!V || isa<PointerType>(V->getType())) &&
1532 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001533 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1534 if (N) return SDOperand(N, 0);
1535
1536 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001537 AllNodes.push_back(N);
1538 return SDOperand(N, 0);
1539}
1540
Nate Begemanacc398c2006-01-25 18:21:52 +00001541SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1542 SDOperand Chain, SDOperand Ptr,
1543 SDOperand SV) {
1544 std::vector<SDOperand> Ops;
1545 Ops.reserve(3);
1546 Ops.push_back(Chain);
1547 Ops.push_back(Ptr);
1548 Ops.push_back(SV);
1549 std::vector<MVT::ValueType> VTs;
1550 VTs.reserve(2);
1551 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1552 return getNode(ISD::VAARG, VTs, Ops);
1553}
1554
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001555SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001556 std::vector<SDOperand> &Ops) {
1557 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001558 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001559 case 1: return getNode(Opcode, VT, Ops[0]);
1560 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1561 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001562 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001563 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001564
Chris Lattner89c34632005-05-14 06:20:26 +00001565 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001566 switch (Opcode) {
1567 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001568 case ISD::TRUNCSTORE: {
1569 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1570 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1571#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1572 // If this is a truncating store of a constant, convert to the desired type
1573 // and store it instead.
1574 if (isa<Constant>(Ops[0])) {
1575 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1576 if (isa<Constant>(Op))
1577 N1 = Op;
1578 }
1579 // Also for ConstantFP?
1580#endif
1581 if (Ops[0].getValueType() == EVT) // Normal store?
1582 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1583 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1584 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1585 "Can't do FP-INT conversion!");
1586 break;
1587 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001588 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001589 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001590 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1591 "LHS and RHS of condition must have same type!");
1592 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1593 "True and False arms of SelectCC must have same type!");
1594 assert(Ops[2].getValueType() == VT &&
1595 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001596 break;
1597 }
1598 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001599 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001600 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1601 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001602 break;
1603 }
Chris Lattneref847df2005-04-09 03:27:28 +00001604 }
1605
Chris Lattner385328c2005-05-14 07:42:29 +00001606 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001607 SDNode *N;
1608 if (VT != MVT::Flag) {
1609 SDNode *&E =
1610 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1611 if (E) return SDOperand(E, 0);
1612 E = N = new SDNode(Opcode, Ops);
1613 } else {
1614 N = new SDNode(Opcode, Ops);
1615 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001616 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001617 AllNodes.push_back(N);
1618 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001619}
1620
Chris Lattner89c34632005-05-14 06:20:26 +00001621SDOperand SelectionDAG::getNode(unsigned Opcode,
1622 std::vector<MVT::ValueType> &ResultTys,
1623 std::vector<SDOperand> &Ops) {
1624 if (ResultTys.size() == 1)
1625 return getNode(Opcode, ResultTys[0], Ops);
1626
Chris Lattner5f056bf2005-07-10 01:55:33 +00001627 switch (Opcode) {
1628 case ISD::EXTLOAD:
1629 case ISD::SEXTLOAD:
1630 case ISD::ZEXTLOAD: {
1631 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1632 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1633 // If they are asking for an extending load from/to the same thing, return a
1634 // normal load.
1635 if (ResultTys[0] == EVT)
1636 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001637 if (MVT::isVector(ResultTys[0])) {
1638 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1639 "Invalid vector extload!");
1640 } else {
1641 assert(EVT < ResultTys[0] &&
1642 "Should only be an extending load, not truncating!");
1643 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001644 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001645 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001646 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1647 "Cannot convert from FP to Int or Int -> FP!");
1648 break;
1649 }
1650
Chris Lattnere89083a2005-05-14 07:25:05 +00001651 // FIXME: figure out how to safely handle things like
1652 // int foo(int x) { return 1 << (x & 255); }
1653 // int bar() { return foo(256); }
1654#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001655 case ISD::SRA_PARTS:
1656 case ISD::SRL_PARTS:
1657 case ISD::SHL_PARTS:
1658 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001659 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001660 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1661 else if (N3.getOpcode() == ISD::AND)
1662 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1663 // If the and is only masking out bits that cannot effect the shift,
1664 // eliminate the and.
1665 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1666 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1667 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1668 }
1669 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001670#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001671 }
Chris Lattner89c34632005-05-14 06:20:26 +00001672
Chris Lattner43247a12005-08-25 19:12:10 +00001673 // Memoize the node unless it returns a flag.
1674 SDNode *N;
1675 if (ResultTys.back() != MVT::Flag) {
1676 SDNode *&E =
1677 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1678 if (E) return SDOperand(E, 0);
1679 E = N = new SDNode(Opcode, Ops);
1680 } else {
1681 N = new SDNode(Opcode, Ops);
1682 }
Chris Lattnera3255112005-11-08 23:30:28 +00001683 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001684 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001685 return SDOperand(N, 0);
1686}
1687
Chris Lattnera3255112005-11-08 23:30:28 +00001688void SelectionDAG::setNodeValueTypes(SDNode *N,
1689 std::vector<MVT::ValueType> &RetVals) {
1690 switch (RetVals.size()) {
1691 case 0: return;
1692 case 1: N->setValueTypes(RetVals[0]); return;
1693 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1694 default: break;
1695 }
1696
1697 std::list<std::vector<MVT::ValueType> >::iterator I =
1698 std::find(VTList.begin(), VTList.end(), RetVals);
1699 if (I == VTList.end()) {
1700 VTList.push_front(RetVals);
1701 I = VTList.begin();
1702 }
1703
1704 N->setValueTypes(&(*I)[0], I->size());
1705}
1706
1707void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1708 MVT::ValueType VT2) {
1709 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1710 E = VTList.end(); I != E; ++I) {
1711 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1712 N->setValueTypes(&(*I)[0], 2);
1713 return;
1714 }
1715 }
1716 std::vector<MVT::ValueType> V;
1717 V.push_back(VT1);
1718 V.push_back(VT2);
1719 VTList.push_front(V);
1720 N->setValueTypes(&(*VTList.begin())[0], 2);
1721}
1722
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001723/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1724/// specified operands. If the resultant node already exists in the DAG,
1725/// this does not modify the specified node, instead it returns the node that
1726/// already exists. If the resultant node does not exist in the DAG, the
1727/// input node is returned. As a degenerate case, if you specify the same
1728/// input operands as the node already has, the input node is returned.
1729SDOperand SelectionDAG::
1730UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1731 SDNode *N = InN.Val;
1732 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1733
1734 // Check to see if there is no change.
1735 if (Op == N->getOperand(0)) return InN;
1736
1737 // See if the modified node already exists.
1738 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1739 if (NewSlot && *NewSlot)
1740 return SDOperand(*NewSlot, InN.ResNo);
1741
1742 // Nope it doesn't. Remove the node from it's current place in the maps.
1743 if (NewSlot)
1744 RemoveNodeFromCSEMaps(N);
1745
1746 // Now we update the operands.
1747 N->OperandList[0].Val->removeUser(N);
1748 Op.Val->addUser(N);
1749 N->OperandList[0] = Op;
1750
1751 // If this gets put into a CSE map, add it.
1752 if (NewSlot) *NewSlot = N;
1753 return InN;
1754}
1755
1756SDOperand SelectionDAG::
1757UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1758 SDNode *N = InN.Val;
1759 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1760
1761 // Check to see if there is no change.
1762 bool AnyChange = false;
1763 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1764 return InN; // No operands changed, just return the input node.
1765
1766 // See if the modified node already exists.
1767 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1768 if (NewSlot && *NewSlot)
1769 return SDOperand(*NewSlot, InN.ResNo);
1770
1771 // Nope it doesn't. Remove the node from it's current place in the maps.
1772 if (NewSlot)
1773 RemoveNodeFromCSEMaps(N);
1774
1775 // Now we update the operands.
1776 if (N->OperandList[0] != Op1) {
1777 N->OperandList[0].Val->removeUser(N);
1778 Op1.Val->addUser(N);
1779 N->OperandList[0] = Op1;
1780 }
1781 if (N->OperandList[1] != Op2) {
1782 N->OperandList[1].Val->removeUser(N);
1783 Op2.Val->addUser(N);
1784 N->OperandList[1] = Op2;
1785 }
1786
1787 // If this gets put into a CSE map, add it.
1788 if (NewSlot) *NewSlot = N;
1789 return InN;
1790}
1791
1792SDOperand SelectionDAG::
1793UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1794 std::vector<SDOperand> Ops;
1795 Ops.push_back(Op1);
1796 Ops.push_back(Op2);
1797 Ops.push_back(Op3);
1798 return UpdateNodeOperands(N, Ops);
1799}
1800
1801SDOperand SelectionDAG::
1802UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1803 SDOperand Op3, SDOperand Op4) {
1804 std::vector<SDOperand> Ops;
1805 Ops.push_back(Op1);
1806 Ops.push_back(Op2);
1807 Ops.push_back(Op3);
1808 Ops.push_back(Op4);
1809 return UpdateNodeOperands(N, Ops);
1810}
1811
1812SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001813UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1814 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1815 std::vector<SDOperand> Ops;
1816 Ops.push_back(Op1);
1817 Ops.push_back(Op2);
1818 Ops.push_back(Op3);
1819 Ops.push_back(Op4);
1820 Ops.push_back(Op5);
1821 return UpdateNodeOperands(N, Ops);
1822}
1823
1824
1825SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001826UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1827 SDNode *N = InN.Val;
1828 assert(N->getNumOperands() == Ops.size() &&
1829 "Update with wrong number of operands");
1830
1831 // Check to see if there is no change.
1832 unsigned NumOps = Ops.size();
1833 bool AnyChange = false;
1834 for (unsigned i = 0; i != NumOps; ++i) {
1835 if (Ops[i] != N->getOperand(i)) {
1836 AnyChange = true;
1837 break;
1838 }
1839 }
1840
1841 // No operands changed, just return the input node.
1842 if (!AnyChange) return InN;
1843
1844 // See if the modified node already exists.
1845 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1846 if (NewSlot && *NewSlot)
1847 return SDOperand(*NewSlot, InN.ResNo);
1848
1849 // Nope it doesn't. Remove the node from it's current place in the maps.
1850 if (NewSlot)
1851 RemoveNodeFromCSEMaps(N);
1852
1853 // Now we update the operands.
1854 for (unsigned i = 0; i != NumOps; ++i) {
1855 if (N->OperandList[i] != Ops[i]) {
1856 N->OperandList[i].Val->removeUser(N);
1857 Ops[i].Val->addUser(N);
1858 N->OperandList[i] = Ops[i];
1859 }
1860 }
1861
1862 // If this gets put into a CSE map, add it.
1863 if (NewSlot) *NewSlot = N;
1864 return InN;
1865}
1866
1867
1868
Chris Lattner149c58c2005-08-16 18:17:10 +00001869
1870/// SelectNodeTo - These are used for target selectors to *mutate* the
1871/// specified node to have the specified return type, Target opcode, and
1872/// operands. Note that target opcodes are stored as
1873/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001874///
1875/// Note that SelectNodeTo returns the resultant node. If there is already a
1876/// node of the specified opcode and operands, it returns that node instead of
1877/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001878SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1879 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001880 // If an identical node already exists, use it.
1881 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1882 if (ON) return SDOperand(ON, 0);
1883
Chris Lattner7651fa42005-08-24 23:00:29 +00001884 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001885
Chris Lattner7651fa42005-08-24 23:00:29 +00001886 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1887 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001888
1889 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001890 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001891}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001892
Chris Lattnereb19e402005-11-30 22:45:14 +00001893SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1894 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001895 // If an identical node already exists, use it.
1896 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1897 std::make_pair(Op1, VT))];
1898 if (ON) return SDOperand(ON, 0);
1899
Chris Lattner149c58c2005-08-16 18:17:10 +00001900 RemoveNodeFromCSEMaps(N);
1901 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1902 N->setValueTypes(VT);
1903 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001904
1905 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001906 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001907}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001908
Chris Lattnereb19e402005-11-30 22:45:14 +00001909SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1910 MVT::ValueType VT, SDOperand Op1,
1911 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001912 // If an identical node already exists, use it.
1913 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1914 std::make_pair(Op1, Op2))];
1915 if (ON) return SDOperand(ON, 0);
1916
Chris Lattner149c58c2005-08-16 18:17:10 +00001917 RemoveNodeFromCSEMaps(N);
1918 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1919 N->setValueTypes(VT);
1920 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001921
1922 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001923 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001924}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001925
Chris Lattnereb19e402005-11-30 22:45:14 +00001926SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1927 MVT::ValueType VT, SDOperand Op1,
1928 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001929 // If an identical node already exists, use it.
1930 std::vector<SDOperand> OpList;
1931 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1932 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1933 std::make_pair(VT, OpList))];
1934 if (ON) return SDOperand(ON, 0);
1935
Chris Lattner149c58c2005-08-16 18:17:10 +00001936 RemoveNodeFromCSEMaps(N);
1937 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1938 N->setValueTypes(VT);
1939 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001940
1941 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001942 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001943}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001944
Chris Lattnereb19e402005-11-30 22:45:14 +00001945SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1946 MVT::ValueType VT, SDOperand Op1,
1947 SDOperand Op2, SDOperand Op3,
1948 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001949 // If an identical node already exists, use it.
1950 std::vector<SDOperand> OpList;
1951 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1952 OpList.push_back(Op4);
1953 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1954 std::make_pair(VT, OpList))];
1955 if (ON) return SDOperand(ON, 0);
1956
Nate Begeman294a0a12005-08-18 07:30:15 +00001957 RemoveNodeFromCSEMaps(N);
1958 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1959 N->setValueTypes(VT);
1960 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001961
1962 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001963 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001964}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001965
Chris Lattnereb19e402005-11-30 22:45:14 +00001966SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1967 MVT::ValueType VT, SDOperand Op1,
1968 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1969 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001970 // If an identical node already exists, use it.
1971 std::vector<SDOperand> OpList;
1972 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1973 OpList.push_back(Op4); OpList.push_back(Op5);
1974 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1975 std::make_pair(VT, OpList))];
1976 if (ON) return SDOperand(ON, 0);
1977
Chris Lattner6b09a292005-08-21 18:49:33 +00001978 RemoveNodeFromCSEMaps(N);
1979 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1980 N->setValueTypes(VT);
1981 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001982
1983 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001984 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001985}
Chris Lattner149c58c2005-08-16 18:17:10 +00001986
Chris Lattnereb19e402005-11-30 22:45:14 +00001987SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1988 MVT::ValueType VT, SDOperand Op1,
1989 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1990 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001991 // If an identical node already exists, use it.
1992 std::vector<SDOperand> OpList;
1993 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1994 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1995 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1996 std::make_pair(VT, OpList))];
1997 if (ON) return SDOperand(ON, 0);
1998
Evan Cheng61ca74b2005-11-30 02:04:11 +00001999 RemoveNodeFromCSEMaps(N);
2000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2001 N->setValueTypes(VT);
2002 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002003
2004 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002005 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002006}
2007
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002008SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2009 MVT::ValueType VT, SDOperand Op1,
2010 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2011 SDOperand Op5, SDOperand Op6,
2012 SDOperand Op7) {
2013 // If an identical node already exists, use it.
2014 std::vector<SDOperand> OpList;
2015 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2016 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2017 OpList.push_back(Op7);
2018 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2019 std::make_pair(VT, OpList))];
2020 if (ON) return SDOperand(ON, 0);
2021
2022 RemoveNodeFromCSEMaps(N);
2023 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2024 N->setValueTypes(VT);
2025 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2026
2027 ON = N; // Memoize the new node.
2028 return SDOperand(N, 0);
2029}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002030SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2031 MVT::ValueType VT, SDOperand Op1,
2032 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2033 SDOperand Op5, SDOperand Op6,
2034 SDOperand Op7, SDOperand Op8) {
2035 // If an identical node already exists, use it.
2036 std::vector<SDOperand> OpList;
2037 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2038 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2039 OpList.push_back(Op7); OpList.push_back(Op8);
2040 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2041 std::make_pair(VT, OpList))];
2042 if (ON) return SDOperand(ON, 0);
2043
2044 RemoveNodeFromCSEMaps(N);
2045 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2046 N->setValueTypes(VT);
2047 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2048
2049 ON = N; // Memoize the new node.
2050 return SDOperand(N, 0);
2051}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002052
Chris Lattnereb19e402005-11-30 22:45:14 +00002053SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2054 MVT::ValueType VT1, MVT::ValueType VT2,
2055 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002056 // If an identical node already exists, use it.
2057 std::vector<SDOperand> OpList;
2058 OpList.push_back(Op1); OpList.push_back(Op2);
2059 std::vector<MVT::ValueType> VTList;
2060 VTList.push_back(VT1); VTList.push_back(VT2);
2061 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2062 std::make_pair(VTList, OpList))];
2063 if (ON) return SDOperand(ON, 0);
2064
Chris Lattner0fb094f2005-11-19 01:44:53 +00002065 RemoveNodeFromCSEMaps(N);
2066 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2067 setNodeValueTypes(N, VT1, VT2);
2068 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002069
2070 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002071 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002072}
2073
Chris Lattnereb19e402005-11-30 22:45:14 +00002074SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2075 MVT::ValueType VT1, MVT::ValueType VT2,
2076 SDOperand Op1, SDOperand Op2,
2077 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002078 // If an identical node already exists, use it.
2079 std::vector<SDOperand> OpList;
2080 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2081 std::vector<MVT::ValueType> VTList;
2082 VTList.push_back(VT1); VTList.push_back(VT2);
2083 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2084 std::make_pair(VTList, OpList))];
2085 if (ON) return SDOperand(ON, 0);
2086
Chris Lattner0fb094f2005-11-19 01:44:53 +00002087 RemoveNodeFromCSEMaps(N);
2088 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2089 setNodeValueTypes(N, VT1, VT2);
2090 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002091
2092 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002093 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002094}
2095
Chris Lattnereb19e402005-11-30 22:45:14 +00002096SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2097 MVT::ValueType VT1, MVT::ValueType VT2,
2098 SDOperand Op1, SDOperand Op2,
2099 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002100 // If an identical node already exists, use it.
2101 std::vector<SDOperand> OpList;
2102 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2103 OpList.push_back(Op4);
2104 std::vector<MVT::ValueType> VTList;
2105 VTList.push_back(VT1); VTList.push_back(VT2);
2106 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2107 std::make_pair(VTList, OpList))];
2108 if (ON) return SDOperand(ON, 0);
2109
Chris Lattner0fb094f2005-11-19 01:44:53 +00002110 RemoveNodeFromCSEMaps(N);
2111 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2112 setNodeValueTypes(N, VT1, VT2);
2113 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002114
2115 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002116 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002117}
2118
Chris Lattnereb19e402005-11-30 22:45:14 +00002119SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2120 MVT::ValueType VT1, MVT::ValueType VT2,
2121 SDOperand Op1, SDOperand Op2,
2122 SDOperand Op3, SDOperand Op4,
2123 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002124 // If an identical node already exists, use it.
2125 std::vector<SDOperand> OpList;
2126 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2127 OpList.push_back(Op4); OpList.push_back(Op5);
2128 std::vector<MVT::ValueType> VTList;
2129 VTList.push_back(VT1); VTList.push_back(VT2);
2130 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2131 std::make_pair(VTList, OpList))];
2132 if (ON) return SDOperand(ON, 0);
2133
Chris Lattner0fb094f2005-11-19 01:44:53 +00002134 RemoveNodeFromCSEMaps(N);
2135 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2136 setNodeValueTypes(N, VT1, VT2);
2137 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002138
2139 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002140 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002141}
2142
Evan Cheng6ae46c42006-02-09 07:15:23 +00002143/// getTargetNode - These are used for target selectors to create a new node
2144/// with specified return type(s), target opcode, and operands.
2145///
2146/// Note that getTargetNode returns the resultant node. If there is already a
2147/// node of the specified opcode and operands, it returns that node instead of
2148/// the current one.
2149SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2150 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2151}
2152SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2153 SDOperand Op1) {
2154 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2155}
2156SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2157 SDOperand Op1, SDOperand Op2) {
2158 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2159}
2160SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2161 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2162 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2163}
2164SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2165 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2166 SDOperand Op4) {
2167 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2168}
2169SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2170 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2171 SDOperand Op4, SDOperand Op5) {
2172 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2173}
2174SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2175 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2176 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2177 std::vector<SDOperand> Ops;
2178 Ops.reserve(6);
2179 Ops.push_back(Op1);
2180 Ops.push_back(Op2);
2181 Ops.push_back(Op3);
2182 Ops.push_back(Op4);
2183 Ops.push_back(Op5);
2184 Ops.push_back(Op6);
2185 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2186}
2187SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2188 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2189 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2190 SDOperand Op7) {
2191 std::vector<SDOperand> Ops;
2192 Ops.reserve(7);
2193 Ops.push_back(Op1);
2194 Ops.push_back(Op2);
2195 Ops.push_back(Op3);
2196 Ops.push_back(Op4);
2197 Ops.push_back(Op5);
2198 Ops.push_back(Op6);
2199 Ops.push_back(Op7);
2200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2201}
2202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2203 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2204 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2205 SDOperand Op7, SDOperand Op8) {
2206 std::vector<SDOperand> Ops;
2207 Ops.reserve(8);
2208 Ops.push_back(Op1);
2209 Ops.push_back(Op2);
2210 Ops.push_back(Op3);
2211 Ops.push_back(Op4);
2212 Ops.push_back(Op5);
2213 Ops.push_back(Op6);
2214 Ops.push_back(Op7);
2215 Ops.push_back(Op8);
2216 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2217}
2218SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2219 std::vector<SDOperand> &Ops) {
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2223 MVT::ValueType VT2, SDOperand Op1) {
2224 std::vector<MVT::ValueType> ResultTys;
2225 ResultTys.push_back(VT1);
2226 ResultTys.push_back(VT2);
2227 std::vector<SDOperand> Ops;
2228 Ops.push_back(Op1);
2229 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2230}
2231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2232 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
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 Ops.push_back(Op2);
2239 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2240}
2241SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2242 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2243 SDOperand Op3) {
2244 std::vector<MVT::ValueType> ResultTys;
2245 ResultTys.push_back(VT1);
2246 ResultTys.push_back(VT2);
2247 std::vector<SDOperand> Ops;
2248 Ops.push_back(Op1);
2249 Ops.push_back(Op2);
2250 Ops.push_back(Op3);
2251 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2252}
2253SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2254 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2255 SDOperand Op3, SDOperand Op4) {
2256 std::vector<MVT::ValueType> ResultTys;
2257 ResultTys.push_back(VT1);
2258 ResultTys.push_back(VT2);
2259 std::vector<SDOperand> Ops;
2260 Ops.push_back(Op1);
2261 Ops.push_back(Op2);
2262 Ops.push_back(Op3);
2263 Ops.push_back(Op4);
2264 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2265}
2266SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2267 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2268 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2269 std::vector<MVT::ValueType> ResultTys;
2270 ResultTys.push_back(VT1);
2271 ResultTys.push_back(VT2);
2272 std::vector<SDOperand> Ops;
2273 Ops.push_back(Op1);
2274 Ops.push_back(Op2);
2275 Ops.push_back(Op3);
2276 Ops.push_back(Op4);
2277 Ops.push_back(Op5);
2278 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2279}
2280SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2281 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2282 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2283 SDOperand Op6) {
2284 std::vector<MVT::ValueType> ResultTys;
2285 ResultTys.push_back(VT1);
2286 ResultTys.push_back(VT2);
2287 std::vector<SDOperand> Ops;
2288 Ops.push_back(Op1);
2289 Ops.push_back(Op2);
2290 Ops.push_back(Op3);
2291 Ops.push_back(Op4);
2292 Ops.push_back(Op5);
2293 Ops.push_back(Op6);
2294 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2295}
2296SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2297 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2298 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2299 SDOperand Op6, SDOperand Op7) {
2300 std::vector<MVT::ValueType> ResultTys;
2301 ResultTys.push_back(VT1);
2302 ResultTys.push_back(VT2);
2303 std::vector<SDOperand> Ops;
2304 Ops.push_back(Op1);
2305 Ops.push_back(Op2);
2306 Ops.push_back(Op3);
2307 Ops.push_back(Op4);
2308 Ops.push_back(Op5);
2309 Ops.push_back(Op6);
2310 Ops.push_back(Op7);
2311 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2312}
2313SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2314 MVT::ValueType VT2, MVT::ValueType VT3,
2315 SDOperand Op1, SDOperand Op2) {
2316 std::vector<MVT::ValueType> ResultTys;
2317 ResultTys.push_back(VT1);
2318 ResultTys.push_back(VT2);
2319 ResultTys.push_back(VT3);
2320 std::vector<SDOperand> Ops;
2321 Ops.push_back(Op1);
2322 Ops.push_back(Op2);
2323 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2324}
2325SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2326 MVT::ValueType VT2, MVT::ValueType VT3,
2327 SDOperand Op1, SDOperand Op2,
2328 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2329 std::vector<MVT::ValueType> ResultTys;
2330 ResultTys.push_back(VT1);
2331 ResultTys.push_back(VT2);
2332 ResultTys.push_back(VT3);
2333 std::vector<SDOperand> Ops;
2334 Ops.push_back(Op1);
2335 Ops.push_back(Op2);
2336 Ops.push_back(Op3);
2337 Ops.push_back(Op4);
2338 Ops.push_back(Op5);
2339 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2340}
2341SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2342 MVT::ValueType VT2, MVT::ValueType VT3,
2343 SDOperand Op1, SDOperand Op2,
2344 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2345 SDOperand Op6) {
2346 std::vector<MVT::ValueType> ResultTys;
2347 ResultTys.push_back(VT1);
2348 ResultTys.push_back(VT2);
2349 ResultTys.push_back(VT3);
2350 std::vector<SDOperand> Ops;
2351 Ops.push_back(Op1);
2352 Ops.push_back(Op2);
2353 Ops.push_back(Op3);
2354 Ops.push_back(Op4);
2355 Ops.push_back(Op5);
2356 Ops.push_back(Op6);
2357 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2358}
2359SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2360 MVT::ValueType VT2, MVT::ValueType VT3,
2361 SDOperand Op1, SDOperand Op2,
2362 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2363 SDOperand Op6, SDOperand Op7) {
2364 std::vector<MVT::ValueType> ResultTys;
2365 ResultTys.push_back(VT1);
2366 ResultTys.push_back(VT2);
2367 ResultTys.push_back(VT3);
2368 std::vector<SDOperand> Ops;
2369 Ops.push_back(Op1);
2370 Ops.push_back(Op2);
2371 Ops.push_back(Op3);
2372 Ops.push_back(Op4);
2373 Ops.push_back(Op5);
2374 Ops.push_back(Op6);
2375 Ops.push_back(Op7);
2376 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2377}
2378SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2379 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2380 std::vector<MVT::ValueType> ResultTys;
2381 ResultTys.push_back(VT1);
2382 ResultTys.push_back(VT2);
2383 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2384}
2385
Chris Lattner0fb094f2005-11-19 01:44:53 +00002386// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002387/// This can cause recursive merging of nodes in the DAG.
2388///
Chris Lattner8b52f212005-08-26 18:36:28 +00002389/// This version assumes From/To have a single result value.
2390///
Chris Lattner1e111c72005-09-07 05:37:01 +00002391void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2392 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002393 SDNode *From = FromN.Val, *To = ToN.Val;
2394 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2395 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002396 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002397
Chris Lattner8b8749f2005-08-17 19:00:20 +00002398 while (!From->use_empty()) {
2399 // Process users until they are all gone.
2400 SDNode *U = *From->use_begin();
2401
2402 // This node is about to morph, remove its old self from the CSE maps.
2403 RemoveNodeFromCSEMaps(U);
2404
Chris Lattner65113b22005-11-08 22:07:03 +00002405 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2406 I != E; ++I)
2407 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002408 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002409 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002410 To->addUser(U);
2411 }
2412
2413 // Now that we have modified U, add it back to the CSE maps. If it already
2414 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002415 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2416 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002417 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002418 if (Deleted) Deleted->push_back(U);
2419 DeleteNodeNotInCSEMaps(U);
2420 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002421 }
2422}
2423
2424/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2425/// This can cause recursive merging of nodes in the DAG.
2426///
2427/// This version assumes From/To have matching types and numbers of result
2428/// values.
2429///
Chris Lattner1e111c72005-09-07 05:37:01 +00002430void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2431 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002432 assert(From != To && "Cannot replace uses of with self");
2433 assert(From->getNumValues() == To->getNumValues() &&
2434 "Cannot use this version of ReplaceAllUsesWith!");
2435 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002436 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002437 return;
2438 }
2439
2440 while (!From->use_empty()) {
2441 // Process users until they are all gone.
2442 SDNode *U = *From->use_begin();
2443
2444 // This node is about to morph, remove its old self from the CSE maps.
2445 RemoveNodeFromCSEMaps(U);
2446
Chris Lattner65113b22005-11-08 22:07:03 +00002447 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2448 I != E; ++I)
2449 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002450 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002451 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002452 To->addUser(U);
2453 }
2454
2455 // Now that we have modified U, add it back to the CSE maps. If it already
2456 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002457 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2458 ReplaceAllUsesWith(U, Existing, Deleted);
2459 // U is now dead.
2460 if (Deleted) Deleted->push_back(U);
2461 DeleteNodeNotInCSEMaps(U);
2462 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002463 }
2464}
2465
Chris Lattner8b52f212005-08-26 18:36:28 +00002466/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2467/// This can cause recursive merging of nodes in the DAG.
2468///
2469/// This version can replace From with any result values. To must match the
2470/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002471void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002472 const std::vector<SDOperand> &To,
2473 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002474 assert(From->getNumValues() == To.size() &&
2475 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002476 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002477 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002478 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002479 return;
2480 }
2481
2482 while (!From->use_empty()) {
2483 // Process users until they are all gone.
2484 SDNode *U = *From->use_begin();
2485
2486 // This node is about to morph, remove its old self from the CSE maps.
2487 RemoveNodeFromCSEMaps(U);
2488
Chris Lattner65113b22005-11-08 22:07:03 +00002489 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2490 I != E; ++I)
2491 if (I->Val == From) {
2492 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002493 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002494 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002495 ToOp.Val->addUser(U);
2496 }
2497
2498 // Now that we have modified U, add it back to the CSE maps. If it already
2499 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002500 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2501 ReplaceAllUsesWith(U, Existing, Deleted);
2502 // U is now dead.
2503 if (Deleted) Deleted->push_back(U);
2504 DeleteNodeNotInCSEMaps(U);
2505 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002506 }
2507}
2508
Chris Lattner012f2412006-02-17 21:58:01 +00002509/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2510/// uses of other values produced by From.Val alone. The Deleted vector is
2511/// handled the same was as for ReplaceAllUsesWith.
2512void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2513 std::vector<SDNode*> &Deleted) {
2514 assert(From != To && "Cannot replace a value with itself");
2515 // Handle the simple, trivial, case efficiently.
2516 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2517 ReplaceAllUsesWith(From, To, &Deleted);
2518 return;
2519 }
2520
2521 // Get all of the users in a nice, deterministically ordered, uniqued set.
2522 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2523
2524 while (!Users.empty()) {
2525 // We know that this user uses some value of From. If it is the right
2526 // value, update it.
2527 SDNode *User = Users.back();
2528 Users.pop_back();
2529
2530 for (SDOperand *Op = User->OperandList,
2531 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2532 if (*Op == From) {
2533 // Okay, we know this user needs to be updated. Remove its old self
2534 // from the CSE maps.
2535 RemoveNodeFromCSEMaps(User);
2536
2537 // Update all operands that match "From".
2538 for (; Op != E; ++Op) {
2539 if (*Op == From) {
2540 From.Val->removeUser(User);
2541 *Op = To;
2542 To.Val->addUser(User);
2543 }
2544 }
2545
2546 // Now that we have modified User, add it back to the CSE maps. If it
2547 // already exists there, recursively merge the results together.
2548 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2549 unsigned NumDeleted = Deleted.size();
2550 ReplaceAllUsesWith(User, Existing, &Deleted);
2551
2552 // User is now dead.
2553 Deleted.push_back(User);
2554 DeleteNodeNotInCSEMaps(User);
2555
2556 // We have to be careful here, because ReplaceAllUsesWith could have
2557 // deleted a user of From, which means there may be dangling pointers
2558 // in the "Users" setvector. Scan over the deleted node pointers and
2559 // remove them from the setvector.
2560 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2561 Users.remove(Deleted[i]);
2562 }
2563 break; // Exit the operand scanning loop.
2564 }
2565 }
2566 }
2567}
2568
Chris Lattner7b2880c2005-08-24 22:44:39 +00002569
Jim Laskey58b968b2005-08-17 20:08:02 +00002570//===----------------------------------------------------------------------===//
2571// SDNode Class
2572//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002573
Chris Lattnera3255112005-11-08 23:30:28 +00002574
2575/// getValueTypeList - Return a pointer to the specified value type.
2576///
2577MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2578 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2579 VTs[VT] = VT;
2580 return &VTs[VT];
2581}
2582
Chris Lattner5c884562005-01-12 18:37:47 +00002583/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2584/// indicated value. This method ignores uses of other values defined by this
2585/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002586bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002587 assert(Value < getNumValues() && "Bad value!");
2588
2589 // If there is only one value, this is easy.
2590 if (getNumValues() == 1)
2591 return use_size() == NUses;
2592 if (Uses.size() < NUses) return false;
2593
Evan Cheng4ee62112006-02-05 06:29:23 +00002594 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002595
2596 std::set<SDNode*> UsersHandled;
2597
Evan Cheng4ee62112006-02-05 06:29:23 +00002598 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002599 UI != E; ++UI) {
2600 SDNode *User = *UI;
2601 if (User->getNumOperands() == 1 ||
2602 UsersHandled.insert(User).second) // First time we've seen this?
2603 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2604 if (User->getOperand(i) == TheValue) {
2605 if (NUses == 0)
2606 return false; // too many uses
2607 --NUses;
2608 }
2609 }
2610
2611 // Found exactly the right number of uses?
2612 return NUses == 0;
2613}
2614
2615
Evan Cheng4ee62112006-02-05 06:29:23 +00002616// isOnlyUse - Return true if this node is the only use of N.
2617bool SDNode::isOnlyUse(SDNode *N) const {
2618 bool Seen = false;
2619 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2620 SDNode *User = *I;
2621 if (User == this)
2622 Seen = true;
2623 else
2624 return false;
2625 }
2626
2627 return Seen;
2628}
2629
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002630// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002631bool SDOperand::isOperand(SDNode *N) const {
2632 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2633 if (*this == N->getOperand(i))
2634 return true;
2635 return false;
2636}
2637
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002638bool SDNode::isOperand(SDNode *N) const {
2639 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2640 if (this == N->OperandList[i].Val)
2641 return true;
2642 return false;
2643}
Evan Cheng4ee62112006-02-05 06:29:23 +00002644
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002645const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002646 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002647 default:
2648 if (getOpcode() < ISD::BUILTIN_OP_END)
2649 return "<<Unknown DAG Node>>";
2650 else {
Evan Cheng72261582005-12-20 06:22:03 +00002651 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002652 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002653 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2654 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002655
Evan Cheng72261582005-12-20 06:22:03 +00002656 TargetLowering &TLI = G->getTargetLoweringInfo();
2657 const char *Name =
2658 TLI.getTargetNodeName(getOpcode());
2659 if (Name) return Name;
2660 }
2661
2662 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002663 }
2664
Andrew Lenharth95762122005-03-31 21:24:06 +00002665 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002666 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002667 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002668 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002669 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002670 case ISD::AssertSext: return "AssertSext";
2671 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002672
2673 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002674 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002675 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002676 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002677
2678 case ISD::Constant: return "Constant";
2679 case ISD::ConstantFP: return "ConstantFP";
2680 case ISD::GlobalAddress: return "GlobalAddress";
2681 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002682 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002683 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner70a248d2006-03-27 06:45:25 +00002684 case ISD::INTRINSIC:
2685 bool hasChain = getOperand(0).getValueType() == MVT::Other;
2686 unsigned IID = cast<ConstantSDNode>(getOperand(hasChain))->getValue();
2687 return Intrinsic::getName((Intrinsic::ID)IID);
Evan Cheng1ab7d852006-03-01 00:51:13 +00002688
Chris Lattnerb2827b02006-03-19 00:52:58 +00002689 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002690 case ISD::TargetConstant: return "TargetConstant";
2691 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002692 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2693 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002694 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002695 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002696
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002697 case ISD::CopyToReg: return "CopyToReg";
2698 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002699 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002700 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002701 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002702 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002703
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002704 // Unary operators
2705 case ISD::FABS: return "fabs";
2706 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002707 case ISD::FSQRT: return "fsqrt";
2708 case ISD::FSIN: return "fsin";
2709 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002710
2711 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002712 case ISD::ADD: return "add";
2713 case ISD::SUB: return "sub";
2714 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002715 case ISD::MULHU: return "mulhu";
2716 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002717 case ISD::SDIV: return "sdiv";
2718 case ISD::UDIV: return "udiv";
2719 case ISD::SREM: return "srem";
2720 case ISD::UREM: return "urem";
2721 case ISD::AND: return "and";
2722 case ISD::OR: return "or";
2723 case ISD::XOR: return "xor";
2724 case ISD::SHL: return "shl";
2725 case ISD::SRA: return "sra";
2726 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002727 case ISD::ROTL: return "rotl";
2728 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002729 case ISD::FADD: return "fadd";
2730 case ISD::FSUB: return "fsub";
2731 case ISD::FMUL: return "fmul";
2732 case ISD::FDIV: return "fdiv";
2733 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002734 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002735 case ISD::VADD: return "vadd";
2736 case ISD::VSUB: return "vsub";
2737 case ISD::VMUL: return "vmul";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002738
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002739 case ISD::SETCC: return "setcc";
2740 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002741 case ISD::SELECT_CC: return "select_cc";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002742 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2743 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002744 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2745 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerce872152006-03-19 06:31:19 +00002746 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerfb194b92006-03-19 23:56:04 +00002747 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2748 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnere25ca692006-03-22 20:09:35 +00002749 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002750 case ISD::ADDC: return "addc";
2751 case ISD::ADDE: return "adde";
2752 case ISD::SUBC: return "subc";
2753 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002754 case ISD::SHL_PARTS: return "shl_parts";
2755 case ISD::SRA_PARTS: return "sra_parts";
2756 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002757
Chris Lattner7f644642005-04-28 21:44:03 +00002758 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002759 case ISD::SIGN_EXTEND: return "sign_extend";
2760 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002761 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002762 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002763 case ISD::TRUNCATE: return "truncate";
2764 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002765 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002766 case ISD::FP_EXTEND: return "fp_extend";
2767
2768 case ISD::SINT_TO_FP: return "sint_to_fp";
2769 case ISD::UINT_TO_FP: return "uint_to_fp";
2770 case ISD::FP_TO_SINT: return "fp_to_sint";
2771 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002772 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002773
2774 // Control flow instructions
2775 case ISD::BR: return "br";
2776 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002777 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002778 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002779 case ISD::CALLSEQ_START: return "callseq_start";
2780 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002781
2782 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002783 case ISD::LOAD: return "load";
2784 case ISD::STORE: return "store";
2785 case ISD::VLOAD: return "vload";
2786 case ISD::EXTLOAD: return "extload";
2787 case ISD::SEXTLOAD: return "sextload";
2788 case ISD::ZEXTLOAD: return "zextload";
2789 case ISD::TRUNCSTORE: return "truncstore";
2790 case ISD::VAARG: return "vaarg";
2791 case ISD::VACOPY: return "vacopy";
2792 case ISD::VAEND: return "vaend";
2793 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002794 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002795 case ISD::EXTRACT_ELEMENT: return "extract_element";
2796 case ISD::BUILD_PAIR: return "build_pair";
2797 case ISD::STACKSAVE: return "stacksave";
2798 case ISD::STACKRESTORE: return "stackrestore";
2799
2800 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002801 case ISD::MEMSET: return "memset";
2802 case ISD::MEMCPY: return "memcpy";
2803 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002804
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002805 // Bit manipulation
2806 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002807 case ISD::CTPOP: return "ctpop";
2808 case ISD::CTTZ: return "cttz";
2809 case ISD::CTLZ: return "ctlz";
2810
Chris Lattner36ce6912005-11-29 06:21:05 +00002811 // Debug info
2812 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002813 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002814 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002816 case ISD::CONDCODE:
2817 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002818 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002819 case ISD::SETOEQ: return "setoeq";
2820 case ISD::SETOGT: return "setogt";
2821 case ISD::SETOGE: return "setoge";
2822 case ISD::SETOLT: return "setolt";
2823 case ISD::SETOLE: return "setole";
2824 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002825
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002826 case ISD::SETO: return "seto";
2827 case ISD::SETUO: return "setuo";
2828 case ISD::SETUEQ: return "setue";
2829 case ISD::SETUGT: return "setugt";
2830 case ISD::SETUGE: return "setuge";
2831 case ISD::SETULT: return "setult";
2832 case ISD::SETULE: return "setule";
2833 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002834
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002835 case ISD::SETEQ: return "seteq";
2836 case ISD::SETGT: return "setgt";
2837 case ISD::SETGE: return "setge";
2838 case ISD::SETLT: return "setlt";
2839 case ISD::SETLE: return "setle";
2840 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002841 }
2842 }
2843}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002844
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002845void SDNode::dump() const { dump(0); }
2846void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002847 std::cerr << (void*)this << ": ";
2848
2849 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2850 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002851 if (getValueType(i) == MVT::Other)
2852 std::cerr << "ch";
2853 else
2854 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002855 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002856 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002857
2858 std::cerr << " ";
2859 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2860 if (i) std::cerr << ", ";
2861 std::cerr << (void*)getOperand(i).Val;
2862 if (unsigned RN = getOperand(i).ResNo)
2863 std::cerr << ":" << RN;
2864 }
2865
2866 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2867 std::cerr << "<" << CSDN->getValue() << ">";
2868 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2869 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002870 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002871 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002872 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002873 std::cerr << "<";
2874 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002875 if (offset > 0)
2876 std::cerr << " + " << offset;
2877 else
2878 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002879 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002880 std::cerr << "<" << FIDN->getIndex() << ">";
2881 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002882 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002883 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002884 if (offset > 0)
2885 std::cerr << " + " << offset;
2886 else
2887 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002888 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002889 std::cerr << "<";
2890 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2891 if (LBB)
2892 std::cerr << LBB->getName() << " ";
2893 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002894 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002895 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002896 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2897 } else {
2898 std::cerr << " #" << R->getReg();
2899 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002900 } else if (const ExternalSymbolSDNode *ES =
2901 dyn_cast<ExternalSymbolSDNode>(this)) {
2902 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002903 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2904 if (M->getValue())
2905 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2906 else
2907 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002908 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2909 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002910 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002911}
2912
Chris Lattnerde202b32005-11-09 23:47:37 +00002913static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002914 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2915 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002916 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002917 else
2918 std::cerr << "\n" << std::string(indent+2, ' ')
2919 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002920
Chris Lattnerea946cd2005-01-09 20:38:33 +00002921
2922 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002923 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002924}
2925
Chris Lattnerc3aae252005-01-07 07:46:32 +00002926void SelectionDAG::dump() const {
2927 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002928 std::vector<const SDNode*> Nodes;
2929 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2930 I != E; ++I)
2931 Nodes.push_back(I);
2932
Chris Lattner49d24712005-01-09 20:26:36 +00002933 std::sort(Nodes.begin(), Nodes.end());
2934
2935 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002936 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002937 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002938 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002939
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002940 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002941
Chris Lattnerc3aae252005-01-07 07:46:32 +00002942 std::cerr << "\n\n";
2943}
2944
Evan Chengfae9f1c2006-02-09 22:11:03 +00002945/// InsertISelMapEntry - A helper function to insert a key / element pair
2946/// into a SDOperand to SDOperand map. This is added to avoid the map
2947/// insertion operator from being inlined.
2948void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2949 SDNode *Key, unsigned KeyResNo,
2950 SDNode *Element, unsigned ElementResNo) {
2951 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2952 SDOperand(Element, ElementResNo)));
2953}