blob: 4175c38214dbd5ec3de831aaab24479700f87906 [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 Lattner4a283e92006-08-11 18:38:11 +0000433 // Remove it from the CSE Map.
434 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000435 break;
436 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000437#ifndef NDEBUG
438 // Verify that the node was actually in one of the CSE maps, unless it has a
439 // flag result (which cannot be CSE'd) or is one of the special cases that are
440 // not subject to CSE.
441 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000442 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000443 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000444 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000445 assert(0 && "Node is not in map!");
446 }
447#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000448}
449
Chris Lattner8b8749f2005-08-17 19:00:20 +0000450/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
451/// has been taken out and modified in some way. If the specified node already
452/// exists in the CSE maps, do not modify the maps, but return the existing node
453/// instead. If it doesn't exist, add it and return null.
454///
455SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
456 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000457 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000458 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000459
Chris Lattner9f8cc692005-12-19 22:21:21 +0000460 // Check that remaining values produced are not flags.
461 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
462 if (N->getValueType(i) == MVT::Flag)
463 return 0; // Never CSE anything that produces a flag.
464
Chris Lattnera5682852006-08-07 23:03:03 +0000465 SDNode *New = CSEMap.GetOrInsertNode(N);
466 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000467 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000468}
469
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000470/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
471/// were replaced with those specified. If this node is never memoized,
472/// return null, otherwise return a pointer to the slot it would take. If a
473/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000474SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
475 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000476 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000477 return 0; // Never add these nodes.
478
479 // Check that remaining values produced are not flags.
480 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
481 if (N->getValueType(i) == MVT::Flag)
482 return 0; // Never CSE anything that produces a flag.
483
Chris Lattnera5682852006-08-07 23:03:03 +0000484 SelectionDAGCSEMap::NodeID ID;
485 ID.SetOpcode(N->getOpcode());
486 ID.SetValueTypes(N->value_begin());
487 ID.SetOperands(Op);
488 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000489}
490
491/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
492/// were replaced with those specified. If this node is never memoized,
493/// return null, otherwise return a pointer to the slot it would take. If a
494/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000495SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
496 SDOperand Op1, SDOperand Op2,
497 void *&InsertPos) {
498 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
499 return 0; // Never add these nodes.
500
501 // Check that remaining values produced are not flags.
502 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
503 if (N->getValueType(i) == MVT::Flag)
504 return 0; // Never CSE anything that produces a flag.
505
506 SelectionDAGCSEMap::NodeID ID;
507 ID.SetOpcode(N->getOpcode());
508 ID.SetValueTypes(N->value_begin());
509 ID.SetOperands(Op1, Op2);
510 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
511}
512
513
514/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
515/// were replaced with those specified. If this node is never memoized,
516/// return null, otherwise return a pointer to the slot it would take. If a
517/// node already exists with these operands, the slot will be non-null.
518SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000519 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000520 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000521 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000522 return 0; // Never add these nodes.
523
524 // Check that remaining values produced are not flags.
525 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
526 if (N->getValueType(i) == MVT::Flag)
527 return 0; // Never CSE anything that produces a flag.
528
Chris Lattnera5682852006-08-07 23:03:03 +0000529 SelectionDAGCSEMap::NodeID ID;
530 ID.SetOpcode(N->getOpcode());
531 ID.SetValueTypes(N->value_begin());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000532 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000533 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000534}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000535
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536
Chris Lattner78ec3112003-08-11 14:57:33 +0000537SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000538 while (!AllNodes.empty()) {
539 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000540 delete [] N->OperandList;
541 N->OperandList = 0;
542 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000543 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000544 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000545}
546
Chris Lattner0f2287b2005-04-13 02:38:18 +0000547SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000548 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000549 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000550 return getNode(ISD::AND, Op.getValueType(), Op,
551 getConstant(Imm, Op.getValueType()));
552}
553
Chris Lattnerc3aae252005-01-07 07:46:32 +0000554SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
555 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000556 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
557
Chris Lattnerc3aae252005-01-07 07:46:32 +0000558 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000559 if (VT != MVT::i64)
560 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000561
Chris Lattnerc3aae252005-01-07 07:46:32 +0000562 SDNode *&N = Constants[std::make_pair(Val, VT)];
563 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000564 N = new ConstantSDNode(false, Val, VT);
565 AllNodes.push_back(N);
566 return SDOperand(N, 0);
567}
568
Chris Lattner36ce6912005-11-29 06:21:05 +0000569SDOperand SelectionDAG::getString(const std::string &Val) {
570 StringSDNode *&N = StringNodes[Val];
571 if (!N) {
572 N = new StringSDNode(Val);
573 AllNodes.push_back(N);
574 }
575 return SDOperand(N, 0);
576}
577
Chris Lattner37bfbb42005-08-17 00:34:06 +0000578SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
579 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
580 // Mask out any bits that are not valid for this constant.
581 if (VT != MVT::i64)
582 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
583
584 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
585 if (N) return SDOperand(N, 0);
586 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000587 AllNodes.push_back(N);
588 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000589}
590
Chris Lattnerc3aae252005-01-07 07:46:32 +0000591SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
592 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
593 if (VT == MVT::f32)
594 Val = (float)Val; // Mask out extra precision.
595
Chris Lattnerd8658612005-02-17 20:17:32 +0000596 // Do the map lookup using the actual bit pattern for the floating point
597 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
598 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000599 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000600 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000601 N = new ConstantFPSDNode(false, Val, VT);
602 AllNodes.push_back(N);
603 return SDOperand(N, 0);
604}
605
606SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
607 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
608 if (VT == MVT::f32)
609 Val = (float)Val; // Mask out extra precision.
610
611 // Do the map lookup using the actual bit pattern for the floating point
612 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
613 // we don't have issues with SNANs.
614 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
615 if (N) return SDOperand(N, 0);
616 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 AllNodes.push_back(N);
618 return SDOperand(N, 0);
619}
620
Chris Lattnerc3aae252005-01-07 07:46:32 +0000621SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000622 MVT::ValueType VT, int offset) {
623 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000625 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000626 AllNodes.push_back(N);
627 return SDOperand(N, 0);
628}
629
630SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000631 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000632 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000633 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000634 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000635 AllNodes.push_back(N);
636 return SDOperand(N, 0);
637}
638
639SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
640 SDNode *&N = FrameIndices[FI];
641 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000642 N = new FrameIndexSDNode(FI, VT, false);
643 AllNodes.push_back(N);
644 return SDOperand(N, 0);
645}
646
647SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
648 SDNode *&N = TargetFrameIndices[FI];
649 if (N) return SDOperand(N, 0);
650 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651 AllNodes.push_back(N);
652 return SDOperand(N, 0);
653}
654
Nate Begeman37efe672006-04-22 18:53:45 +0000655SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
656 SDNode *&N = JumpTableIndices[JTI];
657 if (N) return SDOperand(N, 0);
658 N = new JumpTableSDNode(JTI, VT, false);
659 AllNodes.push_back(N);
660 return SDOperand(N, 0);
661}
662
663SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
664 SDNode *&N = TargetJumpTableIndices[JTI];
665 if (N) return SDOperand(N, 0);
666 N = new JumpTableSDNode(JTI, VT, true);
667 AllNodes.push_back(N);
668 return SDOperand(N, 0);
669}
670
Evan Chengb8973bd2006-01-31 22:23:14 +0000671SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000672 unsigned Alignment, int Offset) {
673 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
674 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000675 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000676 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000677 AllNodes.push_back(N);
678 return SDOperand(N, 0);
679}
680
Evan Chengb8973bd2006-01-31 22:23:14 +0000681SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000682 unsigned Alignment, int Offset) {
683 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
684 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000685 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000686 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000687 AllNodes.push_back(N);
688 return SDOperand(N, 0);
689}
690
691SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
692 SDNode *&N = BBNodes[MBB];
693 if (N) return SDOperand(N, 0);
694 N = new BasicBlockSDNode(MBB);
695 AllNodes.push_back(N);
696 return SDOperand(N, 0);
697}
698
Chris Lattner15e4b012005-07-10 00:07:11 +0000699SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
700 if ((unsigned)VT >= ValueTypeNodes.size())
701 ValueTypeNodes.resize(VT+1);
702 if (ValueTypeNodes[VT] == 0) {
703 ValueTypeNodes[VT] = new VTSDNode(VT);
704 AllNodes.push_back(ValueTypeNodes[VT]);
705 }
706
707 return SDOperand(ValueTypeNodes[VT], 0);
708}
709
Chris Lattnerc3aae252005-01-07 07:46:32 +0000710SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
711 SDNode *&N = ExternalSymbols[Sym];
712 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000713 N = new ExternalSymbolSDNode(false, Sym, VT);
714 AllNodes.push_back(N);
715 return SDOperand(N, 0);
716}
717
Chris Lattner809ec112006-01-28 10:09:25 +0000718SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
719 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000720 SDNode *&N = TargetExternalSymbols[Sym];
721 if (N) return SDOperand(N, 0);
722 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
725}
726
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000727SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
728 if ((unsigned)Cond >= CondCodeNodes.size())
729 CondCodeNodes.resize(Cond+1);
730
Chris Lattner079a27a2005-08-09 20:40:02 +0000731 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000732 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000733 AllNodes.push_back(CondCodeNodes[Cond]);
734 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000735 return SDOperand(CondCodeNodes[Cond], 0);
736}
737
Chris Lattner0fdd7682005-08-30 22:38:38 +0000738SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
739 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
740 if (!Reg) {
741 Reg = new RegisterSDNode(RegNo, VT);
742 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000743 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000744 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000745}
746
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000747SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
748 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 // These setcc operations always fold.
750 switch (Cond) {
751 default: break;
752 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000753 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000755 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000756
757 case ISD::SETOEQ:
758 case ISD::SETOGT:
759 case ISD::SETOGE:
760 case ISD::SETOLT:
761 case ISD::SETOLE:
762 case ISD::SETONE:
763 case ISD::SETO:
764 case ISD::SETUO:
765 case ISD::SETUEQ:
766 case ISD::SETUNE:
767 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
768 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000769 }
770
Chris Lattner67255a12005-04-07 18:14:58 +0000771 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
772 uint64_t C2 = N2C->getValue();
773 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
774 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000775
Chris Lattnerc3aae252005-01-07 07:46:32 +0000776 // Sign extend the operands if required
777 if (ISD::isSignedIntSetCC(Cond)) {
778 C1 = N1C->getSignExtended();
779 C2 = N2C->getSignExtended();
780 }
781
782 switch (Cond) {
783 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000784 case ISD::SETEQ: return getConstant(C1 == C2, VT);
785 case ISD::SETNE: return getConstant(C1 != C2, VT);
786 case ISD::SETULT: return getConstant(C1 < C2, VT);
787 case ISD::SETUGT: return getConstant(C1 > C2, VT);
788 case ISD::SETULE: return getConstant(C1 <= C2, VT);
789 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
790 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
791 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
792 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
793 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000794 }
Chris Lattner24673922005-04-07 18:58:54 +0000795 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000796 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000797 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
798 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
799
800 // If the comparison constant has bits in the upper part, the
801 // zero-extended value could never match.
802 if (C2 & (~0ULL << InSize)) {
803 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
804 switch (Cond) {
805 case ISD::SETUGT:
806 case ISD::SETUGE:
807 case ISD::SETEQ: return getConstant(0, VT);
808 case ISD::SETULT:
809 case ISD::SETULE:
810 case ISD::SETNE: return getConstant(1, VT);
811 case ISD::SETGT:
812 case ISD::SETGE:
813 // True if the sign bit of C2 is set.
814 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
815 case ISD::SETLT:
816 case ISD::SETLE:
817 // True if the sign bit of C2 isn't set.
818 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
819 default:
820 break;
821 }
822 }
823
824 // Otherwise, we can perform the comparison with the low bits.
825 switch (Cond) {
826 case ISD::SETEQ:
827 case ISD::SETNE:
828 case ISD::SETUGT:
829 case ISD::SETUGE:
830 case ISD::SETULT:
831 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000832 return getSetCC(VT, N1.getOperand(0),
833 getConstant(C2, N1.getOperand(0).getValueType()),
834 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000835 default:
836 break; // todo, be more careful with signed comparisons
837 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000838 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
839 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
840 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
841 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
842 MVT::ValueType ExtDstTy = N1.getValueType();
843 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000844
Chris Lattner7b2880c2005-08-24 22:44:39 +0000845 // If the extended part has any inconsistent bits, it cannot ever
846 // compare equal. In other words, they have to be all ones or all
847 // zeros.
848 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000849 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000850 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
851 return getConstant(Cond == ISD::SETNE, VT);
852
853 // Otherwise, make this a use of a zext.
854 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000855 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000856 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000857 }
858
Chris Lattner67255a12005-04-07 18:14:58 +0000859 uint64_t MinVal, MaxVal;
860 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
861 if (ISD::isSignedIntSetCC(Cond)) {
862 MinVal = 1ULL << (OperandBitSize-1);
863 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
864 MaxVal = ~0ULL >> (65-OperandBitSize);
865 else
866 MaxVal = 0;
867 } else {
868 MinVal = 0;
869 MaxVal = ~0ULL >> (64-OperandBitSize);
870 }
871
872 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
873 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
874 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
875 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000876 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
877 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000878 }
879
880 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
881 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
882 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000883 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
884 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000885 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000886
Nate Begeman72ea2812005-04-14 08:56:52 +0000887 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
888 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000889
Nate Begeman72ea2812005-04-14 08:56:52 +0000890 // Canonicalize setgt X, Min --> setne X, Min
891 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000892 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000893
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000894 // If we have setult X, 1, turn it into seteq X, 0
895 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000896 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
897 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000898 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000899 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000900 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
901 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000902
903 // If we have "setcc X, C1", check to see if we can shrink the immediate
904 // by changing cc.
905
906 // SETUGT X, SINTMAX -> SETLT X, 0
907 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
908 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000909 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000910
911 // FIXME: Implement the rest of these.
912
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000913
914 // Fold bit comparisons when we can.
915 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
916 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
917 if (ConstantSDNode *AndRHS =
918 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
919 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
920 // Perform the xform if the AND RHS is a single bit.
921 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
922 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000923 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000924 TLI.getShiftAmountTy()));
925 }
926 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
927 // (X & 8) == 8 --> (X & 8) >> 3
928 // Perform the xform if C2 is a single bit.
929 if ((C2 & (C2-1)) == 0) {
930 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000931 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000932 }
933 }
934 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000935 }
Chris Lattner67255a12005-04-07 18:14:58 +0000936 } else if (isa<ConstantSDNode>(N1.Val)) {
937 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000938 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000939 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940
941 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
942 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
943 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000944
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 switch (Cond) {
946 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000947 case ISD::SETEQ: return getConstant(C1 == C2, VT);
948 case ISD::SETNE: return getConstant(C1 != C2, VT);
949 case ISD::SETLT: return getConstant(C1 < C2, VT);
950 case ISD::SETGT: return getConstant(C1 > C2, VT);
951 case ISD::SETLE: return getConstant(C1 <= C2, VT);
952 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953 }
954 } else {
955 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000956 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000957 }
958
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000959 // Could not fold it.
960 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000961}
962
Chris Lattnerc3aae252005-01-07 07:46:32 +0000963/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000964///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +0000966 MVT::ValueType *VTs = getNodeValueTypes(VT);
967 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
968 void *IP = 0;
969 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
970 return SDOperand(E, 0);
971 SDNode *N = new SDNode(Opcode, VT);
972 CSEMap.InsertNode(N, IP);
973
974 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 return SDOperand(N, 0);
976}
977
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
979 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000980 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000981 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
983 uint64_t Val = C->getValue();
984 switch (Opcode) {
985 default: break;
986 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000987 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
989 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000990 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
991 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000992 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000993 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000994 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000995 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000996 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000997 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000998 case ISD::BSWAP:
999 switch(VT) {
1000 default: assert(0 && "Invalid bswap!"); break;
1001 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1002 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1003 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1004 }
1005 break;
1006 case ISD::CTPOP:
1007 switch(VT) {
1008 default: assert(0 && "Invalid ctpop!"); break;
1009 case MVT::i1: return getConstant(Val != 0, VT);
1010 case MVT::i8:
1011 Tmp1 = (unsigned)Val & 0xFF;
1012 return getConstant(CountPopulation_32(Tmp1), VT);
1013 case MVT::i16:
1014 Tmp1 = (unsigned)Val & 0xFFFF;
1015 return getConstant(CountPopulation_32(Tmp1), VT);
1016 case MVT::i32:
1017 return getConstant(CountPopulation_32((unsigned)Val), VT);
1018 case MVT::i64:
1019 return getConstant(CountPopulation_64(Val), VT);
1020 }
1021 case ISD::CTLZ:
1022 switch(VT) {
1023 default: assert(0 && "Invalid ctlz!"); break;
1024 case MVT::i1: return getConstant(Val == 0, VT);
1025 case MVT::i8:
1026 Tmp1 = (unsigned)Val & 0xFF;
1027 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1028 case MVT::i16:
1029 Tmp1 = (unsigned)Val & 0xFFFF;
1030 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1031 case MVT::i32:
1032 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1033 case MVT::i64:
1034 return getConstant(CountLeadingZeros_64(Val), VT);
1035 }
1036 case ISD::CTTZ:
1037 switch(VT) {
1038 default: assert(0 && "Invalid cttz!"); break;
1039 case MVT::i1: return getConstant(Val == 0, VT);
1040 case MVT::i8:
1041 Tmp1 = (unsigned)Val | 0x100;
1042 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1043 case MVT::i16:
1044 Tmp1 = (unsigned)Val | 0x10000;
1045 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1046 case MVT::i32:
1047 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1048 case MVT::i64:
1049 return getConstant(CountTrailingZeros_64(Val), VT);
1050 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 }
1052 }
1053
Chris Lattner94683772005-12-23 05:30:37 +00001054 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1056 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001057 case ISD::FNEG:
1058 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001059 case ISD::FABS:
1060 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 case ISD::FP_ROUND:
1062 case ISD::FP_EXTEND:
1063 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001064 case ISD::FP_TO_SINT:
1065 return getConstant((int64_t)C->getValue(), VT);
1066 case ISD::FP_TO_UINT:
1067 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001068 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001069 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001070 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001071 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001072 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001073 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 }
1075
1076 unsigned OpOpcode = Operand.Val->getOpcode();
1077 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001078 case ISD::TokenFactor:
1079 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 case ISD::SIGN_EXTEND:
1081 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001082 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1084 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1085 break;
1086 case ISD::ZERO_EXTEND:
1087 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001088 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001089 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001090 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001092 case ISD::ANY_EXTEND:
1093 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001094 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001095 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1096 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1097 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1098 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 case ISD::TRUNCATE:
1100 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001101 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 if (OpOpcode == ISD::TRUNCATE)
1103 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001104 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1105 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001106 // If the source is smaller than the dest, we still need an extend.
1107 if (Operand.Val->getOperand(0).getValueType() < VT)
1108 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1109 else if (Operand.Val->getOperand(0).getValueType() > VT)
1110 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1111 else
1112 return Operand.Val->getOperand(0);
1113 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 break;
Chris Lattner35481892005-12-23 00:16:34 +00001115 case ISD::BIT_CONVERT:
1116 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001117 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001118 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001119 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001120 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1121 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001122 if (OpOpcode == ISD::UNDEF)
1123 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001124 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001125 case ISD::SCALAR_TO_VECTOR:
1126 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1127 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1128 "Illegal SCALAR_TO_VECTOR node!");
1129 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001130 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001131 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1132 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001133 Operand.Val->getOperand(0));
1134 if (OpOpcode == ISD::FNEG) // --X -> X
1135 return Operand.Val->getOperand(0);
1136 break;
1137 case ISD::FABS:
1138 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1139 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1140 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 }
1142
Chris Lattner43247a12005-08-25 19:12:10 +00001143 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001144 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001145 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001146 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1147 void *IP = 0;
1148 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1149 return SDOperand(E, 0);
1150 N = new SDNode(Opcode, Operand);
1151 N->setValueTypes(VTs, 1);
1152 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001153 } else {
1154 N = new SDNode(Opcode, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001155 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001156 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001157 AllNodes.push_back(N);
1158 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001159}
1160
Chris Lattner36019aa2005-04-18 03:48:41 +00001161
1162
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1164 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001165#ifndef NDEBUG
1166 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001167 case ISD::TokenFactor:
1168 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1169 N2.getValueType() == MVT::Other && "Invalid token factor!");
1170 break;
Chris Lattner76365122005-01-16 02:23:22 +00001171 case ISD::AND:
1172 case ISD::OR:
1173 case ISD::XOR:
1174 case ISD::UDIV:
1175 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001176 case ISD::MULHU:
1177 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001178 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1179 // fall through
1180 case ISD::ADD:
1181 case ISD::SUB:
1182 case ISD::MUL:
1183 case ISD::SDIV:
1184 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001185 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1186 // fall through.
1187 case ISD::FADD:
1188 case ISD::FSUB:
1189 case ISD::FMUL:
1190 case ISD::FDIV:
1191 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001192 assert(N1.getValueType() == N2.getValueType() &&
1193 N1.getValueType() == VT && "Binary operator types must match!");
1194 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001195 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1196 assert(N1.getValueType() == VT &&
1197 MVT::isFloatingPoint(N1.getValueType()) &&
1198 MVT::isFloatingPoint(N2.getValueType()) &&
1199 "Invalid FCOPYSIGN!");
1200 break;
Chris Lattner76365122005-01-16 02:23:22 +00001201 case ISD::SHL:
1202 case ISD::SRA:
1203 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001204 case ISD::ROTL:
1205 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001206 assert(VT == N1.getValueType() &&
1207 "Shift operators return type must be the same as their first arg");
1208 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001209 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001210 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001211 case ISD::FP_ROUND_INREG: {
1212 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1213 assert(VT == N1.getValueType() && "Not an inreg round!");
1214 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1215 "Cannot FP_ROUND_INREG integer types");
1216 assert(EVT <= VT && "Not rounding down!");
1217 break;
1218 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001219 case ISD::AssertSext:
1220 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001221 case ISD::SIGN_EXTEND_INREG: {
1222 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1223 assert(VT == N1.getValueType() && "Not an inreg extend!");
1224 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1225 "Cannot *_EXTEND_INREG FP types");
1226 assert(EVT <= VT && "Not extending!");
1227 }
1228
Chris Lattner76365122005-01-16 02:23:22 +00001229 default: break;
1230 }
1231#endif
1232
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1234 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1235 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001236 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1237 int64_t Val = N1C->getValue();
1238 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1239 Val <<= 64-FromBits;
1240 Val >>= 64-FromBits;
1241 return getConstant(Val, VT);
1242 }
1243
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244 if (N2C) {
1245 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1246 switch (Opcode) {
1247 case ISD::ADD: return getConstant(C1 + C2, VT);
1248 case ISD::SUB: return getConstant(C1 - C2, VT);
1249 case ISD::MUL: return getConstant(C1 * C2, VT);
1250 case ISD::UDIV:
1251 if (C2) return getConstant(C1 / C2, VT);
1252 break;
1253 case ISD::UREM :
1254 if (C2) return getConstant(C1 % C2, VT);
1255 break;
1256 case ISD::SDIV :
1257 if (C2) return getConstant(N1C->getSignExtended() /
1258 N2C->getSignExtended(), VT);
1259 break;
1260 case ISD::SREM :
1261 if (C2) return getConstant(N1C->getSignExtended() %
1262 N2C->getSignExtended(), VT);
1263 break;
1264 case ISD::AND : return getConstant(C1 & C2, VT);
1265 case ISD::OR : return getConstant(C1 | C2, VT);
1266 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001267 case ISD::SHL : return getConstant(C1 << C2, VT);
1268 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001269 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001270 case ISD::ROTL :
1271 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1272 VT);
1273 case ISD::ROTR :
1274 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1275 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001276 default: break;
1277 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001278 } else { // Cannonicalize constant to RHS if commutative
1279 if (isCommutativeBinOp(Opcode)) {
1280 std::swap(N1C, N2C);
1281 std::swap(N1, N2);
1282 }
1283 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 }
1285
1286 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1287 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001288 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001289 if (N2CFP) {
1290 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1291 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001292 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1293 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1294 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1295 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001296 if (C2) return getConstantFP(C1 / C2, VT);
1297 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001298 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001299 if (C2) return getConstantFP(fmod(C1, C2), VT);
1300 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001301 case ISD::FCOPYSIGN: {
1302 union {
1303 double F;
1304 uint64_t I;
1305 } u1;
1306 union {
1307 double F;
1308 int64_t I;
1309 } u2;
1310 u1.F = C1;
1311 u2.F = C2;
1312 if (u2.I < 0) // Sign bit of RHS set?
1313 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1314 else
1315 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1316 return getConstantFP(u1.F, VT);
1317 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001318 default: break;
1319 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320 } else { // Cannonicalize constant to RHS if commutative
1321 if (isCommutativeBinOp(Opcode)) {
1322 std::swap(N1CFP, N2CFP);
1323 std::swap(N1, N2);
1324 }
1325 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001326 }
Chris Lattner62b57722006-04-20 05:39:12 +00001327
1328 // Canonicalize an UNDEF to the RHS, even over a constant.
1329 if (N1.getOpcode() == ISD::UNDEF) {
1330 if (isCommutativeBinOp(Opcode)) {
1331 std::swap(N1, N2);
1332 } else {
1333 switch (Opcode) {
1334 case ISD::FP_ROUND_INREG:
1335 case ISD::SIGN_EXTEND_INREG:
1336 case ISD::SUB:
1337 case ISD::FSUB:
1338 case ISD::FDIV:
1339 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001340 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001341 return N1; // fold op(undef, arg2) -> undef
1342 case ISD::UDIV:
1343 case ISD::SDIV:
1344 case ISD::UREM:
1345 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001346 case ISD::SRL:
1347 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001348 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1349 }
1350 }
1351 }
1352
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001353 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001354 if (N2.getOpcode() == ISD::UNDEF) {
1355 switch (Opcode) {
1356 case ISD::ADD:
1357 case ISD::SUB:
1358 case ISD::FADD:
1359 case ISD::FSUB:
1360 case ISD::FMUL:
1361 case ISD::FDIV:
1362 case ISD::FREM:
1363 case ISD::UDIV:
1364 case ISD::SDIV:
1365 case ISD::UREM:
1366 case ISD::SREM:
1367 case ISD::XOR:
1368 return N2; // fold op(arg1, undef) -> undef
1369 case ISD::MUL:
1370 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001371 case ISD::SRL:
1372 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001373 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1374 case ISD::OR:
1375 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001376 case ISD::SRA:
1377 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001378 }
1379 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001380
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 // Finally, fold operations that do not require constants.
1382 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001383 case ISD::FP_ROUND_INREG:
1384 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1385 break;
1386 case ISD::SIGN_EXTEND_INREG: {
1387 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1388 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001389 break;
1390 }
1391
Nate Begemaneea805e2005-04-13 21:23:31 +00001392 // FIXME: figure out how to safely handle things like
1393 // int foo(int x) { return 1 << (x & 255); }
1394 // int bar() { return foo(256); }
1395#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001396 case ISD::SHL:
1397 case ISD::SRL:
1398 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001399 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001400 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001401 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001402 else if (N2.getOpcode() == ISD::AND)
1403 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1404 // If the and is only masking out bits that cannot effect the shift,
1405 // eliminate the and.
1406 unsigned NumBits = MVT::getSizeInBits(VT);
1407 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1408 return getNode(Opcode, VT, N1, N2.getOperand(0));
1409 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001410 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001411#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001412 }
1413
Chris Lattner27e9b412005-05-11 18:57:39 +00001414 // Memoize this node if possible.
1415 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001416 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001417 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001418 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1419 void *IP = 0;
1420 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1421 return SDOperand(E, 0);
1422 N = new SDNode(Opcode, N1, N2);
1423 N->setValueTypes(VTs, 1);
1424 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001425 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001426 N = new SDNode(Opcode, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001427 N->setValueTypes(VTs, 1);
Chris Lattner27e9b412005-05-11 18:57:39 +00001428 }
1429
Chris Lattnerc3aae252005-01-07 07:46:32 +00001430 AllNodes.push_back(N);
1431 return SDOperand(N, 0);
1432}
1433
Chris Lattnerc3aae252005-01-07 07:46:32 +00001434SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1435 SDOperand N1, SDOperand N2, SDOperand N3) {
1436 // Perform various simplifications.
1437 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1438 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001439 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001440 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001441 case ISD::SETCC: {
1442 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001443 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001444 if (Simp.Val) return Simp;
1445 break;
1446 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001447 case ISD::SELECT:
1448 if (N1C)
1449 if (N1C->getValue())
1450 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001451 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001452 return N3; // select false, X, Y -> Y
1453
1454 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001456 case ISD::BRCOND:
1457 if (N2C)
1458 if (N2C->getValue()) // Unconditional branch
1459 return getNode(ISD::BR, MVT::Other, N1, N3);
1460 else
1461 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001462 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001463 case ISD::VECTOR_SHUFFLE:
1464 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1465 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1466 N3.getOpcode() == ISD::BUILD_VECTOR &&
1467 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1468 "Illegal VECTOR_SHUFFLE node!");
1469 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001470 }
1471
Chris Lattner43247a12005-08-25 19:12:10 +00001472 // Memoize node if it doesn't produce a flag.
1473 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001474 MVT::ValueType *VTs = getNodeValueTypes(VT);
1475
Chris Lattner43247a12005-08-25 19:12:10 +00001476 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001477 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1478 void *IP = 0;
1479 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1480 return SDOperand(E, 0);
1481 N = new SDNode(Opcode, N1, N2, N3);
1482 N->setValueTypes(VTs, 1);
1483 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001484 } else {
1485 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001486 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001487 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001488 AllNodes.push_back(N);
1489 return SDOperand(N, 0);
1490}
1491
1492SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001493 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001494 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001495 SDOperand Ops[] = { N1, N2, N3, N4 };
1496 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001497}
1498
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001499SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1500 SDOperand N1, SDOperand N2, SDOperand N3,
1501 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001502 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1503 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001504}
1505
Evan Cheng7038daf2005-12-10 00:37:58 +00001506SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1507 SDOperand Chain, SDOperand Ptr,
1508 SDOperand SV) {
Chris Lattnera5682852006-08-07 23:03:03 +00001509 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1510
1511 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1512 void *IP = 0;
1513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1514 return SDOperand(E, 0);
1515 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1516 N->setValueTypes(VTs, 2);
1517 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001518 AllNodes.push_back(N);
1519 return SDOperand(N, 0);
1520}
1521
1522SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1523 SDOperand Chain, SDOperand Ptr,
1524 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001525 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1526 getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001527 std::vector<MVT::ValueType> VTs;
1528 VTs.reserve(2);
1529 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001530 return getNode(ISD::VLOAD, VTs, Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001531}
1532
1533SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1534 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1535 MVT::ValueType EVT) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001536 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001537 std::vector<MVT::ValueType> VTs;
1538 VTs.reserve(2);
1539 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001540 return getNode(Opcode, VTs, Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001541}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001542
Chris Lattner0437cdd2005-05-09 04:14:13 +00001543SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001544 assert((!V || isa<PointerType>(V->getType())) &&
1545 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001546 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1547 if (N) return SDOperand(N, 0);
1548
1549 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001550 AllNodes.push_back(N);
1551 return SDOperand(N, 0);
1552}
1553
Nate Begemanacc398c2006-01-25 18:21:52 +00001554SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1555 SDOperand Chain, SDOperand Ptr,
1556 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001557 SDOperand Ops[] = { Chain, Ptr, SV };
Nate Begemanacc398c2006-01-25 18:21:52 +00001558 std::vector<MVT::ValueType> VTs;
1559 VTs.reserve(2);
1560 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001561 return getNode(ISD::VAARG, VTs, Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001562}
1563
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001564SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001565 const SDOperand *Ops, unsigned NumOps) {
1566 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001568 case 1: return getNode(Opcode, VT, Ops[0]);
1569 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1570 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001571 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001573
Chris Lattneref847df2005-04-09 03:27:28 +00001574 switch (Opcode) {
1575 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001576 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001577 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001578 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1579#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1580 // If this is a truncating store of a constant, convert to the desired type
1581 // and store it instead.
1582 if (isa<Constant>(Ops[0])) {
1583 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1584 if (isa<Constant>(Op))
1585 N1 = Op;
1586 }
1587 // Also for ConstantFP?
1588#endif
1589 if (Ops[0].getValueType() == EVT) // Normal store?
1590 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1591 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1592 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1593 "Can't do FP-INT conversion!");
1594 break;
1595 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001596 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001597 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001598 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1599 "LHS and RHS of condition must have same type!");
1600 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1601 "True and False arms of SelectCC must have same type!");
1602 assert(Ops[2].getValueType() == VT &&
1603 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001604 break;
1605 }
1606 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001607 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001608 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1609 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001610 break;
1611 }
Chris Lattneref847df2005-04-09 03:27:28 +00001612 }
1613
Chris Lattner385328c2005-05-14 07:42:29 +00001614 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001615 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001616 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001617 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001618 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001619 void *IP = 0;
1620 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1621 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001622 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001623 N->setValueTypes(VTs, 1);
1624 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001625 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001626 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001627 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001628 }
Chris Lattneref847df2005-04-09 03:27:28 +00001629 AllNodes.push_back(N);
1630 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001631}
1632
Chris Lattner89c34632005-05-14 06:20:26 +00001633SDOperand SelectionDAG::getNode(unsigned Opcode,
1634 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001635 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner89c34632005-05-14 06:20:26 +00001636 if (ResultTys.size() == 1)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001637 return getNode(Opcode, ResultTys[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001638
Chris Lattner5f056bf2005-07-10 01:55:33 +00001639 switch (Opcode) {
1640 case ISD::EXTLOAD:
1641 case ISD::SEXTLOAD:
1642 case ISD::ZEXTLOAD: {
1643 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001644 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001645 // If they are asking for an extending load from/to the same thing, return a
1646 // normal load.
1647 if (ResultTys[0] == EVT)
1648 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001649 if (MVT::isVector(ResultTys[0])) {
1650 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1651 "Invalid vector extload!");
1652 } else {
1653 assert(EVT < ResultTys[0] &&
1654 "Should only be an extending load, not truncating!");
1655 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001656 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001657 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001658 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1659 "Cannot convert from FP to Int or Int -> FP!");
1660 break;
1661 }
1662
Chris Lattnere89083a2005-05-14 07:25:05 +00001663 // FIXME: figure out how to safely handle things like
1664 // int foo(int x) { return 1 << (x & 255); }
1665 // int bar() { return foo(256); }
1666#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001667 case ISD::SRA_PARTS:
1668 case ISD::SRL_PARTS:
1669 case ISD::SHL_PARTS:
1670 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001671 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001672 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1673 else if (N3.getOpcode() == ISD::AND)
1674 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1675 // If the and is only masking out bits that cannot effect the shift,
1676 // eliminate the and.
1677 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1678 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1679 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1680 }
1681 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001682#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001683 }
Chris Lattner89c34632005-05-14 06:20:26 +00001684
Chris Lattner43247a12005-08-25 19:12:10 +00001685 // Memoize the node unless it returns a flag.
1686 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001687 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
Chris Lattner43247a12005-08-25 19:12:10 +00001688 if (ResultTys.back() != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001689 SelectionDAGCSEMap::NodeID ID;
1690 ID.SetOpcode(Opcode);
1691 ID.SetValueTypes(VTs);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001692 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001693 void *IP = 0;
1694 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1695 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001696 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001697 N->setValueTypes(VTs, ResultTys.size());
1698 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001699 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001700 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001701 N->setValueTypes(VTs, ResultTys.size());
Chris Lattner43247a12005-08-25 19:12:10 +00001702 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001703 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001704 return SDOperand(N, 0);
1705}
1706
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001707
Chris Lattnera5682852006-08-07 23:03:03 +00001708MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1709 return SDNode::getValueTypeList(VT);
1710}
1711
1712MVT::ValueType *SelectionDAG::getNodeValueTypes(
1713 std::vector<MVT::ValueType> &RetVals) {
Chris Lattnera3255112005-11-08 23:30:28 +00001714 switch (RetVals.size()) {
Chris Lattnera5682852006-08-07 23:03:03 +00001715 case 0: assert(0 && "Cannot have nodes without results!");
1716 case 1: return SDNode::getValueTypeList(RetVals[0]);
1717 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
Chris Lattnera3255112005-11-08 23:30:28 +00001718 default: break;
1719 }
1720
1721 std::list<std::vector<MVT::ValueType> >::iterator I =
1722 std::find(VTList.begin(), VTList.end(), RetVals);
1723 if (I == VTList.end()) {
1724 VTList.push_front(RetVals);
1725 I = VTList.begin();
1726 }
1727
Chris Lattnera5682852006-08-07 23:03:03 +00001728 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001729}
1730
Chris Lattnera5682852006-08-07 23:03:03 +00001731MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1732 MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001733 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1734 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001735 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1736 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001737 }
1738 std::vector<MVT::ValueType> V;
1739 V.push_back(VT1);
1740 V.push_back(VT2);
1741 VTList.push_front(V);
Chris Lattnera5682852006-08-07 23:03:03 +00001742 return &(*VTList.begin())[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001743}
1744
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001745/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1746/// specified operands. If the resultant node already exists in the DAG,
1747/// this does not modify the specified node, instead it returns the node that
1748/// already exists. If the resultant node does not exist in the DAG, the
1749/// input node is returned. As a degenerate case, if you specify the same
1750/// input operands as the node already has, the input node is returned.
1751SDOperand SelectionDAG::
1752UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1753 SDNode *N = InN.Val;
1754 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1755
1756 // Check to see if there is no change.
1757 if (Op == N->getOperand(0)) return InN;
1758
1759 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001760 void *InsertPos = 0;
1761 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1762 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001763
1764 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001765 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001766 RemoveNodeFromCSEMaps(N);
1767
1768 // Now we update the operands.
1769 N->OperandList[0].Val->removeUser(N);
1770 Op.Val->addUser(N);
1771 N->OperandList[0] = Op;
1772
1773 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001774 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001775 return InN;
1776}
1777
1778SDOperand SelectionDAG::
1779UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1780 SDNode *N = InN.Val;
1781 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1782
1783 // Check to see if there is no change.
1784 bool AnyChange = false;
1785 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1786 return InN; // No operands changed, just return the input node.
1787
1788 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001789 void *InsertPos = 0;
1790 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1791 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001792
1793 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001794 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001795 RemoveNodeFromCSEMaps(N);
1796
1797 // Now we update the operands.
1798 if (N->OperandList[0] != Op1) {
1799 N->OperandList[0].Val->removeUser(N);
1800 Op1.Val->addUser(N);
1801 N->OperandList[0] = Op1;
1802 }
1803 if (N->OperandList[1] != Op2) {
1804 N->OperandList[1].Val->removeUser(N);
1805 Op2.Val->addUser(N);
1806 N->OperandList[1] = Op2;
1807 }
1808
1809 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001810 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001811 return InN;
1812}
1813
1814SDOperand SelectionDAG::
1815UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001816 SDOperand Ops[] = { Op1, Op2, Op3 };
1817 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001818}
1819
1820SDOperand SelectionDAG::
1821UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1822 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001823 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1824 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001825}
1826
1827SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001828UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1829 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001830 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1831 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001832}
1833
1834
1835SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001836UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001837 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001838 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001839 "Update with wrong number of operands");
1840
1841 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001842 bool AnyChange = false;
1843 for (unsigned i = 0; i != NumOps; ++i) {
1844 if (Ops[i] != N->getOperand(i)) {
1845 AnyChange = true;
1846 break;
1847 }
1848 }
1849
1850 // No operands changed, just return the input node.
1851 if (!AnyChange) return InN;
1852
1853 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001854 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001855 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001856 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001857
1858 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001859 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001860 RemoveNodeFromCSEMaps(N);
1861
1862 // Now we update the operands.
1863 for (unsigned i = 0; i != NumOps; ++i) {
1864 if (N->OperandList[i] != Ops[i]) {
1865 N->OperandList[i].Val->removeUser(N);
1866 Ops[i].Val->addUser(N);
1867 N->OperandList[i] = Ops[i];
1868 }
1869 }
1870
1871 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001872 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001873 return InN;
1874}
1875
1876
1877
Chris Lattner149c58c2005-08-16 18:17:10 +00001878
1879/// SelectNodeTo - These are used for target selectors to *mutate* the
1880/// specified node to have the specified return type, Target opcode, and
1881/// operands. Note that target opcodes are stored as
1882/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001883///
1884/// Note that SelectNodeTo returns the resultant node. If there is already a
1885/// node of the specified opcode and operands, it returns that node instead of
1886/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001887SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1888 MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +00001889 MVT::ValueType *VTs = getNodeValueTypes(VT);
1890 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1891 void *IP = 0;
1892 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1893 return SDOperand(ON, 0);
1894
Chris Lattner7651fa42005-08-24 23:00:29 +00001895 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001896
Chris Lattner7651fa42005-08-24 23:00:29 +00001897 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001898 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001899
Chris Lattner4a283e92006-08-11 18:38:11 +00001900 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001901 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001902}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001903
Chris Lattnereb19e402005-11-30 22:45:14 +00001904SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1905 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001906 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001907 MVT::ValueType *VTs = getNodeValueTypes(VT);
1908 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1909 void *IP = 0;
1910 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1911 return SDOperand(ON, 0);
1912
Chris Lattner149c58c2005-08-16 18:17:10 +00001913 RemoveNodeFromCSEMaps(N);
1914 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001915 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001916 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001917 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001918 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001919}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001920
Chris Lattnereb19e402005-11-30 22:45:14 +00001921SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1922 MVT::ValueType VT, SDOperand Op1,
1923 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001924 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001925 MVT::ValueType *VTs = getNodeValueTypes(VT);
1926 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1927 void *IP = 0;
1928 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1929 return SDOperand(ON, 0);
1930
Chris Lattner149c58c2005-08-16 18:17:10 +00001931 RemoveNodeFromCSEMaps(N);
1932 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001933 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001934 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001935
Chris Lattnera5682852006-08-07 23:03:03 +00001936 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001937 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001938}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001939
Chris Lattnereb19e402005-11-30 22:45:14 +00001940SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1941 MVT::ValueType VT, SDOperand Op1,
1942 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001943 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001944 MVT::ValueType *VTs = getNodeValueTypes(VT);
1945 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1946 void *IP = 0;
1947 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1948 return SDOperand(ON, 0);
1949
Chris Lattner149c58c2005-08-16 18:17:10 +00001950 RemoveNodeFromCSEMaps(N);
1951 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001952 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001953 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001954
Chris Lattnera5682852006-08-07 23:03:03 +00001955 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001956 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001957}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001958
Chris Lattnereb19e402005-11-30 22:45:14 +00001959SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1960 MVT::ValueType VT, SDOperand Op1,
1961 SDOperand Op2, SDOperand Op3,
1962 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001963 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001964 MVT::ValueType *VTs = getNodeValueTypes(VT);
1965 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1966 ID.AddOperand(Op1);
1967 ID.AddOperand(Op2);
1968 ID.AddOperand(Op3);
1969 ID.AddOperand(Op4);
1970 void *IP = 0;
1971 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1972 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001973
Nate Begeman294a0a12005-08-18 07:30:15 +00001974 RemoveNodeFromCSEMaps(N);
1975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001976 N->setValueTypes(VTs, 1);
Nate Begeman294a0a12005-08-18 07:30:15 +00001977 N->setOperands(Op1, Op2, Op3, Op4);
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);
Nate Begeman294a0a12005-08-18 07:30:15 +00001981}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001982
Chris Lattnereb19e402005-11-30 22:45:14 +00001983SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1984 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00001985 SDOperand Op2, SDOperand Op3,
1986 SDOperand Op4, SDOperand Op5) {
1987 MVT::ValueType *VTs = getNodeValueTypes(VT);
1988 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1989 ID.AddOperand(Op1);
1990 ID.AddOperand(Op2);
1991 ID.AddOperand(Op3);
1992 ID.AddOperand(Op4);
1993 ID.AddOperand(Op5);
1994 void *IP = 0;
1995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1996 return SDOperand(ON, 0);
1997
Chris Lattner6b09a292005-08-21 18:49:33 +00001998 RemoveNodeFromCSEMaps(N);
1999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002000 N->setValueTypes(VTs, 1);
Chris Lattner6b09a292005-08-21 18:49:33 +00002001 N->setOperands(Op1, Op2, Op3, Op4, Op5);
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);
Chris Lattner6b09a292005-08-21 18:49:33 +00002005}
Chris Lattner149c58c2005-08-16 18:17:10 +00002006
Chris Lattnereb19e402005-11-30 22:45:14 +00002007SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2008 MVT::ValueType VT, SDOperand Op1,
2009 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2010 SDOperand Op5, SDOperand Op6) {
Chris Lattnera5682852006-08-07 23:03:03 +00002011 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 ID.AddOperand(Op6);
2019 void *IP = 0;
2020 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2021 return SDOperand(ON, 0);
2022
Evan Cheng61ca74b2005-11-30 02:04:11 +00002023 RemoveNodeFromCSEMaps(N);
2024 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002025 N->setValueTypes(VTs, 1);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002026 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002027
Chris Lattnera5682852006-08-07 23:03:03 +00002028 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002029 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002030}
2031
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002032SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2033 MVT::ValueType VT, SDOperand Op1,
2034 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2035 SDOperand Op5, SDOperand Op6,
2036 SDOperand Op7) {
Chris Lattnera5682852006-08-07 23:03:03 +00002037 MVT::ValueType *VTs = getNodeValueTypes(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002038 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002039 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2040 ID.AddOperand(Op1);
2041 ID.AddOperand(Op2);
2042 ID.AddOperand(Op3);
2043 ID.AddOperand(Op4);
2044 ID.AddOperand(Op5);
2045 ID.AddOperand(Op6);
2046 ID.AddOperand(Op7);
2047 void *IP = 0;
2048 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2049 return SDOperand(ON, 0);
2050
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002051 RemoveNodeFromCSEMaps(N);
2052 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002053 N->setValueTypes(VTs, 1);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002054 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2055
Chris Lattnera5682852006-08-07 23:03:03 +00002056 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002057 return SDOperand(N, 0);
2058}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002059SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2060 MVT::ValueType VT, SDOperand Op1,
2061 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2062 SDOperand Op5, SDOperand Op6,
2063 SDOperand Op7, SDOperand Op8) {
2064 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002065 MVT::ValueType *VTs = getNodeValueTypes(VT);
2066 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2067 ID.AddOperand(Op1);
2068 ID.AddOperand(Op2);
2069 ID.AddOperand(Op3);
2070 ID.AddOperand(Op4);
2071 ID.AddOperand(Op5);
2072 ID.AddOperand(Op6);
2073 ID.AddOperand(Op7);
2074 ID.AddOperand(Op8);
2075 void *IP = 0;
2076 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2077 return SDOperand(ON, 0);
2078
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002079 RemoveNodeFromCSEMaps(N);
2080 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002081 N->setValueTypes(VTs, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002082 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2083
Chris Lattnera5682852006-08-07 23:03:03 +00002084 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002085 return SDOperand(N, 0);
2086}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002087
Chris Lattnereb19e402005-11-30 22:45:14 +00002088SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2089 MVT::ValueType VT1, MVT::ValueType VT2,
2090 SDOperand Op1, SDOperand Op2) {
Chris Lattnera5682852006-08-07 23:03:03 +00002091 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2092 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2093 void *IP = 0;
2094 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2095 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002096
Chris Lattner0fb094f2005-11-19 01:44:53 +00002097 RemoveNodeFromCSEMaps(N);
2098 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002099 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002100 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002101
Chris Lattnera5682852006-08-07 23:03:03 +00002102 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002103 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002104}
2105
Chris Lattnereb19e402005-11-30 22:45:14 +00002106SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2107 MVT::ValueType VT1, MVT::ValueType VT2,
2108 SDOperand Op1, SDOperand Op2,
2109 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002110 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002111 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2112 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2113 Op1, Op2, Op3);
2114 void *IP = 0;
2115 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2116 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002117
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118 RemoveNodeFromCSEMaps(N);
2119 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002120 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002121 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002122
Chris Lattnera5682852006-08-07 23:03:03 +00002123 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002124 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002125}
2126
Chris Lattnereb19e402005-11-30 22:45:14 +00002127SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2128 MVT::ValueType VT1, MVT::ValueType VT2,
2129 SDOperand Op1, SDOperand Op2,
2130 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002131 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002132 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2133 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2134 ID.AddOperand(Op1);
2135 ID.AddOperand(Op2);
2136 ID.AddOperand(Op3);
2137 ID.AddOperand(Op4);
2138 void *IP = 0;
2139 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2140 return SDOperand(ON, 0);
2141
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, Op4);
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,
2155 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002156 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002157 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2158 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2159 ID.AddOperand(Op1);
2160 ID.AddOperand(Op2);
2161 ID.AddOperand(Op3);
2162 ID.AddOperand(Op4);
2163 ID.AddOperand(Op5);
2164 void *IP = 0;
2165 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2166 return SDOperand(ON, 0);
2167
Chris Lattner0fb094f2005-11-19 01:44:53 +00002168 RemoveNodeFromCSEMaps(N);
2169 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002170 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002171 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002172
Chris Lattnera5682852006-08-07 23:03:03 +00002173 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002174 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002175}
2176
Evan Cheng6ae46c42006-02-09 07:15:23 +00002177/// getTargetNode - These are used for target selectors to create a new node
2178/// with specified return type(s), target opcode, and operands.
2179///
2180/// Note that getTargetNode returns the resultant node. If there is already a
2181/// node of the specified opcode and operands, it returns that node instead of
2182/// the current one.
2183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2184 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2185}
2186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2187 SDOperand Op1) {
2188 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2189}
2190SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2191 SDOperand Op1, SDOperand Op2) {
2192 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2193}
2194SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2195 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2196 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2197}
2198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2199 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2200 SDOperand Op4) {
2201 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2202}
2203SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2204 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2205 SDOperand Op4, SDOperand Op5) {
2206 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2207}
2208SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2209 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002210 SDOperand Op4, SDOperand Op5,
2211 SDOperand Op6) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002212 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2213 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002214}
2215SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2216 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2217 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2218 SDOperand Op7) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002219 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2223 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2224 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2225 SDOperand Op7, SDOperand Op8) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002226 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2227 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002228}
2229SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002230 const SDOperand *Ops, unsigned NumOps) {
2231 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002232}
2233SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2234 MVT::ValueType VT2, SDOperand Op1) {
2235 std::vector<MVT::ValueType> ResultTys;
2236 ResultTys.push_back(VT1);
2237 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002238 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002239}
2240SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002241 MVT::ValueType VT2, SDOperand Op1,
2242 SDOperand Op2) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002243 std::vector<MVT::ValueType> ResultTys;
2244 ResultTys.push_back(VT1);
2245 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002246 SDOperand Ops[] = { Op1, Op2 };
2247 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002248}
2249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002250 MVT::ValueType VT2, SDOperand Op1,
2251 SDOperand Op2, SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002252 std::vector<MVT::ValueType> ResultTys;
2253 ResultTys.push_back(VT1);
2254 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002255 SDOperand Ops[] = { Op1, Op2, Op3 };
2256 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002257}
2258SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002259 MVT::ValueType VT2, SDOperand Op1,
2260 SDOperand Op2, SDOperand Op3,
2261 SDOperand Op4) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002262 std::vector<MVT::ValueType> ResultTys;
2263 ResultTys.push_back(VT1);
2264 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002265 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2266 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002267}
2268SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002269 MVT::ValueType VT2, SDOperand Op1,
2270 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2271 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002272 std::vector<MVT::ValueType> ResultTys;
2273 ResultTys.push_back(VT1);
2274 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002275 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2276 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002277}
2278SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002279 MVT::ValueType VT2, SDOperand Op1,
2280 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2281 SDOperand Op5, SDOperand Op6) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002282 std::vector<MVT::ValueType> ResultTys;
2283 ResultTys.push_back(VT1);
2284 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002285 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2286 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002287}
2288SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002289 MVT::ValueType VT2, SDOperand Op1,
2290 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2291 SDOperand Op5, SDOperand Op6,
2292 SDOperand Op7) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002293 std::vector<MVT::ValueType> ResultTys;
2294 ResultTys.push_back(VT1);
2295 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002296 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2297 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002298}
2299SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2300 MVT::ValueType VT2, MVT::ValueType VT3,
2301 SDOperand Op1, SDOperand Op2) {
2302 std::vector<MVT::ValueType> ResultTys;
2303 ResultTys.push_back(VT1);
2304 ResultTys.push_back(VT2);
2305 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002306 SDOperand Ops[] = { Op1, Op2 };
2307 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002308}
2309SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2310 MVT::ValueType VT2, MVT::ValueType VT3,
2311 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002312 SDOperand Op3, SDOperand Op4,
2313 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002314 std::vector<MVT::ValueType> ResultTys;
2315 ResultTys.push_back(VT1);
2316 ResultTys.push_back(VT2);
2317 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002318 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2319 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002320}
2321SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2322 MVT::ValueType VT2, MVT::ValueType VT3,
2323 SDOperand Op1, SDOperand Op2,
2324 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2325 SDOperand Op6) {
2326 std::vector<MVT::ValueType> ResultTys;
2327 ResultTys.push_back(VT1);
2328 ResultTys.push_back(VT2);
2329 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002330 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2331 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002332}
2333SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2334 MVT::ValueType VT2, MVT::ValueType VT3,
2335 SDOperand Op1, SDOperand Op2,
2336 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2337 SDOperand Op6, SDOperand Op7) {
2338 std::vector<MVT::ValueType> ResultTys;
2339 ResultTys.push_back(VT1);
2340 ResultTys.push_back(VT2);
2341 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002342 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2343 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002344}
2345SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002346 MVT::ValueType VT2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002347 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002348 std::vector<MVT::ValueType> ResultTys;
2349 ResultTys.push_back(VT1);
2350 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002351 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002352}
2353
Evan Cheng99157a02006-08-07 22:13:29 +00002354/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002355/// This can cause recursive merging of nodes in the DAG.
2356///
Chris Lattner8b52f212005-08-26 18:36:28 +00002357/// This version assumes From/To have a single result value.
2358///
Chris Lattner1e111c72005-09-07 05:37:01 +00002359void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2360 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002361 SDNode *From = FromN.Val, *To = ToN.Val;
2362 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2363 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002364 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002365
Chris Lattner8b8749f2005-08-17 19:00:20 +00002366 while (!From->use_empty()) {
2367 // Process users until they are all gone.
2368 SDNode *U = *From->use_begin();
2369
2370 // This node is about to morph, remove its old self from the CSE maps.
2371 RemoveNodeFromCSEMaps(U);
2372
Chris Lattner65113b22005-11-08 22:07:03 +00002373 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2374 I != E; ++I)
2375 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002376 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002377 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002378 To->addUser(U);
2379 }
2380
2381 // Now that we have modified U, add it back to the CSE maps. If it already
2382 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002383 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2384 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002385 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002386 if (Deleted) Deleted->push_back(U);
2387 DeleteNodeNotInCSEMaps(U);
2388 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002389 }
2390}
2391
2392/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2393/// This can cause recursive merging of nodes in the DAG.
2394///
2395/// This version assumes From/To have matching types and numbers of result
2396/// values.
2397///
Chris Lattner1e111c72005-09-07 05:37:01 +00002398void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2399 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002400 assert(From != To && "Cannot replace uses of with self");
2401 assert(From->getNumValues() == To->getNumValues() &&
2402 "Cannot use this version of ReplaceAllUsesWith!");
2403 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002404 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002405 return;
2406 }
2407
2408 while (!From->use_empty()) {
2409 // Process users until they are all gone.
2410 SDNode *U = *From->use_begin();
2411
2412 // This node is about to morph, remove its old self from the CSE maps.
2413 RemoveNodeFromCSEMaps(U);
2414
Chris Lattner65113b22005-11-08 22:07:03 +00002415 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2416 I != E; ++I)
2417 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002418 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002419 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002420 To->addUser(U);
2421 }
2422
2423 // Now that we have modified U, add it back to the CSE maps. If it already
2424 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002425 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2426 ReplaceAllUsesWith(U, Existing, Deleted);
2427 // U is now dead.
2428 if (Deleted) Deleted->push_back(U);
2429 DeleteNodeNotInCSEMaps(U);
2430 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002431 }
2432}
2433
Chris Lattner8b52f212005-08-26 18:36:28 +00002434/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2435/// This can cause recursive merging of nodes in the DAG.
2436///
2437/// This version can replace From with any result values. To must match the
2438/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002439void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002440 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002441 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002442 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002443 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002444 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002445 return;
2446 }
2447
2448 while (!From->use_empty()) {
2449 // Process users until they are all gone.
2450 SDNode *U = *From->use_begin();
2451
2452 // This node is about to morph, remove its old self from the CSE maps.
2453 RemoveNodeFromCSEMaps(U);
2454
Chris Lattner65113b22005-11-08 22:07:03 +00002455 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2456 I != E; ++I)
2457 if (I->Val == From) {
2458 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002459 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002460 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002461 ToOp.Val->addUser(U);
2462 }
2463
2464 // Now that we have modified U, add it back to the CSE maps. If it already
2465 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002466 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2467 ReplaceAllUsesWith(U, Existing, Deleted);
2468 // U is now dead.
2469 if (Deleted) Deleted->push_back(U);
2470 DeleteNodeNotInCSEMaps(U);
2471 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002472 }
2473}
2474
Chris Lattner012f2412006-02-17 21:58:01 +00002475/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2476/// uses of other values produced by From.Val alone. The Deleted vector is
2477/// handled the same was as for ReplaceAllUsesWith.
2478void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2479 std::vector<SDNode*> &Deleted) {
2480 assert(From != To && "Cannot replace a value with itself");
2481 // Handle the simple, trivial, case efficiently.
2482 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2483 ReplaceAllUsesWith(From, To, &Deleted);
2484 return;
2485 }
2486
2487 // Get all of the users in a nice, deterministically ordered, uniqued set.
2488 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2489
2490 while (!Users.empty()) {
2491 // We know that this user uses some value of From. If it is the right
2492 // value, update it.
2493 SDNode *User = Users.back();
2494 Users.pop_back();
2495
2496 for (SDOperand *Op = User->OperandList,
2497 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2498 if (*Op == From) {
2499 // Okay, we know this user needs to be updated. Remove its old self
2500 // from the CSE maps.
2501 RemoveNodeFromCSEMaps(User);
2502
2503 // Update all operands that match "From".
2504 for (; Op != E; ++Op) {
2505 if (*Op == From) {
2506 From.Val->removeUser(User);
2507 *Op = To;
2508 To.Val->addUser(User);
2509 }
2510 }
2511
2512 // Now that we have modified User, add it back to the CSE maps. If it
2513 // already exists there, recursively merge the results together.
2514 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2515 unsigned NumDeleted = Deleted.size();
2516 ReplaceAllUsesWith(User, Existing, &Deleted);
2517
2518 // User is now dead.
2519 Deleted.push_back(User);
2520 DeleteNodeNotInCSEMaps(User);
2521
2522 // We have to be careful here, because ReplaceAllUsesWith could have
2523 // deleted a user of From, which means there may be dangling pointers
2524 // in the "Users" setvector. Scan over the deleted node pointers and
2525 // remove them from the setvector.
2526 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2527 Users.remove(Deleted[i]);
2528 }
2529 break; // Exit the operand scanning loop.
2530 }
2531 }
2532 }
2533}
2534
Chris Lattner7b2880c2005-08-24 22:44:39 +00002535
Evan Chenge6f35d82006-08-01 08:20:41 +00002536/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2537/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002538unsigned SelectionDAG::AssignNodeIds() {
2539 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002540 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2541 SDNode *N = I;
2542 N->setNodeId(Id++);
2543 }
2544 return Id;
2545}
2546
Evan Chenge6f35d82006-08-01 08:20:41 +00002547/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002548/// based on their topological order. It returns the maximum id and a vector
2549/// of the SDNodes* in assigned order by reference.
2550unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002551 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002552 std::vector<unsigned> InDegree(DAGSize);
2553 std::vector<SDNode*> Sources;
2554
2555 // Use a two pass approach to avoid using a std::map which is slow.
2556 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002557 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2558 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002559 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002560 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002561 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002562 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002563 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002564 }
2565
Evan Cheng99157a02006-08-07 22:13:29 +00002566 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002567 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002568 SDNode *N = Sources.back();
2569 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002570 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002571 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2572 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002573 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002574 if (Degree == 0)
2575 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002576 }
2577 }
2578
Evan Chengc384d6c2006-08-02 22:00:34 +00002579 // Second pass, assign the actual topological order as node ids.
2580 Id = 0;
2581 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2582 TI != TE; ++TI)
2583 (*TI)->setNodeId(Id++);
2584
2585 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002586}
2587
2588
Evan Cheng091cba12006-07-27 06:39:06 +00002589
Jim Laskey58b968b2005-08-17 20:08:02 +00002590//===----------------------------------------------------------------------===//
2591// SDNode Class
2592//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002593
Chris Lattner917d2c92006-07-19 00:00:37 +00002594// Out-of-line virtual method to give class a home.
2595void SDNode::ANCHOR() {
2596}
2597
Chris Lattnera3255112005-11-08 23:30:28 +00002598/// getValueTypeList - Return a pointer to the specified value type.
2599///
2600MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2601 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2602 VTs[VT] = VT;
2603 return &VTs[VT];
2604}
Chris Lattnera5682852006-08-07 23:03:03 +00002605
Chris Lattner5c884562005-01-12 18:37:47 +00002606/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2607/// indicated value. This method ignores uses of other values defined by this
2608/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002609bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002610 assert(Value < getNumValues() && "Bad value!");
2611
2612 // If there is only one value, this is easy.
2613 if (getNumValues() == 1)
2614 return use_size() == NUses;
2615 if (Uses.size() < NUses) return false;
2616
Evan Cheng4ee62112006-02-05 06:29:23 +00002617 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002618
2619 std::set<SDNode*> UsersHandled;
2620
Evan Cheng4ee62112006-02-05 06:29:23 +00002621 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002622 UI != E; ++UI) {
2623 SDNode *User = *UI;
2624 if (User->getNumOperands() == 1 ||
2625 UsersHandled.insert(User).second) // First time we've seen this?
2626 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2627 if (User->getOperand(i) == TheValue) {
2628 if (NUses == 0)
2629 return false; // too many uses
2630 --NUses;
2631 }
2632 }
2633
2634 // Found exactly the right number of uses?
2635 return NUses == 0;
2636}
2637
2638
Evan Cheng4ee62112006-02-05 06:29:23 +00002639// isOnlyUse - Return true if this node is the only use of N.
2640bool SDNode::isOnlyUse(SDNode *N) const {
2641 bool Seen = false;
2642 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2643 SDNode *User = *I;
2644 if (User == this)
2645 Seen = true;
2646 else
2647 return false;
2648 }
2649
2650 return Seen;
2651}
2652
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002653// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002654bool SDOperand::isOperand(SDNode *N) const {
2655 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2656 if (*this == N->getOperand(i))
2657 return true;
2658 return false;
2659}
2660
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002661bool SDNode::isOperand(SDNode *N) const {
2662 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2663 if (this == N->OperandList[i].Val)
2664 return true;
2665 return false;
2666}
Evan Cheng4ee62112006-02-05 06:29:23 +00002667
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002668const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002669 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002670 default:
2671 if (getOpcode() < ISD::BUILTIN_OP_END)
2672 return "<<Unknown DAG Node>>";
2673 else {
Evan Cheng72261582005-12-20 06:22:03 +00002674 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002675 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002676 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2677 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002678
Evan Cheng72261582005-12-20 06:22:03 +00002679 TargetLowering &TLI = G->getTargetLoweringInfo();
2680 const char *Name =
2681 TLI.getTargetNodeName(getOpcode());
2682 if (Name) return Name;
2683 }
2684
2685 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002686 }
2687
Andrew Lenharth95762122005-03-31 21:24:06 +00002688 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002689 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002690 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002691 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002692 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002693 case ISD::AssertSext: return "AssertSext";
2694 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002695
2696 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002697 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002698 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002699 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002700
2701 case ISD::Constant: return "Constant";
2702 case ISD::ConstantFP: return "ConstantFP";
2703 case ISD::GlobalAddress: return "GlobalAddress";
2704 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002705 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002706 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002707 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002708 case ISD::INTRINSIC_WO_CHAIN: {
2709 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2710 return Intrinsic::getName((Intrinsic::ID)IID);
2711 }
2712 case ISD::INTRINSIC_VOID:
2713 case ISD::INTRINSIC_W_CHAIN: {
2714 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002715 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002716 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002717
Chris Lattnerb2827b02006-03-19 00:52:58 +00002718 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002719 case ISD::TargetConstant: return "TargetConstant";
2720 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002721 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2722 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002723 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002724 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002725 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002726
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002727 case ISD::CopyToReg: return "CopyToReg";
2728 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002729 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002730 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002731 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002732 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002733 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002734 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002735
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002736 // Unary operators
2737 case ISD::FABS: return "fabs";
2738 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002739 case ISD::FSQRT: return "fsqrt";
2740 case ISD::FSIN: return "fsin";
2741 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002742
2743 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002744 case ISD::ADD: return "add";
2745 case ISD::SUB: return "sub";
2746 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002747 case ISD::MULHU: return "mulhu";
2748 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002749 case ISD::SDIV: return "sdiv";
2750 case ISD::UDIV: return "udiv";
2751 case ISD::SREM: return "srem";
2752 case ISD::UREM: return "urem";
2753 case ISD::AND: return "and";
2754 case ISD::OR: return "or";
2755 case ISD::XOR: return "xor";
2756 case ISD::SHL: return "shl";
2757 case ISD::SRA: return "sra";
2758 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002759 case ISD::ROTL: return "rotl";
2760 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002761 case ISD::FADD: return "fadd";
2762 case ISD::FSUB: return "fsub";
2763 case ISD::FMUL: return "fmul";
2764 case ISD::FDIV: return "fdiv";
2765 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002766 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002767 case ISD::VADD: return "vadd";
2768 case ISD::VSUB: return "vsub";
2769 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002770 case ISD::VSDIV: return "vsdiv";
2771 case ISD::VUDIV: return "vudiv";
2772 case ISD::VAND: return "vand";
2773 case ISD::VOR: return "vor";
2774 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002775
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002776 case ISD::SETCC: return "setcc";
2777 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002778 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002779 case ISD::VSELECT: return "vselect";
2780 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2781 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2782 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002783 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002784 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2785 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2786 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2787 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2788 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002789 case ISD::ADDC: return "addc";
2790 case ISD::ADDE: return "adde";
2791 case ISD::SUBC: return "subc";
2792 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002793 case ISD::SHL_PARTS: return "shl_parts";
2794 case ISD::SRA_PARTS: return "sra_parts";
2795 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002796
Chris Lattner7f644642005-04-28 21:44:03 +00002797 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002798 case ISD::SIGN_EXTEND: return "sign_extend";
2799 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002800 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002801 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002802 case ISD::TRUNCATE: return "truncate";
2803 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002804 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002805 case ISD::FP_EXTEND: return "fp_extend";
2806
2807 case ISD::SINT_TO_FP: return "sint_to_fp";
2808 case ISD::UINT_TO_FP: return "uint_to_fp";
2809 case ISD::FP_TO_SINT: return "fp_to_sint";
2810 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002811 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002812
2813 // Control flow instructions
2814 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002815 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002816 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002817 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002818 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002819 case ISD::CALLSEQ_START: return "callseq_start";
2820 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002821
2822 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002823 case ISD::LOAD: return "load";
2824 case ISD::STORE: return "store";
2825 case ISD::VLOAD: return "vload";
2826 case ISD::EXTLOAD: return "extload";
2827 case ISD::SEXTLOAD: return "sextload";
2828 case ISD::ZEXTLOAD: return "zextload";
2829 case ISD::TRUNCSTORE: return "truncstore";
2830 case ISD::VAARG: return "vaarg";
2831 case ISD::VACOPY: return "vacopy";
2832 case ISD::VAEND: return "vaend";
2833 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002834 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002835 case ISD::EXTRACT_ELEMENT: return "extract_element";
2836 case ISD::BUILD_PAIR: return "build_pair";
2837 case ISD::STACKSAVE: return "stacksave";
2838 case ISD::STACKRESTORE: return "stackrestore";
2839
2840 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002841 case ISD::MEMSET: return "memset";
2842 case ISD::MEMCPY: return "memcpy";
2843 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002844
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002845 // Bit manipulation
2846 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002847 case ISD::CTPOP: return "ctpop";
2848 case ISD::CTTZ: return "cttz";
2849 case ISD::CTLZ: return "ctlz";
2850
Chris Lattner36ce6912005-11-29 06:21:05 +00002851 // Debug info
2852 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002853 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002854 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002855
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002856 case ISD::CONDCODE:
2857 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002858 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002859 case ISD::SETOEQ: return "setoeq";
2860 case ISD::SETOGT: return "setogt";
2861 case ISD::SETOGE: return "setoge";
2862 case ISD::SETOLT: return "setolt";
2863 case ISD::SETOLE: return "setole";
2864 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002865
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002866 case ISD::SETO: return "seto";
2867 case ISD::SETUO: return "setuo";
2868 case ISD::SETUEQ: return "setue";
2869 case ISD::SETUGT: return "setugt";
2870 case ISD::SETUGE: return "setuge";
2871 case ISD::SETULT: return "setult";
2872 case ISD::SETULE: return "setule";
2873 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002874
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002875 case ISD::SETEQ: return "seteq";
2876 case ISD::SETGT: return "setgt";
2877 case ISD::SETGE: return "setge";
2878 case ISD::SETLT: return "setlt";
2879 case ISD::SETLE: return "setle";
2880 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002881 }
2882 }
2883}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002884
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002885void SDNode::dump() const { dump(0); }
2886void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002887 std::cerr << (void*)this << ": ";
2888
2889 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2890 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002891 if (getValueType(i) == MVT::Other)
2892 std::cerr << "ch";
2893 else
2894 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002895 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002896 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002897
2898 std::cerr << " ";
2899 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2900 if (i) std::cerr << ", ";
2901 std::cerr << (void*)getOperand(i).Val;
2902 if (unsigned RN = getOperand(i).ResNo)
2903 std::cerr << ":" << RN;
2904 }
2905
2906 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2907 std::cerr << "<" << CSDN->getValue() << ">";
2908 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2909 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002910 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002911 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002912 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002913 std::cerr << "<";
2914 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002915 if (offset > 0)
2916 std::cerr << " + " << offset;
2917 else
2918 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002919 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002920 std::cerr << "<" << FIDN->getIndex() << ">";
2921 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002922 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002923 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002924 if (offset > 0)
2925 std::cerr << " + " << offset;
2926 else
2927 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002928 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002929 std::cerr << "<";
2930 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2931 if (LBB)
2932 std::cerr << LBB->getName() << " ";
2933 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002934 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002935 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002936 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2937 } else {
2938 std::cerr << " #" << R->getReg();
2939 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002940 } else if (const ExternalSymbolSDNode *ES =
2941 dyn_cast<ExternalSymbolSDNode>(this)) {
2942 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002943 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2944 if (M->getValue())
2945 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2946 else
2947 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002948 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2949 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002950 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002951}
2952
Chris Lattnerde202b32005-11-09 23:47:37 +00002953static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002954 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2955 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002956 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002957 else
2958 std::cerr << "\n" << std::string(indent+2, ' ')
2959 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002960
Chris Lattnerea946cd2005-01-09 20:38:33 +00002961
2962 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002963 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002964}
2965
Chris Lattnerc3aae252005-01-07 07:46:32 +00002966void SelectionDAG::dump() const {
2967 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002968 std::vector<const SDNode*> Nodes;
2969 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2970 I != E; ++I)
2971 Nodes.push_back(I);
2972
Chris Lattner49d24712005-01-09 20:26:36 +00002973 std::sort(Nodes.begin(), Nodes.end());
2974
2975 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002976 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002977 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002978 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002979
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002980 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002981
Chris Lattnerc3aae252005-01-07 07:46:32 +00002982 std::cerr << "\n\n";
2983}
2984
Evan Chengfae9f1c2006-02-09 22:11:03 +00002985/// InsertISelMapEntry - A helper function to insert a key / element pair
2986/// into a SDOperand to SDOperand map. This is added to avoid the map
2987/// insertion operator from being inlined.
2988void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2989 SDNode *Key, unsigned KeyResNo,
2990 SDNode *Element, unsigned ElementResNo) {
2991 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2992 SDOperand(Element, ElementResNo)));
2993}