blob: ec1050fd72a2028900bc3112a72aa599768b1ec4 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000021#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000022#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000025#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000026#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000027#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000028#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000029#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000030#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner5cdcc582005-01-09 20:52:51 +000033static bool isCommutativeBinOp(unsigned Opcode) {
34 switch (Opcode) {
35 case ISD::ADD:
36 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000037 case ISD::MULHU:
38 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000039 case ISD::FADD:
40 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000041 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need commutative info for user ops!
45 }
46}
47
Chris Lattner5cdcc582005-01-09 20:52:51 +000048// isInvertibleForFree - Return true if there is no cost to emitting the logical
49// inverse of this node.
50static bool isInvertibleForFree(SDOperand N) {
51 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000052 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000053 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000054 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000055}
56
Jim Laskey58b968b2005-08-17 20:08:02 +000057//===----------------------------------------------------------------------===//
58// ConstantFPSDNode Class
59//===----------------------------------------------------------------------===//
60
61/// isExactlyValue - We don't rely on operator== working on double values, as
62/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
63/// As such, this method can be used to do an exact bit-for-bit comparison of
64/// two floating point values.
65bool ConstantFPSDNode::isExactlyValue(double V) const {
66 return DoubleToBits(V) == DoubleToBits(Value);
67}
68
69//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000070// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000071//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000072
Evan Chenga8df1662006-03-27 06:58:47 +000073/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000074/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000075bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000076 // Look through a bit convert.
77 if (N->getOpcode() == ISD::BIT_CONVERT)
78 N = N->getOperand(0).Val;
79
Evan Chenga8df1662006-03-27 06:58:47 +000080 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000081
82 unsigned i = 0, e = N->getNumOperands();
83
84 // Skip over all of the undef values.
85 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
86 ++i;
87
88 // Do not accept an all-undef vector.
89 if (i == e) return false;
90
91 // Do not accept build_vectors that aren't all constants or which have non-~0
92 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000093 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000094 if (isa<ConstantSDNode>(NotZero)) {
95 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
96 return false;
97 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000098 MVT::ValueType VT = NotZero.getValueType();
99 if (VT== MVT::f64) {
100 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
101 (uint64_t)-1)
102 return false;
103 } else {
104 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 (uint32_t)-1)
106 return false;
107 }
Evan Chenga8df1662006-03-27 06:58:47 +0000108 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000109 return false;
110
111 // Okay, we have at least one ~0 value, check to see if the rest match or are
112 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000113 for (++i; i != e; ++i)
114 if (N->getOperand(i) != NotZero &&
115 N->getOperand(i).getOpcode() != ISD::UNDEF)
116 return false;
117 return true;
118}
119
120
Evan Cheng4a147842006-03-26 09:50:58 +0000121/// isBuildVectorAllZeros - Return true if the specified node is a
122/// BUILD_VECTOR where all of the elements are 0 or undef.
123bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000124 // Look through a bit convert.
125 if (N->getOpcode() == ISD::BIT_CONVERT)
126 N = N->getOperand(0).Val;
127
Evan Cheng4a147842006-03-26 09:50:58 +0000128 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129
130 unsigned i = 0, e = N->getNumOperands();
131
132 // Skip over all of the undef values.
133 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
134 ++i;
135
136 // Do not accept an all-undef vector.
137 if (i == e) return false;
138
139 // Do not accept build_vectors that aren't all constants or which have non-~0
140 // elements.
141 SDOperand Zero = N->getOperand(i);
142 if (isa<ConstantSDNode>(Zero)) {
143 if (!cast<ConstantSDNode>(Zero)->isNullValue())
144 return false;
145 } else if (isa<ConstantFPSDNode>(Zero)) {
146 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
147 return false;
148 } else
149 return false;
150
151 // Okay, we have at least one ~0 value, check to see if the rest match or are
152 // undefs.
153 for (++i; i != e; ++i)
154 if (N->getOperand(i) != Zero &&
155 N->getOperand(i).getOpcode() != ISD::UNDEF)
156 return false;
157 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000158}
159
Chris Lattnerc3aae252005-01-07 07:46:32 +0000160/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
161/// when given the operation for (X op Y).
162ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
163 // To perform this operation, we just need to swap the L and G bits of the
164 // operation.
165 unsigned OldL = (Operation >> 2) & 1;
166 unsigned OldG = (Operation >> 1) & 1;
167 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
168 (OldL << 1) | // New G bit
169 (OldG << 2)); // New L bit.
170}
171
172/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
173/// 'op' is a valid SetCC operation.
174ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
175 unsigned Operation = Op;
176 if (isInteger)
177 Operation ^= 7; // Flip L, G, E bits, but not U.
178 else
179 Operation ^= 15; // Flip all of the condition bits.
180 if (Operation > ISD::SETTRUE2)
181 Operation &= ~8; // Don't let N and U bits get set.
182 return ISD::CondCode(Operation);
183}
184
185
186/// isSignedOp - For an integer comparison, return 1 if the comparison is a
187/// signed operation and 2 if the result is an unsigned comparison. Return zero
188/// if the operation does not depend on the sign of the input (setne and seteq).
189static int isSignedOp(ISD::CondCode Opcode) {
190 switch (Opcode) {
191 default: assert(0 && "Illegal integer setcc operation!");
192 case ISD::SETEQ:
193 case ISD::SETNE: return 0;
194 case ISD::SETLT:
195 case ISD::SETLE:
196 case ISD::SETGT:
197 case ISD::SETGE: return 1;
198 case ISD::SETULT:
199 case ISD::SETULE:
200 case ISD::SETUGT:
201 case ISD::SETUGE: return 2;
202 }
203}
204
205/// getSetCCOrOperation - Return the result of a logical OR between different
206/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
207/// returns SETCC_INVALID if it is not possible to represent the resultant
208/// comparison.
209ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
210 bool isInteger) {
211 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
212 // Cannot fold a signed integer setcc with an unsigned integer setcc.
213 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000214
Chris Lattnerc3aae252005-01-07 07:46:32 +0000215 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216
Chris Lattnerc3aae252005-01-07 07:46:32 +0000217 // If the N and U bits get set then the resultant comparison DOES suddenly
218 // care about orderedness, and is true when ordered.
219 if (Op > ISD::SETTRUE2)
220 Op &= ~16; // Clear the N bit.
221 return ISD::CondCode(Op);
222}
223
224/// getSetCCAndOperation - Return the result of a logical AND between different
225/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
226/// function returns zero if it is not possible to represent the resultant
227/// comparison.
228ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
229 bool isInteger) {
230 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
231 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000232 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000233
234 // Combine all of the condition bits.
235 return ISD::CondCode(Op1 & Op2);
236}
237
Chris Lattnerb48da392005-01-23 04:39:44 +0000238const TargetMachine &SelectionDAG::getTarget() const {
239 return TLI.getTargetMachine();
240}
241
Jim Laskey58b968b2005-08-17 20:08:02 +0000242//===----------------------------------------------------------------------===//
243// SelectionDAG Class
244//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000245
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000246/// RemoveDeadNodes - This method deletes all unreachable nodes in the
247/// SelectionDAG, including nodes (like loads) that have uses of their token
248/// chain but no other uses and no side effect. If a node is passed in as an
249/// argument, it is used as the seed for node deletion.
250void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000251 // Create a dummy node (which is not added to allnodes), that adds a reference
252 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000253 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254
Chris Lattnerf469cb62005-11-08 18:52:27 +0000255 bool MadeChange = false;
256
Chris Lattner8b8749f2005-08-17 19:00:20 +0000257 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000258 if (N && N->use_empty()) {
259 DestroyDeadNode(N);
260 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 }
262
Chris Lattnerde202b32005-11-09 23:47:37 +0000263 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
264 if (I->use_empty() && I->getOpcode() != 65535) {
265 // Node is dead, recursively delete newly dead uses.
266 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000267 MadeChange = true;
268 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000269
270 // Walk the nodes list, removing the nodes we've marked as dead.
271 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000272 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
273 SDNode *N = I++;
274 if (N->use_empty())
275 AllNodes.erase(N);
276 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000277 }
278
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000279 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000280 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000281}
282
Chris Lattnerf469cb62005-11-08 18:52:27 +0000283/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
284/// graph. If it is the last user of any of its operands, recursively process
285/// them the same way.
286///
287void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 // Okay, we really are going to delete this node. First take this out of the
289 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000290 RemoveNodeFromCSEMaps(N);
291
292 // Next, brutally remove the operand list. This is safe to do, as there are
293 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000294 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
295 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000296 O->removeUser(N);
297
298 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000299 if (O->use_empty())
300 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000301 }
Chris Lattner65113b22005-11-08 22:07:03 +0000302 delete[] N->OperandList;
303 N->OperandList = 0;
304 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000305
306 // Mark the node as dead.
307 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000308}
309
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000310void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
312
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
315
Chris Lattner1e111c72005-09-07 05:37:01 +0000316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
319}
320
321void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
322
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000323 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000324 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000325
326 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
330 N->OperandList = 0;
331 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000332
333 delete N;
334}
335
Chris Lattner149c58c2005-08-16 18:17:10 +0000336/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337/// correspond to it. This is useful when we're about to delete or repurpose
338/// the node. We don't want future request for structurally identical nodes
339/// to return N anymore.
340void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000343 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000344 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000345 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
346 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000348 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000349 Erased = TargetConstants.erase(std::make_pair(
350 cast<ConstantSDNode>(N)->getValue(),
351 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000352 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000353 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000354 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000355 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000356 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000357 }
Chris Lattner3181a772006-01-29 06:26:56 +0000358 case ISD::TargetConstantFP: {
359 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
360 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
361 break;
362 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000363 case ISD::STRING:
364 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
365 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000366 case ISD::CONDCODE:
367 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
368 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000370 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
371 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000372 case ISD::GlobalAddress: {
373 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
374 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
375 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000377 }
378 case ISD::TargetGlobalAddress: {
379 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
380 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
381 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000382 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000383 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000386 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000387 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000389 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000390 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000391 Erased = ConstantPoolIndices.
392 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000393 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
394 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000395 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000396 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000397 Erased = TargetConstantPoolIndices.
398 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000399 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
400 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000401 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000403 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000404 break;
405 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000406 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000407 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000408 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000409 Erased =
410 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000411 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000412 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000413 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000414 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
415 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000416 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000417 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
418 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000419 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000420 case ISD::SRCVALUE: {
421 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000422 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000423 break;
424 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000425 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000426 Erased = Loads.erase(std::make_pair(N->getOperand(1),
427 std::make_pair(N->getOperand(0),
428 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000429 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000430 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000431 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000432 if (N->getNumOperands() == 0) {
433 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
434 N->getValueType(0)));
435 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000436 Erased =
437 UnaryOps.erase(std::make_pair(N->getOpcode(),
438 std::make_pair(N->getOperand(0),
439 N->getValueType(0))));
440 } else if (N->getNumOperands() == 2) {
441 Erased =
442 BinaryOps.erase(std::make_pair(N->getOpcode(),
443 std::make_pair(N->getOperand(0),
444 N->getOperand(1))));
445 } else {
446 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
447 Erased =
448 OneResultNodes.erase(std::make_pair(N->getOpcode(),
449 std::make_pair(N->getValueType(0),
450 Ops)));
451 }
Chris Lattner385328c2005-05-14 07:42:29 +0000452 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000453 // Remove the node from the ArbitraryNodes map.
454 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
455 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000456 Erased =
457 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
458 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000459 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000460 break;
461 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000462#ifndef NDEBUG
463 // Verify that the node was actually in one of the CSE maps, unless it has a
464 // flag result (which cannot be CSE'd) or is one of the special cases that are
465 // not subject to CSE.
466 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000467 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000468 N->dump();
469 assert(0 && "Node is not in map!");
470 }
471#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000472}
473
Chris Lattner8b8749f2005-08-17 19:00:20 +0000474/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
475/// has been taken out and modified in some way. If the specified node already
476/// exists in the CSE maps, do not modify the maps, but return the existing node
477/// instead. If it doesn't exist, add it and return null.
478///
479SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
480 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000481 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000482 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000483
Chris Lattner9f8cc692005-12-19 22:21:21 +0000484 // Check that remaining values produced are not flags.
485 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
486 if (N->getValueType(i) == MVT::Flag)
487 return 0; // Never CSE anything that produces a flag.
488
Chris Lattnerfe14b342005-12-01 23:14:50 +0000489 if (N->getNumValues() == 1) {
490 if (N->getNumOperands() == 1) {
491 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
492 std::make_pair(N->getOperand(0),
493 N->getValueType(0)))];
494 if (U) return U;
495 U = N;
496 } else if (N->getNumOperands() == 2) {
497 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
498 std::make_pair(N->getOperand(0),
499 N->getOperand(1)))];
500 if (B) return B;
501 B = N;
502 } else {
503 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
504 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000505 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000506 if (ORN) return ORN;
507 ORN = N;
508 }
509 } else {
510 if (N->getOpcode() == ISD::LOAD) {
511 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
512 std::make_pair(N->getOperand(0),
513 N->getValueType(0)))];
514 if (L) return L;
515 L = N;
516 } else {
517 // Remove the node from the ArbitraryNodes map.
518 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
519 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
520 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
521 std::make_pair(RV, Ops))];
522 if (AN) return AN;
523 AN = N;
524 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000525 }
526 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000527}
528
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000529/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
530/// were replaced with those specified. If this node is never memoized,
531/// return null, otherwise return a pointer to the slot it would take. If a
532/// node already exists with these operands, the slot will be non-null.
533SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000534 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000535 return 0; // Never add these nodes.
536
537 // Check that remaining values produced are not flags.
538 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
539 if (N->getValueType(i) == MVT::Flag)
540 return 0; // Never CSE anything that produces a flag.
541
542 if (N->getNumValues() == 1) {
543 return &UnaryOps[std::make_pair(N->getOpcode(),
544 std::make_pair(Op, N->getValueType(0)))];
545 } else {
546 // Remove the node from the ArbitraryNodes map.
547 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
548 std::vector<SDOperand> Ops;
549 Ops.push_back(Op);
550 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
551 std::make_pair(RV, Ops))];
552 }
553 return 0;
554}
555
556/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
557/// were replaced with those specified. If this node is never memoized,
558/// return null, otherwise return a pointer to the slot it would take. If a
559/// node already exists with these operands, the slot will be non-null.
560SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
561 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000562 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000563 return 0; // Never add these nodes.
564
565 // Check that remaining values produced are not flags.
566 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
567 if (N->getValueType(i) == MVT::Flag)
568 return 0; // Never CSE anything that produces a flag.
569
570 if (N->getNumValues() == 1) {
571 return &BinaryOps[std::make_pair(N->getOpcode(),
572 std::make_pair(Op1, Op2))];
573 } else {
574 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
575 std::vector<SDOperand> Ops;
576 Ops.push_back(Op1);
577 Ops.push_back(Op2);
578 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
579 std::make_pair(RV, Ops))];
580 }
581 return 0;
582}
583
584
585/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
586/// were replaced with those specified. If this node is never memoized,
587/// return null, otherwise return a pointer to the slot it would take. If a
588/// node already exists with these operands, the slot will be non-null.
589SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
590 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000591 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000592 return 0; // Never add these nodes.
593
594 // Check that remaining values produced are not flags.
595 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
596 if (N->getValueType(i) == MVT::Flag)
597 return 0; // Never CSE anything that produces a flag.
598
599 if (N->getNumValues() == 1) {
600 if (N->getNumOperands() == 1) {
601 return &UnaryOps[std::make_pair(N->getOpcode(),
602 std::make_pair(Ops[0],
603 N->getValueType(0)))];
604 } else if (N->getNumOperands() == 2) {
605 return &BinaryOps[std::make_pair(N->getOpcode(),
606 std::make_pair(Ops[0], Ops[1]))];
607 } else {
608 return &OneResultNodes[std::make_pair(N->getOpcode(),
609 std::make_pair(N->getValueType(0),
610 Ops))];
611 }
612 } else {
613 if (N->getOpcode() == ISD::LOAD) {
614 return &Loads[std::make_pair(Ops[1],
615 std::make_pair(Ops[0], N->getValueType(0)))];
616 } else {
617 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
618 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
619 std::make_pair(RV, Ops))];
620 }
621 }
622 return 0;
623}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000624
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000625
Chris Lattner78ec3112003-08-11 14:57:33 +0000626SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000627 while (!AllNodes.empty()) {
628 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000629 delete [] N->OperandList;
630 N->OperandList = 0;
631 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000632 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000633 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000634}
635
Chris Lattner0f2287b2005-04-13 02:38:18 +0000636SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000637 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000638 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000639 return getNode(ISD::AND, Op.getValueType(), Op,
640 getConstant(Imm, Op.getValueType()));
641}
642
Chris Lattnerc3aae252005-01-07 07:46:32 +0000643SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
644 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000645 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
646
Chris Lattnerc3aae252005-01-07 07:46:32 +0000647 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000648 if (VT != MVT::i64)
649 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000650
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651 SDNode *&N = Constants[std::make_pair(Val, VT)];
652 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000653 N = new ConstantSDNode(false, Val, VT);
654 AllNodes.push_back(N);
655 return SDOperand(N, 0);
656}
657
Chris Lattner36ce6912005-11-29 06:21:05 +0000658SDOperand SelectionDAG::getString(const std::string &Val) {
659 StringSDNode *&N = StringNodes[Val];
660 if (!N) {
661 N = new StringSDNode(Val);
662 AllNodes.push_back(N);
663 }
664 return SDOperand(N, 0);
665}
666
Chris Lattner37bfbb42005-08-17 00:34:06 +0000667SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
668 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
669 // Mask out any bits that are not valid for this constant.
670 if (VT != MVT::i64)
671 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
672
673 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
674 if (N) return SDOperand(N, 0);
675 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 AllNodes.push_back(N);
677 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000678}
679
Chris Lattnerc3aae252005-01-07 07:46:32 +0000680SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
681 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
682 if (VT == MVT::f32)
683 Val = (float)Val; // Mask out extra precision.
684
Chris Lattnerd8658612005-02-17 20:17:32 +0000685 // Do the map lookup using the actual bit pattern for the floating point
686 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
687 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000688 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000690 N = new ConstantFPSDNode(false, Val, VT);
691 AllNodes.push_back(N);
692 return SDOperand(N, 0);
693}
694
695SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
696 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
697 if (VT == MVT::f32)
698 Val = (float)Val; // Mask out extra precision.
699
700 // Do the map lookup using the actual bit pattern for the floating point
701 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
702 // we don't have issues with SNANs.
703 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
704 if (N) return SDOperand(N, 0);
705 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
708}
709
Chris Lattnerc3aae252005-01-07 07:46:32 +0000710SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000711 MVT::ValueType VT, int offset) {
712 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000713 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000714 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000715 AllNodes.push_back(N);
716 return SDOperand(N, 0);
717}
718
719SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000720 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000721 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000722 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000723 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
726}
727
728SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
729 SDNode *&N = FrameIndices[FI];
730 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000731 N = new FrameIndexSDNode(FI, VT, false);
732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
734}
735
736SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
737 SDNode *&N = TargetFrameIndices[FI];
738 if (N) return SDOperand(N, 0);
739 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740 AllNodes.push_back(N);
741 return SDOperand(N, 0);
742}
743
Evan Chengb8973bd2006-01-31 22:23:14 +0000744SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000745 unsigned Alignment, int Offset) {
746 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
747 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000748 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000749 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000750 AllNodes.push_back(N);
751 return SDOperand(N, 0);
752}
753
Evan Chengb8973bd2006-01-31 22:23:14 +0000754SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000755 unsigned Alignment, int Offset) {
756 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
757 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000758 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000759 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000760 AllNodes.push_back(N);
761 return SDOperand(N, 0);
762}
763
764SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
765 SDNode *&N = BBNodes[MBB];
766 if (N) return SDOperand(N, 0);
767 N = new BasicBlockSDNode(MBB);
768 AllNodes.push_back(N);
769 return SDOperand(N, 0);
770}
771
Chris Lattner15e4b012005-07-10 00:07:11 +0000772SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
773 if ((unsigned)VT >= ValueTypeNodes.size())
774 ValueTypeNodes.resize(VT+1);
775 if (ValueTypeNodes[VT] == 0) {
776 ValueTypeNodes[VT] = new VTSDNode(VT);
777 AllNodes.push_back(ValueTypeNodes[VT]);
778 }
779
780 return SDOperand(ValueTypeNodes[VT], 0);
781}
782
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
784 SDNode *&N = ExternalSymbols[Sym];
785 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000786 N = new ExternalSymbolSDNode(false, Sym, VT);
787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
789}
790
Chris Lattner809ec112006-01-28 10:09:25 +0000791SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
792 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000793 SDNode *&N = TargetExternalSymbols[Sym];
794 if (N) return SDOperand(N, 0);
795 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000796 AllNodes.push_back(N);
797 return SDOperand(N, 0);
798}
799
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000800SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
801 if ((unsigned)Cond >= CondCodeNodes.size())
802 CondCodeNodes.resize(Cond+1);
803
Chris Lattner079a27a2005-08-09 20:40:02 +0000804 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000805 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000806 AllNodes.push_back(CondCodeNodes[Cond]);
807 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000808 return SDOperand(CondCodeNodes[Cond], 0);
809}
810
Chris Lattner0fdd7682005-08-30 22:38:38 +0000811SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
812 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
813 if (!Reg) {
814 Reg = new RegisterSDNode(RegNo, VT);
815 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000816 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000817 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000818}
819
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000820SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
821 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822 // These setcc operations always fold.
823 switch (Cond) {
824 default: break;
825 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000826 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000827 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000828 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000829 }
830
Chris Lattner67255a12005-04-07 18:14:58 +0000831 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
832 uint64_t C2 = N2C->getValue();
833 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
834 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000835
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836 // Sign extend the operands if required
837 if (ISD::isSignedIntSetCC(Cond)) {
838 C1 = N1C->getSignExtended();
839 C2 = N2C->getSignExtended();
840 }
841
842 switch (Cond) {
843 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000844 case ISD::SETEQ: return getConstant(C1 == C2, VT);
845 case ISD::SETNE: return getConstant(C1 != C2, VT);
846 case ISD::SETULT: return getConstant(C1 < C2, VT);
847 case ISD::SETUGT: return getConstant(C1 > C2, VT);
848 case ISD::SETULE: return getConstant(C1 <= C2, VT);
849 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
850 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
851 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
852 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
853 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 }
Chris Lattner24673922005-04-07 18:58:54 +0000855 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000856 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000857 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
858 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
859
860 // If the comparison constant has bits in the upper part, the
861 // zero-extended value could never match.
862 if (C2 & (~0ULL << InSize)) {
863 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
864 switch (Cond) {
865 case ISD::SETUGT:
866 case ISD::SETUGE:
867 case ISD::SETEQ: return getConstant(0, VT);
868 case ISD::SETULT:
869 case ISD::SETULE:
870 case ISD::SETNE: return getConstant(1, VT);
871 case ISD::SETGT:
872 case ISD::SETGE:
873 // True if the sign bit of C2 is set.
874 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
875 case ISD::SETLT:
876 case ISD::SETLE:
877 // True if the sign bit of C2 isn't set.
878 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
879 default:
880 break;
881 }
882 }
883
884 // Otherwise, we can perform the comparison with the low bits.
885 switch (Cond) {
886 case ISD::SETEQ:
887 case ISD::SETNE:
888 case ISD::SETUGT:
889 case ISD::SETUGE:
890 case ISD::SETULT:
891 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000892 return getSetCC(VT, N1.getOperand(0),
893 getConstant(C2, N1.getOperand(0).getValueType()),
894 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000895 default:
896 break; // todo, be more careful with signed comparisons
897 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000898 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
899 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
900 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
901 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
902 MVT::ValueType ExtDstTy = N1.getValueType();
903 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000904
Chris Lattner7b2880c2005-08-24 22:44:39 +0000905 // If the extended part has any inconsistent bits, it cannot ever
906 // compare equal. In other words, they have to be all ones or all
907 // zeros.
908 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000909 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000910 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
911 return getConstant(Cond == ISD::SETNE, VT);
912
913 // Otherwise, make this a use of a zext.
914 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000915 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000916 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000917 }
918
Chris Lattner67255a12005-04-07 18:14:58 +0000919 uint64_t MinVal, MaxVal;
920 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
921 if (ISD::isSignedIntSetCC(Cond)) {
922 MinVal = 1ULL << (OperandBitSize-1);
923 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
924 MaxVal = ~0ULL >> (65-OperandBitSize);
925 else
926 MaxVal = 0;
927 } else {
928 MinVal = 0;
929 MaxVal = ~0ULL >> (64-OperandBitSize);
930 }
931
932 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
933 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
934 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
935 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000936 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
937 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000938 }
939
940 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
941 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
942 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000943 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
944 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000945 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000946
Nate Begeman72ea2812005-04-14 08:56:52 +0000947 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
948 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000949
Nate Begeman72ea2812005-04-14 08:56:52 +0000950 // Canonicalize setgt X, Min --> setne X, Min
951 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000952 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000953
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000954 // If we have setult X, 1, turn it into seteq X, 0
955 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000956 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
957 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000958 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000959 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000960 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
961 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000962
963 // If we have "setcc X, C1", check to see if we can shrink the immediate
964 // by changing cc.
965
966 // SETUGT X, SINTMAX -> SETLT X, 0
967 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
968 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000969 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000970
971 // FIXME: Implement the rest of these.
972
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000973
974 // Fold bit comparisons when we can.
975 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
976 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
977 if (ConstantSDNode *AndRHS =
978 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
979 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
980 // Perform the xform if the AND RHS is a single bit.
981 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
982 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000983 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000984 TLI.getShiftAmountTy()));
985 }
986 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
987 // (X & 8) == 8 --> (X & 8) >> 3
988 // Perform the xform if C2 is a single bit.
989 if ((C2 & (C2-1)) == 0) {
990 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000991 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000992 }
993 }
994 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 }
Chris Lattner67255a12005-04-07 18:14:58 +0000996 } else if (isa<ConstantSDNode>(N1.Val)) {
997 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000998 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000999 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000
1001 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1002 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1003 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001004
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 switch (Cond) {
1006 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001007 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1008 case ISD::SETNE: return getConstant(C1 != C2, VT);
1009 case ISD::SETLT: return getConstant(C1 < C2, VT);
1010 case ISD::SETGT: return getConstant(C1 > C2, VT);
1011 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1012 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 }
1014 } else {
1015 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001016 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 }
1018
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001019 // Could not fold it.
1020 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021}
1022
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001024///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001026 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1027 if (!N) {
1028 N = new SDNode(Opcode, VT);
1029 AllNodes.push_back(N);
1030 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 return SDOperand(N, 0);
1032}
1033
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1035 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001036 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001037 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1039 uint64_t Val = C->getValue();
1040 switch (Opcode) {
1041 default: break;
1042 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001043 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1045 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001046 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1047 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001048 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001049 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001050 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001051 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001052 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001053 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001054 case ISD::BSWAP:
1055 switch(VT) {
1056 default: assert(0 && "Invalid bswap!"); break;
1057 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1058 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1059 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1060 }
1061 break;
1062 case ISD::CTPOP:
1063 switch(VT) {
1064 default: assert(0 && "Invalid ctpop!"); break;
1065 case MVT::i1: return getConstant(Val != 0, VT);
1066 case MVT::i8:
1067 Tmp1 = (unsigned)Val & 0xFF;
1068 return getConstant(CountPopulation_32(Tmp1), VT);
1069 case MVT::i16:
1070 Tmp1 = (unsigned)Val & 0xFFFF;
1071 return getConstant(CountPopulation_32(Tmp1), VT);
1072 case MVT::i32:
1073 return getConstant(CountPopulation_32((unsigned)Val), VT);
1074 case MVT::i64:
1075 return getConstant(CountPopulation_64(Val), VT);
1076 }
1077 case ISD::CTLZ:
1078 switch(VT) {
1079 default: assert(0 && "Invalid ctlz!"); break;
1080 case MVT::i1: return getConstant(Val == 0, VT);
1081 case MVT::i8:
1082 Tmp1 = (unsigned)Val & 0xFF;
1083 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1084 case MVT::i16:
1085 Tmp1 = (unsigned)Val & 0xFFFF;
1086 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1087 case MVT::i32:
1088 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1089 case MVT::i64:
1090 return getConstant(CountLeadingZeros_64(Val), VT);
1091 }
1092 case ISD::CTTZ:
1093 switch(VT) {
1094 default: assert(0 && "Invalid cttz!"); break;
1095 case MVT::i1: return getConstant(Val == 0, VT);
1096 case MVT::i8:
1097 Tmp1 = (unsigned)Val | 0x100;
1098 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1099 case MVT::i16:
1100 Tmp1 = (unsigned)Val | 0x10000;
1101 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1102 case MVT::i32:
1103 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1104 case MVT::i64:
1105 return getConstant(CountTrailingZeros_64(Val), VT);
1106 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107 }
1108 }
1109
Chris Lattner94683772005-12-23 05:30:37 +00001110 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1112 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001113 case ISD::FNEG:
1114 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001115 case ISD::FABS:
1116 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001117 case ISD::FP_ROUND:
1118 case ISD::FP_EXTEND:
1119 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001120 case ISD::FP_TO_SINT:
1121 return getConstant((int64_t)C->getValue(), VT);
1122 case ISD::FP_TO_UINT:
1123 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001124 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001125 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001126 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001127 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001128 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001129 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 }
1131
1132 unsigned OpOpcode = Operand.Val->getOpcode();
1133 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001134 case ISD::TokenFactor:
1135 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001136 case ISD::SIGN_EXTEND:
1137 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001138 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001139 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1140 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1141 break;
1142 case ISD::ZERO_EXTEND:
1143 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001144 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001145 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001146 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001148 case ISD::ANY_EXTEND:
1149 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001150 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001151 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1152 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1153 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1154 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155 case ISD::TRUNCATE:
1156 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001157 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001158 if (OpOpcode == ISD::TRUNCATE)
1159 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001160 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1161 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001162 // If the source is smaller than the dest, we still need an extend.
1163 if (Operand.Val->getOperand(0).getValueType() < VT)
1164 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1165 else if (Operand.Val->getOperand(0).getValueType() > VT)
1166 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1167 else
1168 return Operand.Val->getOperand(0);
1169 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001170 break;
Chris Lattner35481892005-12-23 00:16:34 +00001171 case ISD::BIT_CONVERT:
1172 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001173 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001174 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001175 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001176 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1177 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001178 if (OpOpcode == ISD::UNDEF)
1179 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001180 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001181 case ISD::SCALAR_TO_VECTOR:
1182 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1183 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1184 "Illegal SCALAR_TO_VECTOR node!");
1185 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001186 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001187 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1188 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001189 Operand.Val->getOperand(0));
1190 if (OpOpcode == ISD::FNEG) // --X -> X
1191 return Operand.Val->getOperand(0);
1192 break;
1193 case ISD::FABS:
1194 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1195 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1196 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197 }
1198
Chris Lattner43247a12005-08-25 19:12:10 +00001199 SDNode *N;
1200 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1201 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001202 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001203 E = N = new SDNode(Opcode, Operand);
1204 } else {
1205 N = new SDNode(Opcode, Operand);
1206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 N->setValueTypes(VT);
1208 AllNodes.push_back(N);
1209 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001210}
1211
Chris Lattner36019aa2005-04-18 03:48:41 +00001212
1213
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1215 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001216#ifndef NDEBUG
1217 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001218 case ISD::TokenFactor:
1219 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1220 N2.getValueType() == MVT::Other && "Invalid token factor!");
1221 break;
Chris Lattner76365122005-01-16 02:23:22 +00001222 case ISD::AND:
1223 case ISD::OR:
1224 case ISD::XOR:
1225 case ISD::UDIV:
1226 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001227 case ISD::MULHU:
1228 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001229 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1230 // fall through
1231 case ISD::ADD:
1232 case ISD::SUB:
1233 case ISD::MUL:
1234 case ISD::SDIV:
1235 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001236 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1237 // fall through.
1238 case ISD::FADD:
1239 case ISD::FSUB:
1240 case ISD::FMUL:
1241 case ISD::FDIV:
1242 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001243 assert(N1.getValueType() == N2.getValueType() &&
1244 N1.getValueType() == VT && "Binary operator types must match!");
1245 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001246 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1247 assert(N1.getValueType() == VT &&
1248 MVT::isFloatingPoint(N1.getValueType()) &&
1249 MVT::isFloatingPoint(N2.getValueType()) &&
1250 "Invalid FCOPYSIGN!");
1251 break;
Chris Lattner76365122005-01-16 02:23:22 +00001252 case ISD::SHL:
1253 case ISD::SRA:
1254 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001255 case ISD::ROTL:
1256 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001257 assert(VT == N1.getValueType() &&
1258 "Shift operators return type must be the same as their first arg");
1259 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001260 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001261 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 case ISD::FP_ROUND_INREG: {
1263 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1264 assert(VT == N1.getValueType() && "Not an inreg round!");
1265 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1266 "Cannot FP_ROUND_INREG integer types");
1267 assert(EVT <= VT && "Not rounding down!");
1268 break;
1269 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001270 case ISD::AssertSext:
1271 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001272 case ISD::SIGN_EXTEND_INREG: {
1273 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1274 assert(VT == N1.getValueType() && "Not an inreg extend!");
1275 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1276 "Cannot *_EXTEND_INREG FP types");
1277 assert(EVT <= VT && "Not extending!");
1278 }
1279
Chris Lattner76365122005-01-16 02:23:22 +00001280 default: break;
1281 }
1282#endif
1283
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1285 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1286 if (N1C) {
1287 if (N2C) {
1288 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1289 switch (Opcode) {
1290 case ISD::ADD: return getConstant(C1 + C2, VT);
1291 case ISD::SUB: return getConstant(C1 - C2, VT);
1292 case ISD::MUL: return getConstant(C1 * C2, VT);
1293 case ISD::UDIV:
1294 if (C2) return getConstant(C1 / C2, VT);
1295 break;
1296 case ISD::UREM :
1297 if (C2) return getConstant(C1 % C2, VT);
1298 break;
1299 case ISD::SDIV :
1300 if (C2) return getConstant(N1C->getSignExtended() /
1301 N2C->getSignExtended(), VT);
1302 break;
1303 case ISD::SREM :
1304 if (C2) return getConstant(N1C->getSignExtended() %
1305 N2C->getSignExtended(), VT);
1306 break;
1307 case ISD::AND : return getConstant(C1 & C2, VT);
1308 case ISD::OR : return getConstant(C1 | C2, VT);
1309 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001310 case ISD::SHL : return getConstant(C1 << C2, VT);
1311 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001312 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001313 case ISD::ROTL :
1314 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1315 VT);
1316 case ISD::ROTR :
1317 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1318 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001319 default: break;
1320 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 } else { // Cannonicalize constant to RHS if commutative
1322 if (isCommutativeBinOp(Opcode)) {
1323 std::swap(N1C, N2C);
1324 std::swap(N1, N2);
1325 }
1326 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 }
1328
1329 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1330 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001331 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 if (N2CFP) {
1333 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1334 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001335 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1336 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1337 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1338 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339 if (C2) return getConstantFP(C1 / C2, VT);
1340 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001341 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 if (C2) return getConstantFP(fmod(C1, C2), VT);
1343 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001344 case ISD::FCOPYSIGN: {
1345 union {
1346 double F;
1347 uint64_t I;
1348 } u1;
1349 union {
1350 double F;
1351 int64_t I;
1352 } u2;
1353 u1.F = C1;
1354 u2.F = C2;
1355 if (u2.I < 0) // Sign bit of RHS set?
1356 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1357 else
1358 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1359 return getConstantFP(u1.F, VT);
1360 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 default: break;
1362 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001363 } else { // Cannonicalize constant to RHS if commutative
1364 if (isCommutativeBinOp(Opcode)) {
1365 std::swap(N1CFP, N2CFP);
1366 std::swap(N1, N2);
1367 }
1368 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001369 }
Chris Lattner62b57722006-04-20 05:39:12 +00001370
1371 // Canonicalize an UNDEF to the RHS, even over a constant.
1372 if (N1.getOpcode() == ISD::UNDEF) {
1373 if (isCommutativeBinOp(Opcode)) {
1374 std::swap(N1, N2);
1375 } else {
1376 switch (Opcode) {
1377 case ISD::FP_ROUND_INREG:
1378 case ISD::SIGN_EXTEND_INREG:
1379 case ISD::SUB:
1380 case ISD::FSUB:
1381 case ISD::FDIV:
1382 case ISD::FREM:
1383 return N1; // fold op(undef, arg2) -> undef
1384 case ISD::UDIV:
1385 case ISD::SDIV:
1386 case ISD::UREM:
1387 case ISD::SREM:
1388 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1389 }
1390 }
1391 }
1392
1393 // Fold a bunch of operators that
1394 if (N2.getOpcode() == ISD::UNDEF) {
1395 switch (Opcode) {
1396 case ISD::ADD:
1397 case ISD::SUB:
1398 case ISD::FADD:
1399 case ISD::FSUB:
1400 case ISD::FMUL:
1401 case ISD::FDIV:
1402 case ISD::FREM:
1403 case ISD::UDIV:
1404 case ISD::SDIV:
1405 case ISD::UREM:
1406 case ISD::SREM:
1407 case ISD::XOR:
1408 return N2; // fold op(arg1, undef) -> undef
1409 case ISD::MUL:
1410 case ISD::AND:
1411 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1412 case ISD::OR:
1413 return getConstant(MVT::getIntVTBitMask(VT), VT);
1414 }
1415 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001416
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 // Finally, fold operations that do not require constants.
1418 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001419 case ISD::FP_ROUND_INREG:
1420 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1421 break;
1422 case ISD::SIGN_EXTEND_INREG: {
1423 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1424 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001425 break;
1426 }
1427
Nate Begemaneea805e2005-04-13 21:23:31 +00001428 // FIXME: figure out how to safely handle things like
1429 // int foo(int x) { return 1 << (x & 255); }
1430 // int bar() { return foo(256); }
1431#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001432 case ISD::SHL:
1433 case ISD::SRL:
1434 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001435 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001436 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001437 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001438 else if (N2.getOpcode() == ISD::AND)
1439 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1440 // If the and is only masking out bits that cannot effect the shift,
1441 // eliminate the and.
1442 unsigned NumBits = MVT::getSizeInBits(VT);
1443 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1444 return getNode(Opcode, VT, N1, N2.getOperand(0));
1445 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001446 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001447#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001448 }
1449
Chris Lattner27e9b412005-05-11 18:57:39 +00001450 // Memoize this node if possible.
1451 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001452 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001453 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1454 if (BON) return SDOperand(BON, 0);
1455
1456 BON = N = new SDNode(Opcode, N1, N2);
1457 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001458 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001459 }
1460
Chris Lattner3e011362005-05-14 07:45:46 +00001461 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001462 AllNodes.push_back(N);
1463 return SDOperand(N, 0);
1464}
1465
Chris Lattnerc3aae252005-01-07 07:46:32 +00001466SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1467 SDOperand N1, SDOperand N2, SDOperand N3) {
1468 // Perform various simplifications.
1469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1470 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1471 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1472 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001473 case ISD::SETCC: {
1474 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001475 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001476 if (Simp.Val) return Simp;
1477 break;
1478 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001479 case ISD::SELECT:
1480 if (N1C)
1481 if (N1C->getValue())
1482 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001483 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001484 return N3; // select false, X, Y -> Y
1485
1486 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001488 case ISD::BRCOND:
1489 if (N2C)
1490 if (N2C->getValue()) // Unconditional branch
1491 return getNode(ISD::BR, MVT::Other, N1, N3);
1492 else
1493 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001494 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001495 case ISD::VECTOR_SHUFFLE:
1496 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1497 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1498 N3.getOpcode() == ISD::BUILD_VECTOR &&
1499 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1500 "Illegal VECTOR_SHUFFLE node!");
1501 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001502 }
1503
Chris Lattner385328c2005-05-14 07:42:29 +00001504 std::vector<SDOperand> Ops;
1505 Ops.reserve(3);
1506 Ops.push_back(N1);
1507 Ops.push_back(N2);
1508 Ops.push_back(N3);
1509
Chris Lattner43247a12005-08-25 19:12:10 +00001510 // Memoize node if it doesn't produce a flag.
1511 SDNode *N;
1512 if (VT != MVT::Flag) {
1513 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1514 if (E) return SDOperand(E, 0);
1515 E = N = new SDNode(Opcode, N1, N2, N3);
1516 } else {
1517 N = new SDNode(Opcode, N1, N2, N3);
1518 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001519 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001520 AllNodes.push_back(N);
1521 return SDOperand(N, 0);
1522}
1523
1524SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001525 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001526 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001527 std::vector<SDOperand> Ops;
1528 Ops.reserve(4);
1529 Ops.push_back(N1);
1530 Ops.push_back(N2);
1531 Ops.push_back(N3);
1532 Ops.push_back(N4);
1533 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001534}
1535
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001536SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1537 SDOperand N1, SDOperand N2, SDOperand N3,
1538 SDOperand N4, SDOperand N5) {
1539 std::vector<SDOperand> Ops;
1540 Ops.reserve(5);
1541 Ops.push_back(N1);
1542 Ops.push_back(N2);
1543 Ops.push_back(N3);
1544 Ops.push_back(N4);
1545 Ops.push_back(N5);
1546 return getNode(Opcode, VT, Ops);
1547}
1548
Evan Cheng7038daf2005-12-10 00:37:58 +00001549SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1550 SDOperand Chain, SDOperand Ptr,
1551 SDOperand SV) {
1552 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1553 if (N) return SDOperand(N, 0);
1554 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1555
1556 // Loads have a token chain.
1557 setNodeValueTypes(N, VT, MVT::Other);
1558 AllNodes.push_back(N);
1559 return SDOperand(N, 0);
1560}
1561
1562SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1563 SDOperand Chain, SDOperand Ptr,
1564 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001565 std::vector<SDOperand> Ops;
1566 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001567 Ops.push_back(Chain);
1568 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001569 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001570 Ops.push_back(getConstant(Count, MVT::i32));
1571 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001572 std::vector<MVT::ValueType> VTs;
1573 VTs.reserve(2);
1574 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1575 return getNode(ISD::VLOAD, VTs, Ops);
1576}
1577
1578SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1579 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1580 MVT::ValueType EVT) {
1581 std::vector<SDOperand> Ops;
1582 Ops.reserve(4);
1583 Ops.push_back(Chain);
1584 Ops.push_back(Ptr);
1585 Ops.push_back(SV);
1586 Ops.push_back(getValueType(EVT));
1587 std::vector<MVT::ValueType> VTs;
1588 VTs.reserve(2);
1589 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1590 return getNode(Opcode, VTs, Ops);
1591}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001592
Chris Lattner0437cdd2005-05-09 04:14:13 +00001593SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001594 assert((!V || isa<PointerType>(V->getType())) &&
1595 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001596 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1597 if (N) return SDOperand(N, 0);
1598
1599 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001600 AllNodes.push_back(N);
1601 return SDOperand(N, 0);
1602}
1603
Nate Begemanacc398c2006-01-25 18:21:52 +00001604SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1605 SDOperand Chain, SDOperand Ptr,
1606 SDOperand SV) {
1607 std::vector<SDOperand> Ops;
1608 Ops.reserve(3);
1609 Ops.push_back(Chain);
1610 Ops.push_back(Ptr);
1611 Ops.push_back(SV);
1612 std::vector<MVT::ValueType> VTs;
1613 VTs.reserve(2);
1614 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1615 return getNode(ISD::VAARG, VTs, Ops);
1616}
1617
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001618SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001619 std::vector<SDOperand> &Ops) {
1620 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001621 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001622 case 1: return getNode(Opcode, VT, Ops[0]);
1623 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1624 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001625 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001626 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001627
Chris Lattner89c34632005-05-14 06:20:26 +00001628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001629 switch (Opcode) {
1630 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001631 case ISD::TRUNCSTORE: {
1632 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1633 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1634#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1635 // If this is a truncating store of a constant, convert to the desired type
1636 // and store it instead.
1637 if (isa<Constant>(Ops[0])) {
1638 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1639 if (isa<Constant>(Op))
1640 N1 = Op;
1641 }
1642 // Also for ConstantFP?
1643#endif
1644 if (Ops[0].getValueType() == EVT) // Normal store?
1645 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1646 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1647 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1648 "Can't do FP-INT conversion!");
1649 break;
1650 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001651 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001652 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001653 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1654 "LHS and RHS of condition must have same type!");
1655 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1656 "True and False arms of SelectCC must have same type!");
1657 assert(Ops[2].getValueType() == VT &&
1658 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001659 break;
1660 }
1661 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001662 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001663 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1664 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001665 break;
1666 }
Chris Lattneref847df2005-04-09 03:27:28 +00001667 }
1668
Chris Lattner385328c2005-05-14 07:42:29 +00001669 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001670 SDNode *N;
1671 if (VT != MVT::Flag) {
1672 SDNode *&E =
1673 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1674 if (E) return SDOperand(E, 0);
1675 E = N = new SDNode(Opcode, Ops);
1676 } else {
1677 N = new SDNode(Opcode, Ops);
1678 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001679 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001680 AllNodes.push_back(N);
1681 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001682}
1683
Chris Lattner89c34632005-05-14 06:20:26 +00001684SDOperand SelectionDAG::getNode(unsigned Opcode,
1685 std::vector<MVT::ValueType> &ResultTys,
1686 std::vector<SDOperand> &Ops) {
1687 if (ResultTys.size() == 1)
1688 return getNode(Opcode, ResultTys[0], Ops);
1689
Chris Lattner5f056bf2005-07-10 01:55:33 +00001690 switch (Opcode) {
1691 case ISD::EXTLOAD:
1692 case ISD::SEXTLOAD:
1693 case ISD::ZEXTLOAD: {
1694 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1695 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1696 // If they are asking for an extending load from/to the same thing, return a
1697 // normal load.
1698 if (ResultTys[0] == EVT)
1699 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001700 if (MVT::isVector(ResultTys[0])) {
1701 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1702 "Invalid vector extload!");
1703 } else {
1704 assert(EVT < ResultTys[0] &&
1705 "Should only be an extending load, not truncating!");
1706 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001707 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001708 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001709 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1710 "Cannot convert from FP to Int or Int -> FP!");
1711 break;
1712 }
1713
Chris Lattnere89083a2005-05-14 07:25:05 +00001714 // FIXME: figure out how to safely handle things like
1715 // int foo(int x) { return 1 << (x & 255); }
1716 // int bar() { return foo(256); }
1717#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001718 case ISD::SRA_PARTS:
1719 case ISD::SRL_PARTS:
1720 case ISD::SHL_PARTS:
1721 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001722 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001723 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1724 else if (N3.getOpcode() == ISD::AND)
1725 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1726 // If the and is only masking out bits that cannot effect the shift,
1727 // eliminate the and.
1728 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1729 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1730 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1731 }
1732 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001733#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001734 }
Chris Lattner89c34632005-05-14 06:20:26 +00001735
Chris Lattner43247a12005-08-25 19:12:10 +00001736 // Memoize the node unless it returns a flag.
1737 SDNode *N;
1738 if (ResultTys.back() != MVT::Flag) {
1739 SDNode *&E =
1740 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1741 if (E) return SDOperand(E, 0);
1742 E = N = new SDNode(Opcode, Ops);
1743 } else {
1744 N = new SDNode(Opcode, Ops);
1745 }
Chris Lattnera3255112005-11-08 23:30:28 +00001746 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001747 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001748 return SDOperand(N, 0);
1749}
1750
Chris Lattnera3255112005-11-08 23:30:28 +00001751void SelectionDAG::setNodeValueTypes(SDNode *N,
1752 std::vector<MVT::ValueType> &RetVals) {
1753 switch (RetVals.size()) {
1754 case 0: return;
1755 case 1: N->setValueTypes(RetVals[0]); return;
1756 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1757 default: break;
1758 }
1759
1760 std::list<std::vector<MVT::ValueType> >::iterator I =
1761 std::find(VTList.begin(), VTList.end(), RetVals);
1762 if (I == VTList.end()) {
1763 VTList.push_front(RetVals);
1764 I = VTList.begin();
1765 }
1766
1767 N->setValueTypes(&(*I)[0], I->size());
1768}
1769
1770void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1771 MVT::ValueType VT2) {
1772 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1773 E = VTList.end(); I != E; ++I) {
1774 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1775 N->setValueTypes(&(*I)[0], 2);
1776 return;
1777 }
1778 }
1779 std::vector<MVT::ValueType> V;
1780 V.push_back(VT1);
1781 V.push_back(VT2);
1782 VTList.push_front(V);
1783 N->setValueTypes(&(*VTList.begin())[0], 2);
1784}
1785
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001786/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1787/// specified operands. If the resultant node already exists in the DAG,
1788/// this does not modify the specified node, instead it returns the node that
1789/// already exists. If the resultant node does not exist in the DAG, the
1790/// input node is returned. As a degenerate case, if you specify the same
1791/// input operands as the node already has, the input node is returned.
1792SDOperand SelectionDAG::
1793UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1794 SDNode *N = InN.Val;
1795 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1796
1797 // Check to see if there is no change.
1798 if (Op == N->getOperand(0)) return InN;
1799
1800 // See if the modified node already exists.
1801 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1802 if (NewSlot && *NewSlot)
1803 return SDOperand(*NewSlot, InN.ResNo);
1804
1805 // Nope it doesn't. Remove the node from it's current place in the maps.
1806 if (NewSlot)
1807 RemoveNodeFromCSEMaps(N);
1808
1809 // Now we update the operands.
1810 N->OperandList[0].Val->removeUser(N);
1811 Op.Val->addUser(N);
1812 N->OperandList[0] = Op;
1813
1814 // If this gets put into a CSE map, add it.
1815 if (NewSlot) *NewSlot = N;
1816 return InN;
1817}
1818
1819SDOperand SelectionDAG::
1820UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1821 SDNode *N = InN.Val;
1822 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1823
1824 // Check to see if there is no change.
1825 bool AnyChange = false;
1826 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1827 return InN; // No operands changed, just return the input node.
1828
1829 // See if the modified node already exists.
1830 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1831 if (NewSlot && *NewSlot)
1832 return SDOperand(*NewSlot, InN.ResNo);
1833
1834 // Nope it doesn't. Remove the node from it's current place in the maps.
1835 if (NewSlot)
1836 RemoveNodeFromCSEMaps(N);
1837
1838 // Now we update the operands.
1839 if (N->OperandList[0] != Op1) {
1840 N->OperandList[0].Val->removeUser(N);
1841 Op1.Val->addUser(N);
1842 N->OperandList[0] = Op1;
1843 }
1844 if (N->OperandList[1] != Op2) {
1845 N->OperandList[1].Val->removeUser(N);
1846 Op2.Val->addUser(N);
1847 N->OperandList[1] = Op2;
1848 }
1849
1850 // If this gets put into a CSE map, add it.
1851 if (NewSlot) *NewSlot = N;
1852 return InN;
1853}
1854
1855SDOperand SelectionDAG::
1856UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1857 std::vector<SDOperand> Ops;
1858 Ops.push_back(Op1);
1859 Ops.push_back(Op2);
1860 Ops.push_back(Op3);
1861 return UpdateNodeOperands(N, Ops);
1862}
1863
1864SDOperand SelectionDAG::
1865UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1866 SDOperand Op3, SDOperand Op4) {
1867 std::vector<SDOperand> Ops;
1868 Ops.push_back(Op1);
1869 Ops.push_back(Op2);
1870 Ops.push_back(Op3);
1871 Ops.push_back(Op4);
1872 return UpdateNodeOperands(N, Ops);
1873}
1874
1875SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001876UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1877 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1878 std::vector<SDOperand> Ops;
1879 Ops.push_back(Op1);
1880 Ops.push_back(Op2);
1881 Ops.push_back(Op3);
1882 Ops.push_back(Op4);
1883 Ops.push_back(Op5);
1884 return UpdateNodeOperands(N, Ops);
1885}
1886
1887
1888SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001889UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1890 SDNode *N = InN.Val;
1891 assert(N->getNumOperands() == Ops.size() &&
1892 "Update with wrong number of operands");
1893
1894 // Check to see if there is no change.
1895 unsigned NumOps = Ops.size();
1896 bool AnyChange = false;
1897 for (unsigned i = 0; i != NumOps; ++i) {
1898 if (Ops[i] != N->getOperand(i)) {
1899 AnyChange = true;
1900 break;
1901 }
1902 }
1903
1904 // No operands changed, just return the input node.
1905 if (!AnyChange) return InN;
1906
1907 // See if the modified node already exists.
1908 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1909 if (NewSlot && *NewSlot)
1910 return SDOperand(*NewSlot, InN.ResNo);
1911
1912 // Nope it doesn't. Remove the node from it's current place in the maps.
1913 if (NewSlot)
1914 RemoveNodeFromCSEMaps(N);
1915
1916 // Now we update the operands.
1917 for (unsigned i = 0; i != NumOps; ++i) {
1918 if (N->OperandList[i] != Ops[i]) {
1919 N->OperandList[i].Val->removeUser(N);
1920 Ops[i].Val->addUser(N);
1921 N->OperandList[i] = Ops[i];
1922 }
1923 }
1924
1925 // If this gets put into a CSE map, add it.
1926 if (NewSlot) *NewSlot = N;
1927 return InN;
1928}
1929
1930
1931
Chris Lattner149c58c2005-08-16 18:17:10 +00001932
1933/// SelectNodeTo - These are used for target selectors to *mutate* the
1934/// specified node to have the specified return type, Target opcode, and
1935/// operands. Note that target opcodes are stored as
1936/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001937///
1938/// Note that SelectNodeTo returns the resultant node. If there is already a
1939/// node of the specified opcode and operands, it returns that node instead of
1940/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001941SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1942 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001943 // If an identical node already exists, use it.
1944 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1945 if (ON) return SDOperand(ON, 0);
1946
Chris Lattner7651fa42005-08-24 23:00:29 +00001947 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001948
Chris Lattner7651fa42005-08-24 23:00:29 +00001949 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1950 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001951
1952 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001953 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001954}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001955
Chris Lattnereb19e402005-11-30 22:45:14 +00001956SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1957 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001958 // If an identical node already exists, use it.
1959 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1960 std::make_pair(Op1, VT))];
1961 if (ON) return SDOperand(ON, 0);
1962
Chris Lattner149c58c2005-08-16 18:17:10 +00001963 RemoveNodeFromCSEMaps(N);
1964 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1965 N->setValueTypes(VT);
1966 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001967
1968 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001969 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001970}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001971
Chris Lattnereb19e402005-11-30 22:45:14 +00001972SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1973 MVT::ValueType VT, SDOperand Op1,
1974 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001975 // If an identical node already exists, use it.
1976 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1977 std::make_pair(Op1, Op2))];
1978 if (ON) return SDOperand(ON, 0);
1979
Chris Lattner149c58c2005-08-16 18:17:10 +00001980 RemoveNodeFromCSEMaps(N);
1981 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1982 N->setValueTypes(VT);
1983 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001984
1985 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001986 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001987}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001988
Chris Lattnereb19e402005-11-30 22:45:14 +00001989SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1990 MVT::ValueType VT, SDOperand Op1,
1991 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001992 // If an identical node already exists, use it.
1993 std::vector<SDOperand> OpList;
1994 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1995 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
Chris Lattner149c58c2005-08-16 18:17:10 +00001999 RemoveNodeFromCSEMaps(N);
2000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2001 N->setValueTypes(VT);
2002 N->setOperands(Op1, Op2, Op3);
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);
Chris Lattner149c58c2005-08-16 18:17:10 +00002006}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002007
Chris Lattnereb19e402005-11-30 22:45:14 +00002008SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2009 MVT::ValueType VT, SDOperand Op1,
2010 SDOperand Op2, SDOperand Op3,
2011 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002012 // If an identical node already exists, use it.
2013 std::vector<SDOperand> OpList;
2014 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2015 OpList.push_back(Op4);
2016 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2017 std::make_pair(VT, OpList))];
2018 if (ON) return SDOperand(ON, 0);
2019
Nate Begeman294a0a12005-08-18 07:30:15 +00002020 RemoveNodeFromCSEMaps(N);
2021 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2022 N->setValueTypes(VT);
2023 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002024
2025 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002026 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002027}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002028
Chris Lattnereb19e402005-11-30 22:45:14 +00002029SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2030 MVT::ValueType VT, SDOperand Op1,
2031 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2032 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002033 // If an identical node already exists, use it.
2034 std::vector<SDOperand> OpList;
2035 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2036 OpList.push_back(Op4); OpList.push_back(Op5);
2037 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2038 std::make_pair(VT, OpList))];
2039 if (ON) return SDOperand(ON, 0);
2040
Chris Lattner6b09a292005-08-21 18:49:33 +00002041 RemoveNodeFromCSEMaps(N);
2042 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2043 N->setValueTypes(VT);
2044 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002045
2046 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002047 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002048}
Chris Lattner149c58c2005-08-16 18:17:10 +00002049
Chris Lattnereb19e402005-11-30 22:45:14 +00002050SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2051 MVT::ValueType VT, SDOperand Op1,
2052 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2053 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002054 // If an identical node already exists, use it.
2055 std::vector<SDOperand> OpList;
2056 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2057 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2058 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2059 std::make_pair(VT, OpList))];
2060 if (ON) return SDOperand(ON, 0);
2061
Evan Cheng61ca74b2005-11-30 02:04:11 +00002062 RemoveNodeFromCSEMaps(N);
2063 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2064 N->setValueTypes(VT);
2065 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002066
2067 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002068 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002069}
2070
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002071SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2072 MVT::ValueType VT, SDOperand Op1,
2073 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2074 SDOperand Op5, SDOperand Op6,
2075 SDOperand Op7) {
2076 // If an identical node already exists, use it.
2077 std::vector<SDOperand> OpList;
2078 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2079 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2080 OpList.push_back(Op7);
2081 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2082 std::make_pair(VT, OpList))];
2083 if (ON) return SDOperand(ON, 0);
2084
2085 RemoveNodeFromCSEMaps(N);
2086 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2087 N->setValueTypes(VT);
2088 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2089
2090 ON = N; // Memoize the new node.
2091 return SDOperand(N, 0);
2092}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002093SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2094 MVT::ValueType VT, SDOperand Op1,
2095 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2096 SDOperand Op5, SDOperand Op6,
2097 SDOperand Op7, SDOperand Op8) {
2098 // If an identical node already exists, use it.
2099 std::vector<SDOperand> OpList;
2100 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2101 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2102 OpList.push_back(Op7); OpList.push_back(Op8);
2103 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2104 std::make_pair(VT, OpList))];
2105 if (ON) return SDOperand(ON, 0);
2106
2107 RemoveNodeFromCSEMaps(N);
2108 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2109 N->setValueTypes(VT);
2110 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2111
2112 ON = N; // Memoize the new node.
2113 return SDOperand(N, 0);
2114}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002115
Chris Lattnereb19e402005-11-30 22:45:14 +00002116SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2117 MVT::ValueType VT1, MVT::ValueType VT2,
2118 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002119 // If an identical node already exists, use it.
2120 std::vector<SDOperand> OpList;
2121 OpList.push_back(Op1); OpList.push_back(Op2);
2122 std::vector<MVT::ValueType> VTList;
2123 VTList.push_back(VT1); VTList.push_back(VT2);
2124 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2125 std::make_pair(VTList, OpList))];
2126 if (ON) return SDOperand(ON, 0);
2127
Chris Lattner0fb094f2005-11-19 01:44:53 +00002128 RemoveNodeFromCSEMaps(N);
2129 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2130 setNodeValueTypes(N, VT1, VT2);
2131 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002132
2133 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002134 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002135}
2136
Chris Lattnereb19e402005-11-30 22:45:14 +00002137SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2138 MVT::ValueType VT1, MVT::ValueType VT2,
2139 SDOperand Op1, SDOperand Op2,
2140 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002141 // If an identical node already exists, use it.
2142 std::vector<SDOperand> OpList;
2143 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2144 std::vector<MVT::ValueType> VTList;
2145 VTList.push_back(VT1); VTList.push_back(VT2);
2146 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2147 std::make_pair(VTList, OpList))];
2148 if (ON) return SDOperand(ON, 0);
2149
Chris Lattner0fb094f2005-11-19 01:44:53 +00002150 RemoveNodeFromCSEMaps(N);
2151 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2152 setNodeValueTypes(N, VT1, VT2);
2153 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002154
2155 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002156 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002157}
2158
Chris Lattnereb19e402005-11-30 22:45:14 +00002159SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2160 MVT::ValueType VT1, MVT::ValueType VT2,
2161 SDOperand Op1, SDOperand Op2,
2162 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002163 // If an identical node already exists, use it.
2164 std::vector<SDOperand> OpList;
2165 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2166 OpList.push_back(Op4);
2167 std::vector<MVT::ValueType> VTList;
2168 VTList.push_back(VT1); VTList.push_back(VT2);
2169 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2170 std::make_pair(VTList, OpList))];
2171 if (ON) return SDOperand(ON, 0);
2172
Chris Lattner0fb094f2005-11-19 01:44:53 +00002173 RemoveNodeFromCSEMaps(N);
2174 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2175 setNodeValueTypes(N, VT1, VT2);
2176 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002177
2178 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002179 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002180}
2181
Chris Lattnereb19e402005-11-30 22:45:14 +00002182SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2183 MVT::ValueType VT1, MVT::ValueType VT2,
2184 SDOperand Op1, SDOperand Op2,
2185 SDOperand Op3, SDOperand Op4,
2186 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002187 // If an identical node already exists, use it.
2188 std::vector<SDOperand> OpList;
2189 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2190 OpList.push_back(Op4); OpList.push_back(Op5);
2191 std::vector<MVT::ValueType> VTList;
2192 VTList.push_back(VT1); VTList.push_back(VT2);
2193 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2194 std::make_pair(VTList, OpList))];
2195 if (ON) return SDOperand(ON, 0);
2196
Chris Lattner0fb094f2005-11-19 01:44:53 +00002197 RemoveNodeFromCSEMaps(N);
2198 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2199 setNodeValueTypes(N, VT1, VT2);
2200 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002201
2202 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002203 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002204}
2205
Evan Cheng6ae46c42006-02-09 07:15:23 +00002206/// getTargetNode - These are used for target selectors to create a new node
2207/// with specified return type(s), target opcode, and operands.
2208///
2209/// Note that getTargetNode returns the resultant node. If there is already a
2210/// node of the specified opcode and operands, it returns that node instead of
2211/// the current one.
2212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2213 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2214}
2215SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2216 SDOperand Op1) {
2217 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2218}
2219SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2220 SDOperand Op1, SDOperand Op2) {
2221 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2222}
2223SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2224 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2228 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2229 SDOperand Op4) {
2230 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2231}
2232SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2233 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2234 SDOperand Op4, SDOperand Op5) {
2235 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2236}
2237SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2238 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2239 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2240 std::vector<SDOperand> Ops;
2241 Ops.reserve(6);
2242 Ops.push_back(Op1);
2243 Ops.push_back(Op2);
2244 Ops.push_back(Op3);
2245 Ops.push_back(Op4);
2246 Ops.push_back(Op5);
2247 Ops.push_back(Op6);
2248 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2249}
2250SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2251 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2252 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2253 SDOperand Op7) {
2254 std::vector<SDOperand> Ops;
2255 Ops.reserve(7);
2256 Ops.push_back(Op1);
2257 Ops.push_back(Op2);
2258 Ops.push_back(Op3);
2259 Ops.push_back(Op4);
2260 Ops.push_back(Op5);
2261 Ops.push_back(Op6);
2262 Ops.push_back(Op7);
2263 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2264}
2265SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2266 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2267 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2268 SDOperand Op7, SDOperand Op8) {
2269 std::vector<SDOperand> Ops;
2270 Ops.reserve(8);
2271 Ops.push_back(Op1);
2272 Ops.push_back(Op2);
2273 Ops.push_back(Op3);
2274 Ops.push_back(Op4);
2275 Ops.push_back(Op5);
2276 Ops.push_back(Op6);
2277 Ops.push_back(Op7);
2278 Ops.push_back(Op8);
2279 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2280}
2281SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2282 std::vector<SDOperand> &Ops) {
2283 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2284}
2285SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2286 MVT::ValueType VT2, SDOperand Op1) {
2287 std::vector<MVT::ValueType> ResultTys;
2288 ResultTys.push_back(VT1);
2289 ResultTys.push_back(VT2);
2290 std::vector<SDOperand> Ops;
2291 Ops.push_back(Op1);
2292 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2293}
2294SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2295 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2296 std::vector<MVT::ValueType> ResultTys;
2297 ResultTys.push_back(VT1);
2298 ResultTys.push_back(VT2);
2299 std::vector<SDOperand> Ops;
2300 Ops.push_back(Op1);
2301 Ops.push_back(Op2);
2302 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2303}
2304SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2305 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2306 SDOperand Op3) {
2307 std::vector<MVT::ValueType> ResultTys;
2308 ResultTys.push_back(VT1);
2309 ResultTys.push_back(VT2);
2310 std::vector<SDOperand> Ops;
2311 Ops.push_back(Op1);
2312 Ops.push_back(Op2);
2313 Ops.push_back(Op3);
2314 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2315}
2316SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2317 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2318 SDOperand Op3, SDOperand Op4) {
2319 std::vector<MVT::ValueType> ResultTys;
2320 ResultTys.push_back(VT1);
2321 ResultTys.push_back(VT2);
2322 std::vector<SDOperand> Ops;
2323 Ops.push_back(Op1);
2324 Ops.push_back(Op2);
2325 Ops.push_back(Op3);
2326 Ops.push_back(Op4);
2327 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2328}
2329SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2330 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2331 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2332 std::vector<MVT::ValueType> ResultTys;
2333 ResultTys.push_back(VT1);
2334 ResultTys.push_back(VT2);
2335 std::vector<SDOperand> Ops;
2336 Ops.push_back(Op1);
2337 Ops.push_back(Op2);
2338 Ops.push_back(Op3);
2339 Ops.push_back(Op4);
2340 Ops.push_back(Op5);
2341 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2342}
2343SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2344 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2345 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2346 SDOperand Op6) {
2347 std::vector<MVT::ValueType> ResultTys;
2348 ResultTys.push_back(VT1);
2349 ResultTys.push_back(VT2);
2350 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, SDOperand Op1, SDOperand Op2,
2361 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2362 SDOperand Op6, SDOperand Op7) {
2363 std::vector<MVT::ValueType> ResultTys;
2364 ResultTys.push_back(VT1);
2365 ResultTys.push_back(VT2);
2366 std::vector<SDOperand> Ops;
2367 Ops.push_back(Op1);
2368 Ops.push_back(Op2);
2369 Ops.push_back(Op3);
2370 Ops.push_back(Op4);
2371 Ops.push_back(Op5);
2372 Ops.push_back(Op6);
2373 Ops.push_back(Op7);
2374 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2375}
2376SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2377 MVT::ValueType VT2, MVT::ValueType VT3,
2378 SDOperand Op1, SDOperand Op2) {
2379 std::vector<MVT::ValueType> ResultTys;
2380 ResultTys.push_back(VT1);
2381 ResultTys.push_back(VT2);
2382 ResultTys.push_back(VT3);
2383 std::vector<SDOperand> Ops;
2384 Ops.push_back(Op1);
2385 Ops.push_back(Op2);
2386 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2387}
2388SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2389 MVT::ValueType VT2, MVT::ValueType VT3,
2390 SDOperand Op1, SDOperand Op2,
2391 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2392 std::vector<MVT::ValueType> ResultTys;
2393 ResultTys.push_back(VT1);
2394 ResultTys.push_back(VT2);
2395 ResultTys.push_back(VT3);
2396 std::vector<SDOperand> Ops;
2397 Ops.push_back(Op1);
2398 Ops.push_back(Op2);
2399 Ops.push_back(Op3);
2400 Ops.push_back(Op4);
2401 Ops.push_back(Op5);
2402 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2403}
2404SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2405 MVT::ValueType VT2, MVT::ValueType VT3,
2406 SDOperand Op1, SDOperand Op2,
2407 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2408 SDOperand Op6) {
2409 std::vector<MVT::ValueType> ResultTys;
2410 ResultTys.push_back(VT1);
2411 ResultTys.push_back(VT2);
2412 ResultTys.push_back(VT3);
2413 std::vector<SDOperand> Ops;
2414 Ops.push_back(Op1);
2415 Ops.push_back(Op2);
2416 Ops.push_back(Op3);
2417 Ops.push_back(Op4);
2418 Ops.push_back(Op5);
2419 Ops.push_back(Op6);
2420 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2421}
2422SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2423 MVT::ValueType VT2, MVT::ValueType VT3,
2424 SDOperand Op1, SDOperand Op2,
2425 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2426 SDOperand Op6, SDOperand Op7) {
2427 std::vector<MVT::ValueType> ResultTys;
2428 ResultTys.push_back(VT1);
2429 ResultTys.push_back(VT2);
2430 ResultTys.push_back(VT3);
2431 std::vector<SDOperand> Ops;
2432 Ops.push_back(Op1);
2433 Ops.push_back(Op2);
2434 Ops.push_back(Op3);
2435 Ops.push_back(Op4);
2436 Ops.push_back(Op5);
2437 Ops.push_back(Op6);
2438 Ops.push_back(Op7);
2439 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2440}
2441SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2442 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2443 std::vector<MVT::ValueType> ResultTys;
2444 ResultTys.push_back(VT1);
2445 ResultTys.push_back(VT2);
2446 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2447}
2448
Chris Lattner0fb094f2005-11-19 01:44:53 +00002449// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002450/// This can cause recursive merging of nodes in the DAG.
2451///
Chris Lattner8b52f212005-08-26 18:36:28 +00002452/// This version assumes From/To have a single result value.
2453///
Chris Lattner1e111c72005-09-07 05:37:01 +00002454void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2455 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002456 SDNode *From = FromN.Val, *To = ToN.Val;
2457 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2458 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002459 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002460
Chris Lattner8b8749f2005-08-17 19:00:20 +00002461 while (!From->use_empty()) {
2462 // Process users until they are all gone.
2463 SDNode *U = *From->use_begin();
2464
2465 // This node is about to morph, remove its old self from the CSE maps.
2466 RemoveNodeFromCSEMaps(U);
2467
Chris Lattner65113b22005-11-08 22:07:03 +00002468 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2469 I != E; ++I)
2470 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002471 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002472 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002473 To->addUser(U);
2474 }
2475
2476 // Now that we have modified U, add it back to the CSE maps. If it already
2477 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002478 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2479 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002480 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002481 if (Deleted) Deleted->push_back(U);
2482 DeleteNodeNotInCSEMaps(U);
2483 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002484 }
2485}
2486
2487/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2488/// This can cause recursive merging of nodes in the DAG.
2489///
2490/// This version assumes From/To have matching types and numbers of result
2491/// values.
2492///
Chris Lattner1e111c72005-09-07 05:37:01 +00002493void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2494 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002495 assert(From != To && "Cannot replace uses of with self");
2496 assert(From->getNumValues() == To->getNumValues() &&
2497 "Cannot use this version of ReplaceAllUsesWith!");
2498 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002499 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002500 return;
2501 }
2502
2503 while (!From->use_empty()) {
2504 // Process users until they are all gone.
2505 SDNode *U = *From->use_begin();
2506
2507 // This node is about to morph, remove its old self from the CSE maps.
2508 RemoveNodeFromCSEMaps(U);
2509
Chris Lattner65113b22005-11-08 22:07:03 +00002510 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2511 I != E; ++I)
2512 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002513 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002514 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002515 To->addUser(U);
2516 }
2517
2518 // Now that we have modified U, add it back to the CSE maps. If it already
2519 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002520 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2521 ReplaceAllUsesWith(U, Existing, Deleted);
2522 // U is now dead.
2523 if (Deleted) Deleted->push_back(U);
2524 DeleteNodeNotInCSEMaps(U);
2525 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002526 }
2527}
2528
Chris Lattner8b52f212005-08-26 18:36:28 +00002529/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2530/// This can cause recursive merging of nodes in the DAG.
2531///
2532/// This version can replace From with any result values. To must match the
2533/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002534void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002535 const std::vector<SDOperand> &To,
2536 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002537 assert(From->getNumValues() == To.size() &&
2538 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002539 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002540 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002541 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002542 return;
2543 }
2544
2545 while (!From->use_empty()) {
2546 // Process users until they are all gone.
2547 SDNode *U = *From->use_begin();
2548
2549 // This node is about to morph, remove its old self from the CSE maps.
2550 RemoveNodeFromCSEMaps(U);
2551
Chris Lattner65113b22005-11-08 22:07:03 +00002552 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2553 I != E; ++I)
2554 if (I->Val == From) {
2555 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002556 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002557 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002558 ToOp.Val->addUser(U);
2559 }
2560
2561 // Now that we have modified U, add it back to the CSE maps. If it already
2562 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002563 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2564 ReplaceAllUsesWith(U, Existing, Deleted);
2565 // U is now dead.
2566 if (Deleted) Deleted->push_back(U);
2567 DeleteNodeNotInCSEMaps(U);
2568 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002569 }
2570}
2571
Chris Lattner012f2412006-02-17 21:58:01 +00002572/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2573/// uses of other values produced by From.Val alone. The Deleted vector is
2574/// handled the same was as for ReplaceAllUsesWith.
2575void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2576 std::vector<SDNode*> &Deleted) {
2577 assert(From != To && "Cannot replace a value with itself");
2578 // Handle the simple, trivial, case efficiently.
2579 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2580 ReplaceAllUsesWith(From, To, &Deleted);
2581 return;
2582 }
2583
2584 // Get all of the users in a nice, deterministically ordered, uniqued set.
2585 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2586
2587 while (!Users.empty()) {
2588 // We know that this user uses some value of From. If it is the right
2589 // value, update it.
2590 SDNode *User = Users.back();
2591 Users.pop_back();
2592
2593 for (SDOperand *Op = User->OperandList,
2594 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2595 if (*Op == From) {
2596 // Okay, we know this user needs to be updated. Remove its old self
2597 // from the CSE maps.
2598 RemoveNodeFromCSEMaps(User);
2599
2600 // Update all operands that match "From".
2601 for (; Op != E; ++Op) {
2602 if (*Op == From) {
2603 From.Val->removeUser(User);
2604 *Op = To;
2605 To.Val->addUser(User);
2606 }
2607 }
2608
2609 // Now that we have modified User, add it back to the CSE maps. If it
2610 // already exists there, recursively merge the results together.
2611 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2612 unsigned NumDeleted = Deleted.size();
2613 ReplaceAllUsesWith(User, Existing, &Deleted);
2614
2615 // User is now dead.
2616 Deleted.push_back(User);
2617 DeleteNodeNotInCSEMaps(User);
2618
2619 // We have to be careful here, because ReplaceAllUsesWith could have
2620 // deleted a user of From, which means there may be dangling pointers
2621 // in the "Users" setvector. Scan over the deleted node pointers and
2622 // remove them from the setvector.
2623 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2624 Users.remove(Deleted[i]);
2625 }
2626 break; // Exit the operand scanning loop.
2627 }
2628 }
2629 }
2630}
2631
Chris Lattner7b2880c2005-08-24 22:44:39 +00002632
Jim Laskey58b968b2005-08-17 20:08:02 +00002633//===----------------------------------------------------------------------===//
2634// SDNode Class
2635//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002636
Chris Lattnera3255112005-11-08 23:30:28 +00002637
2638/// getValueTypeList - Return a pointer to the specified value type.
2639///
2640MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2641 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2642 VTs[VT] = VT;
2643 return &VTs[VT];
2644}
2645
Chris Lattner5c884562005-01-12 18:37:47 +00002646/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2647/// indicated value. This method ignores uses of other values defined by this
2648/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002649bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002650 assert(Value < getNumValues() && "Bad value!");
2651
2652 // If there is only one value, this is easy.
2653 if (getNumValues() == 1)
2654 return use_size() == NUses;
2655 if (Uses.size() < NUses) return false;
2656
Evan Cheng4ee62112006-02-05 06:29:23 +00002657 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002658
2659 std::set<SDNode*> UsersHandled;
2660
Evan Cheng4ee62112006-02-05 06:29:23 +00002661 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002662 UI != E; ++UI) {
2663 SDNode *User = *UI;
2664 if (User->getNumOperands() == 1 ||
2665 UsersHandled.insert(User).second) // First time we've seen this?
2666 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2667 if (User->getOperand(i) == TheValue) {
2668 if (NUses == 0)
2669 return false; // too many uses
2670 --NUses;
2671 }
2672 }
2673
2674 // Found exactly the right number of uses?
2675 return NUses == 0;
2676}
2677
2678
Evan Cheng4ee62112006-02-05 06:29:23 +00002679// isOnlyUse - Return true if this node is the only use of N.
2680bool SDNode::isOnlyUse(SDNode *N) const {
2681 bool Seen = false;
2682 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2683 SDNode *User = *I;
2684 if (User == this)
2685 Seen = true;
2686 else
2687 return false;
2688 }
2689
2690 return Seen;
2691}
2692
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002693// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002694bool SDOperand::isOperand(SDNode *N) const {
2695 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2696 if (*this == N->getOperand(i))
2697 return true;
2698 return false;
2699}
2700
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002701bool SDNode::isOperand(SDNode *N) const {
2702 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2703 if (this == N->OperandList[i].Val)
2704 return true;
2705 return false;
2706}
Evan Cheng4ee62112006-02-05 06:29:23 +00002707
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002708const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002709 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002710 default:
2711 if (getOpcode() < ISD::BUILTIN_OP_END)
2712 return "<<Unknown DAG Node>>";
2713 else {
Evan Cheng72261582005-12-20 06:22:03 +00002714 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002715 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002716 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2717 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002718
Evan Cheng72261582005-12-20 06:22:03 +00002719 TargetLowering &TLI = G->getTargetLoweringInfo();
2720 const char *Name =
2721 TLI.getTargetNodeName(getOpcode());
2722 if (Name) return Name;
2723 }
2724
2725 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002726 }
2727
Andrew Lenharth95762122005-03-31 21:24:06 +00002728 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002729 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002730 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002731 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002732 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002733 case ISD::AssertSext: return "AssertSext";
2734 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002735
2736 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002737 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002738 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002739 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002740
2741 case ISD::Constant: return "Constant";
2742 case ISD::ConstantFP: return "ConstantFP";
2743 case ISD::GlobalAddress: return "GlobalAddress";
2744 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002745 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002746 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002747 case ISD::INTRINSIC_WO_CHAIN: {
2748 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2749 return Intrinsic::getName((Intrinsic::ID)IID);
2750 }
2751 case ISD::INTRINSIC_VOID:
2752 case ISD::INTRINSIC_W_CHAIN: {
2753 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002754 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002755 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002756
Chris Lattnerb2827b02006-03-19 00:52:58 +00002757 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002758 case ISD::TargetConstant: return "TargetConstant";
2759 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002760 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2761 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002762 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002763 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002764
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002765 case ISD::CopyToReg: return "CopyToReg";
2766 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002767 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002768 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002769 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002770 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002771 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002772
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002773 // Unary operators
2774 case ISD::FABS: return "fabs";
2775 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002776 case ISD::FSQRT: return "fsqrt";
2777 case ISD::FSIN: return "fsin";
2778 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002779
2780 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002781 case ISD::ADD: return "add";
2782 case ISD::SUB: return "sub";
2783 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002784 case ISD::MULHU: return "mulhu";
2785 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002786 case ISD::SDIV: return "sdiv";
2787 case ISD::UDIV: return "udiv";
2788 case ISD::SREM: return "srem";
2789 case ISD::UREM: return "urem";
2790 case ISD::AND: return "and";
2791 case ISD::OR: return "or";
2792 case ISD::XOR: return "xor";
2793 case ISD::SHL: return "shl";
2794 case ISD::SRA: return "sra";
2795 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002796 case ISD::ROTL: return "rotl";
2797 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002798 case ISD::FADD: return "fadd";
2799 case ISD::FSUB: return "fsub";
2800 case ISD::FMUL: return "fmul";
2801 case ISD::FDIV: return "fdiv";
2802 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002803 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002804 case ISD::VADD: return "vadd";
2805 case ISD::VSUB: return "vsub";
2806 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002807 case ISD::VSDIV: return "vsdiv";
2808 case ISD::VUDIV: return "vudiv";
2809 case ISD::VAND: return "vand";
2810 case ISD::VOR: return "vor";
2811 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002812
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002813 case ISD::SETCC: return "setcc";
2814 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002815 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002816 case ISD::VSELECT: return "vselect";
2817 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2818 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2819 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002820 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002821 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2822 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2823 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2824 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2825 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002826 case ISD::ADDC: return "addc";
2827 case ISD::ADDE: return "adde";
2828 case ISD::SUBC: return "subc";
2829 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002830 case ISD::SHL_PARTS: return "shl_parts";
2831 case ISD::SRA_PARTS: return "sra_parts";
2832 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002833
Chris Lattner7f644642005-04-28 21:44:03 +00002834 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002835 case ISD::SIGN_EXTEND: return "sign_extend";
2836 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002837 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002838 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002839 case ISD::TRUNCATE: return "truncate";
2840 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002841 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002842 case ISD::FP_EXTEND: return "fp_extend";
2843
2844 case ISD::SINT_TO_FP: return "sint_to_fp";
2845 case ISD::UINT_TO_FP: return "uint_to_fp";
2846 case ISD::FP_TO_SINT: return "fp_to_sint";
2847 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002848 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002849
2850 // Control flow instructions
2851 case ISD::BR: return "br";
2852 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002853 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002854 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002855 case ISD::CALLSEQ_START: return "callseq_start";
2856 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002857
2858 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002859 case ISD::LOAD: return "load";
2860 case ISD::STORE: return "store";
2861 case ISD::VLOAD: return "vload";
2862 case ISD::EXTLOAD: return "extload";
2863 case ISD::SEXTLOAD: return "sextload";
2864 case ISD::ZEXTLOAD: return "zextload";
2865 case ISD::TRUNCSTORE: return "truncstore";
2866 case ISD::VAARG: return "vaarg";
2867 case ISD::VACOPY: return "vacopy";
2868 case ISD::VAEND: return "vaend";
2869 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002870 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002871 case ISD::EXTRACT_ELEMENT: return "extract_element";
2872 case ISD::BUILD_PAIR: return "build_pair";
2873 case ISD::STACKSAVE: return "stacksave";
2874 case ISD::STACKRESTORE: return "stackrestore";
2875
2876 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002877 case ISD::MEMSET: return "memset";
2878 case ISD::MEMCPY: return "memcpy";
2879 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002880
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002881 // Bit manipulation
2882 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002883 case ISD::CTPOP: return "ctpop";
2884 case ISD::CTTZ: return "cttz";
2885 case ISD::CTLZ: return "ctlz";
2886
Chris Lattner36ce6912005-11-29 06:21:05 +00002887 // Debug info
2888 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002889 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002890 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002891
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002892 case ISD::CONDCODE:
2893 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002894 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002895 case ISD::SETOEQ: return "setoeq";
2896 case ISD::SETOGT: return "setogt";
2897 case ISD::SETOGE: return "setoge";
2898 case ISD::SETOLT: return "setolt";
2899 case ISD::SETOLE: return "setole";
2900 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002901
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002902 case ISD::SETO: return "seto";
2903 case ISD::SETUO: return "setuo";
2904 case ISD::SETUEQ: return "setue";
2905 case ISD::SETUGT: return "setugt";
2906 case ISD::SETUGE: return "setuge";
2907 case ISD::SETULT: return "setult";
2908 case ISD::SETULE: return "setule";
2909 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002910
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002911 case ISD::SETEQ: return "seteq";
2912 case ISD::SETGT: return "setgt";
2913 case ISD::SETGE: return "setge";
2914 case ISD::SETLT: return "setlt";
2915 case ISD::SETLE: return "setle";
2916 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002917 }
2918 }
2919}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002920
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002921void SDNode::dump() const { dump(0); }
2922void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002923 std::cerr << (void*)this << ": ";
2924
2925 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2926 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002927 if (getValueType(i) == MVT::Other)
2928 std::cerr << "ch";
2929 else
2930 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002931 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002932 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002933
2934 std::cerr << " ";
2935 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2936 if (i) std::cerr << ", ";
2937 std::cerr << (void*)getOperand(i).Val;
2938 if (unsigned RN = getOperand(i).ResNo)
2939 std::cerr << ":" << RN;
2940 }
2941
2942 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2943 std::cerr << "<" << CSDN->getValue() << ">";
2944 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2945 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002946 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002947 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002948 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002949 std::cerr << "<";
2950 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002951 if (offset > 0)
2952 std::cerr << " + " << offset;
2953 else
2954 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002955 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002956 std::cerr << "<" << FIDN->getIndex() << ">";
2957 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002958 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002959 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002960 if (offset > 0)
2961 std::cerr << " + " << offset;
2962 else
2963 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002964 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002965 std::cerr << "<";
2966 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2967 if (LBB)
2968 std::cerr << LBB->getName() << " ";
2969 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002970 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002971 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002972 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2973 } else {
2974 std::cerr << " #" << R->getReg();
2975 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002976 } else if (const ExternalSymbolSDNode *ES =
2977 dyn_cast<ExternalSymbolSDNode>(this)) {
2978 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002979 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2980 if (M->getValue())
2981 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2982 else
2983 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002984 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2985 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002986 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002987}
2988
Chris Lattnerde202b32005-11-09 23:47:37 +00002989static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002990 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2991 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002992 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002993 else
2994 std::cerr << "\n" << std::string(indent+2, ' ')
2995 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002996
Chris Lattnerea946cd2005-01-09 20:38:33 +00002997
2998 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002999 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003000}
3001
Chris Lattnerc3aae252005-01-07 07:46:32 +00003002void SelectionDAG::dump() const {
3003 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003004 std::vector<const SDNode*> Nodes;
3005 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3006 I != E; ++I)
3007 Nodes.push_back(I);
3008
Chris Lattner49d24712005-01-09 20:26:36 +00003009 std::sort(Nodes.begin(), Nodes.end());
3010
3011 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003012 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003013 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003014 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003015
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003016 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003017
Chris Lattnerc3aae252005-01-07 07:46:32 +00003018 std::cerr << "\n\n";
3019}
3020
Evan Chengfae9f1c2006-02-09 22:11:03 +00003021/// InsertISelMapEntry - A helper function to insert a key / element pair
3022/// into a SDOperand to SDOperand map. This is added to avoid the map
3023/// insertion operator from being inlined.
3024void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3025 SDNode *Key, unsigned KeyResNo,
3026 SDNode *Element, unsigned ElementResNo) {
3027 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3028 SDOperand(Element, ElementResNo)));
3029}