blob: ab38d94ca6d4728ea88b4246b09f9bee9fb37256 [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 Lattnerd8658612005-02-17 20:17:32 +0000344 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000345 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000348 }
Chris Lattner3181a772006-01-29 06:26:56 +0000349 case ISD::TargetConstantFP: {
350 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
351 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
352 break;
353 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000354 case ISD::STRING:
355 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
356 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000357 case ISD::CONDCODE:
358 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
359 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000360 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000361 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
362 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000363 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000364 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000365 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000366 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000367 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000368 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000369 case ISD::JumpTable:
370 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
371 break;
372 case ISD::TargetJumpTable:
373 Erased =
374 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
375 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000377 Erased = ConstantPoolIndices.
378 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000379 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
380 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000381 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000382 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000383 Erased = TargetConstantPoolIndices.
384 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000385 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
386 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000387 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000388 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000389 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000390 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000391 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000392 Erased =
393 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000394 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000395 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000396 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000397 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
398 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000399 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000400 // Remove it from the CSE Map.
401 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 break;
403 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000404#ifndef NDEBUG
405 // Verify that the node was actually in one of the CSE maps, unless it has a
406 // flag result (which cannot be CSE'd) or is one of the special cases that are
407 // not subject to CSE.
408 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000409 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000410 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000411 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000412 assert(0 && "Node is not in map!");
413 }
414#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415}
416
Chris Lattner8b8749f2005-08-17 19:00:20 +0000417/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
418/// has been taken out and modified in some way. If the specified node already
419/// exists in the CSE maps, do not modify the maps, but return the existing node
420/// instead. If it doesn't exist, add it and return null.
421///
422SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
423 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000424 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000425 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000426
Chris Lattner9f8cc692005-12-19 22:21:21 +0000427 // Check that remaining values produced are not flags.
428 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
429 if (N->getValueType(i) == MVT::Flag)
430 return 0; // Never CSE anything that produces a flag.
431
Chris Lattnera5682852006-08-07 23:03:03 +0000432 SDNode *New = CSEMap.GetOrInsertNode(N);
433 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000434 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000435}
436
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000437/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
438/// were replaced with those specified. If this node is never memoized,
439/// return null, otherwise return a pointer to the slot it would take. If a
440/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000441SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
442 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000443 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000444 return 0; // Never add these nodes.
445
446 // Check that remaining values produced are not flags.
447 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
448 if (N->getValueType(i) == MVT::Flag)
449 return 0; // Never CSE anything that produces a flag.
450
Chris Lattnera5682852006-08-07 23:03:03 +0000451 SelectionDAGCSEMap::NodeID ID;
452 ID.SetOpcode(N->getOpcode());
453 ID.SetValueTypes(N->value_begin());
454 ID.SetOperands(Op);
455 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000456}
457
458/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
459/// were replaced with those specified. If this node is never memoized,
460/// return null, otherwise return a pointer to the slot it would take. If a
461/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000462SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
463 SDOperand Op1, SDOperand Op2,
464 void *&InsertPos) {
465 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
466 return 0; // Never add these nodes.
467
468 // Check that remaining values produced are not flags.
469 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
470 if (N->getValueType(i) == MVT::Flag)
471 return 0; // Never CSE anything that produces a flag.
472
473 SelectionDAGCSEMap::NodeID ID;
474 ID.SetOpcode(N->getOpcode());
475 ID.SetValueTypes(N->value_begin());
476 ID.SetOperands(Op1, Op2);
477 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
478}
479
480
481/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
482/// were replaced with those specified. If this node is never memoized,
483/// return null, otherwise return a pointer to the slot it would take. If a
484/// node already exists with these operands, the slot will be non-null.
485SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000486 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000487 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000488 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000489 return 0; // Never add these nodes.
490
491 // Check that remaining values produced are not flags.
492 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
493 if (N->getValueType(i) == MVT::Flag)
494 return 0; // Never CSE anything that produces a flag.
495
Chris Lattnera5682852006-08-07 23:03:03 +0000496 SelectionDAGCSEMap::NodeID ID;
497 ID.SetOpcode(N->getOpcode());
498 ID.SetValueTypes(N->value_begin());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000499 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000500 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000501}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000502
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000503
Chris Lattner78ec3112003-08-11 14:57:33 +0000504SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000505 while (!AllNodes.empty()) {
506 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000507 delete [] N->OperandList;
508 N->OperandList = 0;
509 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000510 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000511 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000512}
513
Chris Lattner0f2287b2005-04-13 02:38:18 +0000514SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000515 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000516 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000517 return getNode(ISD::AND, Op.getValueType(), Op,
518 getConstant(Imm, Op.getValueType()));
519}
520
Chris Lattner36ce6912005-11-29 06:21:05 +0000521SDOperand SelectionDAG::getString(const std::string &Val) {
522 StringSDNode *&N = StringNodes[Val];
523 if (!N) {
524 N = new StringSDNode(Val);
525 AllNodes.push_back(N);
526 }
527 return SDOperand(N, 0);
528}
529
Chris Lattner61b09412006-08-11 21:01:22 +0000530SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000531 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000532 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000533
Chris Lattner61b09412006-08-11 21:01:22 +0000534 // Mask out any bits that are not valid for this constant.
535 Val &= MVT::getIntVTBitMask(VT);
536
537 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
538 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
539 ID.AddInteger(Val);
540 void *IP = 0;
541 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
542 return SDOperand(E, 0);
543 SDNode *N = new ConstantSDNode(isT, Val, VT);
544 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000545 AllNodes.push_back(N);
546 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000547}
548
Chris Lattner61b09412006-08-11 21:01:22 +0000549
Chris Lattnerc3aae252005-01-07 07:46:32 +0000550SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
551 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
552 if (VT == MVT::f32)
553 Val = (float)Val; // Mask out extra precision.
554
Chris Lattnerd8658612005-02-17 20:17:32 +0000555 // Do the map lookup using the actual bit pattern for the floating point
556 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
557 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000558 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000559 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000560 N = new ConstantFPSDNode(false, Val, VT);
561 AllNodes.push_back(N);
562 return SDOperand(N, 0);
563}
564
565SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
566 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
567 if (VT == MVT::f32)
568 Val = (float)Val; // Mask out extra precision.
569
570 // Do the map lookup using the actual bit pattern for the floating point
571 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
572 // we don't have issues with SNANs.
573 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
574 if (N) return SDOperand(N, 0);
575 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000576 AllNodes.push_back(N);
577 return SDOperand(N, 0);
578}
579
Chris Lattnerc3aae252005-01-07 07:46:32 +0000580SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000581 MVT::ValueType VT, int Offset,
582 bool isTargetGA) {
583 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
584 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
585 ID.AddPointer(GV);
586 ID.AddInteger(Offset);
587 void *IP = 0;
588 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
589 return SDOperand(E, 0);
590 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
591 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 AllNodes.push_back(N);
593 return SDOperand(N, 0);
594}
595
596SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
597 SDNode *&N = FrameIndices[FI];
598 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000599 N = new FrameIndexSDNode(FI, VT, false);
600 AllNodes.push_back(N);
601 return SDOperand(N, 0);
602}
603
604SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
605 SDNode *&N = TargetFrameIndices[FI];
606 if (N) return SDOperand(N, 0);
607 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000608 AllNodes.push_back(N);
609 return SDOperand(N, 0);
610}
611
Nate Begeman37efe672006-04-22 18:53:45 +0000612SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
613 SDNode *&N = JumpTableIndices[JTI];
614 if (N) return SDOperand(N, 0);
615 N = new JumpTableSDNode(JTI, VT, false);
616 AllNodes.push_back(N);
617 return SDOperand(N, 0);
618}
619
620SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
621 SDNode *&N = TargetJumpTableIndices[JTI];
622 if (N) return SDOperand(N, 0);
623 N = new JumpTableSDNode(JTI, VT, true);
624 AllNodes.push_back(N);
625 return SDOperand(N, 0);
626}
627
Evan Chengb8973bd2006-01-31 22:23:14 +0000628SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000629 unsigned Alignment, int Offset) {
630 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
631 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000632 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000633 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000634 AllNodes.push_back(N);
635 return SDOperand(N, 0);
636}
637
Evan Chengb8973bd2006-01-31 22:23:14 +0000638SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000639 unsigned Alignment, int Offset) {
640 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
641 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000642 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000643 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000644 AllNodes.push_back(N);
645 return SDOperand(N, 0);
646}
647
648SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner61b09412006-08-11 21:01:22 +0000649 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getNodeValueTypes(MVT::Other));
650 ID.AddPointer(MBB);
651 void *IP = 0;
652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
653 return SDOperand(E, 0);
654 SDNode *N = new BasicBlockSDNode(MBB);
655 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
658}
659
Chris Lattner15e4b012005-07-10 00:07:11 +0000660SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
661 if ((unsigned)VT >= ValueTypeNodes.size())
662 ValueTypeNodes.resize(VT+1);
663 if (ValueTypeNodes[VT] == 0) {
664 ValueTypeNodes[VT] = new VTSDNode(VT);
665 AllNodes.push_back(ValueTypeNodes[VT]);
666 }
667
668 return SDOperand(ValueTypeNodes[VT], 0);
669}
670
Chris Lattnerc3aae252005-01-07 07:46:32 +0000671SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
672 SDNode *&N = ExternalSymbols[Sym];
673 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000674 N = new ExternalSymbolSDNode(false, Sym, VT);
675 AllNodes.push_back(N);
676 return SDOperand(N, 0);
677}
678
Chris Lattner809ec112006-01-28 10:09:25 +0000679SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
680 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000681 SDNode *&N = TargetExternalSymbols[Sym];
682 if (N) return SDOperand(N, 0);
683 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000684 AllNodes.push_back(N);
685 return SDOperand(N, 0);
686}
687
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000688SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
689 if ((unsigned)Cond >= CondCodeNodes.size())
690 CondCodeNodes.resize(Cond+1);
691
Chris Lattner079a27a2005-08-09 20:40:02 +0000692 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000693 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000694 AllNodes.push_back(CondCodeNodes[Cond]);
695 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000696 return SDOperand(CondCodeNodes[Cond], 0);
697}
698
Chris Lattner0fdd7682005-08-30 22:38:38 +0000699SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner61b09412006-08-11 21:01:22 +0000700 SelectionDAGCSEMap::NodeID ID(ISD::Register, getNodeValueTypes(VT));
701 ID.AddInteger(RegNo);
702 void *IP = 0;
703 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
704 return SDOperand(E, 0);
705 SDNode *N = new RegisterSDNode(RegNo, VT);
706 CSEMap.InsertNode(N, IP);
707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
709}
710
711SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
712 assert((!V || isa<PointerType>(V->getType())) &&
713 "SrcValue is not a pointer?");
714
715 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getNodeValueTypes(MVT::Other));
716 ID.AddPointer(V);
717 ID.AddInteger(Offset);
718 void *IP = 0;
719 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
720 return SDOperand(E, 0);
721 SDNode *N = new SrcValueSDNode(V, Offset);
722 CSEMap.InsertNode(N, IP);
723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000725}
726
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000727SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
728 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000729 // These setcc operations always fold.
730 switch (Cond) {
731 default: break;
732 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000733 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000735 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000736
737 case ISD::SETOEQ:
738 case ISD::SETOGT:
739 case ISD::SETOGE:
740 case ISD::SETOLT:
741 case ISD::SETOLE:
742 case ISD::SETONE:
743 case ISD::SETO:
744 case ISD::SETUO:
745 case ISD::SETUEQ:
746 case ISD::SETUNE:
747 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
748 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 }
750
Chris Lattner67255a12005-04-07 18:14:58 +0000751 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
752 uint64_t C2 = N2C->getValue();
753 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
754 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000755
Chris Lattnerc3aae252005-01-07 07:46:32 +0000756 // Sign extend the operands if required
757 if (ISD::isSignedIntSetCC(Cond)) {
758 C1 = N1C->getSignExtended();
759 C2 = N2C->getSignExtended();
760 }
761
762 switch (Cond) {
763 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000764 case ISD::SETEQ: return getConstant(C1 == C2, VT);
765 case ISD::SETNE: return getConstant(C1 != C2, VT);
766 case ISD::SETULT: return getConstant(C1 < C2, VT);
767 case ISD::SETUGT: return getConstant(C1 > C2, VT);
768 case ISD::SETULE: return getConstant(C1 <= C2, VT);
769 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
770 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
771 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
772 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
773 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000774 }
Chris Lattner24673922005-04-07 18:58:54 +0000775 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000776 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000777 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
778 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
779
780 // If the comparison constant has bits in the upper part, the
781 // zero-extended value could never match.
782 if (C2 & (~0ULL << InSize)) {
783 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
784 switch (Cond) {
785 case ISD::SETUGT:
786 case ISD::SETUGE:
787 case ISD::SETEQ: return getConstant(0, VT);
788 case ISD::SETULT:
789 case ISD::SETULE:
790 case ISD::SETNE: return getConstant(1, VT);
791 case ISD::SETGT:
792 case ISD::SETGE:
793 // True if the sign bit of C2 is set.
794 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
795 case ISD::SETLT:
796 case ISD::SETLE:
797 // True if the sign bit of C2 isn't set.
798 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
799 default:
800 break;
801 }
802 }
803
804 // Otherwise, we can perform the comparison with the low bits.
805 switch (Cond) {
806 case ISD::SETEQ:
807 case ISD::SETNE:
808 case ISD::SETUGT:
809 case ISD::SETUGE:
810 case ISD::SETULT:
811 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000812 return getSetCC(VT, N1.getOperand(0),
813 getConstant(C2, N1.getOperand(0).getValueType()),
814 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000815 default:
816 break; // todo, be more careful with signed comparisons
817 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000818 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
819 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
820 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
821 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
822 MVT::ValueType ExtDstTy = N1.getValueType();
823 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000824
Chris Lattner7b2880c2005-08-24 22:44:39 +0000825 // If the extended part has any inconsistent bits, it cannot ever
826 // compare equal. In other words, they have to be all ones or all
827 // zeros.
828 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000829 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000830 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
831 return getConstant(Cond == ISD::SETNE, VT);
832
833 // Otherwise, make this a use of a zext.
834 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000835 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000836 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000837 }
838
Chris Lattner67255a12005-04-07 18:14:58 +0000839 uint64_t MinVal, MaxVal;
840 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
841 if (ISD::isSignedIntSetCC(Cond)) {
842 MinVal = 1ULL << (OperandBitSize-1);
843 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
844 MaxVal = ~0ULL >> (65-OperandBitSize);
845 else
846 MaxVal = 0;
847 } else {
848 MinVal = 0;
849 MaxVal = ~0ULL >> (64-OperandBitSize);
850 }
851
852 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
853 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
854 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
855 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000856 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
857 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000858 }
859
860 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
861 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
862 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000863 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
864 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000865 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000866
Nate Begeman72ea2812005-04-14 08:56:52 +0000867 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
868 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000869
Nate Begeman72ea2812005-04-14 08:56:52 +0000870 // Canonicalize setgt X, Min --> setne X, Min
871 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000872 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000874 // If we have setult X, 1, turn it into seteq X, 0
875 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
877 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000878 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000879 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
881 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000882
883 // If we have "setcc X, C1", check to see if we can shrink the immediate
884 // by changing cc.
885
886 // SETUGT X, SINTMAX -> SETLT X, 0
887 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
888 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000889 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000890
891 // FIXME: Implement the rest of these.
892
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000893
894 // Fold bit comparisons when we can.
895 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
896 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
897 if (ConstantSDNode *AndRHS =
898 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
899 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
900 // Perform the xform if the AND RHS is a single bit.
901 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
902 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000903 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000904 TLI.getShiftAmountTy()));
905 }
906 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
907 // (X & 8) == 8 --> (X & 8) >> 3
908 // Perform the xform if C2 is a single bit.
909 if ((C2 & (C2-1)) == 0) {
910 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000911 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000912 }
913 }
914 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915 }
Chris Lattner67255a12005-04-07 18:14:58 +0000916 } else if (isa<ConstantSDNode>(N1.Val)) {
917 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000918 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000919 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920
921 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
922 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
923 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000924
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 switch (Cond) {
926 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000927 case ISD::SETEQ: return getConstant(C1 == C2, VT);
928 case ISD::SETNE: return getConstant(C1 != C2, VT);
929 case ISD::SETLT: return getConstant(C1 < C2, VT);
930 case ISD::SETGT: return getConstant(C1 > C2, VT);
931 case ISD::SETLE: return getConstant(C1 <= C2, VT);
932 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934 } else {
935 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000936 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 }
938
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000939 // Could not fold it.
940 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941}
942
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000944///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +0000946 MVT::ValueType *VTs = getNodeValueTypes(VT);
947 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
948 void *IP = 0;
949 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
950 return SDOperand(E, 0);
951 SDNode *N = new SDNode(Opcode, VT);
952 CSEMap.InsertNode(N, IP);
953
954 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 return SDOperand(N, 0);
956}
957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
959 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000960 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000961 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
963 uint64_t Val = C->getValue();
964 switch (Opcode) {
965 default: break;
966 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000967 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000968 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
969 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000970 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
971 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000972 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000973 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000974 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000975 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000976 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000977 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000978 case ISD::BSWAP:
979 switch(VT) {
980 default: assert(0 && "Invalid bswap!"); break;
981 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
982 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
983 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
984 }
985 break;
986 case ISD::CTPOP:
987 switch(VT) {
988 default: assert(0 && "Invalid ctpop!"); break;
989 case MVT::i1: return getConstant(Val != 0, VT);
990 case MVT::i8:
991 Tmp1 = (unsigned)Val & 0xFF;
992 return getConstant(CountPopulation_32(Tmp1), VT);
993 case MVT::i16:
994 Tmp1 = (unsigned)Val & 0xFFFF;
995 return getConstant(CountPopulation_32(Tmp1), VT);
996 case MVT::i32:
997 return getConstant(CountPopulation_32((unsigned)Val), VT);
998 case MVT::i64:
999 return getConstant(CountPopulation_64(Val), VT);
1000 }
1001 case ISD::CTLZ:
1002 switch(VT) {
1003 default: assert(0 && "Invalid ctlz!"); break;
1004 case MVT::i1: return getConstant(Val == 0, VT);
1005 case MVT::i8:
1006 Tmp1 = (unsigned)Val & 0xFF;
1007 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1008 case MVT::i16:
1009 Tmp1 = (unsigned)Val & 0xFFFF;
1010 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1011 case MVT::i32:
1012 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1013 case MVT::i64:
1014 return getConstant(CountLeadingZeros_64(Val), VT);
1015 }
1016 case ISD::CTTZ:
1017 switch(VT) {
1018 default: assert(0 && "Invalid cttz!"); break;
1019 case MVT::i1: return getConstant(Val == 0, VT);
1020 case MVT::i8:
1021 Tmp1 = (unsigned)Val | 0x100;
1022 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1023 case MVT::i16:
1024 Tmp1 = (unsigned)Val | 0x10000;
1025 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1026 case MVT::i32:
1027 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1028 case MVT::i64:
1029 return getConstant(CountTrailingZeros_64(Val), VT);
1030 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 }
1032 }
1033
Chris Lattner94683772005-12-23 05:30:37 +00001034 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1036 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001037 case ISD::FNEG:
1038 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001039 case ISD::FABS:
1040 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001041 case ISD::FP_ROUND:
1042 case ISD::FP_EXTEND:
1043 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001044 case ISD::FP_TO_SINT:
1045 return getConstant((int64_t)C->getValue(), VT);
1046 case ISD::FP_TO_UINT:
1047 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001048 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001049 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001050 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001051 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001052 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001053 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 }
1055
1056 unsigned OpOpcode = Operand.Val->getOpcode();
1057 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001058 case ISD::TokenFactor:
1059 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060 case ISD::SIGN_EXTEND:
1061 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001062 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1064 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1065 break;
1066 case ISD::ZERO_EXTEND:
1067 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001068 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001069 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001070 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001072 case ISD::ANY_EXTEND:
1073 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001074 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001075 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1076 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1077 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1078 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001079 case ISD::TRUNCATE:
1080 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001081 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 if (OpOpcode == ISD::TRUNCATE)
1083 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001084 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1085 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001086 // If the source is smaller than the dest, we still need an extend.
1087 if (Operand.Val->getOperand(0).getValueType() < VT)
1088 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1089 else if (Operand.Val->getOperand(0).getValueType() > VT)
1090 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1091 else
1092 return Operand.Val->getOperand(0);
1093 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 break;
Chris Lattner35481892005-12-23 00:16:34 +00001095 case ISD::BIT_CONVERT:
1096 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001097 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001098 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001099 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001100 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1101 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001102 if (OpOpcode == ISD::UNDEF)
1103 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001104 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001105 case ISD::SCALAR_TO_VECTOR:
1106 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1107 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1108 "Illegal SCALAR_TO_VECTOR node!");
1109 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001110 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001111 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1112 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001113 Operand.Val->getOperand(0));
1114 if (OpOpcode == ISD::FNEG) // --X -> X
1115 return Operand.Val->getOperand(0);
1116 break;
1117 case ISD::FABS:
1118 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1119 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1120 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001121 }
1122
Chris Lattner43247a12005-08-25 19:12:10 +00001123 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001124 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001125 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001126 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1127 void *IP = 0;
1128 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1129 return SDOperand(E, 0);
1130 N = new SDNode(Opcode, Operand);
1131 N->setValueTypes(VTs, 1);
1132 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001133 } else {
1134 N = new SDNode(Opcode, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001135 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001136 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 AllNodes.push_back(N);
1138 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001139}
1140
Chris Lattner36019aa2005-04-18 03:48:41 +00001141
1142
Chris Lattnerc3aae252005-01-07 07:46:32 +00001143SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1144 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001145#ifndef NDEBUG
1146 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001147 case ISD::TokenFactor:
1148 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1149 N2.getValueType() == MVT::Other && "Invalid token factor!");
1150 break;
Chris Lattner76365122005-01-16 02:23:22 +00001151 case ISD::AND:
1152 case ISD::OR:
1153 case ISD::XOR:
1154 case ISD::UDIV:
1155 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001156 case ISD::MULHU:
1157 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001158 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1159 // fall through
1160 case ISD::ADD:
1161 case ISD::SUB:
1162 case ISD::MUL:
1163 case ISD::SDIV:
1164 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001165 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1166 // fall through.
1167 case ISD::FADD:
1168 case ISD::FSUB:
1169 case ISD::FMUL:
1170 case ISD::FDIV:
1171 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001172 assert(N1.getValueType() == N2.getValueType() &&
1173 N1.getValueType() == VT && "Binary operator types must match!");
1174 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001175 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1176 assert(N1.getValueType() == VT &&
1177 MVT::isFloatingPoint(N1.getValueType()) &&
1178 MVT::isFloatingPoint(N2.getValueType()) &&
1179 "Invalid FCOPYSIGN!");
1180 break;
Chris Lattner76365122005-01-16 02:23:22 +00001181 case ISD::SHL:
1182 case ISD::SRA:
1183 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001184 case ISD::ROTL:
1185 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001186 assert(VT == N1.getValueType() &&
1187 "Shift operators return type must be the same as their first arg");
1188 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001189 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001190 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001191 case ISD::FP_ROUND_INREG: {
1192 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1193 assert(VT == N1.getValueType() && "Not an inreg round!");
1194 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1195 "Cannot FP_ROUND_INREG integer types");
1196 assert(EVT <= VT && "Not rounding down!");
1197 break;
1198 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001199 case ISD::AssertSext:
1200 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001201 case ISD::SIGN_EXTEND_INREG: {
1202 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1203 assert(VT == N1.getValueType() && "Not an inreg extend!");
1204 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1205 "Cannot *_EXTEND_INREG FP types");
1206 assert(EVT <= VT && "Not extending!");
1207 }
1208
Chris Lattner76365122005-01-16 02:23:22 +00001209 default: break;
1210 }
1211#endif
1212
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1215 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001216 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1217 int64_t Val = N1C->getValue();
1218 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1219 Val <<= 64-FromBits;
1220 Val >>= 64-FromBits;
1221 return getConstant(Val, VT);
1222 }
1223
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 if (N2C) {
1225 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1226 switch (Opcode) {
1227 case ISD::ADD: return getConstant(C1 + C2, VT);
1228 case ISD::SUB: return getConstant(C1 - C2, VT);
1229 case ISD::MUL: return getConstant(C1 * C2, VT);
1230 case ISD::UDIV:
1231 if (C2) return getConstant(C1 / C2, VT);
1232 break;
1233 case ISD::UREM :
1234 if (C2) return getConstant(C1 % C2, VT);
1235 break;
1236 case ISD::SDIV :
1237 if (C2) return getConstant(N1C->getSignExtended() /
1238 N2C->getSignExtended(), VT);
1239 break;
1240 case ISD::SREM :
1241 if (C2) return getConstant(N1C->getSignExtended() %
1242 N2C->getSignExtended(), VT);
1243 break;
1244 case ISD::AND : return getConstant(C1 & C2, VT);
1245 case ISD::OR : return getConstant(C1 | C2, VT);
1246 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001247 case ISD::SHL : return getConstant(C1 << C2, VT);
1248 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001249 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001250 case ISD::ROTL :
1251 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1252 VT);
1253 case ISD::ROTR :
1254 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1255 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 default: break;
1257 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001258 } else { // Cannonicalize constant to RHS if commutative
1259 if (isCommutativeBinOp(Opcode)) {
1260 std::swap(N1C, N2C);
1261 std::swap(N1, N2);
1262 }
1263 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001264 }
1265
1266 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1267 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001268 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001269 if (N2CFP) {
1270 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1271 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001272 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1273 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1274 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1275 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001276 if (C2) return getConstantFP(C1 / C2, VT);
1277 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001278 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279 if (C2) return getConstantFP(fmod(C1, C2), VT);
1280 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001281 case ISD::FCOPYSIGN: {
1282 union {
1283 double F;
1284 uint64_t I;
1285 } u1;
1286 union {
1287 double F;
1288 int64_t I;
1289 } u2;
1290 u1.F = C1;
1291 u2.F = C2;
1292 if (u2.I < 0) // Sign bit of RHS set?
1293 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1294 else
1295 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1296 return getConstantFP(u1.F, VT);
1297 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001298 default: break;
1299 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001300 } else { // Cannonicalize constant to RHS if commutative
1301 if (isCommutativeBinOp(Opcode)) {
1302 std::swap(N1CFP, N2CFP);
1303 std::swap(N1, N2);
1304 }
1305 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001306 }
Chris Lattner62b57722006-04-20 05:39:12 +00001307
1308 // Canonicalize an UNDEF to the RHS, even over a constant.
1309 if (N1.getOpcode() == ISD::UNDEF) {
1310 if (isCommutativeBinOp(Opcode)) {
1311 std::swap(N1, N2);
1312 } else {
1313 switch (Opcode) {
1314 case ISD::FP_ROUND_INREG:
1315 case ISD::SIGN_EXTEND_INREG:
1316 case ISD::SUB:
1317 case ISD::FSUB:
1318 case ISD::FDIV:
1319 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001320 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001321 return N1; // fold op(undef, arg2) -> undef
1322 case ISD::UDIV:
1323 case ISD::SDIV:
1324 case ISD::UREM:
1325 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001326 case ISD::SRL:
1327 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001328 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1329 }
1330 }
1331 }
1332
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001333 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001334 if (N2.getOpcode() == ISD::UNDEF) {
1335 switch (Opcode) {
1336 case ISD::ADD:
1337 case ISD::SUB:
1338 case ISD::FADD:
1339 case ISD::FSUB:
1340 case ISD::FMUL:
1341 case ISD::FDIV:
1342 case ISD::FREM:
1343 case ISD::UDIV:
1344 case ISD::SDIV:
1345 case ISD::UREM:
1346 case ISD::SREM:
1347 case ISD::XOR:
1348 return N2; // fold op(arg1, undef) -> undef
1349 case ISD::MUL:
1350 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001351 case ISD::SRL:
1352 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001353 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1354 case ISD::OR:
1355 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001356 case ISD::SRA:
1357 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001358 }
1359 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001360
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 // Finally, fold operations that do not require constants.
1362 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001363 case ISD::FP_ROUND_INREG:
1364 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1365 break;
1366 case ISD::SIGN_EXTEND_INREG: {
1367 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1368 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001369 break;
1370 }
1371
Nate Begemaneea805e2005-04-13 21:23:31 +00001372 // FIXME: figure out how to safely handle things like
1373 // int foo(int x) { return 1 << (x & 255); }
1374 // int bar() { return foo(256); }
1375#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001376 case ISD::SHL:
1377 case ISD::SRL:
1378 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001379 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001380 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001381 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001382 else if (N2.getOpcode() == ISD::AND)
1383 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1384 // If the and is only masking out bits that cannot effect the shift,
1385 // eliminate the and.
1386 unsigned NumBits = MVT::getSizeInBits(VT);
1387 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1388 return getNode(Opcode, VT, N1, N2.getOperand(0));
1389 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001390 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001391#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001392 }
1393
Chris Lattner27e9b412005-05-11 18:57:39 +00001394 // Memoize this node if possible.
1395 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001396 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001397 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001398 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1399 void *IP = 0;
1400 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1401 return SDOperand(E, 0);
1402 N = new SDNode(Opcode, N1, N2);
1403 N->setValueTypes(VTs, 1);
1404 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001405 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001406 N = new SDNode(Opcode, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001407 N->setValueTypes(VTs, 1);
Chris Lattner27e9b412005-05-11 18:57:39 +00001408 }
1409
Chris Lattnerc3aae252005-01-07 07:46:32 +00001410 AllNodes.push_back(N);
1411 return SDOperand(N, 0);
1412}
1413
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1415 SDOperand N1, SDOperand N2, SDOperand N3) {
1416 // Perform various simplifications.
1417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1418 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001419 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001420 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001421 case ISD::SETCC: {
1422 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001423 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001424 if (Simp.Val) return Simp;
1425 break;
1426 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001427 case ISD::SELECT:
1428 if (N1C)
1429 if (N1C->getValue())
1430 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001431 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001432 return N3; // select false, X, Y -> Y
1433
1434 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001435 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001436 case ISD::BRCOND:
1437 if (N2C)
1438 if (N2C->getValue()) // Unconditional branch
1439 return getNode(ISD::BR, MVT::Other, N1, N3);
1440 else
1441 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001442 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001443 case ISD::VECTOR_SHUFFLE:
1444 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1445 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1446 N3.getOpcode() == ISD::BUILD_VECTOR &&
1447 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1448 "Illegal VECTOR_SHUFFLE node!");
1449 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001450 }
1451
Chris Lattner43247a12005-08-25 19:12:10 +00001452 // Memoize node if it doesn't produce a flag.
1453 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001454 MVT::ValueType *VTs = getNodeValueTypes(VT);
1455
Chris Lattner43247a12005-08-25 19:12:10 +00001456 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001457 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1458 void *IP = 0;
1459 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1460 return SDOperand(E, 0);
1461 N = new SDNode(Opcode, N1, N2, N3);
1462 N->setValueTypes(VTs, 1);
1463 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001464 } else {
1465 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001466 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001467 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001468 AllNodes.push_back(N);
1469 return SDOperand(N, 0);
1470}
1471
1472SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001473 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001474 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001475 SDOperand Ops[] = { N1, N2, N3, N4 };
1476 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001477}
1478
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001479SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1480 SDOperand N1, SDOperand N2, SDOperand N3,
1481 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001482 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1483 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001484}
1485
Evan Cheng7038daf2005-12-10 00:37:58 +00001486SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1487 SDOperand Chain, SDOperand Ptr,
1488 SDOperand SV) {
Chris Lattnera5682852006-08-07 23:03:03 +00001489 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1490
1491 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1492 void *IP = 0;
1493 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1494 return SDOperand(E, 0);
1495 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1496 N->setValueTypes(VTs, 2);
1497 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001498 AllNodes.push_back(N);
1499 return SDOperand(N, 0);
1500}
1501
1502SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1503 SDOperand Chain, SDOperand Ptr,
1504 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001505 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1506 getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001507 std::vector<MVT::ValueType> VTs;
1508 VTs.reserve(2);
1509 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001510 return getNode(ISD::VLOAD, VTs, Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001511}
1512
1513SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1514 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1515 MVT::ValueType EVT) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001516 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001517 std::vector<MVT::ValueType> VTs;
1518 VTs.reserve(2);
1519 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001520 return getNode(Opcode, VTs, Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001521}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001522
Nate Begemanacc398c2006-01-25 18:21:52 +00001523SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1524 SDOperand Chain, SDOperand Ptr,
1525 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001526 SDOperand Ops[] = { Chain, Ptr, SV };
Nate Begemanacc398c2006-01-25 18:21:52 +00001527 std::vector<MVT::ValueType> VTs;
1528 VTs.reserve(2);
1529 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001530 return getNode(ISD::VAARG, VTs, Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001531}
1532
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001533SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001534 const SDOperand *Ops, unsigned NumOps) {
1535 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001536 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001537 case 1: return getNode(Opcode, VT, Ops[0]);
1538 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1539 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001540 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001541 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001542
Chris Lattneref847df2005-04-09 03:27:28 +00001543 switch (Opcode) {
1544 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001545 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001546 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001547 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1548#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1549 // If this is a truncating store of a constant, convert to the desired type
1550 // and store it instead.
1551 if (isa<Constant>(Ops[0])) {
1552 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1553 if (isa<Constant>(Op))
1554 N1 = Op;
1555 }
1556 // Also for ConstantFP?
1557#endif
1558 if (Ops[0].getValueType() == EVT) // Normal store?
1559 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1560 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1561 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1562 "Can't do FP-INT conversion!");
1563 break;
1564 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001565 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001566 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001567 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1568 "LHS and RHS of condition must have same type!");
1569 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1570 "True and False arms of SelectCC must have same type!");
1571 assert(Ops[2].getValueType() == VT &&
1572 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001573 break;
1574 }
1575 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001576 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001577 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1578 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001579 break;
1580 }
Chris Lattneref847df2005-04-09 03:27:28 +00001581 }
1582
Chris Lattner385328c2005-05-14 07:42:29 +00001583 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001584 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001585 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001586 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001587 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001588 void *IP = 0;
1589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1590 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001591 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001592 N->setValueTypes(VTs, 1);
1593 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001594 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001595 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001596 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001597 }
Chris Lattneref847df2005-04-09 03:27:28 +00001598 AllNodes.push_back(N);
1599 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001600}
1601
Chris Lattner89c34632005-05-14 06:20:26 +00001602SDOperand SelectionDAG::getNode(unsigned Opcode,
1603 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001604 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner89c34632005-05-14 06:20:26 +00001605 if (ResultTys.size() == 1)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001606 return getNode(Opcode, ResultTys[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001607
Chris Lattner5f056bf2005-07-10 01:55:33 +00001608 switch (Opcode) {
1609 case ISD::EXTLOAD:
1610 case ISD::SEXTLOAD:
1611 case ISD::ZEXTLOAD: {
1612 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001613 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001614 // If they are asking for an extending load from/to the same thing, return a
1615 // normal load.
1616 if (ResultTys[0] == EVT)
1617 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001618 if (MVT::isVector(ResultTys[0])) {
1619 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1620 "Invalid vector extload!");
1621 } else {
1622 assert(EVT < ResultTys[0] &&
1623 "Should only be an extending load, not truncating!");
1624 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001625 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001626 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001627 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1628 "Cannot convert from FP to Int or Int -> FP!");
1629 break;
1630 }
1631
Chris Lattnere89083a2005-05-14 07:25:05 +00001632 // FIXME: figure out how to safely handle things like
1633 // int foo(int x) { return 1 << (x & 255); }
1634 // int bar() { return foo(256); }
1635#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001636 case ISD::SRA_PARTS:
1637 case ISD::SRL_PARTS:
1638 case ISD::SHL_PARTS:
1639 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001640 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001641 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1642 else if (N3.getOpcode() == ISD::AND)
1643 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1644 // If the and is only masking out bits that cannot effect the shift,
1645 // eliminate the and.
1646 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1647 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1648 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1649 }
1650 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001651#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001652 }
Chris Lattner89c34632005-05-14 06:20:26 +00001653
Chris Lattner43247a12005-08-25 19:12:10 +00001654 // Memoize the node unless it returns a flag.
1655 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001656 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
Chris Lattner43247a12005-08-25 19:12:10 +00001657 if (ResultTys.back() != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001658 SelectionDAGCSEMap::NodeID ID;
1659 ID.SetOpcode(Opcode);
1660 ID.SetValueTypes(VTs);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001661 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001662 void *IP = 0;
1663 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1664 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001665 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001666 N->setValueTypes(VTs, ResultTys.size());
1667 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001668 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001669 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001670 N->setValueTypes(VTs, ResultTys.size());
Chris Lattner43247a12005-08-25 19:12:10 +00001671 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001672 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001673 return SDOperand(N, 0);
1674}
1675
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001676
Chris Lattnera5682852006-08-07 23:03:03 +00001677MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1678 return SDNode::getValueTypeList(VT);
1679}
1680
1681MVT::ValueType *SelectionDAG::getNodeValueTypes(
1682 std::vector<MVT::ValueType> &RetVals) {
Chris Lattnera3255112005-11-08 23:30:28 +00001683 switch (RetVals.size()) {
Chris Lattnera5682852006-08-07 23:03:03 +00001684 case 0: assert(0 && "Cannot have nodes without results!");
1685 case 1: return SDNode::getValueTypeList(RetVals[0]);
1686 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
Chris Lattnera3255112005-11-08 23:30:28 +00001687 default: break;
1688 }
1689
1690 std::list<std::vector<MVT::ValueType> >::iterator I =
1691 std::find(VTList.begin(), VTList.end(), RetVals);
1692 if (I == VTList.end()) {
1693 VTList.push_front(RetVals);
1694 I = VTList.begin();
1695 }
1696
Chris Lattnera5682852006-08-07 23:03:03 +00001697 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001698}
1699
Chris Lattnera5682852006-08-07 23:03:03 +00001700MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1701 MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001702 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1703 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001704 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1705 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001706 }
1707 std::vector<MVT::ValueType> V;
1708 V.push_back(VT1);
1709 V.push_back(VT2);
1710 VTList.push_front(V);
Chris Lattnera5682852006-08-07 23:03:03 +00001711 return &(*VTList.begin())[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001712}
1713
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001714/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1715/// specified operands. If the resultant node already exists in the DAG,
1716/// this does not modify the specified node, instead it returns the node that
1717/// already exists. If the resultant node does not exist in the DAG, the
1718/// input node is returned. As a degenerate case, if you specify the same
1719/// input operands as the node already has, the input node is returned.
1720SDOperand SelectionDAG::
1721UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1722 SDNode *N = InN.Val;
1723 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1724
1725 // Check to see if there is no change.
1726 if (Op == N->getOperand(0)) return InN;
1727
1728 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001729 void *InsertPos = 0;
1730 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1731 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001732
1733 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001734 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001735 RemoveNodeFromCSEMaps(N);
1736
1737 // Now we update the operands.
1738 N->OperandList[0].Val->removeUser(N);
1739 Op.Val->addUser(N);
1740 N->OperandList[0] = Op;
1741
1742 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001743 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001744 return InN;
1745}
1746
1747SDOperand SelectionDAG::
1748UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1749 SDNode *N = InN.Val;
1750 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1751
1752 // Check to see if there is no change.
1753 bool AnyChange = false;
1754 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1755 return InN; // No operands changed, just return the input node.
1756
1757 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001758 void *InsertPos = 0;
1759 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1760 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001761
1762 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001763 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001764 RemoveNodeFromCSEMaps(N);
1765
1766 // Now we update the operands.
1767 if (N->OperandList[0] != Op1) {
1768 N->OperandList[0].Val->removeUser(N);
1769 Op1.Val->addUser(N);
1770 N->OperandList[0] = Op1;
1771 }
1772 if (N->OperandList[1] != Op2) {
1773 N->OperandList[1].Val->removeUser(N);
1774 Op2.Val->addUser(N);
1775 N->OperandList[1] = Op2;
1776 }
1777
1778 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001779 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001780 return InN;
1781}
1782
1783SDOperand SelectionDAG::
1784UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001785 SDOperand Ops[] = { Op1, Op2, Op3 };
1786 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001787}
1788
1789SDOperand SelectionDAG::
1790UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1791 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001792 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1793 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001794}
1795
1796SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001797UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1798 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001799 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1800 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001801}
1802
1803
1804SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001805UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001806 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001807 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001808 "Update with wrong number of operands");
1809
1810 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001811 bool AnyChange = false;
1812 for (unsigned i = 0; i != NumOps; ++i) {
1813 if (Ops[i] != N->getOperand(i)) {
1814 AnyChange = true;
1815 break;
1816 }
1817 }
1818
1819 // No operands changed, just return the input node.
1820 if (!AnyChange) return InN;
1821
1822 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001823 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001824 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001825 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001826
1827 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001828 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001829 RemoveNodeFromCSEMaps(N);
1830
1831 // Now we update the operands.
1832 for (unsigned i = 0; i != NumOps; ++i) {
1833 if (N->OperandList[i] != Ops[i]) {
1834 N->OperandList[i].Val->removeUser(N);
1835 Ops[i].Val->addUser(N);
1836 N->OperandList[i] = Ops[i];
1837 }
1838 }
1839
1840 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001841 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001842 return InN;
1843}
1844
1845
1846
Chris Lattner149c58c2005-08-16 18:17:10 +00001847
1848/// SelectNodeTo - These are used for target selectors to *mutate* the
1849/// specified node to have the specified return type, Target opcode, and
1850/// operands. Note that target opcodes are stored as
1851/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001852///
1853/// Note that SelectNodeTo returns the resultant node. If there is already a
1854/// node of the specified opcode and operands, it returns that node instead of
1855/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001856SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1857 MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +00001858 MVT::ValueType *VTs = getNodeValueTypes(VT);
1859 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1860 void *IP = 0;
1861 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1862 return SDOperand(ON, 0);
1863
Chris Lattner7651fa42005-08-24 23:00:29 +00001864 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001865
Chris Lattner7651fa42005-08-24 23:00:29 +00001866 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001867 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001868
Chris Lattner4a283e92006-08-11 18:38:11 +00001869 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001870 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001871}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001872
Chris Lattnereb19e402005-11-30 22:45:14 +00001873SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1874 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001875 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001876 MVT::ValueType *VTs = getNodeValueTypes(VT);
1877 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1878 void *IP = 0;
1879 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1880 return SDOperand(ON, 0);
1881
Chris Lattner149c58c2005-08-16 18:17:10 +00001882 RemoveNodeFromCSEMaps(N);
1883 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001884 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001885 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001886 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001887 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001888}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001889
Chris Lattnereb19e402005-11-30 22:45:14 +00001890SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1891 MVT::ValueType VT, SDOperand Op1,
1892 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001893 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001894 MVT::ValueType *VTs = getNodeValueTypes(VT);
1895 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1896 void *IP = 0;
1897 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1898 return SDOperand(ON, 0);
1899
Chris Lattner149c58c2005-08-16 18:17:10 +00001900 RemoveNodeFromCSEMaps(N);
1901 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001902 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001903 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001904
Chris Lattnera5682852006-08-07 23:03:03 +00001905 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001906 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001907}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001908
Chris Lattnereb19e402005-11-30 22:45:14 +00001909SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1910 MVT::ValueType VT, SDOperand Op1,
1911 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001912 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001913 MVT::ValueType *VTs = getNodeValueTypes(VT);
1914 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1915 void *IP = 0;
1916 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1917 return SDOperand(ON, 0);
1918
Chris Lattner149c58c2005-08-16 18:17:10 +00001919 RemoveNodeFromCSEMaps(N);
1920 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001921 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001922 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001923
Chris Lattnera5682852006-08-07 23:03:03 +00001924 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001925 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001926}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001927
Chris Lattnereb19e402005-11-30 22:45:14 +00001928SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1929 MVT::ValueType VT, SDOperand Op1,
1930 SDOperand Op2, SDOperand Op3,
1931 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001932 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001933 MVT::ValueType *VTs = getNodeValueTypes(VT);
1934 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1935 ID.AddOperand(Op1);
1936 ID.AddOperand(Op2);
1937 ID.AddOperand(Op3);
1938 ID.AddOperand(Op4);
1939 void *IP = 0;
1940 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1941 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001942
Nate Begeman294a0a12005-08-18 07:30:15 +00001943 RemoveNodeFromCSEMaps(N);
1944 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001945 N->setValueTypes(VTs, 1);
Nate Begeman294a0a12005-08-18 07:30:15 +00001946 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001947
Chris Lattnera5682852006-08-07 23:03:03 +00001948 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001949 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001950}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001951
Chris Lattnereb19e402005-11-30 22:45:14 +00001952SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1953 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00001954 SDOperand Op2, SDOperand Op3,
1955 SDOperand Op4, SDOperand Op5) {
1956 MVT::ValueType *VTs = getNodeValueTypes(VT);
1957 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1958 ID.AddOperand(Op1);
1959 ID.AddOperand(Op2);
1960 ID.AddOperand(Op3);
1961 ID.AddOperand(Op4);
1962 ID.AddOperand(Op5);
1963 void *IP = 0;
1964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1965 return SDOperand(ON, 0);
1966
Chris Lattner6b09a292005-08-21 18:49:33 +00001967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001969 N->setValueTypes(VTs, 1);
Chris Lattner6b09a292005-08-21 18:49:33 +00001970 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971
Chris Lattnera5682852006-08-07 23:03:03 +00001972 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001973 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001974}
Chris Lattner149c58c2005-08-16 18:17:10 +00001975
Chris Lattnereb19e402005-11-30 22:45:14 +00001976SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1977 MVT::ValueType VT, SDOperand Op1,
1978 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1979 SDOperand Op5, SDOperand Op6) {
Chris Lattnera5682852006-08-07 23:03:03 +00001980 MVT::ValueType *VTs = getNodeValueTypes(VT);
1981 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1982 ID.AddOperand(Op1);
1983 ID.AddOperand(Op2);
1984 ID.AddOperand(Op3);
1985 ID.AddOperand(Op4);
1986 ID.AddOperand(Op5);
1987 ID.AddOperand(Op6);
1988 void *IP = 0;
1989 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1990 return SDOperand(ON, 0);
1991
Evan Cheng61ca74b2005-11-30 02:04:11 +00001992 RemoveNodeFromCSEMaps(N);
1993 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001994 N->setValueTypes(VTs, 1);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001995 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001996
Chris Lattnera5682852006-08-07 23:03:03 +00001997 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001998 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001999}
2000
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002001SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2002 MVT::ValueType VT, SDOperand Op1,
2003 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2004 SDOperand Op5, SDOperand Op6,
2005 SDOperand Op7) {
Chris Lattnera5682852006-08-07 23:03:03 +00002006 MVT::ValueType *VTs = getNodeValueTypes(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002007 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002008 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2009 ID.AddOperand(Op1);
2010 ID.AddOperand(Op2);
2011 ID.AddOperand(Op3);
2012 ID.AddOperand(Op4);
2013 ID.AddOperand(Op5);
2014 ID.AddOperand(Op6);
2015 ID.AddOperand(Op7);
2016 void *IP = 0;
2017 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2018 return SDOperand(ON, 0);
2019
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002020 RemoveNodeFromCSEMaps(N);
2021 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002022 N->setValueTypes(VTs, 1);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002023 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2024
Chris Lattnera5682852006-08-07 23:03:03 +00002025 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002026 return SDOperand(N, 0);
2027}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002028SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2029 MVT::ValueType VT, SDOperand Op1,
2030 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2031 SDOperand Op5, SDOperand Op6,
2032 SDOperand Op7, SDOperand Op8) {
2033 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002034 MVT::ValueType *VTs = getNodeValueTypes(VT);
2035 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2036 ID.AddOperand(Op1);
2037 ID.AddOperand(Op2);
2038 ID.AddOperand(Op3);
2039 ID.AddOperand(Op4);
2040 ID.AddOperand(Op5);
2041 ID.AddOperand(Op6);
2042 ID.AddOperand(Op7);
2043 ID.AddOperand(Op8);
2044 void *IP = 0;
2045 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2046 return SDOperand(ON, 0);
2047
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002048 RemoveNodeFromCSEMaps(N);
2049 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002050 N->setValueTypes(VTs, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002051 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2052
Chris Lattnera5682852006-08-07 23:03:03 +00002053 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002054 return SDOperand(N, 0);
2055}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002056
Chris Lattnereb19e402005-11-30 22:45:14 +00002057SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2058 MVT::ValueType VT1, MVT::ValueType VT2,
2059 SDOperand Op1, SDOperand Op2) {
Chris Lattnera5682852006-08-07 23:03:03 +00002060 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2061 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2062 void *IP = 0;
2063 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2064 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002065
Chris Lattner0fb094f2005-11-19 01:44:53 +00002066 RemoveNodeFromCSEMaps(N);
2067 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002068 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002069 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002070
Chris Lattnera5682852006-08-07 23:03:03 +00002071 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002072 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002073}
2074
Chris Lattnereb19e402005-11-30 22:45:14 +00002075SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2076 MVT::ValueType VT1, MVT::ValueType VT2,
2077 SDOperand Op1, SDOperand Op2,
2078 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002079 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002080 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2081 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2082 Op1, Op2, Op3);
2083 void *IP = 0;
2084 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2085 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002086
Chris Lattner0fb094f2005-11-19 01:44:53 +00002087 RemoveNodeFromCSEMaps(N);
2088 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002089 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002090 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002091
Chris Lattnera5682852006-08-07 23:03:03 +00002092 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002093 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002094}
2095
Chris Lattnereb19e402005-11-30 22:45:14 +00002096SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2097 MVT::ValueType VT1, MVT::ValueType VT2,
2098 SDOperand Op1, SDOperand Op2,
2099 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002100 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002101 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2102 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2103 ID.AddOperand(Op1);
2104 ID.AddOperand(Op2);
2105 ID.AddOperand(Op3);
2106 ID.AddOperand(Op4);
2107 void *IP = 0;
2108 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2109 return SDOperand(ON, 0);
2110
Chris Lattner0fb094f2005-11-19 01:44:53 +00002111 RemoveNodeFromCSEMaps(N);
2112 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002113 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002114 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002115
Chris Lattnera5682852006-08-07 23:03:03 +00002116 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002117 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118}
2119
Chris Lattnereb19e402005-11-30 22:45:14 +00002120SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2121 MVT::ValueType VT1, MVT::ValueType VT2,
2122 SDOperand Op1, SDOperand Op2,
2123 SDOperand Op3, SDOperand Op4,
2124 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002125 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002126 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2127 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2128 ID.AddOperand(Op1);
2129 ID.AddOperand(Op2);
2130 ID.AddOperand(Op3);
2131 ID.AddOperand(Op4);
2132 ID.AddOperand(Op5);
2133 void *IP = 0;
2134 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2135 return SDOperand(ON, 0);
2136
Chris Lattner0fb094f2005-11-19 01:44:53 +00002137 RemoveNodeFromCSEMaps(N);
2138 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002139 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002140 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002141
Chris Lattnera5682852006-08-07 23:03:03 +00002142 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002143 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002144}
2145
Evan Cheng6ae46c42006-02-09 07:15:23 +00002146/// getTargetNode - These are used for target selectors to create a new node
2147/// with specified return type(s), target opcode, and operands.
2148///
2149/// Note that getTargetNode returns the resultant node. If there is already a
2150/// node of the specified opcode and operands, it returns that node instead of
2151/// the current one.
2152SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2153 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2154}
2155SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2156 SDOperand Op1) {
2157 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2158}
2159SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2160 SDOperand Op1, SDOperand Op2) {
2161 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2162}
2163SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2164 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2165 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2166}
2167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2168 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2169 SDOperand Op4) {
2170 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2171}
2172SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2173 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2174 SDOperand Op4, SDOperand Op5) {
2175 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2176}
2177SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2178 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002179 SDOperand Op4, SDOperand Op5,
2180 SDOperand Op6) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002181 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2182 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002183}
2184SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2185 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2186 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2187 SDOperand Op7) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002188 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2189 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002190}
2191SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2192 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2193 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2194 SDOperand Op7, SDOperand Op8) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002195 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2196 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002197}
2198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002199 const SDOperand *Ops, unsigned NumOps) {
2200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002201}
2202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2203 MVT::ValueType VT2, SDOperand Op1) {
2204 std::vector<MVT::ValueType> ResultTys;
2205 ResultTys.push_back(VT1);
2206 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002207 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002208}
2209SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002210 MVT::ValueType VT2, SDOperand Op1,
2211 SDOperand Op2) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002212 std::vector<MVT::ValueType> ResultTys;
2213 ResultTys.push_back(VT1);
2214 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002215 SDOperand Ops[] = { Op1, Op2 };
2216 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002217}
2218SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002219 MVT::ValueType VT2, SDOperand Op1,
2220 SDOperand Op2, SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002221 std::vector<MVT::ValueType> ResultTys;
2222 ResultTys.push_back(VT1);
2223 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002224 SDOperand Ops[] = { Op1, Op2, Op3 };
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002228 MVT::ValueType VT2, SDOperand Op1,
2229 SDOperand Op2, SDOperand Op3,
2230 SDOperand Op4) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002231 std::vector<MVT::ValueType> ResultTys;
2232 ResultTys.push_back(VT1);
2233 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002234 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2235 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002236}
2237SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002238 MVT::ValueType VT2, SDOperand Op1,
2239 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2240 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002241 std::vector<MVT::ValueType> ResultTys;
2242 ResultTys.push_back(VT1);
2243 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002244 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2245 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002246}
2247SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002248 MVT::ValueType VT2, SDOperand Op1,
2249 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2250 SDOperand Op5, SDOperand Op6) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002251 std::vector<MVT::ValueType> ResultTys;
2252 ResultTys.push_back(VT1);
2253 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002254 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2255 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002256}
2257SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002258 MVT::ValueType VT2, SDOperand Op1,
2259 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2260 SDOperand Op5, SDOperand Op6,
2261 SDOperand Op7) {
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, Op5, Op6, Op7 };
2266 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002267}
2268SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2269 MVT::ValueType VT2, MVT::ValueType VT3,
2270 SDOperand Op1, SDOperand Op2) {
2271 std::vector<MVT::ValueType> ResultTys;
2272 ResultTys.push_back(VT1);
2273 ResultTys.push_back(VT2);
2274 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002275 SDOperand Ops[] = { Op1, Op2 };
2276 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002277}
2278SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2279 MVT::ValueType VT2, MVT::ValueType VT3,
2280 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002281 SDOperand Op3, SDOperand Op4,
2282 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002283 std::vector<MVT::ValueType> ResultTys;
2284 ResultTys.push_back(VT1);
2285 ResultTys.push_back(VT2);
2286 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002287 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2288 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002289}
2290SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2291 MVT::ValueType VT2, MVT::ValueType VT3,
2292 SDOperand Op1, SDOperand Op2,
2293 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2294 SDOperand Op6) {
2295 std::vector<MVT::ValueType> ResultTys;
2296 ResultTys.push_back(VT1);
2297 ResultTys.push_back(VT2);
2298 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002299 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2300 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002301}
2302SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2303 MVT::ValueType VT2, MVT::ValueType VT3,
2304 SDOperand Op1, SDOperand Op2,
2305 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2306 SDOperand Op6, SDOperand Op7) {
2307 std::vector<MVT::ValueType> ResultTys;
2308 ResultTys.push_back(VT1);
2309 ResultTys.push_back(VT2);
2310 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002311 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2312 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002313}
2314SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002315 MVT::ValueType VT2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002316 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002317 std::vector<MVT::ValueType> ResultTys;
2318 ResultTys.push_back(VT1);
2319 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002320 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002321}
2322
Evan Cheng99157a02006-08-07 22:13:29 +00002323/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002324/// This can cause recursive merging of nodes in the DAG.
2325///
Chris Lattner8b52f212005-08-26 18:36:28 +00002326/// This version assumes From/To have a single result value.
2327///
Chris Lattner1e111c72005-09-07 05:37:01 +00002328void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2329 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002330 SDNode *From = FromN.Val, *To = ToN.Val;
2331 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2332 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002333 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002334
Chris Lattner8b8749f2005-08-17 19:00:20 +00002335 while (!From->use_empty()) {
2336 // Process users until they are all gone.
2337 SDNode *U = *From->use_begin();
2338
2339 // This node is about to morph, remove its old self from the CSE maps.
2340 RemoveNodeFromCSEMaps(U);
2341
Chris Lattner65113b22005-11-08 22:07:03 +00002342 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2343 I != E; ++I)
2344 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002345 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002346 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002347 To->addUser(U);
2348 }
2349
2350 // Now that we have modified U, add it back to the CSE maps. If it already
2351 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002352 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2353 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002354 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002355 if (Deleted) Deleted->push_back(U);
2356 DeleteNodeNotInCSEMaps(U);
2357 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002358 }
2359}
2360
2361/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2362/// This can cause recursive merging of nodes in the DAG.
2363///
2364/// This version assumes From/To have matching types and numbers of result
2365/// values.
2366///
Chris Lattner1e111c72005-09-07 05:37:01 +00002367void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2368 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002369 assert(From != To && "Cannot replace uses of with self");
2370 assert(From->getNumValues() == To->getNumValues() &&
2371 "Cannot use this version of ReplaceAllUsesWith!");
2372 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002373 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002374 return;
2375 }
2376
2377 while (!From->use_empty()) {
2378 // Process users until they are all gone.
2379 SDNode *U = *From->use_begin();
2380
2381 // This node is about to morph, remove its old self from the CSE maps.
2382 RemoveNodeFromCSEMaps(U);
2383
Chris Lattner65113b22005-11-08 22:07:03 +00002384 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2385 I != E; ++I)
2386 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002387 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002388 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002389 To->addUser(U);
2390 }
2391
2392 // Now that we have modified U, add it back to the CSE maps. If it already
2393 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002394 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2395 ReplaceAllUsesWith(U, Existing, Deleted);
2396 // U is now dead.
2397 if (Deleted) Deleted->push_back(U);
2398 DeleteNodeNotInCSEMaps(U);
2399 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002400 }
2401}
2402
Chris Lattner8b52f212005-08-26 18:36:28 +00002403/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2404/// This can cause recursive merging of nodes in the DAG.
2405///
2406/// This version can replace From with any result values. To must match the
2407/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002408void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002409 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002410 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002411 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002412 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002413 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002414 return;
2415 }
2416
2417 while (!From->use_empty()) {
2418 // Process users until they are all gone.
2419 SDNode *U = *From->use_begin();
2420
2421 // This node is about to morph, remove its old self from the CSE maps.
2422 RemoveNodeFromCSEMaps(U);
2423
Chris Lattner65113b22005-11-08 22:07:03 +00002424 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2425 I != E; ++I)
2426 if (I->Val == From) {
2427 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002428 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002429 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002430 ToOp.Val->addUser(U);
2431 }
2432
2433 // Now that we have modified U, add it back to the CSE maps. If it already
2434 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002435 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2436 ReplaceAllUsesWith(U, Existing, Deleted);
2437 // U is now dead.
2438 if (Deleted) Deleted->push_back(U);
2439 DeleteNodeNotInCSEMaps(U);
2440 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002441 }
2442}
2443
Chris Lattner012f2412006-02-17 21:58:01 +00002444/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2445/// uses of other values produced by From.Val alone. The Deleted vector is
2446/// handled the same was as for ReplaceAllUsesWith.
2447void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2448 std::vector<SDNode*> &Deleted) {
2449 assert(From != To && "Cannot replace a value with itself");
2450 // Handle the simple, trivial, case efficiently.
2451 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2452 ReplaceAllUsesWith(From, To, &Deleted);
2453 return;
2454 }
2455
2456 // Get all of the users in a nice, deterministically ordered, uniqued set.
2457 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2458
2459 while (!Users.empty()) {
2460 // We know that this user uses some value of From. If it is the right
2461 // value, update it.
2462 SDNode *User = Users.back();
2463 Users.pop_back();
2464
2465 for (SDOperand *Op = User->OperandList,
2466 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2467 if (*Op == From) {
2468 // Okay, we know this user needs to be updated. Remove its old self
2469 // from the CSE maps.
2470 RemoveNodeFromCSEMaps(User);
2471
2472 // Update all operands that match "From".
2473 for (; Op != E; ++Op) {
2474 if (*Op == From) {
2475 From.Val->removeUser(User);
2476 *Op = To;
2477 To.Val->addUser(User);
2478 }
2479 }
2480
2481 // Now that we have modified User, add it back to the CSE maps. If it
2482 // already exists there, recursively merge the results together.
2483 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2484 unsigned NumDeleted = Deleted.size();
2485 ReplaceAllUsesWith(User, Existing, &Deleted);
2486
2487 // User is now dead.
2488 Deleted.push_back(User);
2489 DeleteNodeNotInCSEMaps(User);
2490
2491 // We have to be careful here, because ReplaceAllUsesWith could have
2492 // deleted a user of From, which means there may be dangling pointers
2493 // in the "Users" setvector. Scan over the deleted node pointers and
2494 // remove them from the setvector.
2495 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2496 Users.remove(Deleted[i]);
2497 }
2498 break; // Exit the operand scanning loop.
2499 }
2500 }
2501 }
2502}
2503
Chris Lattner7b2880c2005-08-24 22:44:39 +00002504
Evan Chenge6f35d82006-08-01 08:20:41 +00002505/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2506/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002507unsigned SelectionDAG::AssignNodeIds() {
2508 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002509 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2510 SDNode *N = I;
2511 N->setNodeId(Id++);
2512 }
2513 return Id;
2514}
2515
Evan Chenge6f35d82006-08-01 08:20:41 +00002516/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002517/// based on their topological order. It returns the maximum id and a vector
2518/// of the SDNodes* in assigned order by reference.
2519unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002520 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002521 std::vector<unsigned> InDegree(DAGSize);
2522 std::vector<SDNode*> Sources;
2523
2524 // Use a two pass approach to avoid using a std::map which is slow.
2525 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002526 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2527 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002528 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002529 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002530 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002531 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002532 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002533 }
2534
Evan Cheng99157a02006-08-07 22:13:29 +00002535 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002536 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002537 SDNode *N = Sources.back();
2538 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002539 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002540 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2541 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002542 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002543 if (Degree == 0)
2544 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002545 }
2546 }
2547
Evan Chengc384d6c2006-08-02 22:00:34 +00002548 // Second pass, assign the actual topological order as node ids.
2549 Id = 0;
2550 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2551 TI != TE; ++TI)
2552 (*TI)->setNodeId(Id++);
2553
2554 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002555}
2556
2557
Evan Cheng091cba12006-07-27 06:39:06 +00002558
Jim Laskey58b968b2005-08-17 20:08:02 +00002559//===----------------------------------------------------------------------===//
2560// SDNode Class
2561//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002562
Chris Lattner917d2c92006-07-19 00:00:37 +00002563// Out-of-line virtual method to give class a home.
2564void SDNode::ANCHOR() {
2565}
2566
Chris Lattnera3255112005-11-08 23:30:28 +00002567/// getValueTypeList - Return a pointer to the specified value type.
2568///
2569MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2570 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2571 VTs[VT] = VT;
2572 return &VTs[VT];
2573}
Chris Lattnera5682852006-08-07 23:03:03 +00002574
Chris Lattner5c884562005-01-12 18:37:47 +00002575/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2576/// indicated value. This method ignores uses of other values defined by this
2577/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002578bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002579 assert(Value < getNumValues() && "Bad value!");
2580
2581 // If there is only one value, this is easy.
2582 if (getNumValues() == 1)
2583 return use_size() == NUses;
2584 if (Uses.size() < NUses) return false;
2585
Evan Cheng4ee62112006-02-05 06:29:23 +00002586 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002587
2588 std::set<SDNode*> UsersHandled;
2589
Evan Cheng4ee62112006-02-05 06:29:23 +00002590 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002591 UI != E; ++UI) {
2592 SDNode *User = *UI;
2593 if (User->getNumOperands() == 1 ||
2594 UsersHandled.insert(User).second) // First time we've seen this?
2595 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2596 if (User->getOperand(i) == TheValue) {
2597 if (NUses == 0)
2598 return false; // too many uses
2599 --NUses;
2600 }
2601 }
2602
2603 // Found exactly the right number of uses?
2604 return NUses == 0;
2605}
2606
2607
Evan Cheng4ee62112006-02-05 06:29:23 +00002608// isOnlyUse - Return true if this node is the only use of N.
2609bool SDNode::isOnlyUse(SDNode *N) const {
2610 bool Seen = false;
2611 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2612 SDNode *User = *I;
2613 if (User == this)
2614 Seen = true;
2615 else
2616 return false;
2617 }
2618
2619 return Seen;
2620}
2621
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002622// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002623bool SDOperand::isOperand(SDNode *N) const {
2624 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2625 if (*this == N->getOperand(i))
2626 return true;
2627 return false;
2628}
2629
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002630bool SDNode::isOperand(SDNode *N) const {
2631 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2632 if (this == N->OperandList[i].Val)
2633 return true;
2634 return false;
2635}
Evan Cheng4ee62112006-02-05 06:29:23 +00002636
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002637const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002638 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002639 default:
2640 if (getOpcode() < ISD::BUILTIN_OP_END)
2641 return "<<Unknown DAG Node>>";
2642 else {
Evan Cheng72261582005-12-20 06:22:03 +00002643 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002644 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002645 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2646 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002647
Evan Cheng72261582005-12-20 06:22:03 +00002648 TargetLowering &TLI = G->getTargetLoweringInfo();
2649 const char *Name =
2650 TLI.getTargetNodeName(getOpcode());
2651 if (Name) return Name;
2652 }
2653
2654 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002655 }
2656
Andrew Lenharth95762122005-03-31 21:24:06 +00002657 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002658 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002659 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002660 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002661 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002662 case ISD::AssertSext: return "AssertSext";
2663 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002664
2665 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002666 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002667 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002668 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002669
2670 case ISD::Constant: return "Constant";
2671 case ISD::ConstantFP: return "ConstantFP";
2672 case ISD::GlobalAddress: return "GlobalAddress";
2673 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002674 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002675 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002676 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002677 case ISD::INTRINSIC_WO_CHAIN: {
2678 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2679 return Intrinsic::getName((Intrinsic::ID)IID);
2680 }
2681 case ISD::INTRINSIC_VOID:
2682 case ISD::INTRINSIC_W_CHAIN: {
2683 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002684 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002685 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002686
Chris Lattnerb2827b02006-03-19 00:52:58 +00002687 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002688 case ISD::TargetConstant: return "TargetConstant";
2689 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002690 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2691 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002692 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002693 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002694 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002695
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002696 case ISD::CopyToReg: return "CopyToReg";
2697 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002698 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002699 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002700 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002701 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002702 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002703 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002704
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002705 // Unary operators
2706 case ISD::FABS: return "fabs";
2707 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002708 case ISD::FSQRT: return "fsqrt";
2709 case ISD::FSIN: return "fsin";
2710 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002711
2712 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002713 case ISD::ADD: return "add";
2714 case ISD::SUB: return "sub";
2715 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002716 case ISD::MULHU: return "mulhu";
2717 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002718 case ISD::SDIV: return "sdiv";
2719 case ISD::UDIV: return "udiv";
2720 case ISD::SREM: return "srem";
2721 case ISD::UREM: return "urem";
2722 case ISD::AND: return "and";
2723 case ISD::OR: return "or";
2724 case ISD::XOR: return "xor";
2725 case ISD::SHL: return "shl";
2726 case ISD::SRA: return "sra";
2727 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002728 case ISD::ROTL: return "rotl";
2729 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002730 case ISD::FADD: return "fadd";
2731 case ISD::FSUB: return "fsub";
2732 case ISD::FMUL: return "fmul";
2733 case ISD::FDIV: return "fdiv";
2734 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002735 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002736 case ISD::VADD: return "vadd";
2737 case ISD::VSUB: return "vsub";
2738 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002739 case ISD::VSDIV: return "vsdiv";
2740 case ISD::VUDIV: return "vudiv";
2741 case ISD::VAND: return "vand";
2742 case ISD::VOR: return "vor";
2743 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002744
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002745 case ISD::SETCC: return "setcc";
2746 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002747 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002748 case ISD::VSELECT: return "vselect";
2749 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2750 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2751 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002752 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002753 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2754 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2755 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2756 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2757 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002758 case ISD::ADDC: return "addc";
2759 case ISD::ADDE: return "adde";
2760 case ISD::SUBC: return "subc";
2761 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002762 case ISD::SHL_PARTS: return "shl_parts";
2763 case ISD::SRA_PARTS: return "sra_parts";
2764 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002765
Chris Lattner7f644642005-04-28 21:44:03 +00002766 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002767 case ISD::SIGN_EXTEND: return "sign_extend";
2768 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002769 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002770 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002771 case ISD::TRUNCATE: return "truncate";
2772 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002773 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002774 case ISD::FP_EXTEND: return "fp_extend";
2775
2776 case ISD::SINT_TO_FP: return "sint_to_fp";
2777 case ISD::UINT_TO_FP: return "uint_to_fp";
2778 case ISD::FP_TO_SINT: return "fp_to_sint";
2779 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002780 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002781
2782 // Control flow instructions
2783 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002784 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002785 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002786 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002787 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002788 case ISD::CALLSEQ_START: return "callseq_start";
2789 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002790
2791 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002792 case ISD::LOAD: return "load";
2793 case ISD::STORE: return "store";
2794 case ISD::VLOAD: return "vload";
2795 case ISD::EXTLOAD: return "extload";
2796 case ISD::SEXTLOAD: return "sextload";
2797 case ISD::ZEXTLOAD: return "zextload";
2798 case ISD::TRUNCSTORE: return "truncstore";
2799 case ISD::VAARG: return "vaarg";
2800 case ISD::VACOPY: return "vacopy";
2801 case ISD::VAEND: return "vaend";
2802 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002803 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002804 case ISD::EXTRACT_ELEMENT: return "extract_element";
2805 case ISD::BUILD_PAIR: return "build_pair";
2806 case ISD::STACKSAVE: return "stacksave";
2807 case ISD::STACKRESTORE: return "stackrestore";
2808
2809 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002810 case ISD::MEMSET: return "memset";
2811 case ISD::MEMCPY: return "memcpy";
2812 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002813
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002814 // Bit manipulation
2815 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002816 case ISD::CTPOP: return "ctpop";
2817 case ISD::CTTZ: return "cttz";
2818 case ISD::CTLZ: return "ctlz";
2819
Chris Lattner36ce6912005-11-29 06:21:05 +00002820 // Debug info
2821 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002822 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002823 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002824
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002825 case ISD::CONDCODE:
2826 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002827 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002828 case ISD::SETOEQ: return "setoeq";
2829 case ISD::SETOGT: return "setogt";
2830 case ISD::SETOGE: return "setoge";
2831 case ISD::SETOLT: return "setolt";
2832 case ISD::SETOLE: return "setole";
2833 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002834
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002835 case ISD::SETO: return "seto";
2836 case ISD::SETUO: return "setuo";
2837 case ISD::SETUEQ: return "setue";
2838 case ISD::SETUGT: return "setugt";
2839 case ISD::SETUGE: return "setuge";
2840 case ISD::SETULT: return "setult";
2841 case ISD::SETULE: return "setule";
2842 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002843
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002844 case ISD::SETEQ: return "seteq";
2845 case ISD::SETGT: return "setgt";
2846 case ISD::SETGE: return "setge";
2847 case ISD::SETLT: return "setlt";
2848 case ISD::SETLE: return "setle";
2849 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002850 }
2851 }
2852}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002853
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002854void SDNode::dump() const { dump(0); }
2855void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002856 std::cerr << (void*)this << ": ";
2857
2858 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2859 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002860 if (getValueType(i) == MVT::Other)
2861 std::cerr << "ch";
2862 else
2863 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002864 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002865 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002866
2867 std::cerr << " ";
2868 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2869 if (i) std::cerr << ", ";
2870 std::cerr << (void*)getOperand(i).Val;
2871 if (unsigned RN = getOperand(i).ResNo)
2872 std::cerr << ":" << RN;
2873 }
2874
2875 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2876 std::cerr << "<" << CSDN->getValue() << ">";
2877 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2878 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002879 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002880 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002881 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002882 std::cerr << "<";
2883 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002884 if (offset > 0)
2885 std::cerr << " + " << offset;
2886 else
2887 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002888 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002889 std::cerr << "<" << FIDN->getIndex() << ">";
2890 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002891 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002892 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002893 if (offset > 0)
2894 std::cerr << " + " << offset;
2895 else
2896 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002897 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002898 std::cerr << "<";
2899 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2900 if (LBB)
2901 std::cerr << LBB->getName() << " ";
2902 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002903 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002904 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002905 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2906 } else {
2907 std::cerr << " #" << R->getReg();
2908 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002909 } else if (const ExternalSymbolSDNode *ES =
2910 dyn_cast<ExternalSymbolSDNode>(this)) {
2911 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002912 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2913 if (M->getValue())
2914 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2915 else
2916 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002917 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2918 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002919 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002920}
2921
Chris Lattnerde202b32005-11-09 23:47:37 +00002922static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002923 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2924 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002925 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002926 else
2927 std::cerr << "\n" << std::string(indent+2, ' ')
2928 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002929
Chris Lattnerea946cd2005-01-09 20:38:33 +00002930
2931 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002932 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002933}
2934
Chris Lattnerc3aae252005-01-07 07:46:32 +00002935void SelectionDAG::dump() const {
2936 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002937 std::vector<const SDNode*> Nodes;
2938 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2939 I != E; ++I)
2940 Nodes.push_back(I);
2941
Chris Lattner49d24712005-01-09 20:26:36 +00002942 std::sort(Nodes.begin(), Nodes.end());
2943
2944 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002945 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002946 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002947 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002948
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002949 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002950
Chris Lattnerc3aae252005-01-07 07:46:32 +00002951 std::cerr << "\n\n";
2952}
2953
Evan Chengfae9f1c2006-02-09 22:11:03 +00002954/// InsertISelMapEntry - A helper function to insert a key / element pair
2955/// into a SDOperand to SDOperand map. This is added to avoid the map
2956/// insertion operator from being inlined.
2957void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2958 SDNode *Key, unsigned KeyResNo,
2959 SDNode *Element, unsigned ElementResNo) {
2960 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2961 SDOperand(Element, ElementResNo)));
2962}