blob: 77904bf8287bb85132af65b2a48470c0a84e92b5 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000021#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000022#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000025#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000026#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000027#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000028#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000029#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000030#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner5cdcc582005-01-09 20:52:51 +000033static bool isCommutativeBinOp(unsigned Opcode) {
34 switch (Opcode) {
35 case ISD::ADD:
36 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000037 case ISD::MULHU:
38 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000039 case ISD::FADD:
40 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000041 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need commutative info for user ops!
45 }
46}
47
Chris Lattner5cdcc582005-01-09 20:52:51 +000048// isInvertibleForFree - Return true if there is no cost to emitting the logical
49// inverse of this node.
50static bool isInvertibleForFree(SDOperand N) {
51 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000052 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000053 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000054 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000055}
56
Jim Laskey58b968b2005-08-17 20:08:02 +000057//===----------------------------------------------------------------------===//
58// ConstantFPSDNode Class
59//===----------------------------------------------------------------------===//
60
61/// isExactlyValue - We don't rely on operator== working on double values, as
62/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
63/// As such, this method can be used to do an exact bit-for-bit comparison of
64/// two floating point values.
65bool ConstantFPSDNode::isExactlyValue(double V) const {
66 return DoubleToBits(V) == DoubleToBits(Value);
67}
68
69//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000070// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000071//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000072
Evan Chenga8df1662006-03-27 06:58:47 +000073/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000074/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000075bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000076 // Look through a bit convert.
77 if (N->getOpcode() == ISD::BIT_CONVERT)
78 N = N->getOperand(0).Val;
79
Evan Chenga8df1662006-03-27 06:58:47 +000080 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000081
82 unsigned i = 0, e = N->getNumOperands();
83
84 // Skip over all of the undef values.
85 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
86 ++i;
87
88 // Do not accept an all-undef vector.
89 if (i == e) return false;
90
91 // Do not accept build_vectors that aren't all constants or which have non-~0
92 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000093 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000094 if (isa<ConstantSDNode>(NotZero)) {
95 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
96 return false;
97 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000098 MVT::ValueType VT = NotZero.getValueType();
99 if (VT== MVT::f64) {
100 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
101 (uint64_t)-1)
102 return false;
103 } else {
104 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 (uint32_t)-1)
106 return false;
107 }
Evan Chenga8df1662006-03-27 06:58:47 +0000108 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000109 return false;
110
111 // Okay, we have at least one ~0 value, check to see if the rest match or are
112 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000113 for (++i; i != e; ++i)
114 if (N->getOperand(i) != NotZero &&
115 N->getOperand(i).getOpcode() != ISD::UNDEF)
116 return false;
117 return true;
118}
119
120
Evan Cheng4a147842006-03-26 09:50:58 +0000121/// isBuildVectorAllZeros - Return true if the specified node is a
122/// BUILD_VECTOR where all of the elements are 0 or undef.
123bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000124 // Look through a bit convert.
125 if (N->getOpcode() == ISD::BIT_CONVERT)
126 N = N->getOperand(0).Val;
127
Evan Cheng4a147842006-03-26 09:50:58 +0000128 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129
130 unsigned i = 0, e = N->getNumOperands();
131
132 // Skip over all of the undef values.
133 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
134 ++i;
135
136 // Do not accept an all-undef vector.
137 if (i == e) return false;
138
139 // Do not accept build_vectors that aren't all constants or which have non-~0
140 // elements.
141 SDOperand Zero = N->getOperand(i);
142 if (isa<ConstantSDNode>(Zero)) {
143 if (!cast<ConstantSDNode>(Zero)->isNullValue())
144 return false;
145 } else if (isa<ConstantFPSDNode>(Zero)) {
146 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
147 return false;
148 } else
149 return false;
150
151 // Okay, we have at least one ~0 value, check to see if the rest match or are
152 // undefs.
153 for (++i; i != e; ++i)
154 if (N->getOperand(i) != Zero &&
155 N->getOperand(i).getOpcode() != ISD::UNDEF)
156 return false;
157 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000158}
159
Chris Lattnerc3aae252005-01-07 07:46:32 +0000160/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
161/// when given the operation for (X op Y).
162ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
163 // To perform this operation, we just need to swap the L and G bits of the
164 // operation.
165 unsigned OldL = (Operation >> 2) & 1;
166 unsigned OldG = (Operation >> 1) & 1;
167 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
168 (OldL << 1) | // New G bit
169 (OldG << 2)); // New L bit.
170}
171
172/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
173/// 'op' is a valid SetCC operation.
174ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
175 unsigned Operation = Op;
176 if (isInteger)
177 Operation ^= 7; // Flip L, G, E bits, but not U.
178 else
179 Operation ^= 15; // Flip all of the condition bits.
180 if (Operation > ISD::SETTRUE2)
181 Operation &= ~8; // Don't let N and U bits get set.
182 return ISD::CondCode(Operation);
183}
184
185
186/// isSignedOp - For an integer comparison, return 1 if the comparison is a
187/// signed operation and 2 if the result is an unsigned comparison. Return zero
188/// if the operation does not depend on the sign of the input (setne and seteq).
189static int isSignedOp(ISD::CondCode Opcode) {
190 switch (Opcode) {
191 default: assert(0 && "Illegal integer setcc operation!");
192 case ISD::SETEQ:
193 case ISD::SETNE: return 0;
194 case ISD::SETLT:
195 case ISD::SETLE:
196 case ISD::SETGT:
197 case ISD::SETGE: return 1;
198 case ISD::SETULT:
199 case ISD::SETULE:
200 case ISD::SETUGT:
201 case ISD::SETUGE: return 2;
202 }
203}
204
205/// getSetCCOrOperation - Return the result of a logical OR between different
206/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
207/// returns SETCC_INVALID if it is not possible to represent the resultant
208/// comparison.
209ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
210 bool isInteger) {
211 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
212 // Cannot fold a signed integer setcc with an unsigned integer setcc.
213 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000214
Chris Lattnerc3aae252005-01-07 07:46:32 +0000215 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216
Chris Lattnerc3aae252005-01-07 07:46:32 +0000217 // If the N and U bits get set then the resultant comparison DOES suddenly
218 // care about orderedness, and is true when ordered.
219 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000220 Op &= ~16; // Clear the U bit if the N bit is set.
221
222 // Canonicalize illegal integer setcc's.
223 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
224 Op = ISD::SETNE;
225
Chris Lattnerc3aae252005-01-07 07:46:32 +0000226 return ISD::CondCode(Op);
227}
228
229/// getSetCCAndOperation - Return the result of a logical AND between different
230/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
231/// function returns zero if it is not possible to represent the resultant
232/// comparison.
233ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
234 bool isInteger) {
235 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
236 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000237 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238
239 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000240 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
241
242 // Canonicalize illegal integer setcc's.
243 if (isInteger) {
244 switch (Result) {
245 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000246 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
247 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
248 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
249 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000250 }
251 }
252
253 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000254}
255
Chris Lattnerb48da392005-01-23 04:39:44 +0000256const TargetMachine &SelectionDAG::getTarget() const {
257 return TLI.getTargetMachine();
258}
259
Jim Laskey58b968b2005-08-17 20:08:02 +0000260//===----------------------------------------------------------------------===//
261// SelectionDAG Class
262//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000263
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264/// RemoveDeadNodes - This method deletes all unreachable nodes in the
265/// SelectionDAG, including nodes (like loads) that have uses of their token
266/// chain but no other uses and no side effect. If a node is passed in as an
267/// argument, it is used as the seed for node deletion.
268void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 // Create a dummy node (which is not added to allnodes), that adds a reference
270 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000271 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272
Chris Lattnerf469cb62005-11-08 18:52:27 +0000273 bool MadeChange = false;
274
Chris Lattner8b8749f2005-08-17 19:00:20 +0000275 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000276 if (N && N->use_empty()) {
277 DestroyDeadNode(N);
278 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000279 }
280
Chris Lattnerde202b32005-11-09 23:47:37 +0000281 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
282 if (I->use_empty() && I->getOpcode() != 65535) {
283 // Node is dead, recursively delete newly dead uses.
284 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000285 MadeChange = true;
286 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000287
288 // Walk the nodes list, removing the nodes we've marked as dead.
289 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000290 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
291 SDNode *N = I++;
292 if (N->use_empty())
293 AllNodes.erase(N);
294 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000295 }
296
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000298 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299}
300
Chris Lattnerf469cb62005-11-08 18:52:27 +0000301/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
302/// graph. If it is the last user of any of its operands, recursively process
303/// them the same way.
304///
305void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 // Okay, we really are going to delete this node. First take this out of the
307 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000308 RemoveNodeFromCSEMaps(N);
309
310 // Next, brutally remove the operand list. This is safe to do, as there are
311 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000312 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
313 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000314 O->removeUser(N);
315
316 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000317 if (O->use_empty())
318 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000319 }
Chris Lattner65113b22005-11-08 22:07:03 +0000320 delete[] N->OperandList;
321 N->OperandList = 0;
322 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000323
324 // Mark the node as dead.
325 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000326}
327
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000328void SelectionDAG::DeleteNode(SDNode *N) {
329 assert(N->use_empty() && "Cannot delete a node that is not dead!");
330
331 // First take this out of the appropriate CSE map.
332 RemoveNodeFromCSEMaps(N);
333
Chris Lattner1e111c72005-09-07 05:37:01 +0000334 // Finally, remove uses due to operands of this node, remove from the
335 // AllNodes list, and delete the node.
336 DeleteNodeNotInCSEMaps(N);
337}
338
339void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
340
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000341 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000342 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000343
344 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000345 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
346 I->Val->removeUser(N);
347 delete[] N->OperandList;
348 N->OperandList = 0;
349 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000350
351 delete N;
352}
353
Chris Lattner149c58c2005-08-16 18:17:10 +0000354/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
355/// correspond to it. This is useful when we're about to delete or repurpose
356/// the node. We don't want future request for structurally identical nodes
357/// to return N anymore.
358void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000359 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000360 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000361 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000362 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000363 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
364 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000365 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000366 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000367 Erased = TargetConstants.erase(std::make_pair(
368 cast<ConstantSDNode>(N)->getValue(),
369 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000370 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000371 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000372 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000373 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000374 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000375 }
Chris Lattner3181a772006-01-29 06:26:56 +0000376 case ISD::TargetConstantFP: {
377 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
378 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
379 break;
380 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000381 case ISD::STRING:
382 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
383 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000384 case ISD::CONDCODE:
385 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
386 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000387 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000388 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
389 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000390 case ISD::GlobalAddress: {
391 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
392 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
393 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000394 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000395 }
396 case ISD::TargetGlobalAddress: {
397 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
398 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
399 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000400 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000401 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000403 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000404 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000405 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000406 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000407 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000408 case ISD::JumpTable:
409 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
410 break;
411 case ISD::TargetJumpTable:
412 Erased =
413 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
414 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000416 Erased = ConstantPoolIndices.
417 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000418 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
419 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000420 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000421 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000422 Erased = TargetConstantPoolIndices.
423 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000424 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
425 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000426 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000427 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000428 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000429 break;
430 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000431 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000432 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000433 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000434 Erased =
435 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000436 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000437 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000438 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000439 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
440 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000441 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000442 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
443 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000444 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000445 case ISD::SRCVALUE: {
446 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000447 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000448 break;
449 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000450 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000451 Erased = Loads.erase(std::make_pair(N->getOperand(1),
452 std::make_pair(N->getOperand(0),
453 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000454 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000455 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000456 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000457 if (N->getNumOperands() == 0) {
458 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
459 N->getValueType(0)));
460 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000461 Erased =
462 UnaryOps.erase(std::make_pair(N->getOpcode(),
463 std::make_pair(N->getOperand(0),
464 N->getValueType(0))));
465 } else if (N->getNumOperands() == 2) {
466 Erased =
467 BinaryOps.erase(std::make_pair(N->getOpcode(),
468 std::make_pair(N->getOperand(0),
469 N->getOperand(1))));
470 } else {
471 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
472 Erased =
473 OneResultNodes.erase(std::make_pair(N->getOpcode(),
474 std::make_pair(N->getValueType(0),
475 Ops)));
476 }
Chris Lattner385328c2005-05-14 07:42:29 +0000477 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000478 // Remove the node from the ArbitraryNodes map.
479 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
480 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000481 Erased =
482 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
483 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000484 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000485 break;
486 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000487#ifndef NDEBUG
488 // Verify that the node was actually in one of the CSE maps, unless it has a
489 // flag result (which cannot be CSE'd) or is one of the special cases that are
490 // not subject to CSE.
491 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000492 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000493 N->dump();
494 assert(0 && "Node is not in map!");
495 }
496#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000497}
498
Chris Lattner8b8749f2005-08-17 19:00:20 +0000499/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
500/// has been taken out and modified in some way. If the specified node already
501/// exists in the CSE maps, do not modify the maps, but return the existing node
502/// instead. If it doesn't exist, add it and return null.
503///
504SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
505 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000506 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000507 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000508
Chris Lattner9f8cc692005-12-19 22:21:21 +0000509 // Check that remaining values produced are not flags.
510 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
511 if (N->getValueType(i) == MVT::Flag)
512 return 0; // Never CSE anything that produces a flag.
513
Chris Lattnerfe14b342005-12-01 23:14:50 +0000514 if (N->getNumValues() == 1) {
515 if (N->getNumOperands() == 1) {
516 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
517 std::make_pair(N->getOperand(0),
518 N->getValueType(0)))];
519 if (U) return U;
520 U = N;
521 } else if (N->getNumOperands() == 2) {
522 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
523 std::make_pair(N->getOperand(0),
524 N->getOperand(1)))];
525 if (B) return B;
526 B = N;
527 } else {
528 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
529 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000530 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000531 if (ORN) return ORN;
532 ORN = N;
533 }
534 } else {
535 if (N->getOpcode() == ISD::LOAD) {
536 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
537 std::make_pair(N->getOperand(0),
538 N->getValueType(0)))];
539 if (L) return L;
540 L = N;
541 } else {
542 // Remove the node from the ArbitraryNodes map.
543 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
544 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
545 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
546 std::make_pair(RV, Ops))];
547 if (AN) return AN;
548 AN = N;
549 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000550 }
551 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000552}
553
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000554/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
555/// were replaced with those specified. If this node is never memoized,
556/// return null, otherwise return a pointer to the slot it would take. If a
557/// node already exists with these operands, the slot will be non-null.
558SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000559 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000560 return 0; // Never add these nodes.
561
562 // Check that remaining values produced are not flags.
563 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
564 if (N->getValueType(i) == MVT::Flag)
565 return 0; // Never CSE anything that produces a flag.
566
567 if (N->getNumValues() == 1) {
568 return &UnaryOps[std::make_pair(N->getOpcode(),
569 std::make_pair(Op, N->getValueType(0)))];
570 } else {
571 // Remove the node from the ArbitraryNodes map.
572 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
573 std::vector<SDOperand> Ops;
574 Ops.push_back(Op);
575 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
576 std::make_pair(RV, Ops))];
577 }
578 return 0;
579}
580
581/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
582/// were replaced with those specified. If this node is never memoized,
583/// return null, otherwise return a pointer to the slot it would take. If a
584/// node already exists with these operands, the slot will be non-null.
585SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
586 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000587 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000588 return 0; // Never add these nodes.
589
590 // Check that remaining values produced are not flags.
591 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
592 if (N->getValueType(i) == MVT::Flag)
593 return 0; // Never CSE anything that produces a flag.
594
595 if (N->getNumValues() == 1) {
596 return &BinaryOps[std::make_pair(N->getOpcode(),
597 std::make_pair(Op1, Op2))];
598 } else {
599 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
600 std::vector<SDOperand> Ops;
601 Ops.push_back(Op1);
602 Ops.push_back(Op2);
603 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
604 std::make_pair(RV, Ops))];
605 }
606 return 0;
607}
608
609
610/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
611/// were replaced with those specified. If this node is never memoized,
612/// return null, otherwise return a pointer to the slot it would take. If a
613/// node already exists with these operands, the slot will be non-null.
614SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
615 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000616 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000617 return 0; // Never add these nodes.
618
619 // Check that remaining values produced are not flags.
620 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
621 if (N->getValueType(i) == MVT::Flag)
622 return 0; // Never CSE anything that produces a flag.
623
624 if (N->getNumValues() == 1) {
625 if (N->getNumOperands() == 1) {
626 return &UnaryOps[std::make_pair(N->getOpcode(),
627 std::make_pair(Ops[0],
628 N->getValueType(0)))];
629 } else if (N->getNumOperands() == 2) {
630 return &BinaryOps[std::make_pair(N->getOpcode(),
631 std::make_pair(Ops[0], Ops[1]))];
632 } else {
633 return &OneResultNodes[std::make_pair(N->getOpcode(),
634 std::make_pair(N->getValueType(0),
635 Ops))];
636 }
637 } else {
638 if (N->getOpcode() == ISD::LOAD) {
639 return &Loads[std::make_pair(Ops[1],
640 std::make_pair(Ops[0], N->getValueType(0)))];
641 } else {
642 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
643 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
644 std::make_pair(RV, Ops))];
645 }
646 }
647 return 0;
648}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000649
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650
Chris Lattner78ec3112003-08-11 14:57:33 +0000651SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000652 while (!AllNodes.empty()) {
653 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000654 delete [] N->OperandList;
655 N->OperandList = 0;
656 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000657 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000658 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000659}
660
Chris Lattner0f2287b2005-04-13 02:38:18 +0000661SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000662 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000663 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000664 return getNode(ISD::AND, Op.getValueType(), Op,
665 getConstant(Imm, Op.getValueType()));
666}
667
Chris Lattnerc3aae252005-01-07 07:46:32 +0000668SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
669 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000670 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
671
Chris Lattnerc3aae252005-01-07 07:46:32 +0000672 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000673 if (VT != MVT::i64)
674 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000675
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 SDNode *&N = Constants[std::make_pair(Val, VT)];
677 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000678 N = new ConstantSDNode(false, Val, VT);
679 AllNodes.push_back(N);
680 return SDOperand(N, 0);
681}
682
Chris Lattner36ce6912005-11-29 06:21:05 +0000683SDOperand SelectionDAG::getString(const std::string &Val) {
684 StringSDNode *&N = StringNodes[Val];
685 if (!N) {
686 N = new StringSDNode(Val);
687 AllNodes.push_back(N);
688 }
689 return SDOperand(N, 0);
690}
691
Chris Lattner37bfbb42005-08-17 00:34:06 +0000692SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
693 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
694 // Mask out any bits that are not valid for this constant.
695 if (VT != MVT::i64)
696 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
697
698 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
699 if (N) return SDOperand(N, 0);
700 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000701 AllNodes.push_back(N);
702 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000703}
704
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
706 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
707 if (VT == MVT::f32)
708 Val = (float)Val; // Mask out extra precision.
709
Chris Lattnerd8658612005-02-17 20:17:32 +0000710 // Do the map lookup using the actual bit pattern for the floating point
711 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
712 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000713 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000714 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000715 N = new ConstantFPSDNode(false, Val, VT);
716 AllNodes.push_back(N);
717 return SDOperand(N, 0);
718}
719
720SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
721 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
722 if (VT == MVT::f32)
723 Val = (float)Val; // Mask out extra precision.
724
725 // Do the map lookup using the actual bit pattern for the floating point
726 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
727 // we don't have issues with SNANs.
728 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
729 if (N) return SDOperand(N, 0);
730 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731 AllNodes.push_back(N);
732 return SDOperand(N, 0);
733}
734
Chris Lattnerc3aae252005-01-07 07:46:32 +0000735SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000736 MVT::ValueType VT, int offset) {
737 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000738 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000739 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000740 AllNodes.push_back(N);
741 return SDOperand(N, 0);
742}
743
744SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000745 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000746 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000747 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000748 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 AllNodes.push_back(N);
750 return SDOperand(N, 0);
751}
752
753SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
754 SDNode *&N = FrameIndices[FI];
755 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000756 N = new FrameIndexSDNode(FI, VT, false);
757 AllNodes.push_back(N);
758 return SDOperand(N, 0);
759}
760
761SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
762 SDNode *&N = TargetFrameIndices[FI];
763 if (N) return SDOperand(N, 0);
764 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765 AllNodes.push_back(N);
766 return SDOperand(N, 0);
767}
768
Nate Begeman37efe672006-04-22 18:53:45 +0000769SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
770 SDNode *&N = JumpTableIndices[JTI];
771 if (N) return SDOperand(N, 0);
772 N = new JumpTableSDNode(JTI, VT, false);
773 AllNodes.push_back(N);
774 return SDOperand(N, 0);
775}
776
777SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
778 SDNode *&N = TargetJumpTableIndices[JTI];
779 if (N) return SDOperand(N, 0);
780 N = new JumpTableSDNode(JTI, VT, true);
781 AllNodes.push_back(N);
782 return SDOperand(N, 0);
783}
784
Evan Chengb8973bd2006-01-31 22:23:14 +0000785SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000786 unsigned Alignment, int Offset) {
787 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
788 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000789 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000790 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000791 AllNodes.push_back(N);
792 return SDOperand(N, 0);
793}
794
Evan Chengb8973bd2006-01-31 22:23:14 +0000795SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000796 unsigned Alignment, int Offset) {
797 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
798 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000799 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000800 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 AllNodes.push_back(N);
802 return SDOperand(N, 0);
803}
804
805SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
806 SDNode *&N = BBNodes[MBB];
807 if (N) return SDOperand(N, 0);
808 N = new BasicBlockSDNode(MBB);
809 AllNodes.push_back(N);
810 return SDOperand(N, 0);
811}
812
Chris Lattner15e4b012005-07-10 00:07:11 +0000813SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
814 if ((unsigned)VT >= ValueTypeNodes.size())
815 ValueTypeNodes.resize(VT+1);
816 if (ValueTypeNodes[VT] == 0) {
817 ValueTypeNodes[VT] = new VTSDNode(VT);
818 AllNodes.push_back(ValueTypeNodes[VT]);
819 }
820
821 return SDOperand(ValueTypeNodes[VT], 0);
822}
823
Chris Lattnerc3aae252005-01-07 07:46:32 +0000824SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
825 SDNode *&N = ExternalSymbols[Sym];
826 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000827 N = new ExternalSymbolSDNode(false, Sym, VT);
828 AllNodes.push_back(N);
829 return SDOperand(N, 0);
830}
831
Chris Lattner809ec112006-01-28 10:09:25 +0000832SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
833 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000834 SDNode *&N = TargetExternalSymbols[Sym];
835 if (N) return SDOperand(N, 0);
836 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000837 AllNodes.push_back(N);
838 return SDOperand(N, 0);
839}
840
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000841SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
842 if ((unsigned)Cond >= CondCodeNodes.size())
843 CondCodeNodes.resize(Cond+1);
844
Chris Lattner079a27a2005-08-09 20:40:02 +0000845 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000846 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000847 AllNodes.push_back(CondCodeNodes[Cond]);
848 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000849 return SDOperand(CondCodeNodes[Cond], 0);
850}
851
Chris Lattner0fdd7682005-08-30 22:38:38 +0000852SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
853 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
854 if (!Reg) {
855 Reg = new RegisterSDNode(RegNo, VT);
856 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000857 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000858 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000859}
860
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000861SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
862 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000863 // These setcc operations always fold.
864 switch (Cond) {
865 default: break;
866 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000867 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000869 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000870
871 case ISD::SETOEQ:
872 case ISD::SETOGT:
873 case ISD::SETOGE:
874 case ISD::SETOLT:
875 case ISD::SETOLE:
876 case ISD::SETONE:
877 case ISD::SETO:
878 case ISD::SETUO:
879 case ISD::SETUEQ:
880 case ISD::SETUNE:
881 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
882 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000883 }
884
Chris Lattner67255a12005-04-07 18:14:58 +0000885 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
886 uint64_t C2 = N2C->getValue();
887 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
888 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000889
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890 // Sign extend the operands if required
891 if (ISD::isSignedIntSetCC(Cond)) {
892 C1 = N1C->getSignExtended();
893 C2 = N2C->getSignExtended();
894 }
895
896 switch (Cond) {
897 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000898 case ISD::SETEQ: return getConstant(C1 == C2, VT);
899 case ISD::SETNE: return getConstant(C1 != C2, VT);
900 case ISD::SETULT: return getConstant(C1 < C2, VT);
901 case ISD::SETUGT: return getConstant(C1 > C2, VT);
902 case ISD::SETULE: return getConstant(C1 <= C2, VT);
903 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
904 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
905 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
906 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
907 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000908 }
Chris Lattner24673922005-04-07 18:58:54 +0000909 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000910 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000911 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
912 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
913
914 // If the comparison constant has bits in the upper part, the
915 // zero-extended value could never match.
916 if (C2 & (~0ULL << InSize)) {
917 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
918 switch (Cond) {
919 case ISD::SETUGT:
920 case ISD::SETUGE:
921 case ISD::SETEQ: return getConstant(0, VT);
922 case ISD::SETULT:
923 case ISD::SETULE:
924 case ISD::SETNE: return getConstant(1, VT);
925 case ISD::SETGT:
926 case ISD::SETGE:
927 // True if the sign bit of C2 is set.
928 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
929 case ISD::SETLT:
930 case ISD::SETLE:
931 // True if the sign bit of C2 isn't set.
932 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
933 default:
934 break;
935 }
936 }
937
938 // Otherwise, we can perform the comparison with the low bits.
939 switch (Cond) {
940 case ISD::SETEQ:
941 case ISD::SETNE:
942 case ISD::SETUGT:
943 case ISD::SETUGE:
944 case ISD::SETULT:
945 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000946 return getSetCC(VT, N1.getOperand(0),
947 getConstant(C2, N1.getOperand(0).getValueType()),
948 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000949 default:
950 break; // todo, be more careful with signed comparisons
951 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000952 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
953 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
954 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
955 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
956 MVT::ValueType ExtDstTy = N1.getValueType();
957 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000958
Chris Lattner7b2880c2005-08-24 22:44:39 +0000959 // If the extended part has any inconsistent bits, it cannot ever
960 // compare equal. In other words, they have to be all ones or all
961 // zeros.
962 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000963 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000964 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
965 return getConstant(Cond == ISD::SETNE, VT);
966
967 // Otherwise, make this a use of a zext.
968 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000969 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000970 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000971 }
972
Chris Lattner67255a12005-04-07 18:14:58 +0000973 uint64_t MinVal, MaxVal;
974 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
975 if (ISD::isSignedIntSetCC(Cond)) {
976 MinVal = 1ULL << (OperandBitSize-1);
977 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
978 MaxVal = ~0ULL >> (65-OperandBitSize);
979 else
980 MaxVal = 0;
981 } else {
982 MinVal = 0;
983 MaxVal = ~0ULL >> (64-OperandBitSize);
984 }
985
986 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
987 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
988 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
989 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000990 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
991 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000992 }
993
994 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
995 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
996 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000997 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
998 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000999 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001000
Nate Begeman72ea2812005-04-14 08:56:52 +00001001 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
1002 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +00001003
Nate Begeman72ea2812005-04-14 08:56:52 +00001004 // Canonicalize setgt X, Min --> setne X, Min
1005 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001006 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001007
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001008 // If we have setult X, 1, turn it into seteq X, 0
1009 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001010 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
1011 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +00001012 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001013 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001014 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
1015 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +00001016
1017 // If we have "setcc X, C1", check to see if we can shrink the immediate
1018 // by changing cc.
1019
1020 // SETUGT X, SINTMAX -> SETLT X, 0
1021 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1022 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001023 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +00001024
1025 // FIXME: Implement the rest of these.
1026
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001027
1028 // Fold bit comparisons when we can.
1029 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1030 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1031 if (ConstantSDNode *AndRHS =
1032 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1033 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1034 // Perform the xform if the AND RHS is a single bit.
1035 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1036 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001037 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001038 TLI.getShiftAmountTy()));
1039 }
1040 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1041 // (X & 8) == 8 --> (X & 8) >> 3
1042 // Perform the xform if C2 is a single bit.
1043 if ((C2 & (C2-1)) == 0) {
1044 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001045 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001046 }
1047 }
1048 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 }
Chris Lattner67255a12005-04-07 18:14:58 +00001050 } else if (isa<ConstantSDNode>(N1.Val)) {
1051 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001052 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001053 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054
1055 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1056 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1057 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001058
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 switch (Cond) {
1060 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001061 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1062 case ISD::SETNE: return getConstant(C1 != C2, VT);
1063 case ISD::SETLT: return getConstant(C1 < C2, VT);
1064 case ISD::SETGT: return getConstant(C1 > C2, VT);
1065 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1066 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 }
1068 } else {
1069 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001070 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 }
1072
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001073 // Could not fold it.
1074 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075}
1076
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001078///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001079SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001080 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1081 if (!N) {
1082 N = new SDNode(Opcode, VT);
1083 AllNodes.push_back(N);
1084 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001085 return SDOperand(N, 0);
1086}
1087
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1089 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001090 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001091 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1093 uint64_t Val = C->getValue();
1094 switch (Opcode) {
1095 default: break;
1096 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001097 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1099 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001100 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1101 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001102 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001103 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001104 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001105 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001106 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001107 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001108 case ISD::BSWAP:
1109 switch(VT) {
1110 default: assert(0 && "Invalid bswap!"); break;
1111 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1112 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1113 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1114 }
1115 break;
1116 case ISD::CTPOP:
1117 switch(VT) {
1118 default: assert(0 && "Invalid ctpop!"); break;
1119 case MVT::i1: return getConstant(Val != 0, VT);
1120 case MVT::i8:
1121 Tmp1 = (unsigned)Val & 0xFF;
1122 return getConstant(CountPopulation_32(Tmp1), VT);
1123 case MVT::i16:
1124 Tmp1 = (unsigned)Val & 0xFFFF;
1125 return getConstant(CountPopulation_32(Tmp1), VT);
1126 case MVT::i32:
1127 return getConstant(CountPopulation_32((unsigned)Val), VT);
1128 case MVT::i64:
1129 return getConstant(CountPopulation_64(Val), VT);
1130 }
1131 case ISD::CTLZ:
1132 switch(VT) {
1133 default: assert(0 && "Invalid ctlz!"); break;
1134 case MVT::i1: return getConstant(Val == 0, VT);
1135 case MVT::i8:
1136 Tmp1 = (unsigned)Val & 0xFF;
1137 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1138 case MVT::i16:
1139 Tmp1 = (unsigned)Val & 0xFFFF;
1140 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1141 case MVT::i32:
1142 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1143 case MVT::i64:
1144 return getConstant(CountLeadingZeros_64(Val), VT);
1145 }
1146 case ISD::CTTZ:
1147 switch(VT) {
1148 default: assert(0 && "Invalid cttz!"); break;
1149 case MVT::i1: return getConstant(Val == 0, VT);
1150 case MVT::i8:
1151 Tmp1 = (unsigned)Val | 0x100;
1152 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1153 case MVT::i16:
1154 Tmp1 = (unsigned)Val | 0x10000;
1155 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1156 case MVT::i32:
1157 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1158 case MVT::i64:
1159 return getConstant(CountTrailingZeros_64(Val), VT);
1160 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001161 }
1162 }
1163
Chris Lattner94683772005-12-23 05:30:37 +00001164 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1166 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001167 case ISD::FNEG:
1168 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001169 case ISD::FABS:
1170 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 case ISD::FP_ROUND:
1172 case ISD::FP_EXTEND:
1173 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001174 case ISD::FP_TO_SINT:
1175 return getConstant((int64_t)C->getValue(), VT);
1176 case ISD::FP_TO_UINT:
1177 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001178 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001179 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001180 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001181 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001182 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001183 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 }
1185
1186 unsigned OpOpcode = Operand.Val->getOpcode();
1187 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001188 case ISD::TokenFactor:
1189 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001190 case ISD::SIGN_EXTEND:
1191 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001192 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1194 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1195 break;
1196 case ISD::ZERO_EXTEND:
1197 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001198 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001199 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001200 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001201 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001202 case ISD::ANY_EXTEND:
1203 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001204 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001205 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1206 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1207 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1208 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001209 case ISD::TRUNCATE:
1210 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001211 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001212 if (OpOpcode == ISD::TRUNCATE)
1213 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001214 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1215 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001216 // If the source is smaller than the dest, we still need an extend.
1217 if (Operand.Val->getOperand(0).getValueType() < VT)
1218 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1219 else if (Operand.Val->getOperand(0).getValueType() > VT)
1220 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1221 else
1222 return Operand.Val->getOperand(0);
1223 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 break;
Chris Lattner35481892005-12-23 00:16:34 +00001225 case ISD::BIT_CONVERT:
1226 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001227 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001228 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001229 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001230 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1231 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001232 if (OpOpcode == ISD::UNDEF)
1233 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001234 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001235 case ISD::SCALAR_TO_VECTOR:
1236 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1237 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1238 "Illegal SCALAR_TO_VECTOR node!");
1239 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001240 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001241 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1242 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001243 Operand.Val->getOperand(0));
1244 if (OpOpcode == ISD::FNEG) // --X -> X
1245 return Operand.Val->getOperand(0);
1246 break;
1247 case ISD::FABS:
1248 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1249 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1250 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251 }
1252
Chris Lattner43247a12005-08-25 19:12:10 +00001253 SDNode *N;
1254 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1255 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001256 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001257 E = N = new SDNode(Opcode, Operand);
1258 } else {
1259 N = new SDNode(Opcode, Operand);
1260 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 N->setValueTypes(VT);
1262 AllNodes.push_back(N);
1263 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001264}
1265
Chris Lattner36019aa2005-04-18 03:48:41 +00001266
1267
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1269 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001270#ifndef NDEBUG
1271 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001272 case ISD::TokenFactor:
1273 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1274 N2.getValueType() == MVT::Other && "Invalid token factor!");
1275 break;
Chris Lattner76365122005-01-16 02:23:22 +00001276 case ISD::AND:
1277 case ISD::OR:
1278 case ISD::XOR:
1279 case ISD::UDIV:
1280 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001281 case ISD::MULHU:
1282 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001283 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1284 // fall through
1285 case ISD::ADD:
1286 case ISD::SUB:
1287 case ISD::MUL:
1288 case ISD::SDIV:
1289 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001290 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1291 // fall through.
1292 case ISD::FADD:
1293 case ISD::FSUB:
1294 case ISD::FMUL:
1295 case ISD::FDIV:
1296 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001297 assert(N1.getValueType() == N2.getValueType() &&
1298 N1.getValueType() == VT && "Binary operator types must match!");
1299 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001300 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1301 assert(N1.getValueType() == VT &&
1302 MVT::isFloatingPoint(N1.getValueType()) &&
1303 MVT::isFloatingPoint(N2.getValueType()) &&
1304 "Invalid FCOPYSIGN!");
1305 break;
Chris Lattner76365122005-01-16 02:23:22 +00001306 case ISD::SHL:
1307 case ISD::SRA:
1308 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001309 case ISD::ROTL:
1310 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001311 assert(VT == N1.getValueType() &&
1312 "Shift operators return type must be the same as their first arg");
1313 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001314 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001315 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001316 case ISD::FP_ROUND_INREG: {
1317 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1318 assert(VT == N1.getValueType() && "Not an inreg round!");
1319 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1320 "Cannot FP_ROUND_INREG integer types");
1321 assert(EVT <= VT && "Not rounding down!");
1322 break;
1323 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001324 case ISD::AssertSext:
1325 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001326 case ISD::SIGN_EXTEND_INREG: {
1327 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1328 assert(VT == N1.getValueType() && "Not an inreg extend!");
1329 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1330 "Cannot *_EXTEND_INREG FP types");
1331 assert(EVT <= VT && "Not extending!");
1332 }
1333
Chris Lattner76365122005-01-16 02:23:22 +00001334 default: break;
1335 }
1336#endif
1337
Chris Lattnerc3aae252005-01-07 07:46:32 +00001338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1339 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1340 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001341 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1342 int64_t Val = N1C->getValue();
1343 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1344 Val <<= 64-FromBits;
1345 Val >>= 64-FromBits;
1346 return getConstant(Val, VT);
1347 }
1348
Chris Lattnerc3aae252005-01-07 07:46:32 +00001349 if (N2C) {
1350 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1351 switch (Opcode) {
1352 case ISD::ADD: return getConstant(C1 + C2, VT);
1353 case ISD::SUB: return getConstant(C1 - C2, VT);
1354 case ISD::MUL: return getConstant(C1 * C2, VT);
1355 case ISD::UDIV:
1356 if (C2) return getConstant(C1 / C2, VT);
1357 break;
1358 case ISD::UREM :
1359 if (C2) return getConstant(C1 % C2, VT);
1360 break;
1361 case ISD::SDIV :
1362 if (C2) return getConstant(N1C->getSignExtended() /
1363 N2C->getSignExtended(), VT);
1364 break;
1365 case ISD::SREM :
1366 if (C2) return getConstant(N1C->getSignExtended() %
1367 N2C->getSignExtended(), VT);
1368 break;
1369 case ISD::AND : return getConstant(C1 & C2, VT);
1370 case ISD::OR : return getConstant(C1 | C2, VT);
1371 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001372 case ISD::SHL : return getConstant(C1 << C2, VT);
1373 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001374 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001375 case ISD::ROTL :
1376 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1377 VT);
1378 case ISD::ROTR :
1379 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1380 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 default: break;
1382 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001383 } else { // Cannonicalize constant to RHS if commutative
1384 if (isCommutativeBinOp(Opcode)) {
1385 std::swap(N1C, N2C);
1386 std::swap(N1, N2);
1387 }
1388 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001389 }
1390
1391 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1392 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001393 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001394 if (N2CFP) {
1395 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1396 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001397 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1398 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1399 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1400 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001401 if (C2) return getConstantFP(C1 / C2, VT);
1402 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001403 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001404 if (C2) return getConstantFP(fmod(C1, C2), VT);
1405 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001406 case ISD::FCOPYSIGN: {
1407 union {
1408 double F;
1409 uint64_t I;
1410 } u1;
1411 union {
1412 double F;
1413 int64_t I;
1414 } u2;
1415 u1.F = C1;
1416 u2.F = C2;
1417 if (u2.I < 0) // Sign bit of RHS set?
1418 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1419 else
1420 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1421 return getConstantFP(u1.F, VT);
1422 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001423 default: break;
1424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001425 } else { // Cannonicalize constant to RHS if commutative
1426 if (isCommutativeBinOp(Opcode)) {
1427 std::swap(N1CFP, N2CFP);
1428 std::swap(N1, N2);
1429 }
1430 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001431 }
Chris Lattner62b57722006-04-20 05:39:12 +00001432
1433 // Canonicalize an UNDEF to the RHS, even over a constant.
1434 if (N1.getOpcode() == ISD::UNDEF) {
1435 if (isCommutativeBinOp(Opcode)) {
1436 std::swap(N1, N2);
1437 } else {
1438 switch (Opcode) {
1439 case ISD::FP_ROUND_INREG:
1440 case ISD::SIGN_EXTEND_INREG:
1441 case ISD::SUB:
1442 case ISD::FSUB:
1443 case ISD::FDIV:
1444 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001445 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001446 return N1; // fold op(undef, arg2) -> undef
1447 case ISD::UDIV:
1448 case ISD::SDIV:
1449 case ISD::UREM:
1450 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001451 case ISD::SRL:
1452 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001453 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1454 }
1455 }
1456 }
1457
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001458 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001459 if (N2.getOpcode() == ISD::UNDEF) {
1460 switch (Opcode) {
1461 case ISD::ADD:
1462 case ISD::SUB:
1463 case ISD::FADD:
1464 case ISD::FSUB:
1465 case ISD::FMUL:
1466 case ISD::FDIV:
1467 case ISD::FREM:
1468 case ISD::UDIV:
1469 case ISD::SDIV:
1470 case ISD::UREM:
1471 case ISD::SREM:
1472 case ISD::XOR:
1473 return N2; // fold op(arg1, undef) -> undef
1474 case ISD::MUL:
1475 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001476 case ISD::SRL:
1477 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001478 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1479 case ISD::OR:
1480 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001481 case ISD::SRA:
1482 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001483 }
1484 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001485
Chris Lattnerc3aae252005-01-07 07:46:32 +00001486 // Finally, fold operations that do not require constants.
1487 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001488 case ISD::FP_ROUND_INREG:
1489 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1490 break;
1491 case ISD::SIGN_EXTEND_INREG: {
1492 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1493 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001494 break;
1495 }
1496
Nate Begemaneea805e2005-04-13 21:23:31 +00001497 // FIXME: figure out how to safely handle things like
1498 // int foo(int x) { return 1 << (x & 255); }
1499 // int bar() { return foo(256); }
1500#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001501 case ISD::SHL:
1502 case ISD::SRL:
1503 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001504 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001505 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001506 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001507 else if (N2.getOpcode() == ISD::AND)
1508 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1509 // If the and is only masking out bits that cannot effect the shift,
1510 // eliminate the and.
1511 unsigned NumBits = MVT::getSizeInBits(VT);
1512 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1513 return getNode(Opcode, VT, N1, N2.getOperand(0));
1514 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001515 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001516#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001517 }
1518
Chris Lattner27e9b412005-05-11 18:57:39 +00001519 // Memoize this node if possible.
1520 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001521 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001522 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1523 if (BON) return SDOperand(BON, 0);
1524
1525 BON = N = new SDNode(Opcode, N1, N2);
1526 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001527 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001528 }
1529
Chris Lattner3e011362005-05-14 07:45:46 +00001530 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001531 AllNodes.push_back(N);
1532 return SDOperand(N, 0);
1533}
1534
Chris Lattnerc3aae252005-01-07 07:46:32 +00001535SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1536 SDOperand N1, SDOperand N2, SDOperand N3) {
1537 // Perform various simplifications.
1538 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1539 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001540 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001541 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001542 case ISD::SETCC: {
1543 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001544 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001545 if (Simp.Val) return Simp;
1546 break;
1547 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001548 case ISD::SELECT:
1549 if (N1C)
1550 if (N1C->getValue())
1551 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001552 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001553 return N3; // select false, X, Y -> Y
1554
1555 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001556 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001557 case ISD::BRCOND:
1558 if (N2C)
1559 if (N2C->getValue()) // Unconditional branch
1560 return getNode(ISD::BR, MVT::Other, N1, N3);
1561 else
1562 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001563 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001564 case ISD::VECTOR_SHUFFLE:
1565 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1566 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1567 N3.getOpcode() == ISD::BUILD_VECTOR &&
1568 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1569 "Illegal VECTOR_SHUFFLE node!");
1570 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001571 }
1572
Chris Lattner385328c2005-05-14 07:42:29 +00001573 std::vector<SDOperand> Ops;
1574 Ops.reserve(3);
1575 Ops.push_back(N1);
1576 Ops.push_back(N2);
1577 Ops.push_back(N3);
1578
Chris Lattner43247a12005-08-25 19:12:10 +00001579 // Memoize node if it doesn't produce a flag.
1580 SDNode *N;
1581 if (VT != MVT::Flag) {
1582 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1583 if (E) return SDOperand(E, 0);
1584 E = N = new SDNode(Opcode, N1, N2, N3);
1585 } else {
1586 N = new SDNode(Opcode, N1, N2, N3);
1587 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001588 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001589 AllNodes.push_back(N);
1590 return SDOperand(N, 0);
1591}
1592
1593SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001594 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001595 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001596 std::vector<SDOperand> Ops;
1597 Ops.reserve(4);
1598 Ops.push_back(N1);
1599 Ops.push_back(N2);
1600 Ops.push_back(N3);
1601 Ops.push_back(N4);
1602 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001603}
1604
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001605SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1606 SDOperand N1, SDOperand N2, SDOperand N3,
1607 SDOperand N4, SDOperand N5) {
1608 std::vector<SDOperand> Ops;
1609 Ops.reserve(5);
1610 Ops.push_back(N1);
1611 Ops.push_back(N2);
1612 Ops.push_back(N3);
1613 Ops.push_back(N4);
1614 Ops.push_back(N5);
1615 return getNode(Opcode, VT, Ops);
1616}
1617
Evan Cheng7038daf2005-12-10 00:37:58 +00001618SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1619 SDOperand Chain, SDOperand Ptr,
1620 SDOperand SV) {
1621 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1622 if (N) return SDOperand(N, 0);
1623 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1624
1625 // Loads have a token chain.
1626 setNodeValueTypes(N, VT, MVT::Other);
1627 AllNodes.push_back(N);
1628 return SDOperand(N, 0);
1629}
1630
1631SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1632 SDOperand Chain, SDOperand Ptr,
1633 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001634 std::vector<SDOperand> Ops;
1635 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001636 Ops.push_back(Chain);
1637 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001638 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001639 Ops.push_back(getConstant(Count, MVT::i32));
1640 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001641 std::vector<MVT::ValueType> VTs;
1642 VTs.reserve(2);
1643 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1644 return getNode(ISD::VLOAD, VTs, Ops);
1645}
1646
1647SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1648 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1649 MVT::ValueType EVT) {
1650 std::vector<SDOperand> Ops;
1651 Ops.reserve(4);
1652 Ops.push_back(Chain);
1653 Ops.push_back(Ptr);
1654 Ops.push_back(SV);
1655 Ops.push_back(getValueType(EVT));
1656 std::vector<MVT::ValueType> VTs;
1657 VTs.reserve(2);
1658 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1659 return getNode(Opcode, VTs, Ops);
1660}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001661
Chris Lattner0437cdd2005-05-09 04:14:13 +00001662SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001663 assert((!V || isa<PointerType>(V->getType())) &&
1664 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001665 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1666 if (N) return SDOperand(N, 0);
1667
1668 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001669 AllNodes.push_back(N);
1670 return SDOperand(N, 0);
1671}
1672
Nate Begemanacc398c2006-01-25 18:21:52 +00001673SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1674 SDOperand Chain, SDOperand Ptr,
1675 SDOperand SV) {
1676 std::vector<SDOperand> Ops;
1677 Ops.reserve(3);
1678 Ops.push_back(Chain);
1679 Ops.push_back(Ptr);
1680 Ops.push_back(SV);
1681 std::vector<MVT::ValueType> VTs;
1682 VTs.reserve(2);
1683 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1684 return getNode(ISD::VAARG, VTs, Ops);
1685}
1686
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001687SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001688 std::vector<SDOperand> &Ops) {
1689 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001690 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001691 case 1: return getNode(Opcode, VT, Ops[0]);
1692 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1693 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001694 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001695 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001696
Chris Lattneref847df2005-04-09 03:27:28 +00001697 switch (Opcode) {
1698 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001699 case ISD::TRUNCSTORE: {
1700 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1701 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1702#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1703 // If this is a truncating store of a constant, convert to the desired type
1704 // and store it instead.
1705 if (isa<Constant>(Ops[0])) {
1706 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1707 if (isa<Constant>(Op))
1708 N1 = Op;
1709 }
1710 // Also for ConstantFP?
1711#endif
1712 if (Ops[0].getValueType() == EVT) // Normal store?
1713 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1714 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1715 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1716 "Can't do FP-INT conversion!");
1717 break;
1718 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001719 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001720 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001721 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1722 "LHS and RHS of condition must have same type!");
1723 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1724 "True and False arms of SelectCC must have same type!");
1725 assert(Ops[2].getValueType() == VT &&
1726 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001727 break;
1728 }
1729 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001730 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001731 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1732 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001733 break;
1734 }
Chris Lattneref847df2005-04-09 03:27:28 +00001735 }
1736
Chris Lattner385328c2005-05-14 07:42:29 +00001737 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001738 SDNode *N;
1739 if (VT != MVT::Flag) {
1740 SDNode *&E =
1741 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1742 if (E) return SDOperand(E, 0);
1743 E = N = new SDNode(Opcode, Ops);
1744 } else {
1745 N = new SDNode(Opcode, Ops);
1746 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001747 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001748 AllNodes.push_back(N);
1749 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750}
1751
Chris Lattner89c34632005-05-14 06:20:26 +00001752SDOperand SelectionDAG::getNode(unsigned Opcode,
1753 std::vector<MVT::ValueType> &ResultTys,
1754 std::vector<SDOperand> &Ops) {
1755 if (ResultTys.size() == 1)
1756 return getNode(Opcode, ResultTys[0], Ops);
1757
Chris Lattner5f056bf2005-07-10 01:55:33 +00001758 switch (Opcode) {
1759 case ISD::EXTLOAD:
1760 case ISD::SEXTLOAD:
1761 case ISD::ZEXTLOAD: {
1762 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1763 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1764 // If they are asking for an extending load from/to the same thing, return a
1765 // normal load.
1766 if (ResultTys[0] == EVT)
1767 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001768 if (MVT::isVector(ResultTys[0])) {
1769 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1770 "Invalid vector extload!");
1771 } else {
1772 assert(EVT < ResultTys[0] &&
1773 "Should only be an extending load, not truncating!");
1774 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001775 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001776 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001777 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1778 "Cannot convert from FP to Int or Int -> FP!");
1779 break;
1780 }
1781
Chris Lattnere89083a2005-05-14 07:25:05 +00001782 // FIXME: figure out how to safely handle things like
1783 // int foo(int x) { return 1 << (x & 255); }
1784 // int bar() { return foo(256); }
1785#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001786 case ISD::SRA_PARTS:
1787 case ISD::SRL_PARTS:
1788 case ISD::SHL_PARTS:
1789 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001790 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001791 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1792 else if (N3.getOpcode() == ISD::AND)
1793 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1794 // If the and is only masking out bits that cannot effect the shift,
1795 // eliminate the and.
1796 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1797 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1798 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1799 }
1800 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001801#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001802 }
Chris Lattner89c34632005-05-14 06:20:26 +00001803
Chris Lattner43247a12005-08-25 19:12:10 +00001804 // Memoize the node unless it returns a flag.
1805 SDNode *N;
1806 if (ResultTys.back() != MVT::Flag) {
1807 SDNode *&E =
1808 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1809 if (E) return SDOperand(E, 0);
1810 E = N = new SDNode(Opcode, Ops);
1811 } else {
1812 N = new SDNode(Opcode, Ops);
1813 }
Chris Lattnera3255112005-11-08 23:30:28 +00001814 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001815 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001816 return SDOperand(N, 0);
1817}
1818
Chris Lattnera3255112005-11-08 23:30:28 +00001819void SelectionDAG::setNodeValueTypes(SDNode *N,
1820 std::vector<MVT::ValueType> &RetVals) {
1821 switch (RetVals.size()) {
1822 case 0: return;
1823 case 1: N->setValueTypes(RetVals[0]); return;
1824 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1825 default: break;
1826 }
1827
1828 std::list<std::vector<MVT::ValueType> >::iterator I =
1829 std::find(VTList.begin(), VTList.end(), RetVals);
1830 if (I == VTList.end()) {
1831 VTList.push_front(RetVals);
1832 I = VTList.begin();
1833 }
1834
1835 N->setValueTypes(&(*I)[0], I->size());
1836}
1837
1838void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1839 MVT::ValueType VT2) {
1840 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1841 E = VTList.end(); I != E; ++I) {
1842 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1843 N->setValueTypes(&(*I)[0], 2);
1844 return;
1845 }
1846 }
1847 std::vector<MVT::ValueType> V;
1848 V.push_back(VT1);
1849 V.push_back(VT2);
1850 VTList.push_front(V);
1851 N->setValueTypes(&(*VTList.begin())[0], 2);
1852}
1853
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001854/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1855/// specified operands. If the resultant node already exists in the DAG,
1856/// this does not modify the specified node, instead it returns the node that
1857/// already exists. If the resultant node does not exist in the DAG, the
1858/// input node is returned. As a degenerate case, if you specify the same
1859/// input operands as the node already has, the input node is returned.
1860SDOperand SelectionDAG::
1861UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1862 SDNode *N = InN.Val;
1863 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1864
1865 // Check to see if there is no change.
1866 if (Op == N->getOperand(0)) return InN;
1867
1868 // See if the modified node already exists.
1869 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1870 if (NewSlot && *NewSlot)
1871 return SDOperand(*NewSlot, InN.ResNo);
1872
1873 // Nope it doesn't. Remove the node from it's current place in the maps.
1874 if (NewSlot)
1875 RemoveNodeFromCSEMaps(N);
1876
1877 // Now we update the operands.
1878 N->OperandList[0].Val->removeUser(N);
1879 Op.Val->addUser(N);
1880 N->OperandList[0] = Op;
1881
1882 // If this gets put into a CSE map, add it.
1883 if (NewSlot) *NewSlot = N;
1884 return InN;
1885}
1886
1887SDOperand SelectionDAG::
1888UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1889 SDNode *N = InN.Val;
1890 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1891
1892 // Check to see if there is no change.
1893 bool AnyChange = false;
1894 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1895 return InN; // No operands changed, just return the input node.
1896
1897 // See if the modified node already exists.
1898 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1899 if (NewSlot && *NewSlot)
1900 return SDOperand(*NewSlot, InN.ResNo);
1901
1902 // Nope it doesn't. Remove the node from it's current place in the maps.
1903 if (NewSlot)
1904 RemoveNodeFromCSEMaps(N);
1905
1906 // Now we update the operands.
1907 if (N->OperandList[0] != Op1) {
1908 N->OperandList[0].Val->removeUser(N);
1909 Op1.Val->addUser(N);
1910 N->OperandList[0] = Op1;
1911 }
1912 if (N->OperandList[1] != Op2) {
1913 N->OperandList[1].Val->removeUser(N);
1914 Op2.Val->addUser(N);
1915 N->OperandList[1] = Op2;
1916 }
1917
1918 // If this gets put into a CSE map, add it.
1919 if (NewSlot) *NewSlot = N;
1920 return InN;
1921}
1922
1923SDOperand SelectionDAG::
1924UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1925 std::vector<SDOperand> Ops;
1926 Ops.push_back(Op1);
1927 Ops.push_back(Op2);
1928 Ops.push_back(Op3);
1929 return UpdateNodeOperands(N, Ops);
1930}
1931
1932SDOperand SelectionDAG::
1933UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1934 SDOperand Op3, SDOperand Op4) {
1935 std::vector<SDOperand> Ops;
1936 Ops.push_back(Op1);
1937 Ops.push_back(Op2);
1938 Ops.push_back(Op3);
1939 Ops.push_back(Op4);
1940 return UpdateNodeOperands(N, Ops);
1941}
1942
1943SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001944UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1945 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1946 std::vector<SDOperand> Ops;
1947 Ops.push_back(Op1);
1948 Ops.push_back(Op2);
1949 Ops.push_back(Op3);
1950 Ops.push_back(Op4);
1951 Ops.push_back(Op5);
1952 return UpdateNodeOperands(N, Ops);
1953}
1954
1955
1956SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001957UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1958 SDNode *N = InN.Val;
1959 assert(N->getNumOperands() == Ops.size() &&
1960 "Update with wrong number of operands");
1961
1962 // Check to see if there is no change.
1963 unsigned NumOps = Ops.size();
1964 bool AnyChange = false;
1965 for (unsigned i = 0; i != NumOps; ++i) {
1966 if (Ops[i] != N->getOperand(i)) {
1967 AnyChange = true;
1968 break;
1969 }
1970 }
1971
1972 // No operands changed, just return the input node.
1973 if (!AnyChange) return InN;
1974
1975 // See if the modified node already exists.
1976 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1977 if (NewSlot && *NewSlot)
1978 return SDOperand(*NewSlot, InN.ResNo);
1979
1980 // Nope it doesn't. Remove the node from it's current place in the maps.
1981 if (NewSlot)
1982 RemoveNodeFromCSEMaps(N);
1983
1984 // Now we update the operands.
1985 for (unsigned i = 0; i != NumOps; ++i) {
1986 if (N->OperandList[i] != Ops[i]) {
1987 N->OperandList[i].Val->removeUser(N);
1988 Ops[i].Val->addUser(N);
1989 N->OperandList[i] = Ops[i];
1990 }
1991 }
1992
1993 // If this gets put into a CSE map, add it.
1994 if (NewSlot) *NewSlot = N;
1995 return InN;
1996}
1997
1998
1999
Chris Lattner149c58c2005-08-16 18:17:10 +00002000
2001/// SelectNodeTo - These are used for target selectors to *mutate* the
2002/// specified node to have the specified return type, Target opcode, and
2003/// operands. Note that target opcodes are stored as
2004/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002005///
2006/// Note that SelectNodeTo returns the resultant node. If there is already a
2007/// node of the specified opcode and operands, it returns that node instead of
2008/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00002009SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2010 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002011 // If an identical node already exists, use it.
2012 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
2013 if (ON) return SDOperand(ON, 0);
2014
Chris Lattner7651fa42005-08-24 23:00:29 +00002015 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002016
Chris Lattner7651fa42005-08-24 23:00:29 +00002017 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2018 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002019
2020 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002021 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00002022}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002023
Chris Lattnereb19e402005-11-30 22:45:14 +00002024SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2025 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026 // If an identical node already exists, use it.
2027 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2028 std::make_pair(Op1, VT))];
2029 if (ON) return SDOperand(ON, 0);
2030
Chris Lattner149c58c2005-08-16 18:17:10 +00002031 RemoveNodeFromCSEMaps(N);
2032 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2033 N->setValueTypes(VT);
2034 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002035
2036 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002037 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002038}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039
Chris Lattnereb19e402005-11-30 22:45:14 +00002040SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2041 MVT::ValueType VT, SDOperand Op1,
2042 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002043 // If an identical node already exists, use it.
2044 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2045 std::make_pair(Op1, Op2))];
2046 if (ON) return SDOperand(ON, 0);
2047
Chris Lattner149c58c2005-08-16 18:17:10 +00002048 RemoveNodeFromCSEMaps(N);
2049 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2050 N->setValueTypes(VT);
2051 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002052
2053 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002054 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002055}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002056
Chris Lattnereb19e402005-11-30 22:45:14 +00002057SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2058 MVT::ValueType VT, SDOperand Op1,
2059 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002060 // If an identical node already exists, use it.
2061 std::vector<SDOperand> OpList;
2062 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2063 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2064 std::make_pair(VT, OpList))];
2065 if (ON) return SDOperand(ON, 0);
2066
Chris Lattner149c58c2005-08-16 18:17:10 +00002067 RemoveNodeFromCSEMaps(N);
2068 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2069 N->setValueTypes(VT);
2070 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002071
2072 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002073 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002074}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002075
Chris Lattnereb19e402005-11-30 22:45:14 +00002076SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2077 MVT::ValueType VT, SDOperand Op1,
2078 SDOperand Op2, SDOperand Op3,
2079 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002080 // If an identical node already exists, use it.
2081 std::vector<SDOperand> OpList;
2082 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2083 OpList.push_back(Op4);
2084 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2085 std::make_pair(VT, OpList))];
2086 if (ON) return SDOperand(ON, 0);
2087
Nate Begeman294a0a12005-08-18 07:30:15 +00002088 RemoveNodeFromCSEMaps(N);
2089 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2090 N->setValueTypes(VT);
2091 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002092
2093 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002094 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002095}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002096
Chris Lattnereb19e402005-11-30 22:45:14 +00002097SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2098 MVT::ValueType VT, SDOperand Op1,
2099 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2100 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002101 // If an identical node already exists, use it.
2102 std::vector<SDOperand> OpList;
2103 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2104 OpList.push_back(Op4); OpList.push_back(Op5);
2105 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2106 std::make_pair(VT, OpList))];
2107 if (ON) return SDOperand(ON, 0);
2108
Chris Lattner6b09a292005-08-21 18:49:33 +00002109 RemoveNodeFromCSEMaps(N);
2110 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2111 N->setValueTypes(VT);
2112 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002113
2114 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002115 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002116}
Chris Lattner149c58c2005-08-16 18:17:10 +00002117
Chris Lattnereb19e402005-11-30 22:45:14 +00002118SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2119 MVT::ValueType VT, SDOperand Op1,
2120 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2121 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002122 // If an identical node already exists, use it.
2123 std::vector<SDOperand> OpList;
2124 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2125 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2126 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2127 std::make_pair(VT, OpList))];
2128 if (ON) return SDOperand(ON, 0);
2129
Evan Cheng61ca74b2005-11-30 02:04:11 +00002130 RemoveNodeFromCSEMaps(N);
2131 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2132 N->setValueTypes(VT);
2133 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002134
2135 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002136 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002137}
2138
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002139SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2140 MVT::ValueType VT, SDOperand Op1,
2141 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2142 SDOperand Op5, SDOperand Op6,
2143 SDOperand Op7) {
2144 // If an identical node already exists, use it.
2145 std::vector<SDOperand> OpList;
2146 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2147 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2148 OpList.push_back(Op7);
2149 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2150 std::make_pair(VT, OpList))];
2151 if (ON) return SDOperand(ON, 0);
2152
2153 RemoveNodeFromCSEMaps(N);
2154 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2155 N->setValueTypes(VT);
2156 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2157
2158 ON = N; // Memoize the new node.
2159 return SDOperand(N, 0);
2160}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002161SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2162 MVT::ValueType VT, SDOperand Op1,
2163 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2164 SDOperand Op5, SDOperand Op6,
2165 SDOperand Op7, SDOperand Op8) {
2166 // If an identical node already exists, use it.
2167 std::vector<SDOperand> OpList;
2168 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2169 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2170 OpList.push_back(Op7); OpList.push_back(Op8);
2171 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2172 std::make_pair(VT, OpList))];
2173 if (ON) return SDOperand(ON, 0);
2174
2175 RemoveNodeFromCSEMaps(N);
2176 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2177 N->setValueTypes(VT);
2178 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2179
2180 ON = N; // Memoize the new node.
2181 return SDOperand(N, 0);
2182}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002183
Chris Lattnereb19e402005-11-30 22:45:14 +00002184SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2185 MVT::ValueType VT1, MVT::ValueType VT2,
2186 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002187 // If an identical node already exists, use it.
2188 std::vector<SDOperand> OpList;
2189 OpList.push_back(Op1); OpList.push_back(Op2);
2190 std::vector<MVT::ValueType> VTList;
2191 VTList.push_back(VT1); VTList.push_back(VT2);
2192 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2193 std::make_pair(VTList, OpList))];
2194 if (ON) return SDOperand(ON, 0);
2195
Chris Lattner0fb094f2005-11-19 01:44:53 +00002196 RemoveNodeFromCSEMaps(N);
2197 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2198 setNodeValueTypes(N, VT1, VT2);
2199 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002200
2201 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002202 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002203}
2204
Chris Lattnereb19e402005-11-30 22:45:14 +00002205SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2206 MVT::ValueType VT1, MVT::ValueType VT2,
2207 SDOperand Op1, SDOperand Op2,
2208 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002209 // If an identical node already exists, use it.
2210 std::vector<SDOperand> OpList;
2211 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2212 std::vector<MVT::ValueType> VTList;
2213 VTList.push_back(VT1); VTList.push_back(VT2);
2214 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2215 std::make_pair(VTList, OpList))];
2216 if (ON) return SDOperand(ON, 0);
2217
Chris Lattner0fb094f2005-11-19 01:44:53 +00002218 RemoveNodeFromCSEMaps(N);
2219 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2220 setNodeValueTypes(N, VT1, VT2);
2221 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002222
2223 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002224 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002225}
2226
Chris Lattnereb19e402005-11-30 22:45:14 +00002227SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2228 MVT::ValueType VT1, MVT::ValueType VT2,
2229 SDOperand Op1, SDOperand Op2,
2230 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002231 // If an identical node already exists, use it.
2232 std::vector<SDOperand> OpList;
2233 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2234 OpList.push_back(Op4);
2235 std::vector<MVT::ValueType> VTList;
2236 VTList.push_back(VT1); VTList.push_back(VT2);
2237 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2238 std::make_pair(VTList, OpList))];
2239 if (ON) return SDOperand(ON, 0);
2240
Chris Lattner0fb094f2005-11-19 01:44:53 +00002241 RemoveNodeFromCSEMaps(N);
2242 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2243 setNodeValueTypes(N, VT1, VT2);
2244 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002245
2246 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002247 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002248}
2249
Chris Lattnereb19e402005-11-30 22:45:14 +00002250SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2251 MVT::ValueType VT1, MVT::ValueType VT2,
2252 SDOperand Op1, SDOperand Op2,
2253 SDOperand Op3, SDOperand Op4,
2254 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002255 // If an identical node already exists, use it.
2256 std::vector<SDOperand> OpList;
2257 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2258 OpList.push_back(Op4); OpList.push_back(Op5);
2259 std::vector<MVT::ValueType> VTList;
2260 VTList.push_back(VT1); VTList.push_back(VT2);
2261 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2262 std::make_pair(VTList, OpList))];
2263 if (ON) return SDOperand(ON, 0);
2264
Chris Lattner0fb094f2005-11-19 01:44:53 +00002265 RemoveNodeFromCSEMaps(N);
2266 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2267 setNodeValueTypes(N, VT1, VT2);
2268 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002269
2270 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002271 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002272}
2273
Evan Cheng6ae46c42006-02-09 07:15:23 +00002274/// getTargetNode - These are used for target selectors to create a new node
2275/// with specified return type(s), target opcode, and operands.
2276///
2277/// Note that getTargetNode returns the resultant node. If there is already a
2278/// node of the specified opcode and operands, it returns that node instead of
2279/// the current one.
2280SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2281 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2282}
2283SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2284 SDOperand Op1) {
2285 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2286}
2287SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2288 SDOperand Op1, SDOperand Op2) {
2289 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2290}
2291SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2292 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2293 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2294}
2295SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2296 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2297 SDOperand Op4) {
2298 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2299}
2300SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2301 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2302 SDOperand Op4, SDOperand Op5) {
2303 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2304}
2305SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2306 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2307 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2308 std::vector<SDOperand> Ops;
2309 Ops.reserve(6);
2310 Ops.push_back(Op1);
2311 Ops.push_back(Op2);
2312 Ops.push_back(Op3);
2313 Ops.push_back(Op4);
2314 Ops.push_back(Op5);
2315 Ops.push_back(Op6);
2316 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2317}
2318SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2319 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2320 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2321 SDOperand Op7) {
2322 std::vector<SDOperand> Ops;
2323 Ops.reserve(7);
2324 Ops.push_back(Op1);
2325 Ops.push_back(Op2);
2326 Ops.push_back(Op3);
2327 Ops.push_back(Op4);
2328 Ops.push_back(Op5);
2329 Ops.push_back(Op6);
2330 Ops.push_back(Op7);
2331 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2332}
2333SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2334 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2335 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2336 SDOperand Op7, SDOperand Op8) {
2337 std::vector<SDOperand> Ops;
2338 Ops.reserve(8);
2339 Ops.push_back(Op1);
2340 Ops.push_back(Op2);
2341 Ops.push_back(Op3);
2342 Ops.push_back(Op4);
2343 Ops.push_back(Op5);
2344 Ops.push_back(Op6);
2345 Ops.push_back(Op7);
2346 Ops.push_back(Op8);
2347 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2348}
2349SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2350 std::vector<SDOperand> &Ops) {
2351 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2352}
2353SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2354 MVT::ValueType VT2, SDOperand Op1) {
2355 std::vector<MVT::ValueType> ResultTys;
2356 ResultTys.push_back(VT1);
2357 ResultTys.push_back(VT2);
2358 std::vector<SDOperand> Ops;
2359 Ops.push_back(Op1);
2360 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2361}
2362SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2363 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2364 std::vector<MVT::ValueType> ResultTys;
2365 ResultTys.push_back(VT1);
2366 ResultTys.push_back(VT2);
2367 std::vector<SDOperand> Ops;
2368 Ops.push_back(Op1);
2369 Ops.push_back(Op2);
2370 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2371}
2372SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2373 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2374 SDOperand Op3) {
2375 std::vector<MVT::ValueType> ResultTys;
2376 ResultTys.push_back(VT1);
2377 ResultTys.push_back(VT2);
2378 std::vector<SDOperand> Ops;
2379 Ops.push_back(Op1);
2380 Ops.push_back(Op2);
2381 Ops.push_back(Op3);
2382 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2383}
2384SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2385 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2386 SDOperand Op3, SDOperand Op4) {
2387 std::vector<MVT::ValueType> ResultTys;
2388 ResultTys.push_back(VT1);
2389 ResultTys.push_back(VT2);
2390 std::vector<SDOperand> Ops;
2391 Ops.push_back(Op1);
2392 Ops.push_back(Op2);
2393 Ops.push_back(Op3);
2394 Ops.push_back(Op4);
2395 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2396}
2397SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2398 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2399 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2400 std::vector<MVT::ValueType> ResultTys;
2401 ResultTys.push_back(VT1);
2402 ResultTys.push_back(VT2);
2403 std::vector<SDOperand> Ops;
2404 Ops.push_back(Op1);
2405 Ops.push_back(Op2);
2406 Ops.push_back(Op3);
2407 Ops.push_back(Op4);
2408 Ops.push_back(Op5);
2409 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2410}
2411SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2412 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2413 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2414 SDOperand Op6) {
2415 std::vector<MVT::ValueType> ResultTys;
2416 ResultTys.push_back(VT1);
2417 ResultTys.push_back(VT2);
2418 std::vector<SDOperand> Ops;
2419 Ops.push_back(Op1);
2420 Ops.push_back(Op2);
2421 Ops.push_back(Op3);
2422 Ops.push_back(Op4);
2423 Ops.push_back(Op5);
2424 Ops.push_back(Op6);
2425 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2426}
2427SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2428 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2429 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2430 SDOperand Op6, SDOperand Op7) {
2431 std::vector<MVT::ValueType> ResultTys;
2432 ResultTys.push_back(VT1);
2433 ResultTys.push_back(VT2);
2434 std::vector<SDOperand> Ops;
2435 Ops.push_back(Op1);
2436 Ops.push_back(Op2);
2437 Ops.push_back(Op3);
2438 Ops.push_back(Op4);
2439 Ops.push_back(Op5);
2440 Ops.push_back(Op6);
2441 Ops.push_back(Op7);
2442 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2443}
2444SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2445 MVT::ValueType VT2, MVT::ValueType VT3,
2446 SDOperand Op1, SDOperand Op2) {
2447 std::vector<MVT::ValueType> ResultTys;
2448 ResultTys.push_back(VT1);
2449 ResultTys.push_back(VT2);
2450 ResultTys.push_back(VT3);
2451 std::vector<SDOperand> Ops;
2452 Ops.push_back(Op1);
2453 Ops.push_back(Op2);
2454 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2455}
2456SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2457 MVT::ValueType VT2, MVT::ValueType VT3,
2458 SDOperand Op1, SDOperand Op2,
2459 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2460 std::vector<MVT::ValueType> ResultTys;
2461 ResultTys.push_back(VT1);
2462 ResultTys.push_back(VT2);
2463 ResultTys.push_back(VT3);
2464 std::vector<SDOperand> Ops;
2465 Ops.push_back(Op1);
2466 Ops.push_back(Op2);
2467 Ops.push_back(Op3);
2468 Ops.push_back(Op4);
2469 Ops.push_back(Op5);
2470 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2471}
2472SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2473 MVT::ValueType VT2, MVT::ValueType VT3,
2474 SDOperand Op1, SDOperand Op2,
2475 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2476 SDOperand Op6) {
2477 std::vector<MVT::ValueType> ResultTys;
2478 ResultTys.push_back(VT1);
2479 ResultTys.push_back(VT2);
2480 ResultTys.push_back(VT3);
2481 std::vector<SDOperand> Ops;
2482 Ops.push_back(Op1);
2483 Ops.push_back(Op2);
2484 Ops.push_back(Op3);
2485 Ops.push_back(Op4);
2486 Ops.push_back(Op5);
2487 Ops.push_back(Op6);
2488 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2489}
2490SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2491 MVT::ValueType VT2, MVT::ValueType VT3,
2492 SDOperand Op1, SDOperand Op2,
2493 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2494 SDOperand Op6, SDOperand Op7) {
2495 std::vector<MVT::ValueType> ResultTys;
2496 ResultTys.push_back(VT1);
2497 ResultTys.push_back(VT2);
2498 ResultTys.push_back(VT3);
2499 std::vector<SDOperand> Ops;
2500 Ops.push_back(Op1);
2501 Ops.push_back(Op2);
2502 Ops.push_back(Op3);
2503 Ops.push_back(Op4);
2504 Ops.push_back(Op5);
2505 Ops.push_back(Op6);
2506 Ops.push_back(Op7);
2507 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2508}
2509SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2510 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2511 std::vector<MVT::ValueType> ResultTys;
2512 ResultTys.push_back(VT1);
2513 ResultTys.push_back(VT2);
2514 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2515}
2516
Chris Lattner0fb094f2005-11-19 01:44:53 +00002517// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002518/// This can cause recursive merging of nodes in the DAG.
2519///
Chris Lattner8b52f212005-08-26 18:36:28 +00002520/// This version assumes From/To have a single result value.
2521///
Chris Lattner1e111c72005-09-07 05:37:01 +00002522void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2523 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002524 SDNode *From = FromN.Val, *To = ToN.Val;
2525 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2526 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002527 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002528
Chris Lattner8b8749f2005-08-17 19:00:20 +00002529 while (!From->use_empty()) {
2530 // Process users until they are all gone.
2531 SDNode *U = *From->use_begin();
2532
2533 // This node is about to morph, remove its old self from the CSE maps.
2534 RemoveNodeFromCSEMaps(U);
2535
Chris Lattner65113b22005-11-08 22:07:03 +00002536 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2537 I != E; ++I)
2538 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002539 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002540 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002541 To->addUser(U);
2542 }
2543
2544 // Now that we have modified U, add it back to the CSE maps. If it already
2545 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002546 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2547 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002548 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002549 if (Deleted) Deleted->push_back(U);
2550 DeleteNodeNotInCSEMaps(U);
2551 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002552 }
2553}
2554
2555/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2556/// This can cause recursive merging of nodes in the DAG.
2557///
2558/// This version assumes From/To have matching types and numbers of result
2559/// values.
2560///
Chris Lattner1e111c72005-09-07 05:37:01 +00002561void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2562 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002563 assert(From != To && "Cannot replace uses of with self");
2564 assert(From->getNumValues() == To->getNumValues() &&
2565 "Cannot use this version of ReplaceAllUsesWith!");
2566 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002567 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002568 return;
2569 }
2570
2571 while (!From->use_empty()) {
2572 // Process users until they are all gone.
2573 SDNode *U = *From->use_begin();
2574
2575 // This node is about to morph, remove its old self from the CSE maps.
2576 RemoveNodeFromCSEMaps(U);
2577
Chris Lattner65113b22005-11-08 22:07:03 +00002578 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2579 I != E; ++I)
2580 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002581 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002582 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002583 To->addUser(U);
2584 }
2585
2586 // Now that we have modified U, add it back to the CSE maps. If it already
2587 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002588 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2589 ReplaceAllUsesWith(U, Existing, Deleted);
2590 // U is now dead.
2591 if (Deleted) Deleted->push_back(U);
2592 DeleteNodeNotInCSEMaps(U);
2593 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002594 }
2595}
2596
Chris Lattner8b52f212005-08-26 18:36:28 +00002597/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2598/// This can cause recursive merging of nodes in the DAG.
2599///
2600/// This version can replace From with any result values. To must match the
2601/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002602void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002603 const std::vector<SDOperand> &To,
2604 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002605 assert(From->getNumValues() == To.size() &&
2606 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002607 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002608 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002609 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002610 return;
2611 }
2612
2613 while (!From->use_empty()) {
2614 // Process users until they are all gone.
2615 SDNode *U = *From->use_begin();
2616
2617 // This node is about to morph, remove its old self from the CSE maps.
2618 RemoveNodeFromCSEMaps(U);
2619
Chris Lattner65113b22005-11-08 22:07:03 +00002620 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2621 I != E; ++I)
2622 if (I->Val == From) {
2623 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002624 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002625 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002626 ToOp.Val->addUser(U);
2627 }
2628
2629 // Now that we have modified U, add it back to the CSE maps. If it already
2630 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002631 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2632 ReplaceAllUsesWith(U, Existing, Deleted);
2633 // U is now dead.
2634 if (Deleted) Deleted->push_back(U);
2635 DeleteNodeNotInCSEMaps(U);
2636 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002637 }
2638}
2639
Chris Lattner012f2412006-02-17 21:58:01 +00002640/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2641/// uses of other values produced by From.Val alone. The Deleted vector is
2642/// handled the same was as for ReplaceAllUsesWith.
2643void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2644 std::vector<SDNode*> &Deleted) {
2645 assert(From != To && "Cannot replace a value with itself");
2646 // Handle the simple, trivial, case efficiently.
2647 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2648 ReplaceAllUsesWith(From, To, &Deleted);
2649 return;
2650 }
2651
2652 // Get all of the users in a nice, deterministically ordered, uniqued set.
2653 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2654
2655 while (!Users.empty()) {
2656 // We know that this user uses some value of From. If it is the right
2657 // value, update it.
2658 SDNode *User = Users.back();
2659 Users.pop_back();
2660
2661 for (SDOperand *Op = User->OperandList,
2662 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2663 if (*Op == From) {
2664 // Okay, we know this user needs to be updated. Remove its old self
2665 // from the CSE maps.
2666 RemoveNodeFromCSEMaps(User);
2667
2668 // Update all operands that match "From".
2669 for (; Op != E; ++Op) {
2670 if (*Op == From) {
2671 From.Val->removeUser(User);
2672 *Op = To;
2673 To.Val->addUser(User);
2674 }
2675 }
2676
2677 // Now that we have modified User, add it back to the CSE maps. If it
2678 // already exists there, recursively merge the results together.
2679 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2680 unsigned NumDeleted = Deleted.size();
2681 ReplaceAllUsesWith(User, Existing, &Deleted);
2682
2683 // User is now dead.
2684 Deleted.push_back(User);
2685 DeleteNodeNotInCSEMaps(User);
2686
2687 // We have to be careful here, because ReplaceAllUsesWith could have
2688 // deleted a user of From, which means there may be dangling pointers
2689 // in the "Users" setvector. Scan over the deleted node pointers and
2690 // remove them from the setvector.
2691 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2692 Users.remove(Deleted[i]);
2693 }
2694 break; // Exit the operand scanning loop.
2695 }
2696 }
2697 }
2698}
2699
Chris Lattner7b2880c2005-08-24 22:44:39 +00002700
Evan Cheng091cba12006-07-27 06:39:06 +00002701/// AssignNodeIds - Assign a unique node id for each node in the DAG. It returns
2702/// the maximum id.
2703int SelectionDAG::AssignNodeIds() {
2704 int Id = 0;
2705 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2706 SDNode *N = I;
2707 N->setNodeId(Id++);
2708 }
2709 return Id;
2710}
2711
2712
Jim Laskey58b968b2005-08-17 20:08:02 +00002713//===----------------------------------------------------------------------===//
2714// SDNode Class
2715//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002716
Chris Lattner917d2c92006-07-19 00:00:37 +00002717// Out-of-line virtual method to give class a home.
2718void SDNode::ANCHOR() {
2719}
2720
Chris Lattnera3255112005-11-08 23:30:28 +00002721
2722/// getValueTypeList - Return a pointer to the specified value type.
2723///
2724MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2725 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2726 VTs[VT] = VT;
2727 return &VTs[VT];
2728}
2729
Chris Lattner5c884562005-01-12 18:37:47 +00002730/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2731/// indicated value. This method ignores uses of other values defined by this
2732/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002733bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002734 assert(Value < getNumValues() && "Bad value!");
2735
2736 // If there is only one value, this is easy.
2737 if (getNumValues() == 1)
2738 return use_size() == NUses;
2739 if (Uses.size() < NUses) return false;
2740
Evan Cheng4ee62112006-02-05 06:29:23 +00002741 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002742
2743 std::set<SDNode*> UsersHandled;
2744
Evan Cheng4ee62112006-02-05 06:29:23 +00002745 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002746 UI != E; ++UI) {
2747 SDNode *User = *UI;
2748 if (User->getNumOperands() == 1 ||
2749 UsersHandled.insert(User).second) // First time we've seen this?
2750 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2751 if (User->getOperand(i) == TheValue) {
2752 if (NUses == 0)
2753 return false; // too many uses
2754 --NUses;
2755 }
2756 }
2757
2758 // Found exactly the right number of uses?
2759 return NUses == 0;
2760}
2761
2762
Evan Cheng4ee62112006-02-05 06:29:23 +00002763// isOnlyUse - Return true if this node is the only use of N.
2764bool SDNode::isOnlyUse(SDNode *N) const {
2765 bool Seen = false;
2766 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2767 SDNode *User = *I;
2768 if (User == this)
2769 Seen = true;
2770 else
2771 return false;
2772 }
2773
2774 return Seen;
2775}
2776
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002777// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002778bool SDOperand::isOperand(SDNode *N) const {
2779 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2780 if (*this == N->getOperand(i))
2781 return true;
2782 return false;
2783}
2784
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002785bool SDNode::isOperand(SDNode *N) const {
2786 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2787 if (this == N->OperandList[i].Val)
2788 return true;
2789 return false;
2790}
Evan Cheng4ee62112006-02-05 06:29:23 +00002791
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002792const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002793 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002794 default:
2795 if (getOpcode() < ISD::BUILTIN_OP_END)
2796 return "<<Unknown DAG Node>>";
2797 else {
Evan Cheng72261582005-12-20 06:22:03 +00002798 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002799 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002800 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2801 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002802
Evan Cheng72261582005-12-20 06:22:03 +00002803 TargetLowering &TLI = G->getTargetLoweringInfo();
2804 const char *Name =
2805 TLI.getTargetNodeName(getOpcode());
2806 if (Name) return Name;
2807 }
2808
2809 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002810 }
2811
Andrew Lenharth95762122005-03-31 21:24:06 +00002812 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002813 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002814 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002815 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002816 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002817 case ISD::AssertSext: return "AssertSext";
2818 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002819
2820 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002821 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002822 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002823 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002824
2825 case ISD::Constant: return "Constant";
2826 case ISD::ConstantFP: return "ConstantFP";
2827 case ISD::GlobalAddress: return "GlobalAddress";
2828 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002829 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002830 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002831 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002832 case ISD::INTRINSIC_WO_CHAIN: {
2833 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2834 return Intrinsic::getName((Intrinsic::ID)IID);
2835 }
2836 case ISD::INTRINSIC_VOID:
2837 case ISD::INTRINSIC_W_CHAIN: {
2838 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002839 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002840 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002841
Chris Lattnerb2827b02006-03-19 00:52:58 +00002842 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002843 case ISD::TargetConstant: return "TargetConstant";
2844 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002845 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2846 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002847 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002848 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002849 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002850
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002851 case ISD::CopyToReg: return "CopyToReg";
2852 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002853 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002854 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002855 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002856 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002857 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002858 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002859
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002860 // Unary operators
2861 case ISD::FABS: return "fabs";
2862 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002863 case ISD::FSQRT: return "fsqrt";
2864 case ISD::FSIN: return "fsin";
2865 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002866
2867 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002868 case ISD::ADD: return "add";
2869 case ISD::SUB: return "sub";
2870 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002871 case ISD::MULHU: return "mulhu";
2872 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002873 case ISD::SDIV: return "sdiv";
2874 case ISD::UDIV: return "udiv";
2875 case ISD::SREM: return "srem";
2876 case ISD::UREM: return "urem";
2877 case ISD::AND: return "and";
2878 case ISD::OR: return "or";
2879 case ISD::XOR: return "xor";
2880 case ISD::SHL: return "shl";
2881 case ISD::SRA: return "sra";
2882 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002883 case ISD::ROTL: return "rotl";
2884 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002885 case ISD::FADD: return "fadd";
2886 case ISD::FSUB: return "fsub";
2887 case ISD::FMUL: return "fmul";
2888 case ISD::FDIV: return "fdiv";
2889 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002890 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002891 case ISD::VADD: return "vadd";
2892 case ISD::VSUB: return "vsub";
2893 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002894 case ISD::VSDIV: return "vsdiv";
2895 case ISD::VUDIV: return "vudiv";
2896 case ISD::VAND: return "vand";
2897 case ISD::VOR: return "vor";
2898 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002899
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002900 case ISD::SETCC: return "setcc";
2901 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002902 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002903 case ISD::VSELECT: return "vselect";
2904 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2905 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2906 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002907 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002908 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2909 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2910 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2911 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2912 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002913 case ISD::ADDC: return "addc";
2914 case ISD::ADDE: return "adde";
2915 case ISD::SUBC: return "subc";
2916 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002917 case ISD::SHL_PARTS: return "shl_parts";
2918 case ISD::SRA_PARTS: return "sra_parts";
2919 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002920
Chris Lattner7f644642005-04-28 21:44:03 +00002921 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002922 case ISD::SIGN_EXTEND: return "sign_extend";
2923 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002924 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002925 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002926 case ISD::TRUNCATE: return "truncate";
2927 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002928 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002929 case ISD::FP_EXTEND: return "fp_extend";
2930
2931 case ISD::SINT_TO_FP: return "sint_to_fp";
2932 case ISD::UINT_TO_FP: return "uint_to_fp";
2933 case ISD::FP_TO_SINT: return "fp_to_sint";
2934 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002935 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002936
2937 // Control flow instructions
2938 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002939 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002940 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002941 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002942 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002943 case ISD::CALLSEQ_START: return "callseq_start";
2944 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002945
2946 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002947 case ISD::LOAD: return "load";
2948 case ISD::STORE: return "store";
2949 case ISD::VLOAD: return "vload";
2950 case ISD::EXTLOAD: return "extload";
2951 case ISD::SEXTLOAD: return "sextload";
2952 case ISD::ZEXTLOAD: return "zextload";
2953 case ISD::TRUNCSTORE: return "truncstore";
2954 case ISD::VAARG: return "vaarg";
2955 case ISD::VACOPY: return "vacopy";
2956 case ISD::VAEND: return "vaend";
2957 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002958 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002959 case ISD::EXTRACT_ELEMENT: return "extract_element";
2960 case ISD::BUILD_PAIR: return "build_pair";
2961 case ISD::STACKSAVE: return "stacksave";
2962 case ISD::STACKRESTORE: return "stackrestore";
2963
2964 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002965 case ISD::MEMSET: return "memset";
2966 case ISD::MEMCPY: return "memcpy";
2967 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002968
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002969 // Bit manipulation
2970 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002971 case ISD::CTPOP: return "ctpop";
2972 case ISD::CTTZ: return "cttz";
2973 case ISD::CTLZ: return "ctlz";
2974
Chris Lattner36ce6912005-11-29 06:21:05 +00002975 // Debug info
2976 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002977 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002978 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002979
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002980 case ISD::CONDCODE:
2981 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002982 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002983 case ISD::SETOEQ: return "setoeq";
2984 case ISD::SETOGT: return "setogt";
2985 case ISD::SETOGE: return "setoge";
2986 case ISD::SETOLT: return "setolt";
2987 case ISD::SETOLE: return "setole";
2988 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002989
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002990 case ISD::SETO: return "seto";
2991 case ISD::SETUO: return "setuo";
2992 case ISD::SETUEQ: return "setue";
2993 case ISD::SETUGT: return "setugt";
2994 case ISD::SETUGE: return "setuge";
2995 case ISD::SETULT: return "setult";
2996 case ISD::SETULE: return "setule";
2997 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002998
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002999 case ISD::SETEQ: return "seteq";
3000 case ISD::SETGT: return "setgt";
3001 case ISD::SETGE: return "setge";
3002 case ISD::SETLT: return "setlt";
3003 case ISD::SETLE: return "setle";
3004 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003005 }
3006 }
3007}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003008
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003009void SDNode::dump() const { dump(0); }
3010void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003011 std::cerr << (void*)this << ": ";
3012
3013 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3014 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003015 if (getValueType(i) == MVT::Other)
3016 std::cerr << "ch";
3017 else
3018 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003019 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003020 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003021
3022 std::cerr << " ";
3023 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3024 if (i) std::cerr << ", ";
3025 std::cerr << (void*)getOperand(i).Val;
3026 if (unsigned RN = getOperand(i).ResNo)
3027 std::cerr << ":" << RN;
3028 }
3029
3030 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3031 std::cerr << "<" << CSDN->getValue() << ">";
3032 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3033 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003034 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003035 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003036 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003037 std::cerr << "<";
3038 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003039 if (offset > 0)
3040 std::cerr << " + " << offset;
3041 else
3042 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003043 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003044 std::cerr << "<" << FIDN->getIndex() << ">";
3045 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003046 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003047 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003048 if (offset > 0)
3049 std::cerr << " + " << offset;
3050 else
3051 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003052 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003053 std::cerr << "<";
3054 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3055 if (LBB)
3056 std::cerr << LBB->getName() << " ";
3057 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003058 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003059 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003060 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3061 } else {
3062 std::cerr << " #" << R->getReg();
3063 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003064 } else if (const ExternalSymbolSDNode *ES =
3065 dyn_cast<ExternalSymbolSDNode>(this)) {
3066 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003067 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3068 if (M->getValue())
3069 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3070 else
3071 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003072 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3073 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003074 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003075}
3076
Chris Lattnerde202b32005-11-09 23:47:37 +00003077static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003078 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3079 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003080 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003081 else
3082 std::cerr << "\n" << std::string(indent+2, ' ')
3083 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003084
Chris Lattnerea946cd2005-01-09 20:38:33 +00003085
3086 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003087 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003088}
3089
Chris Lattnerc3aae252005-01-07 07:46:32 +00003090void SelectionDAG::dump() const {
3091 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003092 std::vector<const SDNode*> Nodes;
3093 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3094 I != E; ++I)
3095 Nodes.push_back(I);
3096
Chris Lattner49d24712005-01-09 20:26:36 +00003097 std::sort(Nodes.begin(), Nodes.end());
3098
3099 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003100 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003101 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003102 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003103
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003104 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003105
Chris Lattnerc3aae252005-01-07 07:46:32 +00003106 std::cerr << "\n\n";
3107}
3108
Evan Chengfae9f1c2006-02-09 22:11:03 +00003109/// InsertISelMapEntry - A helper function to insert a key / element pair
3110/// into a SDOperand to SDOperand map. This is added to avoid the map
3111/// insertion operator from being inlined.
3112void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3113 SDNode *Key, unsigned KeyResNo,
3114 SDNode *Element, unsigned ElementResNo) {
3115 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3116 SDOperand(Element, ElementResNo)));
3117}
Evan Cheng322812e2006-06-29 23:57:05 +00003118
3119/// InsertInFlightSetEntry - A helper function to insert a SDNode* to a
3120/// SDNode* set. This is added to avoid the set insertion operator from being
3121/// inlined.
3122void SelectionDAG::InsertInFlightSetEntry(std::set<SDNode*> &Set, SDNode *N) {
3123 Set.insert(N);
3124}
3125
3126/// RemoveInFlightSetEntry - A helper function to remove a SDNode* from a
3127/// SDNode* set. This is added to avoid the set removal operator from being
3128/// inlined.
3129void SelectionDAG::RemoveInFlightSetEntry(std::set<SDNode*> &Set, SDNode *N) {
3130 Set.erase(N);
3131}