blob: 943a950185222ce2ad19f98d3baa5af1a4dee6fe [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>
Evan Chenge6f35d82006-08-01 08:20:41 +000031#include <deque>
Chris Lattnere25738c2004-06-02 04:28:06 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner5cdcc582005-01-09 20:52:51 +000034static bool isCommutativeBinOp(unsigned Opcode) {
35 switch (Opcode) {
36 case ISD::ADD:
37 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000038 case ISD::MULHU:
39 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000040 case ISD::FADD:
41 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000042 case ISD::AND:
43 case ISD::OR:
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need commutative info for user ops!
46 }
47}
48
Chris Lattner5cdcc582005-01-09 20:52:51 +000049// isInvertibleForFree - Return true if there is no cost to emitting the logical
50// inverse of this node.
51static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000053 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000054 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000055 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000056}
57
Jim Laskey58b968b2005-08-17 20:08:02 +000058//===----------------------------------------------------------------------===//
59// ConstantFPSDNode Class
60//===----------------------------------------------------------------------===//
61
62/// isExactlyValue - We don't rely on operator== working on double values, as
63/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
64/// As such, this method can be used to do an exact bit-for-bit comparison of
65/// two floating point values.
66bool ConstantFPSDNode::isExactlyValue(double V) const {
67 return DoubleToBits(V) == DoubleToBits(Value);
68}
69
70//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000071// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000072//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000073
Evan Chenga8df1662006-03-27 06:58:47 +000074/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000075/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000076bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000077 // Look through a bit convert.
78 if (N->getOpcode() == ISD::BIT_CONVERT)
79 N = N->getOperand(0).Val;
80
Evan Chenga8df1662006-03-27 06:58:47 +000081 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000082
83 unsigned i = 0, e = N->getNumOperands();
84
85 // Skip over all of the undef values.
86 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
87 ++i;
88
89 // Do not accept an all-undef vector.
90 if (i == e) return false;
91
92 // Do not accept build_vectors that aren't all constants or which have non-~0
93 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000094 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (isa<ConstantSDNode>(NotZero)) {
96 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
97 return false;
98 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000099 MVT::ValueType VT = NotZero.getValueType();
100 if (VT== MVT::f64) {
101 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
102 (uint64_t)-1)
103 return false;
104 } else {
105 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
106 (uint32_t)-1)
107 return false;
108 }
Evan Chenga8df1662006-03-27 06:58:47 +0000109 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000110 return false;
111
112 // Okay, we have at least one ~0 value, check to see if the rest match or are
113 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000114 for (++i; i != e; ++i)
115 if (N->getOperand(i) != NotZero &&
116 N->getOperand(i).getOpcode() != ISD::UNDEF)
117 return false;
118 return true;
119}
120
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122/// isBuildVectorAllZeros - Return true if the specified node is a
123/// BUILD_VECTOR where all of the elements are 0 or undef.
124bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000125 // Look through a bit convert.
126 if (N->getOpcode() == ISD::BIT_CONVERT)
127 N = N->getOperand(0).Val;
128
Evan Cheng4a147842006-03-26 09:50:58 +0000129 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000130
131 unsigned i = 0, e = N->getNumOperands();
132
133 // Skip over all of the undef values.
134 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
135 ++i;
136
137 // Do not accept an all-undef vector.
138 if (i == e) return false;
139
140 // Do not accept build_vectors that aren't all constants or which have non-~0
141 // elements.
142 SDOperand Zero = N->getOperand(i);
143 if (isa<ConstantSDNode>(Zero)) {
144 if (!cast<ConstantSDNode>(Zero)->isNullValue())
145 return false;
146 } else if (isa<ConstantFPSDNode>(Zero)) {
147 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
148 return false;
149 } else
150 return false;
151
152 // Okay, we have at least one ~0 value, check to see if the rest match or are
153 // undefs.
154 for (++i; i != e; ++i)
155 if (N->getOperand(i) != Zero &&
156 N->getOperand(i).getOpcode() != ISD::UNDEF)
157 return false;
158 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000159}
160
Chris Lattnerc3aae252005-01-07 07:46:32 +0000161/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
162/// when given the operation for (X op Y).
163ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
164 // To perform this operation, we just need to swap the L and G bits of the
165 // operation.
166 unsigned OldL = (Operation >> 2) & 1;
167 unsigned OldG = (Operation >> 1) & 1;
168 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
169 (OldL << 1) | // New G bit
170 (OldG << 2)); // New L bit.
171}
172
173/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
174/// 'op' is a valid SetCC operation.
175ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
176 unsigned Operation = Op;
177 if (isInteger)
178 Operation ^= 7; // Flip L, G, E bits, but not U.
179 else
180 Operation ^= 15; // Flip all of the condition bits.
181 if (Operation > ISD::SETTRUE2)
182 Operation &= ~8; // Don't let N and U bits get set.
183 return ISD::CondCode(Operation);
184}
185
186
187/// isSignedOp - For an integer comparison, return 1 if the comparison is a
188/// signed operation and 2 if the result is an unsigned comparison. Return zero
189/// if the operation does not depend on the sign of the input (setne and seteq).
190static int isSignedOp(ISD::CondCode Opcode) {
191 switch (Opcode) {
192 default: assert(0 && "Illegal integer setcc operation!");
193 case ISD::SETEQ:
194 case ISD::SETNE: return 0;
195 case ISD::SETLT:
196 case ISD::SETLE:
197 case ISD::SETGT:
198 case ISD::SETGE: return 1;
199 case ISD::SETULT:
200 case ISD::SETULE:
201 case ISD::SETUGT:
202 case ISD::SETUGE: return 2;
203 }
204}
205
206/// getSetCCOrOperation - Return the result of a logical OR between different
207/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
208/// returns SETCC_INVALID if it is not possible to represent the resultant
209/// comparison.
210ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
211 bool isInteger) {
212 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
213 // Cannot fold a signed integer setcc with an unsigned integer setcc.
214 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215
Chris Lattnerc3aae252005-01-07 07:46:32 +0000216 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000217
Chris Lattnerc3aae252005-01-07 07:46:32 +0000218 // If the N and U bits get set then the resultant comparison DOES suddenly
219 // care about orderedness, and is true when ordered.
220 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000221 Op &= ~16; // Clear the U bit if the N bit is set.
222
223 // Canonicalize illegal integer setcc's.
224 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
225 Op = ISD::SETNE;
226
Chris Lattnerc3aae252005-01-07 07:46:32 +0000227 return ISD::CondCode(Op);
228}
229
230/// getSetCCAndOperation - Return the result of a logical AND between different
231/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
232/// function returns zero if it is not possible to represent the resultant
233/// comparison.
234ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
235 bool isInteger) {
236 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
237 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239
240 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000241 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
242
243 // Canonicalize illegal integer setcc's.
244 if (isInteger) {
245 switch (Result) {
246 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000247 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
248 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
249 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
250 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000251 }
252 }
253
254 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000255}
256
Chris Lattnerb48da392005-01-23 04:39:44 +0000257const TargetMachine &SelectionDAG::getTarget() const {
258 return TLI.getTargetMachine();
259}
260
Jim Laskey58b968b2005-08-17 20:08:02 +0000261//===----------------------------------------------------------------------===//
262// SelectionDAG Class
263//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000264
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265/// RemoveDeadNodes - This method deletes all unreachable nodes in the
266/// SelectionDAG, including nodes (like loads) that have uses of their token
267/// chain but no other uses and no side effect. If a node is passed in as an
268/// argument, it is used as the seed for node deletion.
269void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000270 // Create a dummy node (which is not added to allnodes), that adds a reference
271 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000272 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273
Chris Lattnerf469cb62005-11-08 18:52:27 +0000274 bool MadeChange = false;
275
Chris Lattner8b8749f2005-08-17 19:00:20 +0000276 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000277 if (N && N->use_empty()) {
278 DestroyDeadNode(N);
279 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280 }
281
Chris Lattnerde202b32005-11-09 23:47:37 +0000282 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
283 if (I->use_empty() && I->getOpcode() != 65535) {
284 // Node is dead, recursively delete newly dead uses.
285 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000286 MadeChange = true;
287 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000288
289 // Walk the nodes list, removing the nodes we've marked as dead.
290 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000291 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
292 SDNode *N = I++;
293 if (N->use_empty())
294 AllNodes.erase(N);
295 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000296 }
297
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000299 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000300}
301
Chris Lattnerf469cb62005-11-08 18:52:27 +0000302/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
303/// graph. If it is the last user of any of its operands, recursively process
304/// them the same way.
305///
306void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 // Okay, we really are going to delete this node. First take this out of the
308 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000309 RemoveNodeFromCSEMaps(N);
310
311 // Next, brutally remove the operand list. This is safe to do, as there are
312 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000313 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
314 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000315 O->removeUser(N);
316
317 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000318 if (O->use_empty())
319 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000320 }
Chris Lattner65113b22005-11-08 22:07:03 +0000321 delete[] N->OperandList;
322 N->OperandList = 0;
323 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000324
325 // Mark the node as dead.
326 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000327}
328
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000329void SelectionDAG::DeleteNode(SDNode *N) {
330 assert(N->use_empty() && "Cannot delete a node that is not dead!");
331
332 // First take this out of the appropriate CSE map.
333 RemoveNodeFromCSEMaps(N);
334
Chris Lattner1e111c72005-09-07 05:37:01 +0000335 // Finally, remove uses due to operands of this node, remove from the
336 // AllNodes list, and delete the node.
337 DeleteNodeNotInCSEMaps(N);
338}
339
340void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
341
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000342 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000343 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000344
345 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000346 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
347 I->Val->removeUser(N);
348 delete[] N->OperandList;
349 N->OperandList = 0;
350 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000351
352 delete N;
353}
354
Chris Lattner149c58c2005-08-16 18:17:10 +0000355/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
356/// correspond to it. This is useful when we're about to delete or repurpose
357/// the node. We don't want future request for structurally identical nodes
358/// to return N anymore.
359void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000360 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000361 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000362 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000363 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000364 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
365 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000366 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000367 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000368 Erased = TargetConstants.erase(std::make_pair(
369 cast<ConstantSDNode>(N)->getValue(),
370 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000371 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000372 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000373 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000374 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000375 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000376 }
Chris Lattner3181a772006-01-29 06:26:56 +0000377 case ISD::TargetConstantFP: {
378 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
379 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
380 break;
381 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000382 case ISD::STRING:
383 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
384 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000385 case ISD::CONDCODE:
386 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
387 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000389 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
390 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000391 case ISD::GlobalAddress: {
392 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
393 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
394 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000395 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000396 }
397 case ISD::TargetGlobalAddress: {
398 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
399 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
400 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000401 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000402 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000403 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000404 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000405 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000406 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000407 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000408 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000409 case ISD::JumpTable:
410 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
411 break;
412 case ISD::TargetJumpTable:
413 Erased =
414 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
415 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000416 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000417 Erased = ConstantPoolIndices.
418 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000419 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
420 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000421 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000422 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000423 Erased = TargetConstantPoolIndices.
424 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000425 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
426 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000427 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000428 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000429 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000430 break;
431 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000432 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000433 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000434 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000435 Erased =
436 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000437 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000438 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000439 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000440 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
441 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000442 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000443 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
444 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000445 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000446 case ISD::SRCVALUE: {
447 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000448 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000449 break;
450 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000451 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000452 Erased = Loads.erase(std::make_pair(N->getOperand(1),
453 std::make_pair(N->getOperand(0),
454 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000455 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000456 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000457 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000458 if (N->getNumOperands() == 0) {
459 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
460 N->getValueType(0)));
461 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000462 Erased =
463 UnaryOps.erase(std::make_pair(N->getOpcode(),
464 std::make_pair(N->getOperand(0),
465 N->getValueType(0))));
466 } else if (N->getNumOperands() == 2) {
467 Erased =
468 BinaryOps.erase(std::make_pair(N->getOpcode(),
469 std::make_pair(N->getOperand(0),
470 N->getOperand(1))));
471 } else {
472 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
473 Erased =
474 OneResultNodes.erase(std::make_pair(N->getOpcode(),
475 std::make_pair(N->getValueType(0),
476 Ops)));
477 }
Chris Lattner385328c2005-05-14 07:42:29 +0000478 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000479 // Remove the node from the ArbitraryNodes map.
480 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
481 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000482 Erased =
483 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
484 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000485 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000486 break;
487 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000488#ifndef NDEBUG
489 // Verify that the node was actually in one of the CSE maps, unless it has a
490 // flag result (which cannot be CSE'd) or is one of the special cases that are
491 // not subject to CSE.
492 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000493 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000494 N->dump();
495 assert(0 && "Node is not in map!");
496 }
497#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000498}
499
Chris Lattner8b8749f2005-08-17 19:00:20 +0000500/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
501/// has been taken out and modified in some way. If the specified node already
502/// exists in the CSE maps, do not modify the maps, but return the existing node
503/// instead. If it doesn't exist, add it and return null.
504///
505SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
506 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000507 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000508 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000509
Chris Lattner9f8cc692005-12-19 22:21:21 +0000510 // Check that remaining values produced are not flags.
511 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
512 if (N->getValueType(i) == MVT::Flag)
513 return 0; // Never CSE anything that produces a flag.
514
Chris Lattnerfe14b342005-12-01 23:14:50 +0000515 if (N->getNumValues() == 1) {
516 if (N->getNumOperands() == 1) {
517 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
518 std::make_pair(N->getOperand(0),
519 N->getValueType(0)))];
520 if (U) return U;
521 U = N;
522 } else if (N->getNumOperands() == 2) {
523 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
524 std::make_pair(N->getOperand(0),
525 N->getOperand(1)))];
526 if (B) return B;
527 B = N;
528 } else {
529 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
530 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000531 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000532 if (ORN) return ORN;
533 ORN = N;
534 }
535 } else {
536 if (N->getOpcode() == ISD::LOAD) {
537 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
538 std::make_pair(N->getOperand(0),
539 N->getValueType(0)))];
540 if (L) return L;
541 L = N;
542 } else {
543 // Remove the node from the ArbitraryNodes map.
544 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
545 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
546 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
547 std::make_pair(RV, Ops))];
548 if (AN) return AN;
549 AN = N;
550 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000551 }
552 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000553}
554
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000555/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
556/// were replaced with those specified. If this node is never memoized,
557/// return null, otherwise return a pointer to the slot it would take. If a
558/// node already exists with these operands, the slot will be non-null.
559SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000560 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000561 return 0; // Never add these nodes.
562
563 // Check that remaining values produced are not flags.
564 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
565 if (N->getValueType(i) == MVT::Flag)
566 return 0; // Never CSE anything that produces a flag.
567
568 if (N->getNumValues() == 1) {
569 return &UnaryOps[std::make_pair(N->getOpcode(),
570 std::make_pair(Op, N->getValueType(0)))];
571 } else {
572 // Remove the node from the ArbitraryNodes map.
573 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
574 std::vector<SDOperand> Ops;
575 Ops.push_back(Op);
576 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
577 std::make_pair(RV, Ops))];
578 }
579 return 0;
580}
581
582/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
583/// were replaced with those specified. If this node is never memoized,
584/// return null, otherwise return a pointer to the slot it would take. If a
585/// node already exists with these operands, the slot will be non-null.
586SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
587 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000588 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000589 return 0; // Never add these nodes.
590
591 // Check that remaining values produced are not flags.
592 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
593 if (N->getValueType(i) == MVT::Flag)
594 return 0; // Never CSE anything that produces a flag.
595
596 if (N->getNumValues() == 1) {
597 return &BinaryOps[std::make_pair(N->getOpcode(),
598 std::make_pair(Op1, Op2))];
599 } else {
600 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
601 std::vector<SDOperand> Ops;
602 Ops.push_back(Op1);
603 Ops.push_back(Op2);
604 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
605 std::make_pair(RV, Ops))];
606 }
607 return 0;
608}
609
610
611/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
612/// were replaced with those specified. If this node is never memoized,
613/// return null, otherwise return a pointer to the slot it would take. If a
614/// node already exists with these operands, the slot will be non-null.
615SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
616 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000617 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000618 return 0; // Never add these nodes.
619
620 // Check that remaining values produced are not flags.
621 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
622 if (N->getValueType(i) == MVT::Flag)
623 return 0; // Never CSE anything that produces a flag.
624
625 if (N->getNumValues() == 1) {
626 if (N->getNumOperands() == 1) {
627 return &UnaryOps[std::make_pair(N->getOpcode(),
628 std::make_pair(Ops[0],
629 N->getValueType(0)))];
630 } else if (N->getNumOperands() == 2) {
631 return &BinaryOps[std::make_pair(N->getOpcode(),
632 std::make_pair(Ops[0], Ops[1]))];
633 } else {
634 return &OneResultNodes[std::make_pair(N->getOpcode(),
635 std::make_pair(N->getValueType(0),
636 Ops))];
637 }
638 } else {
639 if (N->getOpcode() == ISD::LOAD) {
640 return &Loads[std::make_pair(Ops[1],
641 std::make_pair(Ops[0], N->getValueType(0)))];
642 } else {
643 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
644 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
645 std::make_pair(RV, Ops))];
646 }
647 }
648 return 0;
649}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000650
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000651
Chris Lattner78ec3112003-08-11 14:57:33 +0000652SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000655 delete [] N->OperandList;
656 N->OperandList = 0;
657 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000658 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000659 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000660}
661
Chris Lattner0f2287b2005-04-13 02:38:18 +0000662SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000663 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000664 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000665 return getNode(ISD::AND, Op.getValueType(), Op,
666 getConstant(Imm, Op.getValueType()));
667}
668
Chris Lattnerc3aae252005-01-07 07:46:32 +0000669SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
670 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000671 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
672
Chris Lattnerc3aae252005-01-07 07:46:32 +0000673 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000674 if (VT != MVT::i64)
675 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000676
Chris Lattnerc3aae252005-01-07 07:46:32 +0000677 SDNode *&N = Constants[std::make_pair(Val, VT)];
678 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000679 N = new ConstantSDNode(false, Val, VT);
680 AllNodes.push_back(N);
681 return SDOperand(N, 0);
682}
683
Chris Lattner36ce6912005-11-29 06:21:05 +0000684SDOperand SelectionDAG::getString(const std::string &Val) {
685 StringSDNode *&N = StringNodes[Val];
686 if (!N) {
687 N = new StringSDNode(Val);
688 AllNodes.push_back(N);
689 }
690 return SDOperand(N, 0);
691}
692
Chris Lattner37bfbb42005-08-17 00:34:06 +0000693SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
694 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
695 // Mask out any bits that are not valid for this constant.
696 if (VT != MVT::i64)
697 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
698
699 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
700 if (N) return SDOperand(N, 0);
701 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000702 AllNodes.push_back(N);
703 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000704}
705
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
707 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
708 if (VT == MVT::f32)
709 Val = (float)Val; // Mask out extra precision.
710
Chris Lattnerd8658612005-02-17 20:17:32 +0000711 // Do the map lookup using the actual bit pattern for the floating point
712 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
713 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000714 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000716 N = new ConstantFPSDNode(false, Val, VT);
717 AllNodes.push_back(N);
718 return SDOperand(N, 0);
719}
720
721SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
722 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
723 if (VT == MVT::f32)
724 Val = (float)Val; // Mask out extra precision.
725
726 // Do the map lookup using the actual bit pattern for the floating point
727 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
728 // we don't have issues with SNANs.
729 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
730 if (N) return SDOperand(N, 0);
731 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
734}
735
Chris Lattnerc3aae252005-01-07 07:46:32 +0000736SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000737 MVT::ValueType VT, int offset) {
738 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000739 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000740 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000741 AllNodes.push_back(N);
742 return SDOperand(N, 0);
743}
744
745SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000746 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000747 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000748 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000749 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000750 AllNodes.push_back(N);
751 return SDOperand(N, 0);
752}
753
754SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
755 SDNode *&N = FrameIndices[FI];
756 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000757 N = new FrameIndexSDNode(FI, VT, false);
758 AllNodes.push_back(N);
759 return SDOperand(N, 0);
760}
761
762SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
763 SDNode *&N = TargetFrameIndices[FI];
764 if (N) return SDOperand(N, 0);
765 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000766 AllNodes.push_back(N);
767 return SDOperand(N, 0);
768}
769
Nate Begeman37efe672006-04-22 18:53:45 +0000770SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
771 SDNode *&N = JumpTableIndices[JTI];
772 if (N) return SDOperand(N, 0);
773 N = new JumpTableSDNode(JTI, VT, false);
774 AllNodes.push_back(N);
775 return SDOperand(N, 0);
776}
777
778SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
779 SDNode *&N = TargetJumpTableIndices[JTI];
780 if (N) return SDOperand(N, 0);
781 N = new JumpTableSDNode(JTI, VT, true);
782 AllNodes.push_back(N);
783 return SDOperand(N, 0);
784}
785
Evan Chengb8973bd2006-01-31 22:23:14 +0000786SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000787 unsigned Alignment, int Offset) {
788 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
789 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000790 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000791 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000792 AllNodes.push_back(N);
793 return SDOperand(N, 0);
794}
795
Evan Chengb8973bd2006-01-31 22:23:14 +0000796SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000797 unsigned Alignment, int Offset) {
798 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
799 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000800 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000801 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802 AllNodes.push_back(N);
803 return SDOperand(N, 0);
804}
805
806SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
807 SDNode *&N = BBNodes[MBB];
808 if (N) return SDOperand(N, 0);
809 N = new BasicBlockSDNode(MBB);
810 AllNodes.push_back(N);
811 return SDOperand(N, 0);
812}
813
Chris Lattner15e4b012005-07-10 00:07:11 +0000814SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
815 if ((unsigned)VT >= ValueTypeNodes.size())
816 ValueTypeNodes.resize(VT+1);
817 if (ValueTypeNodes[VT] == 0) {
818 ValueTypeNodes[VT] = new VTSDNode(VT);
819 AllNodes.push_back(ValueTypeNodes[VT]);
820 }
821
822 return SDOperand(ValueTypeNodes[VT], 0);
823}
824
Chris Lattnerc3aae252005-01-07 07:46:32 +0000825SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
826 SDNode *&N = ExternalSymbols[Sym];
827 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000828 N = new ExternalSymbolSDNode(false, Sym, VT);
829 AllNodes.push_back(N);
830 return SDOperand(N, 0);
831}
832
Chris Lattner809ec112006-01-28 10:09:25 +0000833SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
834 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000835 SDNode *&N = TargetExternalSymbols[Sym];
836 if (N) return SDOperand(N, 0);
837 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000838 AllNodes.push_back(N);
839 return SDOperand(N, 0);
840}
841
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000842SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
843 if ((unsigned)Cond >= CondCodeNodes.size())
844 CondCodeNodes.resize(Cond+1);
845
Chris Lattner079a27a2005-08-09 20:40:02 +0000846 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000847 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000848 AllNodes.push_back(CondCodeNodes[Cond]);
849 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000850 return SDOperand(CondCodeNodes[Cond], 0);
851}
852
Chris Lattner0fdd7682005-08-30 22:38:38 +0000853SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
854 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
855 if (!Reg) {
856 Reg = new RegisterSDNode(RegNo, VT);
857 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000858 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000859 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000860}
861
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000862SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
863 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000864 // These setcc operations always fold.
865 switch (Cond) {
866 default: break;
867 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000868 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000870 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000871
872 case ISD::SETOEQ:
873 case ISD::SETOGT:
874 case ISD::SETOGE:
875 case ISD::SETOLT:
876 case ISD::SETOLE:
877 case ISD::SETONE:
878 case ISD::SETO:
879 case ISD::SETUO:
880 case ISD::SETUEQ:
881 case ISD::SETUNE:
882 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
883 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 }
885
Chris Lattner67255a12005-04-07 18:14:58 +0000886 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
887 uint64_t C2 = N2C->getValue();
888 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
889 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000890
Chris Lattnerc3aae252005-01-07 07:46:32 +0000891 // Sign extend the operands if required
892 if (ISD::isSignedIntSetCC(Cond)) {
893 C1 = N1C->getSignExtended();
894 C2 = N2C->getSignExtended();
895 }
896
897 switch (Cond) {
898 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000899 case ISD::SETEQ: return getConstant(C1 == C2, VT);
900 case ISD::SETNE: return getConstant(C1 != C2, VT);
901 case ISD::SETULT: return getConstant(C1 < C2, VT);
902 case ISD::SETUGT: return getConstant(C1 > C2, VT);
903 case ISD::SETULE: return getConstant(C1 <= C2, VT);
904 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
905 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
906 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
907 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
908 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000909 }
Chris Lattner24673922005-04-07 18:58:54 +0000910 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000911 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000912 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
913 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
914
915 // If the comparison constant has bits in the upper part, the
916 // zero-extended value could never match.
917 if (C2 & (~0ULL << InSize)) {
918 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
919 switch (Cond) {
920 case ISD::SETUGT:
921 case ISD::SETUGE:
922 case ISD::SETEQ: return getConstant(0, VT);
923 case ISD::SETULT:
924 case ISD::SETULE:
925 case ISD::SETNE: return getConstant(1, VT);
926 case ISD::SETGT:
927 case ISD::SETGE:
928 // True if the sign bit of C2 is set.
929 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
930 case ISD::SETLT:
931 case ISD::SETLE:
932 // True if the sign bit of C2 isn't set.
933 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
934 default:
935 break;
936 }
937 }
938
939 // Otherwise, we can perform the comparison with the low bits.
940 switch (Cond) {
941 case ISD::SETEQ:
942 case ISD::SETNE:
943 case ISD::SETUGT:
944 case ISD::SETUGE:
945 case ISD::SETULT:
946 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000947 return getSetCC(VT, N1.getOperand(0),
948 getConstant(C2, N1.getOperand(0).getValueType()),
949 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000950 default:
951 break; // todo, be more careful with signed comparisons
952 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000953 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
954 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
955 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
956 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
957 MVT::ValueType ExtDstTy = N1.getValueType();
958 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000959
Chris Lattner7b2880c2005-08-24 22:44:39 +0000960 // If the extended part has any inconsistent bits, it cannot ever
961 // compare equal. In other words, they have to be all ones or all
962 // zeros.
963 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000964 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000965 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
966 return getConstant(Cond == ISD::SETNE, VT);
967
968 // Otherwise, make this a use of a zext.
969 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000970 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000971 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000972 }
973
Chris Lattner67255a12005-04-07 18:14:58 +0000974 uint64_t MinVal, MaxVal;
975 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
976 if (ISD::isSignedIntSetCC(Cond)) {
977 MinVal = 1ULL << (OperandBitSize-1);
978 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
979 MaxVal = ~0ULL >> (65-OperandBitSize);
980 else
981 MaxVal = 0;
982 } else {
983 MinVal = 0;
984 MaxVal = ~0ULL >> (64-OperandBitSize);
985 }
986
987 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
988 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
989 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
990 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000991 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
992 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000993 }
994
995 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
996 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
997 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000998 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
999 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +00001000 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001001
Nate Begeman72ea2812005-04-14 08:56:52 +00001002 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
1003 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +00001004
Nate Begeman72ea2812005-04-14 08:56:52 +00001005 // Canonicalize setgt X, Min --> setne X, Min
1006 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001007 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001008
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001009 // If we have setult X, 1, turn it into seteq X, 0
1010 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001011 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
1012 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +00001013 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001014 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001015 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
1016 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +00001017
1018 // If we have "setcc X, C1", check to see if we can shrink the immediate
1019 // by changing cc.
1020
1021 // SETUGT X, SINTMAX -> SETLT X, 0
1022 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1023 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001024 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +00001025
1026 // FIXME: Implement the rest of these.
1027
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001028
1029 // Fold bit comparisons when we can.
1030 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1031 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1032 if (ConstantSDNode *AndRHS =
1033 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1034 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1035 // Perform the xform if the AND RHS is a single bit.
1036 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1037 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001038 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001039 TLI.getShiftAmountTy()));
1040 }
1041 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1042 // (X & 8) == 8 --> (X & 8) >> 3
1043 // Perform the xform if C2 is a single bit.
1044 if ((C2 & (C2-1)) == 0) {
1045 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001046 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001047 }
1048 }
1049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 }
Chris Lattner67255a12005-04-07 18:14:58 +00001051 } else if (isa<ConstantSDNode>(N1.Val)) {
1052 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001053 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001054 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055
1056 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1057 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1058 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001059
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060 switch (Cond) {
1061 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001062 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1063 case ISD::SETNE: return getConstant(C1 != C2, VT);
1064 case ISD::SETLT: return getConstant(C1 < C2, VT);
1065 case ISD::SETGT: return getConstant(C1 > C2, VT);
1066 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1067 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 }
1069 } else {
1070 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001071 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072 }
1073
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001074 // Could not fold it.
1075 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076}
1077
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001079///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001081 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1082 if (!N) {
1083 N = new SDNode(Opcode, VT);
1084 AllNodes.push_back(N);
1085 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 return SDOperand(N, 0);
1087}
1088
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1090 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001091 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001092 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1094 uint64_t Val = C->getValue();
1095 switch (Opcode) {
1096 default: break;
1097 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001098 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1100 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001101 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1102 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001103 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001104 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001105 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001106 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001107 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001108 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001109 case ISD::BSWAP:
1110 switch(VT) {
1111 default: assert(0 && "Invalid bswap!"); break;
1112 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1113 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1114 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1115 }
1116 break;
1117 case ISD::CTPOP:
1118 switch(VT) {
1119 default: assert(0 && "Invalid ctpop!"); break;
1120 case MVT::i1: return getConstant(Val != 0, VT);
1121 case MVT::i8:
1122 Tmp1 = (unsigned)Val & 0xFF;
1123 return getConstant(CountPopulation_32(Tmp1), VT);
1124 case MVT::i16:
1125 Tmp1 = (unsigned)Val & 0xFFFF;
1126 return getConstant(CountPopulation_32(Tmp1), VT);
1127 case MVT::i32:
1128 return getConstant(CountPopulation_32((unsigned)Val), VT);
1129 case MVT::i64:
1130 return getConstant(CountPopulation_64(Val), VT);
1131 }
1132 case ISD::CTLZ:
1133 switch(VT) {
1134 default: assert(0 && "Invalid ctlz!"); break;
1135 case MVT::i1: return getConstant(Val == 0, VT);
1136 case MVT::i8:
1137 Tmp1 = (unsigned)Val & 0xFF;
1138 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1139 case MVT::i16:
1140 Tmp1 = (unsigned)Val & 0xFFFF;
1141 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1142 case MVT::i32:
1143 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1144 case MVT::i64:
1145 return getConstant(CountLeadingZeros_64(Val), VT);
1146 }
1147 case ISD::CTTZ:
1148 switch(VT) {
1149 default: assert(0 && "Invalid cttz!"); break;
1150 case MVT::i1: return getConstant(Val == 0, VT);
1151 case MVT::i8:
1152 Tmp1 = (unsigned)Val | 0x100;
1153 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1154 case MVT::i16:
1155 Tmp1 = (unsigned)Val | 0x10000;
1156 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1157 case MVT::i32:
1158 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1159 case MVT::i64:
1160 return getConstant(CountTrailingZeros_64(Val), VT);
1161 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 }
1163 }
1164
Chris Lattner94683772005-12-23 05:30:37 +00001165 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001166 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1167 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001168 case ISD::FNEG:
1169 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001170 case ISD::FABS:
1171 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 case ISD::FP_ROUND:
1173 case ISD::FP_EXTEND:
1174 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001175 case ISD::FP_TO_SINT:
1176 return getConstant((int64_t)C->getValue(), VT);
1177 case ISD::FP_TO_UINT:
1178 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001179 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001180 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001181 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001182 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001183 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001184 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
1186
1187 unsigned OpOpcode = Operand.Val->getOpcode();
1188 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001189 case ISD::TokenFactor:
1190 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191 case ISD::SIGN_EXTEND:
1192 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001193 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1195 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1196 break;
1197 case ISD::ZERO_EXTEND:
1198 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001199 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001200 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001201 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001202 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001203 case ISD::ANY_EXTEND:
1204 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001205 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001206 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1207 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1208 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1209 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001210 case ISD::TRUNCATE:
1211 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001212 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 if (OpOpcode == ISD::TRUNCATE)
1214 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001215 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1216 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001217 // If the source is smaller than the dest, we still need an extend.
1218 if (Operand.Val->getOperand(0).getValueType() < VT)
1219 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1220 else if (Operand.Val->getOperand(0).getValueType() > VT)
1221 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1222 else
1223 return Operand.Val->getOperand(0);
1224 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 break;
Chris Lattner35481892005-12-23 00:16:34 +00001226 case ISD::BIT_CONVERT:
1227 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001228 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001229 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001230 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001231 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1232 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001233 if (OpOpcode == ISD::UNDEF)
1234 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001235 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001236 case ISD::SCALAR_TO_VECTOR:
1237 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1238 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1239 "Illegal SCALAR_TO_VECTOR node!");
1240 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001241 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001242 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1243 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001244 Operand.Val->getOperand(0));
1245 if (OpOpcode == ISD::FNEG) // --X -> X
1246 return Operand.Val->getOperand(0);
1247 break;
1248 case ISD::FABS:
1249 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1250 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1251 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001252 }
1253
Chris Lattner43247a12005-08-25 19:12:10 +00001254 SDNode *N;
1255 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1256 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001257 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001258 E = N = new SDNode(Opcode, Operand);
1259 } else {
1260 N = new SDNode(Opcode, Operand);
1261 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001262 N->setValueTypes(VT);
1263 AllNodes.push_back(N);
1264 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001265}
1266
Chris Lattner36019aa2005-04-18 03:48:41 +00001267
1268
Chris Lattnerc3aae252005-01-07 07:46:32 +00001269SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1270 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001271#ifndef NDEBUG
1272 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001273 case ISD::TokenFactor:
1274 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1275 N2.getValueType() == MVT::Other && "Invalid token factor!");
1276 break;
Chris Lattner76365122005-01-16 02:23:22 +00001277 case ISD::AND:
1278 case ISD::OR:
1279 case ISD::XOR:
1280 case ISD::UDIV:
1281 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001282 case ISD::MULHU:
1283 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001284 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1285 // fall through
1286 case ISD::ADD:
1287 case ISD::SUB:
1288 case ISD::MUL:
1289 case ISD::SDIV:
1290 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001291 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1292 // fall through.
1293 case ISD::FADD:
1294 case ISD::FSUB:
1295 case ISD::FMUL:
1296 case ISD::FDIV:
1297 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001298 assert(N1.getValueType() == N2.getValueType() &&
1299 N1.getValueType() == VT && "Binary operator types must match!");
1300 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001301 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1302 assert(N1.getValueType() == VT &&
1303 MVT::isFloatingPoint(N1.getValueType()) &&
1304 MVT::isFloatingPoint(N2.getValueType()) &&
1305 "Invalid FCOPYSIGN!");
1306 break;
Chris Lattner76365122005-01-16 02:23:22 +00001307 case ISD::SHL:
1308 case ISD::SRA:
1309 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001310 case ISD::ROTL:
1311 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001312 assert(VT == N1.getValueType() &&
1313 "Shift operators return type must be the same as their first arg");
1314 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001315 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001316 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001317 case ISD::FP_ROUND_INREG: {
1318 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1319 assert(VT == N1.getValueType() && "Not an inreg round!");
1320 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1321 "Cannot FP_ROUND_INREG integer types");
1322 assert(EVT <= VT && "Not rounding down!");
1323 break;
1324 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001325 case ISD::AssertSext:
1326 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001327 case ISD::SIGN_EXTEND_INREG: {
1328 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1329 assert(VT == N1.getValueType() && "Not an inreg extend!");
1330 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1331 "Cannot *_EXTEND_INREG FP types");
1332 assert(EVT <= VT && "Not extending!");
1333 }
1334
Chris Lattner76365122005-01-16 02:23:22 +00001335 default: break;
1336 }
1337#endif
1338
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1340 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1341 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001342 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1343 int64_t Val = N1C->getValue();
1344 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1345 Val <<= 64-FromBits;
1346 Val >>= 64-FromBits;
1347 return getConstant(Val, VT);
1348 }
1349
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 if (N2C) {
1351 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1352 switch (Opcode) {
1353 case ISD::ADD: return getConstant(C1 + C2, VT);
1354 case ISD::SUB: return getConstant(C1 - C2, VT);
1355 case ISD::MUL: return getConstant(C1 * C2, VT);
1356 case ISD::UDIV:
1357 if (C2) return getConstant(C1 / C2, VT);
1358 break;
1359 case ISD::UREM :
1360 if (C2) return getConstant(C1 % C2, VT);
1361 break;
1362 case ISD::SDIV :
1363 if (C2) return getConstant(N1C->getSignExtended() /
1364 N2C->getSignExtended(), VT);
1365 break;
1366 case ISD::SREM :
1367 if (C2) return getConstant(N1C->getSignExtended() %
1368 N2C->getSignExtended(), VT);
1369 break;
1370 case ISD::AND : return getConstant(C1 & C2, VT);
1371 case ISD::OR : return getConstant(C1 | C2, VT);
1372 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001373 case ISD::SHL : return getConstant(C1 << C2, VT);
1374 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001375 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001376 case ISD::ROTL :
1377 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1378 VT);
1379 case ISD::ROTR :
1380 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1381 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001382 default: break;
1383 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001384 } else { // Cannonicalize constant to RHS if commutative
1385 if (isCommutativeBinOp(Opcode)) {
1386 std::swap(N1C, N2C);
1387 std::swap(N1, N2);
1388 }
1389 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001390 }
1391
1392 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1393 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001394 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001395 if (N2CFP) {
1396 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1397 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001398 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1399 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1400 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1401 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001402 if (C2) return getConstantFP(C1 / C2, VT);
1403 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001404 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001405 if (C2) return getConstantFP(fmod(C1, C2), VT);
1406 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001407 case ISD::FCOPYSIGN: {
1408 union {
1409 double F;
1410 uint64_t I;
1411 } u1;
1412 union {
1413 double F;
1414 int64_t I;
1415 } u2;
1416 u1.F = C1;
1417 u2.F = C2;
1418 if (u2.I < 0) // Sign bit of RHS set?
1419 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1420 else
1421 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1422 return getConstantFP(u1.F, VT);
1423 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001424 default: break;
1425 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001426 } else { // Cannonicalize constant to RHS if commutative
1427 if (isCommutativeBinOp(Opcode)) {
1428 std::swap(N1CFP, N2CFP);
1429 std::swap(N1, N2);
1430 }
1431 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001432 }
Chris Lattner62b57722006-04-20 05:39:12 +00001433
1434 // Canonicalize an UNDEF to the RHS, even over a constant.
1435 if (N1.getOpcode() == ISD::UNDEF) {
1436 if (isCommutativeBinOp(Opcode)) {
1437 std::swap(N1, N2);
1438 } else {
1439 switch (Opcode) {
1440 case ISD::FP_ROUND_INREG:
1441 case ISD::SIGN_EXTEND_INREG:
1442 case ISD::SUB:
1443 case ISD::FSUB:
1444 case ISD::FDIV:
1445 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001446 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001447 return N1; // fold op(undef, arg2) -> undef
1448 case ISD::UDIV:
1449 case ISD::SDIV:
1450 case ISD::UREM:
1451 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001452 case ISD::SRL:
1453 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001454 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1455 }
1456 }
1457 }
1458
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001459 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001460 if (N2.getOpcode() == ISD::UNDEF) {
1461 switch (Opcode) {
1462 case ISD::ADD:
1463 case ISD::SUB:
1464 case ISD::FADD:
1465 case ISD::FSUB:
1466 case ISD::FMUL:
1467 case ISD::FDIV:
1468 case ISD::FREM:
1469 case ISD::UDIV:
1470 case ISD::SDIV:
1471 case ISD::UREM:
1472 case ISD::SREM:
1473 case ISD::XOR:
1474 return N2; // fold op(arg1, undef) -> undef
1475 case ISD::MUL:
1476 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001477 case ISD::SRL:
1478 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001479 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1480 case ISD::OR:
1481 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001482 case ISD::SRA:
1483 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001484 }
1485 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001486
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 // Finally, fold operations that do not require constants.
1488 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001489 case ISD::FP_ROUND_INREG:
1490 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1491 break;
1492 case ISD::SIGN_EXTEND_INREG: {
1493 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1494 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001495 break;
1496 }
1497
Nate Begemaneea805e2005-04-13 21:23:31 +00001498 // FIXME: figure out how to safely handle things like
1499 // int foo(int x) { return 1 << (x & 255); }
1500 // int bar() { return foo(256); }
1501#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001502 case ISD::SHL:
1503 case ISD::SRL:
1504 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001505 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001506 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001507 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001508 else if (N2.getOpcode() == ISD::AND)
1509 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1510 // If the and is only masking out bits that cannot effect the shift,
1511 // eliminate the and.
1512 unsigned NumBits = MVT::getSizeInBits(VT);
1513 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1514 return getNode(Opcode, VT, N1, N2.getOperand(0));
1515 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001516 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001517#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001518 }
1519
Chris Lattner27e9b412005-05-11 18:57:39 +00001520 // Memoize this node if possible.
1521 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001522 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001523 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1524 if (BON) return SDOperand(BON, 0);
1525
1526 BON = N = new SDNode(Opcode, N1, N2);
1527 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001528 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001529 }
1530
Chris Lattner3e011362005-05-14 07:45:46 +00001531 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001532 AllNodes.push_back(N);
1533 return SDOperand(N, 0);
1534}
1535
Chris Lattnerc3aae252005-01-07 07:46:32 +00001536SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1537 SDOperand N1, SDOperand N2, SDOperand N3) {
1538 // Perform various simplifications.
1539 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1540 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001541 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001542 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001543 case ISD::SETCC: {
1544 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001545 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001546 if (Simp.Val) return Simp;
1547 break;
1548 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549 case ISD::SELECT:
1550 if (N1C)
1551 if (N1C->getValue())
1552 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001553 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001554 return N3; // select false, X, Y -> Y
1555
1556 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001557 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001558 case ISD::BRCOND:
1559 if (N2C)
1560 if (N2C->getValue()) // Unconditional branch
1561 return getNode(ISD::BR, MVT::Other, N1, N3);
1562 else
1563 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001564 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001565 case ISD::VECTOR_SHUFFLE:
1566 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1567 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1568 N3.getOpcode() == ISD::BUILD_VECTOR &&
1569 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1570 "Illegal VECTOR_SHUFFLE node!");
1571 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 }
1573
Chris Lattner385328c2005-05-14 07:42:29 +00001574 std::vector<SDOperand> Ops;
1575 Ops.reserve(3);
1576 Ops.push_back(N1);
1577 Ops.push_back(N2);
1578 Ops.push_back(N3);
1579
Chris Lattner43247a12005-08-25 19:12:10 +00001580 // Memoize node if it doesn't produce a flag.
1581 SDNode *N;
1582 if (VT != MVT::Flag) {
1583 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1584 if (E) return SDOperand(E, 0);
1585 E = N = new SDNode(Opcode, N1, N2, N3);
1586 } else {
1587 N = new SDNode(Opcode, N1, N2, N3);
1588 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001589 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001590 AllNodes.push_back(N);
1591 return SDOperand(N, 0);
1592}
1593
1594SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001595 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001596 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001597 std::vector<SDOperand> Ops;
1598 Ops.reserve(4);
1599 Ops.push_back(N1);
1600 Ops.push_back(N2);
1601 Ops.push_back(N3);
1602 Ops.push_back(N4);
1603 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001604}
1605
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001606SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1607 SDOperand N1, SDOperand N2, SDOperand N3,
1608 SDOperand N4, SDOperand N5) {
1609 std::vector<SDOperand> Ops;
1610 Ops.reserve(5);
1611 Ops.push_back(N1);
1612 Ops.push_back(N2);
1613 Ops.push_back(N3);
1614 Ops.push_back(N4);
1615 Ops.push_back(N5);
1616 return getNode(Opcode, VT, Ops);
1617}
1618
Evan Cheng7038daf2005-12-10 00:37:58 +00001619SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1620 SDOperand Chain, SDOperand Ptr,
1621 SDOperand SV) {
1622 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1623 if (N) return SDOperand(N, 0);
1624 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1625
1626 // Loads have a token chain.
1627 setNodeValueTypes(N, VT, MVT::Other);
1628 AllNodes.push_back(N);
1629 return SDOperand(N, 0);
1630}
1631
1632SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1633 SDOperand Chain, SDOperand Ptr,
1634 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001635 std::vector<SDOperand> Ops;
1636 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001637 Ops.push_back(Chain);
1638 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001639 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001640 Ops.push_back(getConstant(Count, MVT::i32));
1641 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001642 std::vector<MVT::ValueType> VTs;
1643 VTs.reserve(2);
1644 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1645 return getNode(ISD::VLOAD, VTs, Ops);
1646}
1647
1648SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1649 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1650 MVT::ValueType EVT) {
1651 std::vector<SDOperand> Ops;
1652 Ops.reserve(4);
1653 Ops.push_back(Chain);
1654 Ops.push_back(Ptr);
1655 Ops.push_back(SV);
1656 Ops.push_back(getValueType(EVT));
1657 std::vector<MVT::ValueType> VTs;
1658 VTs.reserve(2);
1659 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1660 return getNode(Opcode, VTs, Ops);
1661}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001662
Chris Lattner0437cdd2005-05-09 04:14:13 +00001663SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001664 assert((!V || isa<PointerType>(V->getType())) &&
1665 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001666 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1667 if (N) return SDOperand(N, 0);
1668
1669 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001670 AllNodes.push_back(N);
1671 return SDOperand(N, 0);
1672}
1673
Nate Begemanacc398c2006-01-25 18:21:52 +00001674SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1675 SDOperand Chain, SDOperand Ptr,
1676 SDOperand SV) {
1677 std::vector<SDOperand> Ops;
1678 Ops.reserve(3);
1679 Ops.push_back(Chain);
1680 Ops.push_back(Ptr);
1681 Ops.push_back(SV);
1682 std::vector<MVT::ValueType> VTs;
1683 VTs.reserve(2);
1684 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1685 return getNode(ISD::VAARG, VTs, Ops);
1686}
1687
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001688SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001689 std::vector<SDOperand> &Ops) {
1690 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001691 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001692 case 1: return getNode(Opcode, VT, Ops[0]);
1693 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1694 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001695 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001696 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001697
Chris Lattneref847df2005-04-09 03:27:28 +00001698 switch (Opcode) {
1699 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001700 case ISD::TRUNCSTORE: {
1701 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1702 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1703#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1704 // If this is a truncating store of a constant, convert to the desired type
1705 // and store it instead.
1706 if (isa<Constant>(Ops[0])) {
1707 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1708 if (isa<Constant>(Op))
1709 N1 = Op;
1710 }
1711 // Also for ConstantFP?
1712#endif
1713 if (Ops[0].getValueType() == EVT) // Normal store?
1714 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1715 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1716 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1717 "Can't do FP-INT conversion!");
1718 break;
1719 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001720 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001721 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001722 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1723 "LHS and RHS of condition must have same type!");
1724 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1725 "True and False arms of SelectCC must have same type!");
1726 assert(Ops[2].getValueType() == VT &&
1727 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001728 break;
1729 }
1730 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001731 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001732 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1733 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001734 break;
1735 }
Chris Lattneref847df2005-04-09 03:27:28 +00001736 }
1737
Chris Lattner385328c2005-05-14 07:42:29 +00001738 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001739 SDNode *N;
1740 if (VT != MVT::Flag) {
1741 SDNode *&E =
1742 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1743 if (E) return SDOperand(E, 0);
1744 E = N = new SDNode(Opcode, Ops);
1745 } else {
1746 N = new SDNode(Opcode, Ops);
1747 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001748 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001749 AllNodes.push_back(N);
1750 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751}
1752
Chris Lattner89c34632005-05-14 06:20:26 +00001753SDOperand SelectionDAG::getNode(unsigned Opcode,
1754 std::vector<MVT::ValueType> &ResultTys,
1755 std::vector<SDOperand> &Ops) {
1756 if (ResultTys.size() == 1)
1757 return getNode(Opcode, ResultTys[0], Ops);
1758
Chris Lattner5f056bf2005-07-10 01:55:33 +00001759 switch (Opcode) {
1760 case ISD::EXTLOAD:
1761 case ISD::SEXTLOAD:
1762 case ISD::ZEXTLOAD: {
1763 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1764 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1765 // If they are asking for an extending load from/to the same thing, return a
1766 // normal load.
1767 if (ResultTys[0] == EVT)
1768 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001769 if (MVT::isVector(ResultTys[0])) {
1770 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1771 "Invalid vector extload!");
1772 } else {
1773 assert(EVT < ResultTys[0] &&
1774 "Should only be an extending load, not truncating!");
1775 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001776 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001777 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001778 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1779 "Cannot convert from FP to Int or Int -> FP!");
1780 break;
1781 }
1782
Chris Lattnere89083a2005-05-14 07:25:05 +00001783 // FIXME: figure out how to safely handle things like
1784 // int foo(int x) { return 1 << (x & 255); }
1785 // int bar() { return foo(256); }
1786#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001787 case ISD::SRA_PARTS:
1788 case ISD::SRL_PARTS:
1789 case ISD::SHL_PARTS:
1790 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001791 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001792 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1793 else if (N3.getOpcode() == ISD::AND)
1794 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1795 // If the and is only masking out bits that cannot effect the shift,
1796 // eliminate the and.
1797 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1798 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1799 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1800 }
1801 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001802#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001803 }
Chris Lattner89c34632005-05-14 06:20:26 +00001804
Chris Lattner43247a12005-08-25 19:12:10 +00001805 // Memoize the node unless it returns a flag.
1806 SDNode *N;
1807 if (ResultTys.back() != MVT::Flag) {
1808 SDNode *&E =
1809 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1810 if (E) return SDOperand(E, 0);
1811 E = N = new SDNode(Opcode, Ops);
1812 } else {
1813 N = new SDNode(Opcode, Ops);
1814 }
Chris Lattnera3255112005-11-08 23:30:28 +00001815 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001816 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001817 return SDOperand(N, 0);
1818}
1819
Chris Lattnera3255112005-11-08 23:30:28 +00001820void SelectionDAG::setNodeValueTypes(SDNode *N,
1821 std::vector<MVT::ValueType> &RetVals) {
1822 switch (RetVals.size()) {
1823 case 0: return;
1824 case 1: N->setValueTypes(RetVals[0]); return;
1825 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1826 default: break;
1827 }
1828
1829 std::list<std::vector<MVT::ValueType> >::iterator I =
1830 std::find(VTList.begin(), VTList.end(), RetVals);
1831 if (I == VTList.end()) {
1832 VTList.push_front(RetVals);
1833 I = VTList.begin();
1834 }
1835
1836 N->setValueTypes(&(*I)[0], I->size());
1837}
1838
1839void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1840 MVT::ValueType VT2) {
1841 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1842 E = VTList.end(); I != E; ++I) {
1843 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1844 N->setValueTypes(&(*I)[0], 2);
1845 return;
1846 }
1847 }
1848 std::vector<MVT::ValueType> V;
1849 V.push_back(VT1);
1850 V.push_back(VT2);
1851 VTList.push_front(V);
1852 N->setValueTypes(&(*VTList.begin())[0], 2);
1853}
1854
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001855/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1856/// specified operands. If the resultant node already exists in the DAG,
1857/// this does not modify the specified node, instead it returns the node that
1858/// already exists. If the resultant node does not exist in the DAG, the
1859/// input node is returned. As a degenerate case, if you specify the same
1860/// input operands as the node already has, the input node is returned.
1861SDOperand SelectionDAG::
1862UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1863 SDNode *N = InN.Val;
1864 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1865
1866 // Check to see if there is no change.
1867 if (Op == N->getOperand(0)) return InN;
1868
1869 // See if the modified node already exists.
1870 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1871 if (NewSlot && *NewSlot)
1872 return SDOperand(*NewSlot, InN.ResNo);
1873
1874 // Nope it doesn't. Remove the node from it's current place in the maps.
1875 if (NewSlot)
1876 RemoveNodeFromCSEMaps(N);
1877
1878 // Now we update the operands.
1879 N->OperandList[0].Val->removeUser(N);
1880 Op.Val->addUser(N);
1881 N->OperandList[0] = Op;
1882
1883 // If this gets put into a CSE map, add it.
1884 if (NewSlot) *NewSlot = N;
1885 return InN;
1886}
1887
1888SDOperand SelectionDAG::
1889UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1890 SDNode *N = InN.Val;
1891 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1892
1893 // Check to see if there is no change.
1894 bool AnyChange = false;
1895 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1896 return InN; // No operands changed, just return the input node.
1897
1898 // See if the modified node already exists.
1899 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1900 if (NewSlot && *NewSlot)
1901 return SDOperand(*NewSlot, InN.ResNo);
1902
1903 // Nope it doesn't. Remove the node from it's current place in the maps.
1904 if (NewSlot)
1905 RemoveNodeFromCSEMaps(N);
1906
1907 // Now we update the operands.
1908 if (N->OperandList[0] != Op1) {
1909 N->OperandList[0].Val->removeUser(N);
1910 Op1.Val->addUser(N);
1911 N->OperandList[0] = Op1;
1912 }
1913 if (N->OperandList[1] != Op2) {
1914 N->OperandList[1].Val->removeUser(N);
1915 Op2.Val->addUser(N);
1916 N->OperandList[1] = Op2;
1917 }
1918
1919 // If this gets put into a CSE map, add it.
1920 if (NewSlot) *NewSlot = N;
1921 return InN;
1922}
1923
1924SDOperand SelectionDAG::
1925UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1926 std::vector<SDOperand> Ops;
1927 Ops.push_back(Op1);
1928 Ops.push_back(Op2);
1929 Ops.push_back(Op3);
1930 return UpdateNodeOperands(N, Ops);
1931}
1932
1933SDOperand SelectionDAG::
1934UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1935 SDOperand Op3, SDOperand Op4) {
1936 std::vector<SDOperand> Ops;
1937 Ops.push_back(Op1);
1938 Ops.push_back(Op2);
1939 Ops.push_back(Op3);
1940 Ops.push_back(Op4);
1941 return UpdateNodeOperands(N, Ops);
1942}
1943
1944SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001945UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1946 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1947 std::vector<SDOperand> Ops;
1948 Ops.push_back(Op1);
1949 Ops.push_back(Op2);
1950 Ops.push_back(Op3);
1951 Ops.push_back(Op4);
1952 Ops.push_back(Op5);
1953 return UpdateNodeOperands(N, Ops);
1954}
1955
1956
1957SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001958UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1959 SDNode *N = InN.Val;
1960 assert(N->getNumOperands() == Ops.size() &&
1961 "Update with wrong number of operands");
1962
1963 // Check to see if there is no change.
1964 unsigned NumOps = Ops.size();
1965 bool AnyChange = false;
1966 for (unsigned i = 0; i != NumOps; ++i) {
1967 if (Ops[i] != N->getOperand(i)) {
1968 AnyChange = true;
1969 break;
1970 }
1971 }
1972
1973 // No operands changed, just return the input node.
1974 if (!AnyChange) return InN;
1975
1976 // See if the modified node already exists.
1977 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1978 if (NewSlot && *NewSlot)
1979 return SDOperand(*NewSlot, InN.ResNo);
1980
1981 // Nope it doesn't. Remove the node from it's current place in the maps.
1982 if (NewSlot)
1983 RemoveNodeFromCSEMaps(N);
1984
1985 // Now we update the operands.
1986 for (unsigned i = 0; i != NumOps; ++i) {
1987 if (N->OperandList[i] != Ops[i]) {
1988 N->OperandList[i].Val->removeUser(N);
1989 Ops[i].Val->addUser(N);
1990 N->OperandList[i] = Ops[i];
1991 }
1992 }
1993
1994 // If this gets put into a CSE map, add it.
1995 if (NewSlot) *NewSlot = N;
1996 return InN;
1997}
1998
1999
2000
Chris Lattner149c58c2005-08-16 18:17:10 +00002001
2002/// SelectNodeTo - These are used for target selectors to *mutate* the
2003/// specified node to have the specified return type, Target opcode, and
2004/// operands. Note that target opcodes are stored as
2005/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002006///
2007/// Note that SelectNodeTo returns the resultant node. If there is already a
2008/// node of the specified opcode and operands, it returns that node instead of
2009/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00002010SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2011 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002012 // If an identical node already exists, use it.
2013 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
2014 if (ON) return SDOperand(ON, 0);
2015
Chris Lattner7651fa42005-08-24 23:00:29 +00002016 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002017
Chris Lattner7651fa42005-08-24 23:00:29 +00002018 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2019 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002020
2021 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002022 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00002023}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002024
Chris Lattnereb19e402005-11-30 22:45:14 +00002025SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2026 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002027 // If an identical node already exists, use it.
2028 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2029 std::make_pair(Op1, VT))];
2030 if (ON) return SDOperand(ON, 0);
2031
Chris Lattner149c58c2005-08-16 18:17:10 +00002032 RemoveNodeFromCSEMaps(N);
2033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2034 N->setValueTypes(VT);
2035 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002036
2037 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002038 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002039}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002040
Chris Lattnereb19e402005-11-30 22:45:14 +00002041SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2042 MVT::ValueType VT, SDOperand Op1,
2043 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002044 // If an identical node already exists, use it.
2045 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2046 std::make_pair(Op1, Op2))];
2047 if (ON) return SDOperand(ON, 0);
2048
Chris Lattner149c58c2005-08-16 18:17:10 +00002049 RemoveNodeFromCSEMaps(N);
2050 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2051 N->setValueTypes(VT);
2052 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002053
2054 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002055 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002056}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002057
Chris Lattnereb19e402005-11-30 22:45:14 +00002058SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2059 MVT::ValueType VT, SDOperand Op1,
2060 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002061 // If an identical node already exists, use it.
2062 std::vector<SDOperand> OpList;
2063 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2064 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2065 std::make_pair(VT, OpList))];
2066 if (ON) return SDOperand(ON, 0);
2067
Chris Lattner149c58c2005-08-16 18:17:10 +00002068 RemoveNodeFromCSEMaps(N);
2069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2070 N->setValueTypes(VT);
2071 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002072
2073 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002074 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002075}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002076
Chris Lattnereb19e402005-11-30 22:45:14 +00002077SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2078 MVT::ValueType VT, SDOperand Op1,
2079 SDOperand Op2, SDOperand Op3,
2080 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002081 // If an identical node already exists, use it.
2082 std::vector<SDOperand> OpList;
2083 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2084 OpList.push_back(Op4);
2085 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2086 std::make_pair(VT, OpList))];
2087 if (ON) return SDOperand(ON, 0);
2088
Nate Begeman294a0a12005-08-18 07:30:15 +00002089 RemoveNodeFromCSEMaps(N);
2090 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2091 N->setValueTypes(VT);
2092 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002093
2094 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002095 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002096}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002097
Chris Lattnereb19e402005-11-30 22:45:14 +00002098SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2099 MVT::ValueType VT, SDOperand Op1,
2100 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2101 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002102 // If an identical node already exists, use it.
2103 std::vector<SDOperand> OpList;
2104 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2105 OpList.push_back(Op4); OpList.push_back(Op5);
2106 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2107 std::make_pair(VT, OpList))];
2108 if (ON) return SDOperand(ON, 0);
2109
Chris Lattner6b09a292005-08-21 18:49:33 +00002110 RemoveNodeFromCSEMaps(N);
2111 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2112 N->setValueTypes(VT);
2113 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002114
2115 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002116 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002117}
Chris Lattner149c58c2005-08-16 18:17:10 +00002118
Chris Lattnereb19e402005-11-30 22:45:14 +00002119SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2120 MVT::ValueType VT, SDOperand Op1,
2121 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2122 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002123 // If an identical node already exists, use it.
2124 std::vector<SDOperand> OpList;
2125 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2126 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2127 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2128 std::make_pair(VT, OpList))];
2129 if (ON) return SDOperand(ON, 0);
2130
Evan Cheng61ca74b2005-11-30 02:04:11 +00002131 RemoveNodeFromCSEMaps(N);
2132 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2133 N->setValueTypes(VT);
2134 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002135
2136 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002137 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002138}
2139
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002140SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2141 MVT::ValueType VT, SDOperand Op1,
2142 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2143 SDOperand Op5, SDOperand Op6,
2144 SDOperand Op7) {
2145 // If an identical node already exists, use it.
2146 std::vector<SDOperand> OpList;
2147 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2148 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2149 OpList.push_back(Op7);
2150 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2151 std::make_pair(VT, OpList))];
2152 if (ON) return SDOperand(ON, 0);
2153
2154 RemoveNodeFromCSEMaps(N);
2155 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2156 N->setValueTypes(VT);
2157 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2158
2159 ON = N; // Memoize the new node.
2160 return SDOperand(N, 0);
2161}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002162SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2163 MVT::ValueType VT, SDOperand Op1,
2164 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2165 SDOperand Op5, SDOperand Op6,
2166 SDOperand Op7, SDOperand Op8) {
2167 // If an identical node already exists, use it.
2168 std::vector<SDOperand> OpList;
2169 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2170 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2171 OpList.push_back(Op7); OpList.push_back(Op8);
2172 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2173 std::make_pair(VT, OpList))];
2174 if (ON) return SDOperand(ON, 0);
2175
2176 RemoveNodeFromCSEMaps(N);
2177 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2178 N->setValueTypes(VT);
2179 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2180
2181 ON = N; // Memoize the new node.
2182 return SDOperand(N, 0);
2183}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002184
Chris Lattnereb19e402005-11-30 22:45:14 +00002185SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2186 MVT::ValueType VT1, MVT::ValueType VT2,
2187 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002188 // If an identical node already exists, use it.
2189 std::vector<SDOperand> OpList;
2190 OpList.push_back(Op1); OpList.push_back(Op2);
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);
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
Chris Lattnereb19e402005-11-30 22:45:14 +00002206SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2207 MVT::ValueType VT1, MVT::ValueType VT2,
2208 SDOperand Op1, SDOperand Op2,
2209 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002210 // If an identical node already exists, use it.
2211 std::vector<SDOperand> OpList;
2212 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2213 std::vector<MVT::ValueType> VTList;
2214 VTList.push_back(VT1); VTList.push_back(VT2);
2215 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2216 std::make_pair(VTList, OpList))];
2217 if (ON) return SDOperand(ON, 0);
2218
Chris Lattner0fb094f2005-11-19 01:44:53 +00002219 RemoveNodeFromCSEMaps(N);
2220 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2221 setNodeValueTypes(N, VT1, VT2);
2222 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002223
2224 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002225 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002226}
2227
Chris Lattnereb19e402005-11-30 22:45:14 +00002228SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2229 MVT::ValueType VT1, MVT::ValueType VT2,
2230 SDOperand Op1, SDOperand Op2,
2231 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002232 // If an identical node already exists, use it.
2233 std::vector<SDOperand> OpList;
2234 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2235 OpList.push_back(Op4);
2236 std::vector<MVT::ValueType> VTList;
2237 VTList.push_back(VT1); VTList.push_back(VT2);
2238 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2239 std::make_pair(VTList, OpList))];
2240 if (ON) return SDOperand(ON, 0);
2241
Chris Lattner0fb094f2005-11-19 01:44:53 +00002242 RemoveNodeFromCSEMaps(N);
2243 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2244 setNodeValueTypes(N, VT1, VT2);
2245 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002246
2247 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002248 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002249}
2250
Chris Lattnereb19e402005-11-30 22:45:14 +00002251SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2252 MVT::ValueType VT1, MVT::ValueType VT2,
2253 SDOperand Op1, SDOperand Op2,
2254 SDOperand Op3, SDOperand Op4,
2255 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002256 // If an identical node already exists, use it.
2257 std::vector<SDOperand> OpList;
2258 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2259 OpList.push_back(Op4); OpList.push_back(Op5);
2260 std::vector<MVT::ValueType> VTList;
2261 VTList.push_back(VT1); VTList.push_back(VT2);
2262 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2263 std::make_pair(VTList, OpList))];
2264 if (ON) return SDOperand(ON, 0);
2265
Chris Lattner0fb094f2005-11-19 01:44:53 +00002266 RemoveNodeFromCSEMaps(N);
2267 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2268 setNodeValueTypes(N, VT1, VT2);
2269 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002270
2271 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002272 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002273}
2274
Evan Cheng6ae46c42006-02-09 07:15:23 +00002275/// getTargetNode - These are used for target selectors to create a new node
2276/// with specified return type(s), target opcode, and operands.
2277///
2278/// Note that getTargetNode returns the resultant node. If there is already a
2279/// node of the specified opcode and operands, it returns that node instead of
2280/// the current one.
2281SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2282 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2283}
2284SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2285 SDOperand Op1) {
2286 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2287}
2288SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2289 SDOperand Op1, SDOperand Op2) {
2290 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2291}
2292SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2293 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2294 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2295}
2296SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2297 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2298 SDOperand Op4) {
2299 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2300}
2301SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2302 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2303 SDOperand Op4, SDOperand Op5) {
2304 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2305}
2306SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2307 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2308 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2309 std::vector<SDOperand> Ops;
2310 Ops.reserve(6);
2311 Ops.push_back(Op1);
2312 Ops.push_back(Op2);
2313 Ops.push_back(Op3);
2314 Ops.push_back(Op4);
2315 Ops.push_back(Op5);
2316 Ops.push_back(Op6);
2317 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2318}
2319SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2320 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2321 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2322 SDOperand Op7) {
2323 std::vector<SDOperand> Ops;
2324 Ops.reserve(7);
2325 Ops.push_back(Op1);
2326 Ops.push_back(Op2);
2327 Ops.push_back(Op3);
2328 Ops.push_back(Op4);
2329 Ops.push_back(Op5);
2330 Ops.push_back(Op6);
2331 Ops.push_back(Op7);
2332 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2333}
2334SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2335 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2336 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2337 SDOperand Op7, SDOperand Op8) {
2338 std::vector<SDOperand> Ops;
2339 Ops.reserve(8);
2340 Ops.push_back(Op1);
2341 Ops.push_back(Op2);
2342 Ops.push_back(Op3);
2343 Ops.push_back(Op4);
2344 Ops.push_back(Op5);
2345 Ops.push_back(Op6);
2346 Ops.push_back(Op7);
2347 Ops.push_back(Op8);
2348 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2349}
2350SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2351 std::vector<SDOperand> &Ops) {
2352 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2353}
2354SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2355 MVT::ValueType VT2, SDOperand Op1) {
2356 std::vector<MVT::ValueType> ResultTys;
2357 ResultTys.push_back(VT1);
2358 ResultTys.push_back(VT2);
2359 std::vector<SDOperand> Ops;
2360 Ops.push_back(Op1);
2361 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2362}
2363SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2364 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2365 std::vector<MVT::ValueType> ResultTys;
2366 ResultTys.push_back(VT1);
2367 ResultTys.push_back(VT2);
2368 std::vector<SDOperand> Ops;
2369 Ops.push_back(Op1);
2370 Ops.push_back(Op2);
2371 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2372}
2373SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2374 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2375 SDOperand Op3) {
2376 std::vector<MVT::ValueType> ResultTys;
2377 ResultTys.push_back(VT1);
2378 ResultTys.push_back(VT2);
2379 std::vector<SDOperand> Ops;
2380 Ops.push_back(Op1);
2381 Ops.push_back(Op2);
2382 Ops.push_back(Op3);
2383 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2384}
2385SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2386 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2387 SDOperand Op3, SDOperand Op4) {
2388 std::vector<MVT::ValueType> ResultTys;
2389 ResultTys.push_back(VT1);
2390 ResultTys.push_back(VT2);
2391 std::vector<SDOperand> Ops;
2392 Ops.push_back(Op1);
2393 Ops.push_back(Op2);
2394 Ops.push_back(Op3);
2395 Ops.push_back(Op4);
2396 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2397}
2398SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2399 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2400 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2401 std::vector<MVT::ValueType> ResultTys;
2402 ResultTys.push_back(VT1);
2403 ResultTys.push_back(VT2);
2404 std::vector<SDOperand> Ops;
2405 Ops.push_back(Op1);
2406 Ops.push_back(Op2);
2407 Ops.push_back(Op3);
2408 Ops.push_back(Op4);
2409 Ops.push_back(Op5);
2410 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2411}
2412SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2413 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2414 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2415 SDOperand Op6) {
2416 std::vector<MVT::ValueType> ResultTys;
2417 ResultTys.push_back(VT1);
2418 ResultTys.push_back(VT2);
2419 std::vector<SDOperand> Ops;
2420 Ops.push_back(Op1);
2421 Ops.push_back(Op2);
2422 Ops.push_back(Op3);
2423 Ops.push_back(Op4);
2424 Ops.push_back(Op5);
2425 Ops.push_back(Op6);
2426 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2427}
2428SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2429 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2430 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2431 SDOperand Op6, SDOperand Op7) {
2432 std::vector<MVT::ValueType> ResultTys;
2433 ResultTys.push_back(VT1);
2434 ResultTys.push_back(VT2);
2435 std::vector<SDOperand> Ops;
2436 Ops.push_back(Op1);
2437 Ops.push_back(Op2);
2438 Ops.push_back(Op3);
2439 Ops.push_back(Op4);
2440 Ops.push_back(Op5);
2441 Ops.push_back(Op6);
2442 Ops.push_back(Op7);
2443 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2444}
2445SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2446 MVT::ValueType VT2, MVT::ValueType VT3,
2447 SDOperand Op1, SDOperand Op2) {
2448 std::vector<MVT::ValueType> ResultTys;
2449 ResultTys.push_back(VT1);
2450 ResultTys.push_back(VT2);
2451 ResultTys.push_back(VT3);
2452 std::vector<SDOperand> Ops;
2453 Ops.push_back(Op1);
2454 Ops.push_back(Op2);
2455 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2456}
2457SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2458 MVT::ValueType VT2, MVT::ValueType VT3,
2459 SDOperand Op1, SDOperand Op2,
2460 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2461 std::vector<MVT::ValueType> ResultTys;
2462 ResultTys.push_back(VT1);
2463 ResultTys.push_back(VT2);
2464 ResultTys.push_back(VT3);
2465 std::vector<SDOperand> Ops;
2466 Ops.push_back(Op1);
2467 Ops.push_back(Op2);
2468 Ops.push_back(Op3);
2469 Ops.push_back(Op4);
2470 Ops.push_back(Op5);
2471 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2472}
2473SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2474 MVT::ValueType VT2, MVT::ValueType VT3,
2475 SDOperand Op1, SDOperand Op2,
2476 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2477 SDOperand Op6) {
2478 std::vector<MVT::ValueType> ResultTys;
2479 ResultTys.push_back(VT1);
2480 ResultTys.push_back(VT2);
2481 ResultTys.push_back(VT3);
2482 std::vector<SDOperand> Ops;
2483 Ops.push_back(Op1);
2484 Ops.push_back(Op2);
2485 Ops.push_back(Op3);
2486 Ops.push_back(Op4);
2487 Ops.push_back(Op5);
2488 Ops.push_back(Op6);
2489 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2490}
2491SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2492 MVT::ValueType VT2, MVT::ValueType VT3,
2493 SDOperand Op1, SDOperand Op2,
2494 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2495 SDOperand Op6, SDOperand Op7) {
2496 std::vector<MVT::ValueType> ResultTys;
2497 ResultTys.push_back(VT1);
2498 ResultTys.push_back(VT2);
2499 ResultTys.push_back(VT3);
2500 std::vector<SDOperand> Ops;
2501 Ops.push_back(Op1);
2502 Ops.push_back(Op2);
2503 Ops.push_back(Op3);
2504 Ops.push_back(Op4);
2505 Ops.push_back(Op5);
2506 Ops.push_back(Op6);
2507 Ops.push_back(Op7);
2508 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2509}
2510SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2511 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2512 std::vector<MVT::ValueType> ResultTys;
2513 ResultTys.push_back(VT1);
2514 ResultTys.push_back(VT2);
2515 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2516}
2517
Chris Lattner0fb094f2005-11-19 01:44:53 +00002518// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002519/// This can cause recursive merging of nodes in the DAG.
2520///
Chris Lattner8b52f212005-08-26 18:36:28 +00002521/// This version assumes From/To have a single result value.
2522///
Chris Lattner1e111c72005-09-07 05:37:01 +00002523void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2524 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002525 SDNode *From = FromN.Val, *To = ToN.Val;
2526 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2527 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002528 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002529
Chris Lattner8b8749f2005-08-17 19:00:20 +00002530 while (!From->use_empty()) {
2531 // Process users until they are all gone.
2532 SDNode *U = *From->use_begin();
2533
2534 // This node is about to morph, remove its old self from the CSE maps.
2535 RemoveNodeFromCSEMaps(U);
2536
Chris Lattner65113b22005-11-08 22:07:03 +00002537 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2538 I != E; ++I)
2539 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002540 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002541 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002542 To->addUser(U);
2543 }
2544
2545 // Now that we have modified U, add it back to the CSE maps. If it already
2546 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002547 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2548 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002549 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002550 if (Deleted) Deleted->push_back(U);
2551 DeleteNodeNotInCSEMaps(U);
2552 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002553 }
2554}
2555
2556/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2557/// This can cause recursive merging of nodes in the DAG.
2558///
2559/// This version assumes From/To have matching types and numbers of result
2560/// values.
2561///
Chris Lattner1e111c72005-09-07 05:37:01 +00002562void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2563 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002564 assert(From != To && "Cannot replace uses of with self");
2565 assert(From->getNumValues() == To->getNumValues() &&
2566 "Cannot use this version of ReplaceAllUsesWith!");
2567 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002568 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002569 return;
2570 }
2571
2572 while (!From->use_empty()) {
2573 // Process users until they are all gone.
2574 SDNode *U = *From->use_begin();
2575
2576 // This node is about to morph, remove its old self from the CSE maps.
2577 RemoveNodeFromCSEMaps(U);
2578
Chris Lattner65113b22005-11-08 22:07:03 +00002579 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2580 I != E; ++I)
2581 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002582 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002583 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002584 To->addUser(U);
2585 }
2586
2587 // Now that we have modified U, add it back to the CSE maps. If it already
2588 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002589 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2590 ReplaceAllUsesWith(U, Existing, Deleted);
2591 // U is now dead.
2592 if (Deleted) Deleted->push_back(U);
2593 DeleteNodeNotInCSEMaps(U);
2594 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002595 }
2596}
2597
Chris Lattner8b52f212005-08-26 18:36:28 +00002598/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2599/// This can cause recursive merging of nodes in the DAG.
2600///
2601/// This version can replace From with any result values. To must match the
2602/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002603void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002604 const std::vector<SDOperand> &To,
2605 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002606 assert(From->getNumValues() == To.size() &&
2607 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002608 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002609 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002610 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002611 return;
2612 }
2613
2614 while (!From->use_empty()) {
2615 // Process users until they are all gone.
2616 SDNode *U = *From->use_begin();
2617
2618 // This node is about to morph, remove its old self from the CSE maps.
2619 RemoveNodeFromCSEMaps(U);
2620
Chris Lattner65113b22005-11-08 22:07:03 +00002621 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2622 I != E; ++I)
2623 if (I->Val == From) {
2624 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002625 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002626 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002627 ToOp.Val->addUser(U);
2628 }
2629
2630 // Now that we have modified U, add it back to the CSE maps. If it already
2631 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002632 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2633 ReplaceAllUsesWith(U, Existing, Deleted);
2634 // U is now dead.
2635 if (Deleted) Deleted->push_back(U);
2636 DeleteNodeNotInCSEMaps(U);
2637 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002638 }
2639}
2640
Chris Lattner012f2412006-02-17 21:58:01 +00002641/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2642/// uses of other values produced by From.Val alone. The Deleted vector is
2643/// handled the same was as for ReplaceAllUsesWith.
2644void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2645 std::vector<SDNode*> &Deleted) {
2646 assert(From != To && "Cannot replace a value with itself");
2647 // Handle the simple, trivial, case efficiently.
2648 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2649 ReplaceAllUsesWith(From, To, &Deleted);
2650 return;
2651 }
2652
2653 // Get all of the users in a nice, deterministically ordered, uniqued set.
2654 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2655
2656 while (!Users.empty()) {
2657 // We know that this user uses some value of From. If it is the right
2658 // value, update it.
2659 SDNode *User = Users.back();
2660 Users.pop_back();
2661
2662 for (SDOperand *Op = User->OperandList,
2663 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2664 if (*Op == From) {
2665 // Okay, we know this user needs to be updated. Remove its old self
2666 // from the CSE maps.
2667 RemoveNodeFromCSEMaps(User);
2668
2669 // Update all operands that match "From".
2670 for (; Op != E; ++Op) {
2671 if (*Op == From) {
2672 From.Val->removeUser(User);
2673 *Op = To;
2674 To.Val->addUser(User);
2675 }
2676 }
2677
2678 // Now that we have modified User, add it back to the CSE maps. If it
2679 // already exists there, recursively merge the results together.
2680 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2681 unsigned NumDeleted = Deleted.size();
2682 ReplaceAllUsesWith(User, Existing, &Deleted);
2683
2684 // User is now dead.
2685 Deleted.push_back(User);
2686 DeleteNodeNotInCSEMaps(User);
2687
2688 // We have to be careful here, because ReplaceAllUsesWith could have
2689 // deleted a user of From, which means there may be dangling pointers
2690 // in the "Users" setvector. Scan over the deleted node pointers and
2691 // remove them from the setvector.
2692 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2693 Users.remove(Deleted[i]);
2694 }
2695 break; // Exit the operand scanning loop.
2696 }
2697 }
2698 }
2699}
2700
Chris Lattner7b2880c2005-08-24 22:44:39 +00002701
Evan Chenge6f35d82006-08-01 08:20:41 +00002702/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2703/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002704unsigned SelectionDAG::AssignNodeIds() {
2705 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002706 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2707 SDNode *N = I;
2708 N->setNodeId(Id++);
2709 }
2710 return Id;
2711}
2712
Evan Chenge6f35d82006-08-01 08:20:41 +00002713/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2714/// based on their topological order. It returns a vector of the SDNodes* in
2715/// assigned order.
2716std::vector<SDNode*> SelectionDAG::AssignTopologicalOrder() {
2717 unsigned DAGSize = AllNodes.size();
2718 std::vector<SDNode*> TopOrder;
2719 std::map<SDNode*, unsigned> InDegree;
2720 std::deque<SDNode*> Sources;
2721 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2722 SDNode *N = I;
2723 unsigned Degree = N->use_size();
2724 InDegree[N] = Degree;
2725 if (Degree == 0)
2726 Sources.push_back(I);
2727 }
2728
2729 int Id = 0;
2730 while (!Sources.empty()) {
2731 SDNode *N = Sources.front();
2732 Sources.pop_front();
2733 TopOrder.push_back(N);
2734 N->setNodeId(Id++);
2735 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2736 SDNode *P = I->Val;
2737 unsigned Degree = InDegree[P] - 1;
2738 if (Degree == 0)
2739 Sources.push_back(P);
2740 InDegree[P] = Degree;
2741 }
2742 }
2743
2744 return TopOrder;
2745}
2746
2747
Evan Cheng091cba12006-07-27 06:39:06 +00002748
Jim Laskey58b968b2005-08-17 20:08:02 +00002749//===----------------------------------------------------------------------===//
2750// SDNode Class
2751//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002752
Chris Lattner917d2c92006-07-19 00:00:37 +00002753// Out-of-line virtual method to give class a home.
2754void SDNode::ANCHOR() {
2755}
2756
Chris Lattnera3255112005-11-08 23:30:28 +00002757
2758/// getValueTypeList - Return a pointer to the specified value type.
2759///
2760MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2761 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2762 VTs[VT] = VT;
2763 return &VTs[VT];
2764}
2765
Chris Lattner5c884562005-01-12 18:37:47 +00002766/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2767/// indicated value. This method ignores uses of other values defined by this
2768/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002769bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002770 assert(Value < getNumValues() && "Bad value!");
2771
2772 // If there is only one value, this is easy.
2773 if (getNumValues() == 1)
2774 return use_size() == NUses;
2775 if (Uses.size() < NUses) return false;
2776
Evan Cheng4ee62112006-02-05 06:29:23 +00002777 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002778
2779 std::set<SDNode*> UsersHandled;
2780
Evan Cheng4ee62112006-02-05 06:29:23 +00002781 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002782 UI != E; ++UI) {
2783 SDNode *User = *UI;
2784 if (User->getNumOperands() == 1 ||
2785 UsersHandled.insert(User).second) // First time we've seen this?
2786 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2787 if (User->getOperand(i) == TheValue) {
2788 if (NUses == 0)
2789 return false; // too many uses
2790 --NUses;
2791 }
2792 }
2793
2794 // Found exactly the right number of uses?
2795 return NUses == 0;
2796}
2797
2798
Evan Cheng4ee62112006-02-05 06:29:23 +00002799// isOnlyUse - Return true if this node is the only use of N.
2800bool SDNode::isOnlyUse(SDNode *N) const {
2801 bool Seen = false;
2802 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2803 SDNode *User = *I;
2804 if (User == this)
2805 Seen = true;
2806 else
2807 return false;
2808 }
2809
2810 return Seen;
2811}
2812
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002813// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002814bool SDOperand::isOperand(SDNode *N) const {
2815 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2816 if (*this == N->getOperand(i))
2817 return true;
2818 return false;
2819}
2820
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002821bool SDNode::isOperand(SDNode *N) const {
2822 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2823 if (this == N->OperandList[i].Val)
2824 return true;
2825 return false;
2826}
Evan Cheng4ee62112006-02-05 06:29:23 +00002827
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002828const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002829 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002830 default:
2831 if (getOpcode() < ISD::BUILTIN_OP_END)
2832 return "<<Unknown DAG Node>>";
2833 else {
Evan Cheng72261582005-12-20 06:22:03 +00002834 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002835 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002836 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2837 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002838
Evan Cheng72261582005-12-20 06:22:03 +00002839 TargetLowering &TLI = G->getTargetLoweringInfo();
2840 const char *Name =
2841 TLI.getTargetNodeName(getOpcode());
2842 if (Name) return Name;
2843 }
2844
2845 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002846 }
2847
Andrew Lenharth95762122005-03-31 21:24:06 +00002848 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002849 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002850 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002851 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002852 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002853 case ISD::AssertSext: return "AssertSext";
2854 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002855
2856 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002857 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002858 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002859 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002860
2861 case ISD::Constant: return "Constant";
2862 case ISD::ConstantFP: return "ConstantFP";
2863 case ISD::GlobalAddress: return "GlobalAddress";
2864 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002865 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002866 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002867 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002868 case ISD::INTRINSIC_WO_CHAIN: {
2869 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2870 return Intrinsic::getName((Intrinsic::ID)IID);
2871 }
2872 case ISD::INTRINSIC_VOID:
2873 case ISD::INTRINSIC_W_CHAIN: {
2874 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002875 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002876 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002877
Chris Lattnerb2827b02006-03-19 00:52:58 +00002878 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002879 case ISD::TargetConstant: return "TargetConstant";
2880 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002881 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2882 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002883 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002884 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002885 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002886
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002887 case ISD::CopyToReg: return "CopyToReg";
2888 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002889 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002890 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002891 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002892 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002893 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002894 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002895
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002896 // Unary operators
2897 case ISD::FABS: return "fabs";
2898 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002899 case ISD::FSQRT: return "fsqrt";
2900 case ISD::FSIN: return "fsin";
2901 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002902
2903 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002904 case ISD::ADD: return "add";
2905 case ISD::SUB: return "sub";
2906 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002907 case ISD::MULHU: return "mulhu";
2908 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002909 case ISD::SDIV: return "sdiv";
2910 case ISD::UDIV: return "udiv";
2911 case ISD::SREM: return "srem";
2912 case ISD::UREM: return "urem";
2913 case ISD::AND: return "and";
2914 case ISD::OR: return "or";
2915 case ISD::XOR: return "xor";
2916 case ISD::SHL: return "shl";
2917 case ISD::SRA: return "sra";
2918 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002919 case ISD::ROTL: return "rotl";
2920 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002921 case ISD::FADD: return "fadd";
2922 case ISD::FSUB: return "fsub";
2923 case ISD::FMUL: return "fmul";
2924 case ISD::FDIV: return "fdiv";
2925 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002926 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002927 case ISD::VADD: return "vadd";
2928 case ISD::VSUB: return "vsub";
2929 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002930 case ISD::VSDIV: return "vsdiv";
2931 case ISD::VUDIV: return "vudiv";
2932 case ISD::VAND: return "vand";
2933 case ISD::VOR: return "vor";
2934 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002935
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002936 case ISD::SETCC: return "setcc";
2937 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002938 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002939 case ISD::VSELECT: return "vselect";
2940 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2941 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2942 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002943 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002944 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2945 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2946 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2947 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2948 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002949 case ISD::ADDC: return "addc";
2950 case ISD::ADDE: return "adde";
2951 case ISD::SUBC: return "subc";
2952 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002953 case ISD::SHL_PARTS: return "shl_parts";
2954 case ISD::SRA_PARTS: return "sra_parts";
2955 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002956
Chris Lattner7f644642005-04-28 21:44:03 +00002957 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002958 case ISD::SIGN_EXTEND: return "sign_extend";
2959 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002960 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002961 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002962 case ISD::TRUNCATE: return "truncate";
2963 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002964 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002965 case ISD::FP_EXTEND: return "fp_extend";
2966
2967 case ISD::SINT_TO_FP: return "sint_to_fp";
2968 case ISD::UINT_TO_FP: return "uint_to_fp";
2969 case ISD::FP_TO_SINT: return "fp_to_sint";
2970 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002971 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002972
2973 // Control flow instructions
2974 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002975 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002976 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002977 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002978 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002979 case ISD::CALLSEQ_START: return "callseq_start";
2980 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002981
2982 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002983 case ISD::LOAD: return "load";
2984 case ISD::STORE: return "store";
2985 case ISD::VLOAD: return "vload";
2986 case ISD::EXTLOAD: return "extload";
2987 case ISD::SEXTLOAD: return "sextload";
2988 case ISD::ZEXTLOAD: return "zextload";
2989 case ISD::TRUNCSTORE: return "truncstore";
2990 case ISD::VAARG: return "vaarg";
2991 case ISD::VACOPY: return "vacopy";
2992 case ISD::VAEND: return "vaend";
2993 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002994 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002995 case ISD::EXTRACT_ELEMENT: return "extract_element";
2996 case ISD::BUILD_PAIR: return "build_pair";
2997 case ISD::STACKSAVE: return "stacksave";
2998 case ISD::STACKRESTORE: return "stackrestore";
2999
3000 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003001 case ISD::MEMSET: return "memset";
3002 case ISD::MEMCPY: return "memcpy";
3003 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003004
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003005 // Bit manipulation
3006 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003007 case ISD::CTPOP: return "ctpop";
3008 case ISD::CTTZ: return "cttz";
3009 case ISD::CTLZ: return "ctlz";
3010
Chris Lattner36ce6912005-11-29 06:21:05 +00003011 // Debug info
3012 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003013 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00003014 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00003015
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003016 case ISD::CONDCODE:
3017 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003018 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003019 case ISD::SETOEQ: return "setoeq";
3020 case ISD::SETOGT: return "setogt";
3021 case ISD::SETOGE: return "setoge";
3022 case ISD::SETOLT: return "setolt";
3023 case ISD::SETOLE: return "setole";
3024 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003025
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003026 case ISD::SETO: return "seto";
3027 case ISD::SETUO: return "setuo";
3028 case ISD::SETUEQ: return "setue";
3029 case ISD::SETUGT: return "setugt";
3030 case ISD::SETUGE: return "setuge";
3031 case ISD::SETULT: return "setult";
3032 case ISD::SETULE: return "setule";
3033 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003034
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003035 case ISD::SETEQ: return "seteq";
3036 case ISD::SETGT: return "setgt";
3037 case ISD::SETGE: return "setge";
3038 case ISD::SETLT: return "setlt";
3039 case ISD::SETLE: return "setle";
3040 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003041 }
3042 }
3043}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003044
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003045void SDNode::dump() const { dump(0); }
3046void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003047 std::cerr << (void*)this << ": ";
3048
3049 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3050 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003051 if (getValueType(i) == MVT::Other)
3052 std::cerr << "ch";
3053 else
3054 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003055 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003056 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003057
3058 std::cerr << " ";
3059 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3060 if (i) std::cerr << ", ";
3061 std::cerr << (void*)getOperand(i).Val;
3062 if (unsigned RN = getOperand(i).ResNo)
3063 std::cerr << ":" << RN;
3064 }
3065
3066 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3067 std::cerr << "<" << CSDN->getValue() << ">";
3068 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3069 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003070 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003071 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003072 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003073 std::cerr << "<";
3074 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003075 if (offset > 0)
3076 std::cerr << " + " << offset;
3077 else
3078 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003079 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003080 std::cerr << "<" << FIDN->getIndex() << ">";
3081 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003082 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003083 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003084 if (offset > 0)
3085 std::cerr << " + " << offset;
3086 else
3087 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003088 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003089 std::cerr << "<";
3090 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3091 if (LBB)
3092 std::cerr << LBB->getName() << " ";
3093 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003094 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003095 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003096 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3097 } else {
3098 std::cerr << " #" << R->getReg();
3099 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003100 } else if (const ExternalSymbolSDNode *ES =
3101 dyn_cast<ExternalSymbolSDNode>(this)) {
3102 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003103 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3104 if (M->getValue())
3105 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3106 else
3107 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003108 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3109 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003110 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003111}
3112
Chris Lattnerde202b32005-11-09 23:47:37 +00003113static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003114 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3115 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003116 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003117 else
3118 std::cerr << "\n" << std::string(indent+2, ' ')
3119 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003120
Chris Lattnerea946cd2005-01-09 20:38:33 +00003121
3122 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003123 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003124}
3125
Chris Lattnerc3aae252005-01-07 07:46:32 +00003126void SelectionDAG::dump() const {
3127 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003128 std::vector<const SDNode*> Nodes;
3129 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3130 I != E; ++I)
3131 Nodes.push_back(I);
3132
Chris Lattner49d24712005-01-09 20:26:36 +00003133 std::sort(Nodes.begin(), Nodes.end());
3134
3135 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003136 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003137 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003138 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003139
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003140 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003141
Chris Lattnerc3aae252005-01-07 07:46:32 +00003142 std::cerr << "\n\n";
3143}
3144
Evan Chengfae9f1c2006-02-09 22:11:03 +00003145/// InsertISelMapEntry - A helper function to insert a key / element pair
3146/// into a SDOperand to SDOperand map. This is added to avoid the map
3147/// insertion operator from being inlined.
3148void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3149 SDNode *Key, unsigned KeyResNo,
3150 SDNode *Element, unsigned ElementResNo) {
3151 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3152 SDOperand(Element, ElementResNo)));
3153}