blob: 154a6ca107a2838ff0dd8889364ef174418d696b [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"
Chris Lattner190a4182006-08-04 17:45:20 +000026#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000027#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000028#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000029#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000030#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000031#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner5cdcc582005-01-09 20:52:51 +000034static bool isCommutativeBinOp(unsigned Opcode) {
35 switch (Opcode) {
36 case ISD::ADD:
37 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000038 case ISD::MULHU:
39 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000040 case ISD::FADD:
41 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000042 case ISD::AND:
43 case ISD::OR:
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need commutative info for user ops!
46 }
47}
48
Chris Lattner5cdcc582005-01-09 20:52:51 +000049// isInvertibleForFree - Return true if there is no cost to emitting the logical
50// inverse of this node.
51static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000053 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000054 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000055 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000056}
57
Jim Laskey58b968b2005-08-17 20:08:02 +000058//===----------------------------------------------------------------------===//
59// ConstantFPSDNode Class
60//===----------------------------------------------------------------------===//
61
62/// isExactlyValue - We don't rely on operator== working on double values, as
63/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
64/// As such, this method can be used to do an exact bit-for-bit comparison of
65/// two floating point values.
66bool ConstantFPSDNode::isExactlyValue(double V) const {
67 return DoubleToBits(V) == DoubleToBits(Value);
68}
69
70//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000071// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000072//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000073
Evan Chenga8df1662006-03-27 06:58:47 +000074/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000075/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000076bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000077 // Look through a bit convert.
78 if (N->getOpcode() == ISD::BIT_CONVERT)
79 N = N->getOperand(0).Val;
80
Evan Chenga8df1662006-03-27 06:58:47 +000081 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000082
83 unsigned i = 0, e = N->getNumOperands();
84
85 // Skip over all of the undef values.
86 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
87 ++i;
88
89 // Do not accept an all-undef vector.
90 if (i == e) return false;
91
92 // Do not accept build_vectors that aren't all constants or which have non-~0
93 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000094 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (isa<ConstantSDNode>(NotZero)) {
96 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
97 return false;
98 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000099 MVT::ValueType VT = NotZero.getValueType();
100 if (VT== MVT::f64) {
101 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
102 (uint64_t)-1)
103 return false;
104 } else {
105 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
106 (uint32_t)-1)
107 return false;
108 }
Evan Chenga8df1662006-03-27 06:58:47 +0000109 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000110 return false;
111
112 // Okay, we have at least one ~0 value, check to see if the rest match or are
113 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000114 for (++i; i != e; ++i)
115 if (N->getOperand(i) != NotZero &&
116 N->getOperand(i).getOpcode() != ISD::UNDEF)
117 return false;
118 return true;
119}
120
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122/// isBuildVectorAllZeros - Return true if the specified node is a
123/// BUILD_VECTOR where all of the elements are 0 or undef.
124bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000125 // Look through a bit convert.
126 if (N->getOpcode() == ISD::BIT_CONVERT)
127 N = N->getOperand(0).Val;
128
Evan Cheng4a147842006-03-26 09:50:58 +0000129 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000130
131 unsigned i = 0, e = N->getNumOperands();
132
133 // Skip over all of the undef values.
134 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
135 ++i;
136
137 // Do not accept an all-undef vector.
138 if (i == e) return false;
139
140 // Do not accept build_vectors that aren't all constants or which have non-~0
141 // elements.
142 SDOperand Zero = N->getOperand(i);
143 if (isa<ConstantSDNode>(Zero)) {
144 if (!cast<ConstantSDNode>(Zero)->isNullValue())
145 return false;
146 } else if (isa<ConstantFPSDNode>(Zero)) {
147 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
148 return false;
149 } else
150 return false;
151
152 // Okay, we have at least one ~0 value, check to see if the rest match or are
153 // undefs.
154 for (++i; i != e; ++i)
155 if (N->getOperand(i) != Zero &&
156 N->getOperand(i).getOpcode() != ISD::UNDEF)
157 return false;
158 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000159}
160
Chris Lattnerc3aae252005-01-07 07:46:32 +0000161/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
162/// when given the operation for (X op Y).
163ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
164 // To perform this operation, we just need to swap the L and G bits of the
165 // operation.
166 unsigned OldL = (Operation >> 2) & 1;
167 unsigned OldG = (Operation >> 1) & 1;
168 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
169 (OldL << 1) | // New G bit
170 (OldG << 2)); // New L bit.
171}
172
173/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
174/// 'op' is a valid SetCC operation.
175ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
176 unsigned Operation = Op;
177 if (isInteger)
178 Operation ^= 7; // Flip L, G, E bits, but not U.
179 else
180 Operation ^= 15; // Flip all of the condition bits.
181 if (Operation > ISD::SETTRUE2)
182 Operation &= ~8; // Don't let N and U bits get set.
183 return ISD::CondCode(Operation);
184}
185
186
187/// isSignedOp - For an integer comparison, return 1 if the comparison is a
188/// signed operation and 2 if the result is an unsigned comparison. Return zero
189/// if the operation does not depend on the sign of the input (setne and seteq).
190static int isSignedOp(ISD::CondCode Opcode) {
191 switch (Opcode) {
192 default: assert(0 && "Illegal integer setcc operation!");
193 case ISD::SETEQ:
194 case ISD::SETNE: return 0;
195 case ISD::SETLT:
196 case ISD::SETLE:
197 case ISD::SETGT:
198 case ISD::SETGE: return 1;
199 case ISD::SETULT:
200 case ISD::SETULE:
201 case ISD::SETUGT:
202 case ISD::SETUGE: return 2;
203 }
204}
205
206/// getSetCCOrOperation - Return the result of a logical OR between different
207/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
208/// returns SETCC_INVALID if it is not possible to represent the resultant
209/// comparison.
210ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
211 bool isInteger) {
212 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
213 // Cannot fold a signed integer setcc with an unsigned integer setcc.
214 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215
Chris Lattnerc3aae252005-01-07 07:46:32 +0000216 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000217
Chris Lattnerc3aae252005-01-07 07:46:32 +0000218 // If the N and U bits get set then the resultant comparison DOES suddenly
219 // care about orderedness, and is true when ordered.
220 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000221 Op &= ~16; // Clear the U bit if the N bit is set.
222
223 // Canonicalize illegal integer setcc's.
224 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
225 Op = ISD::SETNE;
226
Chris Lattnerc3aae252005-01-07 07:46:32 +0000227 return ISD::CondCode(Op);
228}
229
230/// getSetCCAndOperation - Return the result of a logical AND between different
231/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
232/// function returns zero if it is not possible to represent the resultant
233/// comparison.
234ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
235 bool isInteger) {
236 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
237 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239
240 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000241 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
242
243 // Canonicalize illegal integer setcc's.
244 if (isInteger) {
245 switch (Result) {
246 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000247 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
248 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
249 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
250 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000251 }
252 }
253
254 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000255}
256
Chris Lattnerb48da392005-01-23 04:39:44 +0000257const TargetMachine &SelectionDAG::getTarget() const {
258 return TLI.getTargetMachine();
259}
260
Jim Laskey58b968b2005-08-17 20:08:02 +0000261//===----------------------------------------------------------------------===//
262// SelectionDAG Class
263//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000264
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000266/// SelectionDAG.
267void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000268 // Create a dummy node (which is not added to allnodes), that adds a reference
269 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000270 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271
Chris Lattner190a4182006-08-04 17:45:20 +0000272 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000273
Chris Lattner190a4182006-08-04 17:45:20 +0000274 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000275 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000276 if (I->use_empty())
277 DeadNodes.push_back(I);
278
279 // Process the worklist, deleting the nodes and adding their uses to the
280 // worklist.
281 while (!DeadNodes.empty()) {
282 SDNode *N = DeadNodes.back();
283 DeadNodes.pop_back();
284
285 // Take the node out of the appropriate CSE map.
286 RemoveNodeFromCSEMaps(N);
287
288 // Next, brutally remove the operand list. This is safe to do, as there are
289 // no cycles in the graph.
290 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
291 SDNode *Operand = I->Val;
292 Operand->removeUser(N);
293
294 // Now that we removed this operand, see if there are no uses of it left.
295 if (Operand->use_empty())
296 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000297 }
Chris Lattner190a4182006-08-04 17:45:20 +0000298 delete[] N->OperandList;
299 N->OperandList = 0;
300 N->NumOperands = 0;
301
302 // Finally, remove N itself.
303 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000304 }
305
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000307 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308}
309
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000310void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
312
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
315
Chris Lattner1e111c72005-09-07 05:37:01 +0000316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
319}
320
321void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
322
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000323 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000324 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000325
326 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
330 N->OperandList = 0;
331 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000332
333 delete N;
334}
335
Chris Lattner149c58c2005-08-16 18:17:10 +0000336/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337/// correspond to it. This is useful when we're about to delete or repurpose
338/// the node. We don't want future request for structurally identical nodes
339/// to return N anymore.
340void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000343 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000344 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000345 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
346 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000348 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000349 Erased = TargetConstants.erase(std::make_pair(
350 cast<ConstantSDNode>(N)->getValue(),
351 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000352 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000353 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000354 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000355 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000356 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000357 }
Chris Lattner3181a772006-01-29 06:26:56 +0000358 case ISD::TargetConstantFP: {
359 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
360 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
361 break;
362 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000363 case ISD::STRING:
364 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
365 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000366 case ISD::CONDCODE:
367 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
368 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000370 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
371 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000372 case ISD::GlobalAddress: {
373 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
374 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
375 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000377 }
378 case ISD::TargetGlobalAddress: {
379 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
380 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
381 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000382 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000383 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000386 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000387 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000389 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000390 case ISD::JumpTable:
391 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
392 break;
393 case ISD::TargetJumpTable:
394 Erased =
395 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
396 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000397 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000398 Erased = ConstantPoolIndices.
399 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000400 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
401 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000403 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000404 Erased = TargetConstantPoolIndices.
405 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000406 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
407 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000408 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000410 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000411 break;
412 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000413 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000415 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000416 Erased =
417 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000418 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000419 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000420 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000421 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
422 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000423 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000424 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
425 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000426 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000427 case ISD::SRCVALUE: {
428 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000429 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000430 break;
431 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000432 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000433 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000434 if (N->getNumOperands() == 0) {
435 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
436 N->getValueType(0)));
Chris Lattner6621e3b2005-09-02 19:15:44 +0000437 } else {
Chris Lattnera5682852006-08-07 23:03:03 +0000438 // Remove it from the CSE Map.
439 Erased = CSEMap.RemoveNode(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000440 }
Chris Lattner385328c2005-05-14 07:42:29 +0000441 } else {
Chris Lattnera5682852006-08-07 23:03:03 +0000442 // Remove it from the CSE Map.
443 Erased = CSEMap.RemoveNode(N);
Chris Lattner89c34632005-05-14 06:20:26 +0000444 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000445 break;
446 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000447#ifndef NDEBUG
448 // Verify that the node was actually in one of the CSE maps, unless it has a
449 // flag result (which cannot be CSE'd) or is one of the special cases that are
450 // not subject to CSE.
451 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000452 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000453 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000454 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000455 assert(0 && "Node is not in map!");
456 }
457#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000458}
459
Chris Lattner8b8749f2005-08-17 19:00:20 +0000460/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
461/// has been taken out and modified in some way. If the specified node already
462/// exists in the CSE maps, do not modify the maps, but return the existing node
463/// instead. If it doesn't exist, add it and return null.
464///
465SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
466 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000467 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000468 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000469
Chris Lattner9f8cc692005-12-19 22:21:21 +0000470 // Check that remaining values produced are not flags.
471 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
472 if (N->getValueType(i) == MVT::Flag)
473 return 0; // Never CSE anything that produces a flag.
474
Chris Lattnera5682852006-08-07 23:03:03 +0000475 SDNode *New = CSEMap.GetOrInsertNode(N);
476 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000477 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000478}
479
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000480/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
481/// were replaced with those specified. If this node is never memoized,
482/// return null, otherwise return a pointer to the slot it would take. If a
483/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000484SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
485 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000486 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000487 return 0; // Never add these nodes.
488
489 // Check that remaining values produced are not flags.
490 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
491 if (N->getValueType(i) == MVT::Flag)
492 return 0; // Never CSE anything that produces a flag.
493
Chris Lattnera5682852006-08-07 23:03:03 +0000494 SelectionDAGCSEMap::NodeID ID;
495 ID.SetOpcode(N->getOpcode());
496 ID.SetValueTypes(N->value_begin());
497 ID.SetOperands(Op);
498 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000499}
500
501/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
502/// were replaced with those specified. If this node is never memoized,
503/// return null, otherwise return a pointer to the slot it would take. If a
504/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000505SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
506 SDOperand Op1, SDOperand Op2,
507 void *&InsertPos) {
508 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
509 return 0; // Never add these nodes.
510
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return 0; // Never CSE anything that produces a flag.
515
516 SelectionDAGCSEMap::NodeID ID;
517 ID.SetOpcode(N->getOpcode());
518 ID.SetValueTypes(N->value_begin());
519 ID.SetOperands(Op1, Op2);
520 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
521}
522
523
524/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
525/// were replaced with those specified. If this node is never memoized,
526/// return null, otherwise return a pointer to the slot it would take. If a
527/// node already exists with these operands, the slot will be non-null.
528SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000529 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000530 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000531 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000532 return 0; // Never add these nodes.
533
534 // Check that remaining values produced are not flags.
535 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
536 if (N->getValueType(i) == MVT::Flag)
537 return 0; // Never CSE anything that produces a flag.
538
Chris Lattnera5682852006-08-07 23:03:03 +0000539 SelectionDAGCSEMap::NodeID ID;
540 ID.SetOpcode(N->getOpcode());
541 ID.SetValueTypes(N->value_begin());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000542 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000543 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000544}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000545
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000546
Chris Lattner78ec3112003-08-11 14:57:33 +0000547SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000548 while (!AllNodes.empty()) {
549 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000550 delete [] N->OperandList;
551 N->OperandList = 0;
552 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000553 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000554 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000555}
556
Chris Lattner0f2287b2005-04-13 02:38:18 +0000557SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000558 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000559 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000560 return getNode(ISD::AND, Op.getValueType(), Op,
561 getConstant(Imm, Op.getValueType()));
562}
563
Chris Lattnerc3aae252005-01-07 07:46:32 +0000564SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
565 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000566 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
567
Chris Lattnerc3aae252005-01-07 07:46:32 +0000568 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000569 if (VT != MVT::i64)
570 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000571
Chris Lattnerc3aae252005-01-07 07:46:32 +0000572 SDNode *&N = Constants[std::make_pair(Val, VT)];
573 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000574 N = new ConstantSDNode(false, Val, VT);
575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
577}
578
Chris Lattner36ce6912005-11-29 06:21:05 +0000579SDOperand SelectionDAG::getString(const std::string &Val) {
580 StringSDNode *&N = StringNodes[Val];
581 if (!N) {
582 N = new StringSDNode(Val);
583 AllNodes.push_back(N);
584 }
585 return SDOperand(N, 0);
586}
587
Chris Lattner37bfbb42005-08-17 00:34:06 +0000588SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
589 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
590 // Mask out any bits that are not valid for this constant.
591 if (VT != MVT::i64)
592 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
593
594 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
595 if (N) return SDOperand(N, 0);
596 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000597 AllNodes.push_back(N);
598 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000599}
600
Chris Lattnerc3aae252005-01-07 07:46:32 +0000601SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
602 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
603 if (VT == MVT::f32)
604 Val = (float)Val; // Mask out extra precision.
605
Chris Lattnerd8658612005-02-17 20:17:32 +0000606 // Do the map lookup using the actual bit pattern for the floating point
607 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
608 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000609 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000610 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000611 N = new ConstantFPSDNode(false, Val, VT);
612 AllNodes.push_back(N);
613 return SDOperand(N, 0);
614}
615
616SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
617 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
618 if (VT == MVT::f32)
619 Val = (float)Val; // Mask out extra precision.
620
621 // Do the map lookup using the actual bit pattern for the floating point
622 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
623 // we don't have issues with SNANs.
624 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
625 if (N) return SDOperand(N, 0);
626 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
629}
630
Chris Lattnerc3aae252005-01-07 07:46:32 +0000631SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000632 MVT::ValueType VT, int offset) {
633 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000634 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000635 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000636 AllNodes.push_back(N);
637 return SDOperand(N, 0);
638}
639
640SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000641 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000642 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000643 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000644 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000645 AllNodes.push_back(N);
646 return SDOperand(N, 0);
647}
648
649SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
650 SDNode *&N = FrameIndices[FI];
651 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000652 N = new FrameIndexSDNode(FI, VT, false);
653 AllNodes.push_back(N);
654 return SDOperand(N, 0);
655}
656
657SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
658 SDNode *&N = TargetFrameIndices[FI];
659 if (N) return SDOperand(N, 0);
660 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000661 AllNodes.push_back(N);
662 return SDOperand(N, 0);
663}
664
Nate Begeman37efe672006-04-22 18:53:45 +0000665SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
666 SDNode *&N = JumpTableIndices[JTI];
667 if (N) return SDOperand(N, 0);
668 N = new JumpTableSDNode(JTI, VT, false);
669 AllNodes.push_back(N);
670 return SDOperand(N, 0);
671}
672
673SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
674 SDNode *&N = TargetJumpTableIndices[JTI];
675 if (N) return SDOperand(N, 0);
676 N = new JumpTableSDNode(JTI, VT, true);
677 AllNodes.push_back(N);
678 return SDOperand(N, 0);
679}
680
Evan Chengb8973bd2006-01-31 22:23:14 +0000681SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000682 unsigned Alignment, int Offset) {
683 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
684 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000685 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000686 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000687 AllNodes.push_back(N);
688 return SDOperand(N, 0);
689}
690
Evan Chengb8973bd2006-01-31 22:23:14 +0000691SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000692 unsigned Alignment, int Offset) {
693 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
694 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000695 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000696 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
699}
700
701SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
702 SDNode *&N = BBNodes[MBB];
703 if (N) return SDOperand(N, 0);
704 N = new BasicBlockSDNode(MBB);
705 AllNodes.push_back(N);
706 return SDOperand(N, 0);
707}
708
Chris Lattner15e4b012005-07-10 00:07:11 +0000709SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
710 if ((unsigned)VT >= ValueTypeNodes.size())
711 ValueTypeNodes.resize(VT+1);
712 if (ValueTypeNodes[VT] == 0) {
713 ValueTypeNodes[VT] = new VTSDNode(VT);
714 AllNodes.push_back(ValueTypeNodes[VT]);
715 }
716
717 return SDOperand(ValueTypeNodes[VT], 0);
718}
719
Chris Lattnerc3aae252005-01-07 07:46:32 +0000720SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
721 SDNode *&N = ExternalSymbols[Sym];
722 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000723 N = new ExternalSymbolSDNode(false, Sym, VT);
724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
726}
727
Chris Lattner809ec112006-01-28 10:09:25 +0000728SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
729 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000730 SDNode *&N = TargetExternalSymbols[Sym];
731 if (N) return SDOperand(N, 0);
732 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
735}
736
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000737SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
738 if ((unsigned)Cond >= CondCodeNodes.size())
739 CondCodeNodes.resize(Cond+1);
740
Chris Lattner079a27a2005-08-09 20:40:02 +0000741 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000742 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000743 AllNodes.push_back(CondCodeNodes[Cond]);
744 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000745 return SDOperand(CondCodeNodes[Cond], 0);
746}
747
Chris Lattner0fdd7682005-08-30 22:38:38 +0000748SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
749 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
750 if (!Reg) {
751 Reg = new RegisterSDNode(RegNo, VT);
752 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000753 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000754 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000755}
756
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000757SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
758 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000759 // These setcc operations always fold.
760 switch (Cond) {
761 default: break;
762 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000763 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000764 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000765 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000766
767 case ISD::SETOEQ:
768 case ISD::SETOGT:
769 case ISD::SETOGE:
770 case ISD::SETOLT:
771 case ISD::SETOLE:
772 case ISD::SETONE:
773 case ISD::SETO:
774 case ISD::SETUO:
775 case ISD::SETUEQ:
776 case ISD::SETUNE:
777 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
778 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779 }
780
Chris Lattner67255a12005-04-07 18:14:58 +0000781 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
782 uint64_t C2 = N2C->getValue();
783 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
784 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000785
Chris Lattnerc3aae252005-01-07 07:46:32 +0000786 // Sign extend the operands if required
787 if (ISD::isSignedIntSetCC(Cond)) {
788 C1 = N1C->getSignExtended();
789 C2 = N2C->getSignExtended();
790 }
791
792 switch (Cond) {
793 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000794 case ISD::SETEQ: return getConstant(C1 == C2, VT);
795 case ISD::SETNE: return getConstant(C1 != C2, VT);
796 case ISD::SETULT: return getConstant(C1 < C2, VT);
797 case ISD::SETUGT: return getConstant(C1 > C2, VT);
798 case ISD::SETULE: return getConstant(C1 <= C2, VT);
799 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
800 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
801 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
802 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
803 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000804 }
Chris Lattner24673922005-04-07 18:58:54 +0000805 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000806 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000807 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
808 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
809
810 // If the comparison constant has bits in the upper part, the
811 // zero-extended value could never match.
812 if (C2 & (~0ULL << InSize)) {
813 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
814 switch (Cond) {
815 case ISD::SETUGT:
816 case ISD::SETUGE:
817 case ISD::SETEQ: return getConstant(0, VT);
818 case ISD::SETULT:
819 case ISD::SETULE:
820 case ISD::SETNE: return getConstant(1, VT);
821 case ISD::SETGT:
822 case ISD::SETGE:
823 // True if the sign bit of C2 is set.
824 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
825 case ISD::SETLT:
826 case ISD::SETLE:
827 // True if the sign bit of C2 isn't set.
828 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
829 default:
830 break;
831 }
832 }
833
834 // Otherwise, we can perform the comparison with the low bits.
835 switch (Cond) {
836 case ISD::SETEQ:
837 case ISD::SETNE:
838 case ISD::SETUGT:
839 case ISD::SETUGE:
840 case ISD::SETULT:
841 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000842 return getSetCC(VT, N1.getOperand(0),
843 getConstant(C2, N1.getOperand(0).getValueType()),
844 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000845 default:
846 break; // todo, be more careful with signed comparisons
847 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000848 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
849 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
850 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
851 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
852 MVT::ValueType ExtDstTy = N1.getValueType();
853 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000854
Chris Lattner7b2880c2005-08-24 22:44:39 +0000855 // If the extended part has any inconsistent bits, it cannot ever
856 // compare equal. In other words, they have to be all ones or all
857 // zeros.
858 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000859 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000860 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
861 return getConstant(Cond == ISD::SETNE, VT);
862
863 // Otherwise, make this a use of a zext.
864 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000865 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000866 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000867 }
868
Chris Lattner67255a12005-04-07 18:14:58 +0000869 uint64_t MinVal, MaxVal;
870 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
871 if (ISD::isSignedIntSetCC(Cond)) {
872 MinVal = 1ULL << (OperandBitSize-1);
873 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
874 MaxVal = ~0ULL >> (65-OperandBitSize);
875 else
876 MaxVal = 0;
877 } else {
878 MinVal = 0;
879 MaxVal = ~0ULL >> (64-OperandBitSize);
880 }
881
882 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
883 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
884 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
885 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000886 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
887 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000888 }
889
890 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
891 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
892 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000893 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
894 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000895 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000896
Nate Begeman72ea2812005-04-14 08:56:52 +0000897 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
898 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000899
Nate Begeman72ea2812005-04-14 08:56:52 +0000900 // Canonicalize setgt X, Min --> setne X, Min
901 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000902 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000903
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000904 // If we have setult X, 1, turn it into seteq X, 0
905 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000906 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
907 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000908 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000909 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000910 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
911 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000912
913 // If we have "setcc X, C1", check to see if we can shrink the immediate
914 // by changing cc.
915
916 // SETUGT X, SINTMAX -> SETLT X, 0
917 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
918 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000919 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000920
921 // FIXME: Implement the rest of these.
922
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000923
924 // Fold bit comparisons when we can.
925 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
926 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
927 if (ConstantSDNode *AndRHS =
928 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
929 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
930 // Perform the xform if the AND RHS is a single bit.
931 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
932 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000933 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000934 TLI.getShiftAmountTy()));
935 }
936 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
937 // (X & 8) == 8 --> (X & 8) >> 3
938 // Perform the xform if C2 is a single bit.
939 if ((C2 & (C2-1)) == 0) {
940 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000941 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000942 }
943 }
944 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 }
Chris Lattner67255a12005-04-07 18:14:58 +0000946 } else if (isa<ConstantSDNode>(N1.Val)) {
947 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000948 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000949 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950
951 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
952 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
953 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000954
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 switch (Cond) {
956 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000957 case ISD::SETEQ: return getConstant(C1 == C2, VT);
958 case ISD::SETNE: return getConstant(C1 != C2, VT);
959 case ISD::SETLT: return getConstant(C1 < C2, VT);
960 case ISD::SETGT: return getConstant(C1 > C2, VT);
961 case ISD::SETLE: return getConstant(C1 <= C2, VT);
962 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000963 }
964 } else {
965 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000966 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 }
968
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000969 // Could not fold it.
970 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971}
972
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000974///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000976 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
977 if (!N) {
978 N = new SDNode(Opcode, VT);
979 AllNodes.push_back(N);
980 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 return SDOperand(N, 0);
982}
983
Chris Lattnerc3aae252005-01-07 07:46:32 +0000984SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
985 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000986 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000987 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
989 uint64_t Val = C->getValue();
990 switch (Opcode) {
991 default: break;
992 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000993 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000994 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
995 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000996 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
997 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000998 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000999 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001000 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001001 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001002 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001003 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001004 case ISD::BSWAP:
1005 switch(VT) {
1006 default: assert(0 && "Invalid bswap!"); break;
1007 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1008 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1009 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1010 }
1011 break;
1012 case ISD::CTPOP:
1013 switch(VT) {
1014 default: assert(0 && "Invalid ctpop!"); break;
1015 case MVT::i1: return getConstant(Val != 0, VT);
1016 case MVT::i8:
1017 Tmp1 = (unsigned)Val & 0xFF;
1018 return getConstant(CountPopulation_32(Tmp1), VT);
1019 case MVT::i16:
1020 Tmp1 = (unsigned)Val & 0xFFFF;
1021 return getConstant(CountPopulation_32(Tmp1), VT);
1022 case MVT::i32:
1023 return getConstant(CountPopulation_32((unsigned)Val), VT);
1024 case MVT::i64:
1025 return getConstant(CountPopulation_64(Val), VT);
1026 }
1027 case ISD::CTLZ:
1028 switch(VT) {
1029 default: assert(0 && "Invalid ctlz!"); break;
1030 case MVT::i1: return getConstant(Val == 0, VT);
1031 case MVT::i8:
1032 Tmp1 = (unsigned)Val & 0xFF;
1033 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1034 case MVT::i16:
1035 Tmp1 = (unsigned)Val & 0xFFFF;
1036 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1037 case MVT::i32:
1038 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1039 case MVT::i64:
1040 return getConstant(CountLeadingZeros_64(Val), VT);
1041 }
1042 case ISD::CTTZ:
1043 switch(VT) {
1044 default: assert(0 && "Invalid cttz!"); break;
1045 case MVT::i1: return getConstant(Val == 0, VT);
1046 case MVT::i8:
1047 Tmp1 = (unsigned)Val | 0x100;
1048 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1049 case MVT::i16:
1050 Tmp1 = (unsigned)Val | 0x10000;
1051 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1052 case MVT::i32:
1053 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1054 case MVT::i64:
1055 return getConstant(CountTrailingZeros_64(Val), VT);
1056 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 }
1058 }
1059
Chris Lattner94683772005-12-23 05:30:37 +00001060 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1062 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001063 case ISD::FNEG:
1064 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001065 case ISD::FABS:
1066 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 case ISD::FP_ROUND:
1068 case ISD::FP_EXTEND:
1069 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001070 case ISD::FP_TO_SINT:
1071 return getConstant((int64_t)C->getValue(), VT);
1072 case ISD::FP_TO_UINT:
1073 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001074 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001075 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001076 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001077 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001078 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001079 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 }
1081
1082 unsigned OpOpcode = Operand.Val->getOpcode();
1083 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001084 case ISD::TokenFactor:
1085 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 case ISD::SIGN_EXTEND:
1087 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001088 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1090 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1091 break;
1092 case ISD::ZERO_EXTEND:
1093 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001094 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001095 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001096 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001098 case ISD::ANY_EXTEND:
1099 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001100 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001101 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1102 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1103 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1104 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 case ISD::TRUNCATE:
1106 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001107 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 if (OpOpcode == ISD::TRUNCATE)
1109 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001110 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1111 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001112 // If the source is smaller than the dest, we still need an extend.
1113 if (Operand.Val->getOperand(0).getValueType() < VT)
1114 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1115 else if (Operand.Val->getOperand(0).getValueType() > VT)
1116 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1117 else
1118 return Operand.Val->getOperand(0);
1119 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001120 break;
Chris Lattner35481892005-12-23 00:16:34 +00001121 case ISD::BIT_CONVERT:
1122 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001123 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001124 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001125 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001126 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1127 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001128 if (OpOpcode == ISD::UNDEF)
1129 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001130 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001131 case ISD::SCALAR_TO_VECTOR:
1132 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1133 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1134 "Illegal SCALAR_TO_VECTOR node!");
1135 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001136 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001137 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1138 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001139 Operand.Val->getOperand(0));
1140 if (OpOpcode == ISD::FNEG) // --X -> X
1141 return Operand.Val->getOperand(0);
1142 break;
1143 case ISD::FABS:
1144 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1145 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1146 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 }
1148
Chris Lattner43247a12005-08-25 19:12:10 +00001149 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001150 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001151 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001152 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1153 void *IP = 0;
1154 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1155 return SDOperand(E, 0);
1156 N = new SDNode(Opcode, Operand);
1157 N->setValueTypes(VTs, 1);
1158 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001159 } else {
1160 N = new SDNode(Opcode, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001161 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 AllNodes.push_back(N);
1164 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001165}
1166
Chris Lattner36019aa2005-04-18 03:48:41 +00001167
1168
Chris Lattnerc3aae252005-01-07 07:46:32 +00001169SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1170 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001171#ifndef NDEBUG
1172 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001173 case ISD::TokenFactor:
1174 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1175 N2.getValueType() == MVT::Other && "Invalid token factor!");
1176 break;
Chris Lattner76365122005-01-16 02:23:22 +00001177 case ISD::AND:
1178 case ISD::OR:
1179 case ISD::XOR:
1180 case ISD::UDIV:
1181 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001182 case ISD::MULHU:
1183 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001184 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1185 // fall through
1186 case ISD::ADD:
1187 case ISD::SUB:
1188 case ISD::MUL:
1189 case ISD::SDIV:
1190 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001191 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1192 // fall through.
1193 case ISD::FADD:
1194 case ISD::FSUB:
1195 case ISD::FMUL:
1196 case ISD::FDIV:
1197 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001198 assert(N1.getValueType() == N2.getValueType() &&
1199 N1.getValueType() == VT && "Binary operator types must match!");
1200 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001201 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1202 assert(N1.getValueType() == VT &&
1203 MVT::isFloatingPoint(N1.getValueType()) &&
1204 MVT::isFloatingPoint(N2.getValueType()) &&
1205 "Invalid FCOPYSIGN!");
1206 break;
Chris Lattner76365122005-01-16 02:23:22 +00001207 case ISD::SHL:
1208 case ISD::SRA:
1209 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001210 case ISD::ROTL:
1211 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001212 assert(VT == N1.getValueType() &&
1213 "Shift operators return type must be the same as their first arg");
1214 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001215 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001216 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001217 case ISD::FP_ROUND_INREG: {
1218 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1219 assert(VT == N1.getValueType() && "Not an inreg round!");
1220 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1221 "Cannot FP_ROUND_INREG integer types");
1222 assert(EVT <= VT && "Not rounding down!");
1223 break;
1224 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001225 case ISD::AssertSext:
1226 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001227 case ISD::SIGN_EXTEND_INREG: {
1228 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1229 assert(VT == N1.getValueType() && "Not an inreg extend!");
1230 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1231 "Cannot *_EXTEND_INREG FP types");
1232 assert(EVT <= VT && "Not extending!");
1233 }
1234
Chris Lattner76365122005-01-16 02:23:22 +00001235 default: break;
1236 }
1237#endif
1238
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1240 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1241 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001242 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1243 int64_t Val = N1C->getValue();
1244 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1245 Val <<= 64-FromBits;
1246 Val >>= 64-FromBits;
1247 return getConstant(Val, VT);
1248 }
1249
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 if (N2C) {
1251 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1252 switch (Opcode) {
1253 case ISD::ADD: return getConstant(C1 + C2, VT);
1254 case ISD::SUB: return getConstant(C1 - C2, VT);
1255 case ISD::MUL: return getConstant(C1 * C2, VT);
1256 case ISD::UDIV:
1257 if (C2) return getConstant(C1 / C2, VT);
1258 break;
1259 case ISD::UREM :
1260 if (C2) return getConstant(C1 % C2, VT);
1261 break;
1262 case ISD::SDIV :
1263 if (C2) return getConstant(N1C->getSignExtended() /
1264 N2C->getSignExtended(), VT);
1265 break;
1266 case ISD::SREM :
1267 if (C2) return getConstant(N1C->getSignExtended() %
1268 N2C->getSignExtended(), VT);
1269 break;
1270 case ISD::AND : return getConstant(C1 & C2, VT);
1271 case ISD::OR : return getConstant(C1 | C2, VT);
1272 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001273 case ISD::SHL : return getConstant(C1 << C2, VT);
1274 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001275 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001276 case ISD::ROTL :
1277 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1278 VT);
1279 case ISD::ROTR :
1280 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1281 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001282 default: break;
1283 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 } else { // Cannonicalize constant to RHS if commutative
1285 if (isCommutativeBinOp(Opcode)) {
1286 std::swap(N1C, N2C);
1287 std::swap(N1, N2);
1288 }
1289 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001290 }
1291
1292 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1293 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001294 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001295 if (N2CFP) {
1296 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1297 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001298 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1299 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1300 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1301 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001302 if (C2) return getConstantFP(C1 / C2, VT);
1303 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001304 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001305 if (C2) return getConstantFP(fmod(C1, C2), VT);
1306 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001307 case ISD::FCOPYSIGN: {
1308 union {
1309 double F;
1310 uint64_t I;
1311 } u1;
1312 union {
1313 double F;
1314 int64_t I;
1315 } u2;
1316 u1.F = C1;
1317 u2.F = C2;
1318 if (u2.I < 0) // Sign bit of RHS set?
1319 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1320 else
1321 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1322 return getConstantFP(u1.F, VT);
1323 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001324 default: break;
1325 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001326 } else { // Cannonicalize constant to RHS if commutative
1327 if (isCommutativeBinOp(Opcode)) {
1328 std::swap(N1CFP, N2CFP);
1329 std::swap(N1, N2);
1330 }
1331 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001332 }
Chris Lattner62b57722006-04-20 05:39:12 +00001333
1334 // Canonicalize an UNDEF to the RHS, even over a constant.
1335 if (N1.getOpcode() == ISD::UNDEF) {
1336 if (isCommutativeBinOp(Opcode)) {
1337 std::swap(N1, N2);
1338 } else {
1339 switch (Opcode) {
1340 case ISD::FP_ROUND_INREG:
1341 case ISD::SIGN_EXTEND_INREG:
1342 case ISD::SUB:
1343 case ISD::FSUB:
1344 case ISD::FDIV:
1345 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001346 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001347 return N1; // fold op(undef, arg2) -> undef
1348 case ISD::UDIV:
1349 case ISD::SDIV:
1350 case ISD::UREM:
1351 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001352 case ISD::SRL:
1353 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001354 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1355 }
1356 }
1357 }
1358
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001359 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001360 if (N2.getOpcode() == ISD::UNDEF) {
1361 switch (Opcode) {
1362 case ISD::ADD:
1363 case ISD::SUB:
1364 case ISD::FADD:
1365 case ISD::FSUB:
1366 case ISD::FMUL:
1367 case ISD::FDIV:
1368 case ISD::FREM:
1369 case ISD::UDIV:
1370 case ISD::SDIV:
1371 case ISD::UREM:
1372 case ISD::SREM:
1373 case ISD::XOR:
1374 return N2; // fold op(arg1, undef) -> undef
1375 case ISD::MUL:
1376 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001377 case ISD::SRL:
1378 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001379 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1380 case ISD::OR:
1381 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001382 case ISD::SRA:
1383 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001384 }
1385 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001386
Chris Lattnerc3aae252005-01-07 07:46:32 +00001387 // Finally, fold operations that do not require constants.
1388 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001389 case ISD::FP_ROUND_INREG:
1390 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1391 break;
1392 case ISD::SIGN_EXTEND_INREG: {
1393 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1394 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001395 break;
1396 }
1397
Nate Begemaneea805e2005-04-13 21:23:31 +00001398 // FIXME: figure out how to safely handle things like
1399 // int foo(int x) { return 1 << (x & 255); }
1400 // int bar() { return foo(256); }
1401#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001402 case ISD::SHL:
1403 case ISD::SRL:
1404 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001405 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001406 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001407 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001408 else if (N2.getOpcode() == ISD::AND)
1409 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1410 // If the and is only masking out bits that cannot effect the shift,
1411 // eliminate the and.
1412 unsigned NumBits = MVT::getSizeInBits(VT);
1413 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1414 return getNode(Opcode, VT, N1, N2.getOperand(0));
1415 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001416 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001417#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001418 }
1419
Chris Lattner27e9b412005-05-11 18:57:39 +00001420 // Memoize this node if possible.
1421 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001422 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001423 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001424 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1425 void *IP = 0;
1426 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1427 return SDOperand(E, 0);
1428 N = new SDNode(Opcode, N1, N2);
1429 N->setValueTypes(VTs, 1);
1430 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001431 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001432 N = new SDNode(Opcode, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001433 N->setValueTypes(VTs, 1);
Chris Lattner27e9b412005-05-11 18:57:39 +00001434 }
1435
Chris Lattnerc3aae252005-01-07 07:46:32 +00001436 AllNodes.push_back(N);
1437 return SDOperand(N, 0);
1438}
1439
Chris Lattnerc3aae252005-01-07 07:46:32 +00001440SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1441 SDOperand N1, SDOperand N2, SDOperand N3) {
1442 // Perform various simplifications.
1443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1444 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001445 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001446 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001447 case ISD::SETCC: {
1448 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001449 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001450 if (Simp.Val) return Simp;
1451 break;
1452 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 case ISD::SELECT:
1454 if (N1C)
1455 if (N1C->getValue())
1456 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001457 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001458 return N3; // select false, X, Y -> Y
1459
1460 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001461 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001462 case ISD::BRCOND:
1463 if (N2C)
1464 if (N2C->getValue()) // Unconditional branch
1465 return getNode(ISD::BR, MVT::Other, N1, N3);
1466 else
1467 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001468 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001469 case ISD::VECTOR_SHUFFLE:
1470 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1471 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1472 N3.getOpcode() == ISD::BUILD_VECTOR &&
1473 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1474 "Illegal VECTOR_SHUFFLE node!");
1475 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001476 }
1477
Chris Lattner43247a12005-08-25 19:12:10 +00001478 // Memoize node if it doesn't produce a flag.
1479 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001480 MVT::ValueType *VTs = getNodeValueTypes(VT);
1481
Chris Lattner43247a12005-08-25 19:12:10 +00001482 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001483 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1484 void *IP = 0;
1485 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1486 return SDOperand(E, 0);
1487 N = new SDNode(Opcode, N1, N2, N3);
1488 N->setValueTypes(VTs, 1);
1489 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001490 } else {
1491 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001492 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001493 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1496}
1497
1498SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001499 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001500 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001501 SDOperand Ops[] = { N1, N2, N3, N4 };
1502 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001503}
1504
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001505SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1506 SDOperand N1, SDOperand N2, SDOperand N3,
1507 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001508 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1509 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001510}
1511
Evan Cheng7038daf2005-12-10 00:37:58 +00001512SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1513 SDOperand Chain, SDOperand Ptr,
1514 SDOperand SV) {
Chris Lattnera5682852006-08-07 23:03:03 +00001515 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1516
1517 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1518 void *IP = 0;
1519 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1520 return SDOperand(E, 0);
1521 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1522 N->setValueTypes(VTs, 2);
1523 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001524 AllNodes.push_back(N);
1525 return SDOperand(N, 0);
1526}
1527
1528SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1529 SDOperand Chain, SDOperand Ptr,
1530 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001531 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1532 getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001533 std::vector<MVT::ValueType> VTs;
1534 VTs.reserve(2);
1535 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001536 return getNode(ISD::VLOAD, VTs, Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001537}
1538
1539SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1540 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1541 MVT::ValueType EVT) {
1542 std::vector<SDOperand> Ops;
1543 Ops.reserve(4);
1544 Ops.push_back(Chain);
1545 Ops.push_back(Ptr);
1546 Ops.push_back(SV);
1547 Ops.push_back(getValueType(EVT));
1548 std::vector<MVT::ValueType> VTs;
1549 VTs.reserve(2);
1550 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1551 return getNode(Opcode, VTs, Ops);
1552}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001553
Chris Lattner0437cdd2005-05-09 04:14:13 +00001554SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001555 assert((!V || isa<PointerType>(V->getType())) &&
1556 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001557 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1558 if (N) return SDOperand(N, 0);
1559
1560 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001561 AllNodes.push_back(N);
1562 return SDOperand(N, 0);
1563}
1564
Nate Begemanacc398c2006-01-25 18:21:52 +00001565SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1566 SDOperand Chain, SDOperand Ptr,
1567 SDOperand SV) {
1568 std::vector<SDOperand> Ops;
1569 Ops.reserve(3);
1570 Ops.push_back(Chain);
1571 Ops.push_back(Ptr);
1572 Ops.push_back(SV);
1573 std::vector<MVT::ValueType> VTs;
1574 VTs.reserve(2);
1575 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1576 return getNode(ISD::VAARG, VTs, Ops);
1577}
1578
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001579SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001580 const SDOperand *Ops, unsigned NumOps) {
1581 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001582 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001583 case 1: return getNode(Opcode, VT, Ops[0]);
1584 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1585 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001586 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001587 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001588
Chris Lattneref847df2005-04-09 03:27:28 +00001589 switch (Opcode) {
1590 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001591 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001592 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001593 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1594#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1595 // If this is a truncating store of a constant, convert to the desired type
1596 // and store it instead.
1597 if (isa<Constant>(Ops[0])) {
1598 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1599 if (isa<Constant>(Op))
1600 N1 = Op;
1601 }
1602 // Also for ConstantFP?
1603#endif
1604 if (Ops[0].getValueType() == EVT) // Normal store?
1605 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1606 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1607 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1608 "Can't do FP-INT conversion!");
1609 break;
1610 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001611 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001612 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001613 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1614 "LHS and RHS of condition must have same type!");
1615 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1616 "True and False arms of SelectCC must have same type!");
1617 assert(Ops[2].getValueType() == VT &&
1618 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001619 break;
1620 }
1621 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001622 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001623 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1624 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001625 break;
1626 }
Chris Lattneref847df2005-04-09 03:27:28 +00001627 }
1628
Chris Lattner385328c2005-05-14 07:42:29 +00001629 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001630 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001631 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001632 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001633 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001634 void *IP = 0;
1635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1636 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001637 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001638 N->setValueTypes(VTs, 1);
1639 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001640 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001641 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001642 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001643 }
Chris Lattneref847df2005-04-09 03:27:28 +00001644 AllNodes.push_back(N);
1645 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001646}
1647
Chris Lattner89c34632005-05-14 06:20:26 +00001648SDOperand SelectionDAG::getNode(unsigned Opcode,
1649 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001650 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner89c34632005-05-14 06:20:26 +00001651 if (ResultTys.size() == 1)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001652 return getNode(Opcode, ResultTys[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001653
Chris Lattner5f056bf2005-07-10 01:55:33 +00001654 switch (Opcode) {
1655 case ISD::EXTLOAD:
1656 case ISD::SEXTLOAD:
1657 case ISD::ZEXTLOAD: {
1658 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001659 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001660 // If they are asking for an extending load from/to the same thing, return a
1661 // normal load.
1662 if (ResultTys[0] == EVT)
1663 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001664 if (MVT::isVector(ResultTys[0])) {
1665 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1666 "Invalid vector extload!");
1667 } else {
1668 assert(EVT < ResultTys[0] &&
1669 "Should only be an extending load, not truncating!");
1670 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001671 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001672 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001673 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1674 "Cannot convert from FP to Int or Int -> FP!");
1675 break;
1676 }
1677
Chris Lattnere89083a2005-05-14 07:25:05 +00001678 // FIXME: figure out how to safely handle things like
1679 // int foo(int x) { return 1 << (x & 255); }
1680 // int bar() { return foo(256); }
1681#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001682 case ISD::SRA_PARTS:
1683 case ISD::SRL_PARTS:
1684 case ISD::SHL_PARTS:
1685 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001686 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001687 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1688 else if (N3.getOpcode() == ISD::AND)
1689 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1690 // If the and is only masking out bits that cannot effect the shift,
1691 // eliminate the and.
1692 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1693 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1694 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1695 }
1696 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001697#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001698 }
Chris Lattner89c34632005-05-14 06:20:26 +00001699
Chris Lattner43247a12005-08-25 19:12:10 +00001700 // Memoize the node unless it returns a flag.
1701 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001702 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
Chris Lattner43247a12005-08-25 19:12:10 +00001703 if (ResultTys.back() != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001704 SelectionDAGCSEMap::NodeID ID;
1705 ID.SetOpcode(Opcode);
1706 ID.SetValueTypes(VTs);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001707 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001708 void *IP = 0;
1709 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1710 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001711 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001712 N->setValueTypes(VTs, ResultTys.size());
1713 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001714 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001715 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001716 N->setValueTypes(VTs, ResultTys.size());
Chris Lattner43247a12005-08-25 19:12:10 +00001717 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001718 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001719 return SDOperand(N, 0);
1720}
1721
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001722SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1723 std::vector<SDOperand> &Ops) {
1724 return getNode(Opcode, VT, &Ops[0], Ops.size());
1725}
1726
1727SDOperand SelectionDAG::getNode(unsigned Opcode,
1728 std::vector<MVT::ValueType> &ResultTys,
1729 std::vector<SDOperand> &Ops) {
1730 return getNode(Opcode, ResultTys, &Ops[0], Ops.size());
1731}
1732
1733
Chris Lattnera5682852006-08-07 23:03:03 +00001734MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1735 return SDNode::getValueTypeList(VT);
1736}
1737
1738MVT::ValueType *SelectionDAG::getNodeValueTypes(
1739 std::vector<MVT::ValueType> &RetVals) {
Chris Lattnera3255112005-11-08 23:30:28 +00001740 switch (RetVals.size()) {
Chris Lattnera5682852006-08-07 23:03:03 +00001741 case 0: assert(0 && "Cannot have nodes without results!");
1742 case 1: return SDNode::getValueTypeList(RetVals[0]);
1743 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
Chris Lattnera3255112005-11-08 23:30:28 +00001744 default: break;
1745 }
1746
1747 std::list<std::vector<MVT::ValueType> >::iterator I =
1748 std::find(VTList.begin(), VTList.end(), RetVals);
1749 if (I == VTList.end()) {
1750 VTList.push_front(RetVals);
1751 I = VTList.begin();
1752 }
1753
Chris Lattnera5682852006-08-07 23:03:03 +00001754 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001755}
1756
Chris Lattnera5682852006-08-07 23:03:03 +00001757MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1758 MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001759 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1760 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001761 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1762 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001763 }
1764 std::vector<MVT::ValueType> V;
1765 V.push_back(VT1);
1766 V.push_back(VT2);
1767 VTList.push_front(V);
Chris Lattnera5682852006-08-07 23:03:03 +00001768 return &(*VTList.begin())[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001769}
1770
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001771/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1772/// specified operands. If the resultant node already exists in the DAG,
1773/// this does not modify the specified node, instead it returns the node that
1774/// already exists. If the resultant node does not exist in the DAG, the
1775/// input node is returned. As a degenerate case, if you specify the same
1776/// input operands as the node already has, the input node is returned.
1777SDOperand SelectionDAG::
1778UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1779 SDNode *N = InN.Val;
1780 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1781
1782 // Check to see if there is no change.
1783 if (Op == N->getOperand(0)) return InN;
1784
1785 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001786 void *InsertPos = 0;
1787 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1788 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001789
1790 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001791 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001792 RemoveNodeFromCSEMaps(N);
1793
1794 // Now we update the operands.
1795 N->OperandList[0].Val->removeUser(N);
1796 Op.Val->addUser(N);
1797 N->OperandList[0] = Op;
1798
1799 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001800 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001801 return InN;
1802}
1803
1804SDOperand SelectionDAG::
1805UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1806 SDNode *N = InN.Val;
1807 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1808
1809 // Check to see if there is no change.
1810 bool AnyChange = false;
1811 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1812 return InN; // No operands changed, just return the input node.
1813
1814 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001815 void *InsertPos = 0;
1816 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1817 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001818
1819 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001820 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001821 RemoveNodeFromCSEMaps(N);
1822
1823 // Now we update the operands.
1824 if (N->OperandList[0] != Op1) {
1825 N->OperandList[0].Val->removeUser(N);
1826 Op1.Val->addUser(N);
1827 N->OperandList[0] = Op1;
1828 }
1829 if (N->OperandList[1] != Op2) {
1830 N->OperandList[1].Val->removeUser(N);
1831 Op2.Val->addUser(N);
1832 N->OperandList[1] = Op2;
1833 }
1834
1835 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001836 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001837 return InN;
1838}
1839
1840SDOperand SelectionDAG::
1841UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001842 SDOperand Ops[] = { Op1, Op2, Op3 };
1843 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001844}
1845
1846SDOperand SelectionDAG::
1847UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1848 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001849 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1850 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001851}
1852
1853SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001854UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1855 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001856 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1857 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001858}
1859
1860
1861SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001862UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001863 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001864 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001865 "Update with wrong number of operands");
1866
1867 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001868 bool AnyChange = false;
1869 for (unsigned i = 0; i != NumOps; ++i) {
1870 if (Ops[i] != N->getOperand(i)) {
1871 AnyChange = true;
1872 break;
1873 }
1874 }
1875
1876 // No operands changed, just return the input node.
1877 if (!AnyChange) return InN;
1878
1879 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001880 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001881 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001882 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001883
1884 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001885 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001886 RemoveNodeFromCSEMaps(N);
1887
1888 // Now we update the operands.
1889 for (unsigned i = 0; i != NumOps; ++i) {
1890 if (N->OperandList[i] != Ops[i]) {
1891 N->OperandList[i].Val->removeUser(N);
1892 Ops[i].Val->addUser(N);
1893 N->OperandList[i] = Ops[i];
1894 }
1895 }
1896
1897 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001898 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001899 return InN;
1900}
1901
1902
1903
Chris Lattner149c58c2005-08-16 18:17:10 +00001904
1905/// SelectNodeTo - These are used for target selectors to *mutate* the
1906/// specified node to have the specified return type, Target opcode, and
1907/// operands. Note that target opcodes are stored as
1908/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001909///
1910/// Note that SelectNodeTo returns the resultant node. If there is already a
1911/// node of the specified opcode and operands, it returns that node instead of
1912/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001913SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1914 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001915 // If an identical node already exists, use it.
1916 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1917 if (ON) return SDOperand(ON, 0);
1918
Chris Lattner7651fa42005-08-24 23:00:29 +00001919 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001920
Chris Lattner7651fa42005-08-24 23:00:29 +00001921 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001922 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001923
1924 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001925 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001926}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001927
Chris Lattnereb19e402005-11-30 22:45:14 +00001928SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1929 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001930 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001931 MVT::ValueType *VTs = getNodeValueTypes(VT);
1932 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1933 void *IP = 0;
1934 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1935 return SDOperand(ON, 0);
1936
Chris Lattner149c58c2005-08-16 18:17:10 +00001937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001939 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001940 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001941 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001942 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001943}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001944
Chris Lattnereb19e402005-11-30 22:45:14 +00001945SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1946 MVT::ValueType VT, SDOperand Op1,
1947 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001948 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001949 MVT::ValueType *VTs = getNodeValueTypes(VT);
1950 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1951 void *IP = 0;
1952 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1953 return SDOperand(ON, 0);
1954
Chris Lattner149c58c2005-08-16 18:17:10 +00001955 RemoveNodeFromCSEMaps(N);
1956 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001957 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001958 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001959
Chris Lattnera5682852006-08-07 23:03:03 +00001960 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001961 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001962}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001963
Chris Lattnereb19e402005-11-30 22:45:14 +00001964SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1965 MVT::ValueType VT, SDOperand Op1,
1966 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001967 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001968 MVT::ValueType *VTs = getNodeValueTypes(VT);
1969 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1970 void *IP = 0;
1971 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1972 return SDOperand(ON, 0);
1973
Chris Lattner149c58c2005-08-16 18:17:10 +00001974 RemoveNodeFromCSEMaps(N);
1975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001976 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001977 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001978
Chris Lattnera5682852006-08-07 23:03:03 +00001979 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001980 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001981}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001982
Chris Lattnereb19e402005-11-30 22:45:14 +00001983SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1984 MVT::ValueType VT, SDOperand Op1,
1985 SDOperand Op2, SDOperand Op3,
1986 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001987 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001988 MVT::ValueType *VTs = getNodeValueTypes(VT);
1989 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1990 ID.AddOperand(Op1);
1991 ID.AddOperand(Op2);
1992 ID.AddOperand(Op3);
1993 ID.AddOperand(Op4);
1994 void *IP = 0;
1995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1996 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001997
Nate Begeman294a0a12005-08-18 07:30:15 +00001998 RemoveNodeFromCSEMaps(N);
1999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002000 N->setValueTypes(VTs, 1);
Nate Begeman294a0a12005-08-18 07:30:15 +00002001 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002002
Chris Lattnera5682852006-08-07 23:03:03 +00002003 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002004 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002005}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002006
Chris Lattnereb19e402005-11-30 22:45:14 +00002007SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2008 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00002009 SDOperand Op2, SDOperand Op3,
2010 SDOperand Op4, SDOperand Op5) {
2011 MVT::ValueType *VTs = getNodeValueTypes(VT);
2012 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2013 ID.AddOperand(Op1);
2014 ID.AddOperand(Op2);
2015 ID.AddOperand(Op3);
2016 ID.AddOperand(Op4);
2017 ID.AddOperand(Op5);
2018 void *IP = 0;
2019 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2020 return SDOperand(ON, 0);
2021
Chris Lattner6b09a292005-08-21 18:49:33 +00002022 RemoveNodeFromCSEMaps(N);
2023 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002024 N->setValueTypes(VTs, 1);
Chris Lattner6b09a292005-08-21 18:49:33 +00002025 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026
Chris Lattnera5682852006-08-07 23:03:03 +00002027 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002028 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002029}
Chris Lattner149c58c2005-08-16 18:17:10 +00002030
Chris Lattnereb19e402005-11-30 22:45:14 +00002031SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2032 MVT::ValueType VT, SDOperand Op1,
2033 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2034 SDOperand Op5, SDOperand Op6) {
Chris Lattnera5682852006-08-07 23:03:03 +00002035 MVT::ValueType *VTs = getNodeValueTypes(VT);
2036 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2037 ID.AddOperand(Op1);
2038 ID.AddOperand(Op2);
2039 ID.AddOperand(Op3);
2040 ID.AddOperand(Op4);
2041 ID.AddOperand(Op5);
2042 ID.AddOperand(Op6);
2043 void *IP = 0;
2044 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2045 return SDOperand(ON, 0);
2046
Evan Cheng61ca74b2005-11-30 02:04:11 +00002047 RemoveNodeFromCSEMaps(N);
2048 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002049 N->setValueTypes(VTs, 1);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002050 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002051
Chris Lattnera5682852006-08-07 23:03:03 +00002052 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002053 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002054}
2055
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002056SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2057 MVT::ValueType VT, SDOperand Op1,
2058 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2059 SDOperand Op5, SDOperand Op6,
2060 SDOperand Op7) {
Chris Lattnera5682852006-08-07 23:03:03 +00002061 MVT::ValueType *VTs = getNodeValueTypes(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002062 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002063 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2064 ID.AddOperand(Op1);
2065 ID.AddOperand(Op2);
2066 ID.AddOperand(Op3);
2067 ID.AddOperand(Op4);
2068 ID.AddOperand(Op5);
2069 ID.AddOperand(Op6);
2070 ID.AddOperand(Op7);
2071 void *IP = 0;
2072 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2073 return SDOperand(ON, 0);
2074
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002075 RemoveNodeFromCSEMaps(N);
2076 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002077 N->setValueTypes(VTs, 1);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002078 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2079
Chris Lattnera5682852006-08-07 23:03:03 +00002080 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002081 return SDOperand(N, 0);
2082}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002083SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2084 MVT::ValueType VT, SDOperand Op1,
2085 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2086 SDOperand Op5, SDOperand Op6,
2087 SDOperand Op7, SDOperand Op8) {
2088 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002089 MVT::ValueType *VTs = getNodeValueTypes(VT);
2090 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2091 ID.AddOperand(Op1);
2092 ID.AddOperand(Op2);
2093 ID.AddOperand(Op3);
2094 ID.AddOperand(Op4);
2095 ID.AddOperand(Op5);
2096 ID.AddOperand(Op6);
2097 ID.AddOperand(Op7);
2098 ID.AddOperand(Op8);
2099 void *IP = 0;
2100 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2101 return SDOperand(ON, 0);
2102
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002103 RemoveNodeFromCSEMaps(N);
2104 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002105 N->setValueTypes(VTs, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002106 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2107
Chris Lattnera5682852006-08-07 23:03:03 +00002108 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002109 return SDOperand(N, 0);
2110}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002111
Chris Lattnereb19e402005-11-30 22:45:14 +00002112SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2113 MVT::ValueType VT1, MVT::ValueType VT2,
2114 SDOperand Op1, SDOperand Op2) {
Chris Lattnera5682852006-08-07 23:03:03 +00002115 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2116 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2117 void *IP = 0;
2118 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2119 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002120
Chris Lattner0fb094f2005-11-19 01:44:53 +00002121 RemoveNodeFromCSEMaps(N);
2122 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002123 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002124 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002125
Chris Lattnera5682852006-08-07 23:03:03 +00002126 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002127 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002128}
2129
Chris Lattnereb19e402005-11-30 22:45:14 +00002130SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2131 MVT::ValueType VT1, MVT::ValueType VT2,
2132 SDOperand Op1, SDOperand Op2,
2133 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002134 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002135 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2136 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2137 Op1, Op2, Op3);
2138 void *IP = 0;
2139 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2140 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002141
Chris Lattner0fb094f2005-11-19 01:44:53 +00002142 RemoveNodeFromCSEMaps(N);
2143 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002144 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002145 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002146
Chris Lattnera5682852006-08-07 23:03:03 +00002147 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002148 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002149}
2150
Chris Lattnereb19e402005-11-30 22:45:14 +00002151SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2152 MVT::ValueType VT1, MVT::ValueType VT2,
2153 SDOperand Op1, SDOperand Op2,
2154 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002155 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002156 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2157 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2158 ID.AddOperand(Op1);
2159 ID.AddOperand(Op2);
2160 ID.AddOperand(Op3);
2161 ID.AddOperand(Op4);
2162 void *IP = 0;
2163 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2164 return SDOperand(ON, 0);
2165
Chris Lattner0fb094f2005-11-19 01:44:53 +00002166 RemoveNodeFromCSEMaps(N);
2167 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002168 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002169 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002170
Chris Lattnera5682852006-08-07 23:03:03 +00002171 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002172 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002173}
2174
Chris Lattnereb19e402005-11-30 22:45:14 +00002175SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2176 MVT::ValueType VT1, MVT::ValueType VT2,
2177 SDOperand Op1, SDOperand Op2,
2178 SDOperand Op3, SDOperand Op4,
2179 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002180 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002181 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2182 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2183 ID.AddOperand(Op1);
2184 ID.AddOperand(Op2);
2185 ID.AddOperand(Op3);
2186 ID.AddOperand(Op4);
2187 ID.AddOperand(Op5);
2188 void *IP = 0;
2189 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2190 return SDOperand(ON, 0);
2191
Chris Lattner0fb094f2005-11-19 01:44:53 +00002192 RemoveNodeFromCSEMaps(N);
2193 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002194 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002195 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002196
Chris Lattnera5682852006-08-07 23:03:03 +00002197 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002198 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002199}
2200
Evan Cheng6ae46c42006-02-09 07:15:23 +00002201/// getTargetNode - These are used for target selectors to create a new node
2202/// with specified return type(s), target opcode, and operands.
2203///
2204/// Note that getTargetNode returns the resultant node. If there is already a
2205/// node of the specified opcode and operands, it returns that node instead of
2206/// the current one.
2207SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2208 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2209}
2210SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2211 SDOperand Op1) {
2212 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2213}
2214SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2215 SDOperand Op1, SDOperand Op2) {
2216 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2217}
2218SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2219 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2223 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2224 SDOperand Op4) {
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2228 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2229 SDOperand Op4, SDOperand Op5) {
2230 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2231}
2232SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2233 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002234 SDOperand Op4, SDOperand Op5,
2235 SDOperand Op6) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002236 std::vector<SDOperand> Ops;
2237 Ops.reserve(6);
2238 Ops.push_back(Op1);
2239 Ops.push_back(Op2);
2240 Ops.push_back(Op3);
2241 Ops.push_back(Op4);
2242 Ops.push_back(Op5);
2243 Ops.push_back(Op6);
2244 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2245}
2246SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2247 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2248 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2249 SDOperand Op7) {
2250 std::vector<SDOperand> Ops;
2251 Ops.reserve(7);
2252 Ops.push_back(Op1);
2253 Ops.push_back(Op2);
2254 Ops.push_back(Op3);
2255 Ops.push_back(Op4);
2256 Ops.push_back(Op5);
2257 Ops.push_back(Op6);
2258 Ops.push_back(Op7);
2259 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2260}
2261SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2262 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2263 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2264 SDOperand Op7, SDOperand Op8) {
2265 std::vector<SDOperand> Ops;
2266 Ops.reserve(8);
2267 Ops.push_back(Op1);
2268 Ops.push_back(Op2);
2269 Ops.push_back(Op3);
2270 Ops.push_back(Op4);
2271 Ops.push_back(Op5);
2272 Ops.push_back(Op6);
2273 Ops.push_back(Op7);
2274 Ops.push_back(Op8);
2275 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2276}
2277SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2278 std::vector<SDOperand> &Ops) {
2279 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2280}
2281SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2282 MVT::ValueType VT2, SDOperand Op1) {
2283 std::vector<MVT::ValueType> ResultTys;
2284 ResultTys.push_back(VT1);
2285 ResultTys.push_back(VT2);
2286 std::vector<SDOperand> Ops;
2287 Ops.push_back(Op1);
2288 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2289}
2290SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002291 MVT::ValueType VT2, SDOperand Op1,
2292 SDOperand Op2) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002293 std::vector<MVT::ValueType> ResultTys;
2294 ResultTys.push_back(VT1);
2295 ResultTys.push_back(VT2);
2296 std::vector<SDOperand> Ops;
2297 Ops.push_back(Op1);
2298 Ops.push_back(Op2);
2299 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2300}
2301SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002302 MVT::ValueType VT2, SDOperand Op1,
2303 SDOperand Op2, SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002304 std::vector<MVT::ValueType> ResultTys;
2305 ResultTys.push_back(VT1);
2306 ResultTys.push_back(VT2);
2307 std::vector<SDOperand> Ops;
2308 Ops.push_back(Op1);
2309 Ops.push_back(Op2);
2310 Ops.push_back(Op3);
2311 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2312}
2313SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002314 MVT::ValueType VT2, SDOperand Op1,
2315 SDOperand Op2, SDOperand Op3,
2316 SDOperand Op4) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002317 std::vector<MVT::ValueType> ResultTys;
2318 ResultTys.push_back(VT1);
2319 ResultTys.push_back(VT2);
2320 std::vector<SDOperand> Ops;
2321 Ops.push_back(Op1);
2322 Ops.push_back(Op2);
2323 Ops.push_back(Op3);
2324 Ops.push_back(Op4);
2325 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2326}
2327SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002328 MVT::ValueType VT2, SDOperand Op1,
2329 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2330 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002331 std::vector<MVT::ValueType> ResultTys;
2332 ResultTys.push_back(VT1);
2333 ResultTys.push_back(VT2);
2334 std::vector<SDOperand> Ops;
2335 Ops.push_back(Op1);
2336 Ops.push_back(Op2);
2337 Ops.push_back(Op3);
2338 Ops.push_back(Op4);
2339 Ops.push_back(Op5);
2340 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2341}
2342SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002343 MVT::ValueType VT2, SDOperand Op1,
2344 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2345 SDOperand Op5, SDOperand Op6) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002346 std::vector<MVT::ValueType> ResultTys;
2347 ResultTys.push_back(VT1);
2348 ResultTys.push_back(VT2);
2349 std::vector<SDOperand> Ops;
2350 Ops.push_back(Op1);
2351 Ops.push_back(Op2);
2352 Ops.push_back(Op3);
2353 Ops.push_back(Op4);
2354 Ops.push_back(Op5);
2355 Ops.push_back(Op6);
2356 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2357}
2358SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002359 MVT::ValueType VT2, SDOperand Op1,
2360 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2361 SDOperand Op5, SDOperand Op6,
2362 SDOperand Op7) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002363 std::vector<MVT::ValueType> ResultTys;
2364 ResultTys.push_back(VT1);
2365 ResultTys.push_back(VT2);
2366 std::vector<SDOperand> Ops;
2367 Ops.push_back(Op1);
2368 Ops.push_back(Op2);
2369 Ops.push_back(Op3);
2370 Ops.push_back(Op4);
2371 Ops.push_back(Op5);
2372 Ops.push_back(Op6);
2373 Ops.push_back(Op7);
2374 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2375}
2376SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2377 MVT::ValueType VT2, MVT::ValueType VT3,
2378 SDOperand Op1, SDOperand Op2) {
2379 std::vector<MVT::ValueType> ResultTys;
2380 ResultTys.push_back(VT1);
2381 ResultTys.push_back(VT2);
2382 ResultTys.push_back(VT3);
2383 std::vector<SDOperand> Ops;
2384 Ops.push_back(Op1);
2385 Ops.push_back(Op2);
2386 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2387}
2388SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2389 MVT::ValueType VT2, MVT::ValueType VT3,
2390 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002391 SDOperand Op3, SDOperand Op4,
2392 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002393 std::vector<MVT::ValueType> ResultTys;
2394 ResultTys.push_back(VT1);
2395 ResultTys.push_back(VT2);
2396 ResultTys.push_back(VT3);
2397 std::vector<SDOperand> Ops;
2398 Ops.push_back(Op1);
2399 Ops.push_back(Op2);
2400 Ops.push_back(Op3);
2401 Ops.push_back(Op4);
2402 Ops.push_back(Op5);
2403 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2404}
2405SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2406 MVT::ValueType VT2, MVT::ValueType VT3,
2407 SDOperand Op1, SDOperand Op2,
2408 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2409 SDOperand Op6) {
2410 std::vector<MVT::ValueType> ResultTys;
2411 ResultTys.push_back(VT1);
2412 ResultTys.push_back(VT2);
2413 ResultTys.push_back(VT3);
2414 std::vector<SDOperand> Ops;
2415 Ops.push_back(Op1);
2416 Ops.push_back(Op2);
2417 Ops.push_back(Op3);
2418 Ops.push_back(Op4);
2419 Ops.push_back(Op5);
2420 Ops.push_back(Op6);
2421 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2422}
2423SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2424 MVT::ValueType VT2, MVT::ValueType VT3,
2425 SDOperand Op1, SDOperand Op2,
2426 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2427 SDOperand Op6, SDOperand Op7) {
2428 std::vector<MVT::ValueType> ResultTys;
2429 ResultTys.push_back(VT1);
2430 ResultTys.push_back(VT2);
2431 ResultTys.push_back(VT3);
2432 std::vector<SDOperand> Ops;
2433 Ops.push_back(Op1);
2434 Ops.push_back(Op2);
2435 Ops.push_back(Op3);
2436 Ops.push_back(Op4);
2437 Ops.push_back(Op5);
2438 Ops.push_back(Op6);
2439 Ops.push_back(Op7);
2440 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2441}
2442SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002443 MVT::ValueType VT2,
2444 std::vector<SDOperand> &Ops) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002445 std::vector<MVT::ValueType> ResultTys;
2446 ResultTys.push_back(VT1);
2447 ResultTys.push_back(VT2);
2448 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2449}
2450
Evan Cheng99157a02006-08-07 22:13:29 +00002451/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002452/// This can cause recursive merging of nodes in the DAG.
2453///
Chris Lattner8b52f212005-08-26 18:36:28 +00002454/// This version assumes From/To have a single result value.
2455///
Chris Lattner1e111c72005-09-07 05:37:01 +00002456void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2457 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002458 SDNode *From = FromN.Val, *To = ToN.Val;
2459 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2460 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002461 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002462
Chris Lattner8b8749f2005-08-17 19:00:20 +00002463 while (!From->use_empty()) {
2464 // Process users until they are all gone.
2465 SDNode *U = *From->use_begin();
2466
2467 // This node is about to morph, remove its old self from the CSE maps.
2468 RemoveNodeFromCSEMaps(U);
2469
Chris Lattner65113b22005-11-08 22:07:03 +00002470 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2471 I != E; ++I)
2472 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002473 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002474 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002475 To->addUser(U);
2476 }
2477
2478 // Now that we have modified U, add it back to the CSE maps. If it already
2479 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002480 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2481 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002482 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002483 if (Deleted) Deleted->push_back(U);
2484 DeleteNodeNotInCSEMaps(U);
2485 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002486 }
2487}
2488
2489/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2490/// This can cause recursive merging of nodes in the DAG.
2491///
2492/// This version assumes From/To have matching types and numbers of result
2493/// values.
2494///
Chris Lattner1e111c72005-09-07 05:37:01 +00002495void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2496 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002497 assert(From != To && "Cannot replace uses of with self");
2498 assert(From->getNumValues() == To->getNumValues() &&
2499 "Cannot use this version of ReplaceAllUsesWith!");
2500 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002501 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002502 return;
2503 }
2504
2505 while (!From->use_empty()) {
2506 // Process users until they are all gone.
2507 SDNode *U = *From->use_begin();
2508
2509 // This node is about to morph, remove its old self from the CSE maps.
2510 RemoveNodeFromCSEMaps(U);
2511
Chris Lattner65113b22005-11-08 22:07:03 +00002512 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2513 I != E; ++I)
2514 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002515 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002516 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002517 To->addUser(U);
2518 }
2519
2520 // Now that we have modified U, add it back to the CSE maps. If it already
2521 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002522 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2523 ReplaceAllUsesWith(U, Existing, Deleted);
2524 // U is now dead.
2525 if (Deleted) Deleted->push_back(U);
2526 DeleteNodeNotInCSEMaps(U);
2527 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002528 }
2529}
2530
Chris Lattner8b52f212005-08-26 18:36:28 +00002531/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2532/// This can cause recursive merging of nodes in the DAG.
2533///
2534/// This version can replace From with any result values. To must match the
2535/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002536void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002537 const std::vector<SDOperand> &To,
2538 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002539 assert(From->getNumValues() == To.size() &&
2540 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002541 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002542 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002543 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002544 return;
2545 }
2546
2547 while (!From->use_empty()) {
2548 // Process users until they are all gone.
2549 SDNode *U = *From->use_begin();
2550
2551 // This node is about to morph, remove its old self from the CSE maps.
2552 RemoveNodeFromCSEMaps(U);
2553
Chris Lattner65113b22005-11-08 22:07:03 +00002554 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2555 I != E; ++I)
2556 if (I->Val == From) {
2557 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002558 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002559 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002560 ToOp.Val->addUser(U);
2561 }
2562
2563 // Now that we have modified U, add it back to the CSE maps. If it already
2564 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002565 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2566 ReplaceAllUsesWith(U, Existing, Deleted);
2567 // U is now dead.
2568 if (Deleted) Deleted->push_back(U);
2569 DeleteNodeNotInCSEMaps(U);
2570 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002571 }
2572}
2573
Chris Lattner012f2412006-02-17 21:58:01 +00002574/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2575/// uses of other values produced by From.Val alone. The Deleted vector is
2576/// handled the same was as for ReplaceAllUsesWith.
2577void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2578 std::vector<SDNode*> &Deleted) {
2579 assert(From != To && "Cannot replace a value with itself");
2580 // Handle the simple, trivial, case efficiently.
2581 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2582 ReplaceAllUsesWith(From, To, &Deleted);
2583 return;
2584 }
2585
2586 // Get all of the users in a nice, deterministically ordered, uniqued set.
2587 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2588
2589 while (!Users.empty()) {
2590 // We know that this user uses some value of From. If it is the right
2591 // value, update it.
2592 SDNode *User = Users.back();
2593 Users.pop_back();
2594
2595 for (SDOperand *Op = User->OperandList,
2596 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2597 if (*Op == From) {
2598 // Okay, we know this user needs to be updated. Remove its old self
2599 // from the CSE maps.
2600 RemoveNodeFromCSEMaps(User);
2601
2602 // Update all operands that match "From".
2603 for (; Op != E; ++Op) {
2604 if (*Op == From) {
2605 From.Val->removeUser(User);
2606 *Op = To;
2607 To.Val->addUser(User);
2608 }
2609 }
2610
2611 // Now that we have modified User, add it back to the CSE maps. If it
2612 // already exists there, recursively merge the results together.
2613 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2614 unsigned NumDeleted = Deleted.size();
2615 ReplaceAllUsesWith(User, Existing, &Deleted);
2616
2617 // User is now dead.
2618 Deleted.push_back(User);
2619 DeleteNodeNotInCSEMaps(User);
2620
2621 // We have to be careful here, because ReplaceAllUsesWith could have
2622 // deleted a user of From, which means there may be dangling pointers
2623 // in the "Users" setvector. Scan over the deleted node pointers and
2624 // remove them from the setvector.
2625 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2626 Users.remove(Deleted[i]);
2627 }
2628 break; // Exit the operand scanning loop.
2629 }
2630 }
2631 }
2632}
2633
Chris Lattner7b2880c2005-08-24 22:44:39 +00002634
Evan Chenge6f35d82006-08-01 08:20:41 +00002635/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2636/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002637unsigned SelectionDAG::AssignNodeIds() {
2638 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002639 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2640 SDNode *N = I;
2641 N->setNodeId(Id++);
2642 }
2643 return Id;
2644}
2645
Evan Chenge6f35d82006-08-01 08:20:41 +00002646/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002647/// based on their topological order. It returns the maximum id and a vector
2648/// of the SDNodes* in assigned order by reference.
2649unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002650 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002651 std::vector<unsigned> InDegree(DAGSize);
2652 std::vector<SDNode*> Sources;
2653
2654 // Use a two pass approach to avoid using a std::map which is slow.
2655 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002656 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2657 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002658 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002659 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002660 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002661 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002662 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002663 }
2664
Evan Cheng99157a02006-08-07 22:13:29 +00002665 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002666 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002667 SDNode *N = Sources.back();
2668 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002669 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002670 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2671 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002672 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002673 if (Degree == 0)
2674 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002675 }
2676 }
2677
Evan Chengc384d6c2006-08-02 22:00:34 +00002678 // Second pass, assign the actual topological order as node ids.
2679 Id = 0;
2680 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2681 TI != TE; ++TI)
2682 (*TI)->setNodeId(Id++);
2683
2684 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002685}
2686
2687
Evan Cheng091cba12006-07-27 06:39:06 +00002688
Jim Laskey58b968b2005-08-17 20:08:02 +00002689//===----------------------------------------------------------------------===//
2690// SDNode Class
2691//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002692
Chris Lattner917d2c92006-07-19 00:00:37 +00002693// Out-of-line virtual method to give class a home.
2694void SDNode::ANCHOR() {
2695}
2696
Chris Lattnera3255112005-11-08 23:30:28 +00002697/// getValueTypeList - Return a pointer to the specified value type.
2698///
2699MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2700 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2701 VTs[VT] = VT;
2702 return &VTs[VT];
2703}
Chris Lattnera5682852006-08-07 23:03:03 +00002704
Chris Lattner5c884562005-01-12 18:37:47 +00002705/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2706/// indicated value. This method ignores uses of other values defined by this
2707/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002708bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002709 assert(Value < getNumValues() && "Bad value!");
2710
2711 // If there is only one value, this is easy.
2712 if (getNumValues() == 1)
2713 return use_size() == NUses;
2714 if (Uses.size() < NUses) return false;
2715
Evan Cheng4ee62112006-02-05 06:29:23 +00002716 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002717
2718 std::set<SDNode*> UsersHandled;
2719
Evan Cheng4ee62112006-02-05 06:29:23 +00002720 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002721 UI != E; ++UI) {
2722 SDNode *User = *UI;
2723 if (User->getNumOperands() == 1 ||
2724 UsersHandled.insert(User).second) // First time we've seen this?
2725 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2726 if (User->getOperand(i) == TheValue) {
2727 if (NUses == 0)
2728 return false; // too many uses
2729 --NUses;
2730 }
2731 }
2732
2733 // Found exactly the right number of uses?
2734 return NUses == 0;
2735}
2736
2737
Evan Cheng4ee62112006-02-05 06:29:23 +00002738// isOnlyUse - Return true if this node is the only use of N.
2739bool SDNode::isOnlyUse(SDNode *N) const {
2740 bool Seen = false;
2741 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2742 SDNode *User = *I;
2743 if (User == this)
2744 Seen = true;
2745 else
2746 return false;
2747 }
2748
2749 return Seen;
2750}
2751
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002752// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002753bool SDOperand::isOperand(SDNode *N) const {
2754 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2755 if (*this == N->getOperand(i))
2756 return true;
2757 return false;
2758}
2759
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002760bool SDNode::isOperand(SDNode *N) const {
2761 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2762 if (this == N->OperandList[i].Val)
2763 return true;
2764 return false;
2765}
Evan Cheng4ee62112006-02-05 06:29:23 +00002766
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002767const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002768 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002769 default:
2770 if (getOpcode() < ISD::BUILTIN_OP_END)
2771 return "<<Unknown DAG Node>>";
2772 else {
Evan Cheng72261582005-12-20 06:22:03 +00002773 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002774 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002775 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2776 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002777
Evan Cheng72261582005-12-20 06:22:03 +00002778 TargetLowering &TLI = G->getTargetLoweringInfo();
2779 const char *Name =
2780 TLI.getTargetNodeName(getOpcode());
2781 if (Name) return Name;
2782 }
2783
2784 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002785 }
2786
Andrew Lenharth95762122005-03-31 21:24:06 +00002787 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002788 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002789 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002790 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002791 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002792 case ISD::AssertSext: return "AssertSext";
2793 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002794
2795 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002796 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002797 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002798 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002799
2800 case ISD::Constant: return "Constant";
2801 case ISD::ConstantFP: return "ConstantFP";
2802 case ISD::GlobalAddress: return "GlobalAddress";
2803 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002804 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002805 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002806 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002807 case ISD::INTRINSIC_WO_CHAIN: {
2808 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2809 return Intrinsic::getName((Intrinsic::ID)IID);
2810 }
2811 case ISD::INTRINSIC_VOID:
2812 case ISD::INTRINSIC_W_CHAIN: {
2813 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002814 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002815 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002816
Chris Lattnerb2827b02006-03-19 00:52:58 +00002817 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002818 case ISD::TargetConstant: return "TargetConstant";
2819 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002820 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2821 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002822 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002823 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002824 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002825
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002826 case ISD::CopyToReg: return "CopyToReg";
2827 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002828 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002829 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002830 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002831 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002832 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002833 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002834
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002835 // Unary operators
2836 case ISD::FABS: return "fabs";
2837 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002838 case ISD::FSQRT: return "fsqrt";
2839 case ISD::FSIN: return "fsin";
2840 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002841
2842 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002843 case ISD::ADD: return "add";
2844 case ISD::SUB: return "sub";
2845 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002846 case ISD::MULHU: return "mulhu";
2847 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002848 case ISD::SDIV: return "sdiv";
2849 case ISD::UDIV: return "udiv";
2850 case ISD::SREM: return "srem";
2851 case ISD::UREM: return "urem";
2852 case ISD::AND: return "and";
2853 case ISD::OR: return "or";
2854 case ISD::XOR: return "xor";
2855 case ISD::SHL: return "shl";
2856 case ISD::SRA: return "sra";
2857 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002858 case ISD::ROTL: return "rotl";
2859 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002860 case ISD::FADD: return "fadd";
2861 case ISD::FSUB: return "fsub";
2862 case ISD::FMUL: return "fmul";
2863 case ISD::FDIV: return "fdiv";
2864 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002865 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002866 case ISD::VADD: return "vadd";
2867 case ISD::VSUB: return "vsub";
2868 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002869 case ISD::VSDIV: return "vsdiv";
2870 case ISD::VUDIV: return "vudiv";
2871 case ISD::VAND: return "vand";
2872 case ISD::VOR: return "vor";
2873 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002874
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002875 case ISD::SETCC: return "setcc";
2876 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002877 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002878 case ISD::VSELECT: return "vselect";
2879 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2880 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2881 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002882 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002883 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2884 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2885 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2886 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2887 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002888 case ISD::ADDC: return "addc";
2889 case ISD::ADDE: return "adde";
2890 case ISD::SUBC: return "subc";
2891 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002892 case ISD::SHL_PARTS: return "shl_parts";
2893 case ISD::SRA_PARTS: return "sra_parts";
2894 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002895
Chris Lattner7f644642005-04-28 21:44:03 +00002896 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002897 case ISD::SIGN_EXTEND: return "sign_extend";
2898 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002899 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002900 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002901 case ISD::TRUNCATE: return "truncate";
2902 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002903 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002904 case ISD::FP_EXTEND: return "fp_extend";
2905
2906 case ISD::SINT_TO_FP: return "sint_to_fp";
2907 case ISD::UINT_TO_FP: return "uint_to_fp";
2908 case ISD::FP_TO_SINT: return "fp_to_sint";
2909 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002910 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002911
2912 // Control flow instructions
2913 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002914 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002915 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002916 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002917 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002918 case ISD::CALLSEQ_START: return "callseq_start";
2919 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002920
2921 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002922 case ISD::LOAD: return "load";
2923 case ISD::STORE: return "store";
2924 case ISD::VLOAD: return "vload";
2925 case ISD::EXTLOAD: return "extload";
2926 case ISD::SEXTLOAD: return "sextload";
2927 case ISD::ZEXTLOAD: return "zextload";
2928 case ISD::TRUNCSTORE: return "truncstore";
2929 case ISD::VAARG: return "vaarg";
2930 case ISD::VACOPY: return "vacopy";
2931 case ISD::VAEND: return "vaend";
2932 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002933 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002934 case ISD::EXTRACT_ELEMENT: return "extract_element";
2935 case ISD::BUILD_PAIR: return "build_pair";
2936 case ISD::STACKSAVE: return "stacksave";
2937 case ISD::STACKRESTORE: return "stackrestore";
2938
2939 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002940 case ISD::MEMSET: return "memset";
2941 case ISD::MEMCPY: return "memcpy";
2942 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002943
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002944 // Bit manipulation
2945 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002946 case ISD::CTPOP: return "ctpop";
2947 case ISD::CTTZ: return "cttz";
2948 case ISD::CTLZ: return "ctlz";
2949
Chris Lattner36ce6912005-11-29 06:21:05 +00002950 // Debug info
2951 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002952 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002953 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002954
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002955 case ISD::CONDCODE:
2956 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002957 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002958 case ISD::SETOEQ: return "setoeq";
2959 case ISD::SETOGT: return "setogt";
2960 case ISD::SETOGE: return "setoge";
2961 case ISD::SETOLT: return "setolt";
2962 case ISD::SETOLE: return "setole";
2963 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002964
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002965 case ISD::SETO: return "seto";
2966 case ISD::SETUO: return "setuo";
2967 case ISD::SETUEQ: return "setue";
2968 case ISD::SETUGT: return "setugt";
2969 case ISD::SETUGE: return "setuge";
2970 case ISD::SETULT: return "setult";
2971 case ISD::SETULE: return "setule";
2972 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002973
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002974 case ISD::SETEQ: return "seteq";
2975 case ISD::SETGT: return "setgt";
2976 case ISD::SETGE: return "setge";
2977 case ISD::SETLT: return "setlt";
2978 case ISD::SETLE: return "setle";
2979 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002980 }
2981 }
2982}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002983
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002984void SDNode::dump() const { dump(0); }
2985void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002986 std::cerr << (void*)this << ": ";
2987
2988 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2989 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002990 if (getValueType(i) == MVT::Other)
2991 std::cerr << "ch";
2992 else
2993 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002994 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002995 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002996
2997 std::cerr << " ";
2998 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2999 if (i) std::cerr << ", ";
3000 std::cerr << (void*)getOperand(i).Val;
3001 if (unsigned RN = getOperand(i).ResNo)
3002 std::cerr << ":" << RN;
3003 }
3004
3005 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3006 std::cerr << "<" << CSDN->getValue() << ">";
3007 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3008 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003009 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003010 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003011 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003012 std::cerr << "<";
3013 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003014 if (offset > 0)
3015 std::cerr << " + " << offset;
3016 else
3017 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003018 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003019 std::cerr << "<" << FIDN->getIndex() << ">";
3020 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003021 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003022 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003023 if (offset > 0)
3024 std::cerr << " + " << offset;
3025 else
3026 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003027 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003028 std::cerr << "<";
3029 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3030 if (LBB)
3031 std::cerr << LBB->getName() << " ";
3032 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003033 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003034 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003035 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3036 } else {
3037 std::cerr << " #" << R->getReg();
3038 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003039 } else if (const ExternalSymbolSDNode *ES =
3040 dyn_cast<ExternalSymbolSDNode>(this)) {
3041 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003042 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3043 if (M->getValue())
3044 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3045 else
3046 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003047 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3048 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003050}
3051
Chris Lattnerde202b32005-11-09 23:47:37 +00003052static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003053 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3054 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003055 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003056 else
3057 std::cerr << "\n" << std::string(indent+2, ' ')
3058 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003059
Chris Lattnerea946cd2005-01-09 20:38:33 +00003060
3061 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003062 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003063}
3064
Chris Lattnerc3aae252005-01-07 07:46:32 +00003065void SelectionDAG::dump() const {
3066 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003067 std::vector<const SDNode*> Nodes;
3068 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3069 I != E; ++I)
3070 Nodes.push_back(I);
3071
Chris Lattner49d24712005-01-09 20:26:36 +00003072 std::sort(Nodes.begin(), Nodes.end());
3073
3074 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003075 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003076 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003077 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003078
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003079 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003080
Chris Lattnerc3aae252005-01-07 07:46:32 +00003081 std::cerr << "\n\n";
3082}
3083
Evan Chengfae9f1c2006-02-09 22:11:03 +00003084/// InsertISelMapEntry - A helper function to insert a key / element pair
3085/// into a SDOperand to SDOperand map. This is added to avoid the map
3086/// insertion operator from being inlined.
3087void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3088 SDNode *Key, unsigned KeyResNo,
3089 SDNode *Element, unsigned ElementResNo) {
3090 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3091 SDOperand(Element, ElementResNo)));
3092}