blob: 45fd350496c4bbf2f3f340755b3b558c1a8b9026 [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"
Evan Chengd6594ae2006-09-12 21:00:35 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000021#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000022#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000024#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000026#include "llvm/ADT/SetVector.h"
Chris Lattner190a4182006-08-04 17:45:20 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000028#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000029#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000030#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000031#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000032#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner0b3e5252006-08-15 19:11:05 +000035/// makeVTList - Return an instance of the SDVTList struct initialized with the
36/// specified members.
37static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
38 SDVTList Res = {VTs, NumVTs};
39 return Res;
40}
41
Chris Lattner5cdcc582005-01-09 20:52:51 +000042// isInvertibleForFree - Return true if there is no cost to emitting the logical
43// inverse of this node.
44static bool isInvertibleForFree(SDOperand N) {
45 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000046 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000047 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000048 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000049}
50
Jim Laskey58b968b2005-08-17 20:08:02 +000051//===----------------------------------------------------------------------===//
52// ConstantFPSDNode Class
53//===----------------------------------------------------------------------===//
54
55/// isExactlyValue - We don't rely on operator== working on double values, as
56/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
57/// As such, this method can be used to do an exact bit-for-bit comparison of
58/// two floating point values.
59bool ConstantFPSDNode::isExactlyValue(double V) const {
60 return DoubleToBits(V) == DoubleToBits(Value);
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000064// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000066
Evan Chenga8df1662006-03-27 06:58:47 +000067/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000068/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000069bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000070 // Look through a bit convert.
71 if (N->getOpcode() == ISD::BIT_CONVERT)
72 N = N->getOperand(0).Val;
73
Evan Chenga8df1662006-03-27 06:58:47 +000074 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000075
76 unsigned i = 0, e = N->getNumOperands();
77
78 // Skip over all of the undef values.
79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
80 ++i;
81
82 // Do not accept an all-undef vector.
83 if (i == e) return false;
84
85 // Do not accept build_vectors that aren't all constants or which have non-~0
86 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000087 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000088 if (isa<ConstantSDNode>(NotZero)) {
89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
90 return false;
91 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000092 MVT::ValueType VT = NotZero.getValueType();
93 if (VT== MVT::f64) {
94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
95 (uint64_t)-1)
96 return false;
97 } else {
98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
99 (uint32_t)-1)
100 return false;
101 }
Evan Chenga8df1662006-03-27 06:58:47 +0000102 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000103 return false;
104
105 // Okay, we have at least one ~0 value, check to see if the rest match or are
106 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000107 for (++i; i != e; ++i)
108 if (N->getOperand(i) != NotZero &&
109 N->getOperand(i).getOpcode() != ISD::UNDEF)
110 return false;
111 return true;
112}
113
114
Evan Cheng4a147842006-03-26 09:50:58 +0000115/// isBuildVectorAllZeros - Return true if the specified node is a
116/// BUILD_VECTOR where all of the elements are 0 or undef.
117bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000118 // Look through a bit convert.
119 if (N->getOpcode() == ISD::BIT_CONVERT)
120 N = N->getOperand(0).Val;
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123
124 unsigned i = 0, e = N->getNumOperands();
125
126 // Skip over all of the undef values.
127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
128 ++i;
129
130 // Do not accept an all-undef vector.
131 if (i == e) return false;
132
133 // Do not accept build_vectors that aren't all constants or which have non-~0
134 // elements.
135 SDOperand Zero = N->getOperand(i);
136 if (isa<ConstantSDNode>(Zero)) {
137 if (!cast<ConstantSDNode>(Zero)->isNullValue())
138 return false;
139 } else if (isa<ConstantFPSDNode>(Zero)) {
140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
141 return false;
142 } else
143 return false;
144
145 // Okay, we have at least one ~0 value, check to see if the rest match or are
146 // undefs.
147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != Zero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
150 return false;
151 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000152}
153
Chris Lattnerc3aae252005-01-07 07:46:32 +0000154/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
155/// when given the operation for (X op Y).
156ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
157 // To perform this operation, we just need to swap the L and G bits of the
158 // operation.
159 unsigned OldL = (Operation >> 2) & 1;
160 unsigned OldG = (Operation >> 1) & 1;
161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
162 (OldL << 1) | // New G bit
163 (OldG << 2)); // New L bit.
164}
165
166/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
167/// 'op' is a valid SetCC operation.
168ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
169 unsigned Operation = Op;
170 if (isInteger)
171 Operation ^= 7; // Flip L, G, E bits, but not U.
172 else
173 Operation ^= 15; // Flip all of the condition bits.
174 if (Operation > ISD::SETTRUE2)
175 Operation &= ~8; // Don't let N and U bits get set.
176 return ISD::CondCode(Operation);
177}
178
179
180/// isSignedOp - For an integer comparison, return 1 if the comparison is a
181/// signed operation and 2 if the result is an unsigned comparison. Return zero
182/// if the operation does not depend on the sign of the input (setne and seteq).
183static int isSignedOp(ISD::CondCode Opcode) {
184 switch (Opcode) {
185 default: assert(0 && "Illegal integer setcc operation!");
186 case ISD::SETEQ:
187 case ISD::SETNE: return 0;
188 case ISD::SETLT:
189 case ISD::SETLE:
190 case ISD::SETGT:
191 case ISD::SETGE: return 1;
192 case ISD::SETULT:
193 case ISD::SETULE:
194 case ISD::SETUGT:
195 case ISD::SETUGE: return 2;
196 }
197}
198
199/// getSetCCOrOperation - Return the result of a logical OR between different
200/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
201/// returns SETCC_INVALID if it is not possible to represent the resultant
202/// comparison.
203ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
204 bool isInteger) {
205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
206 // Cannot fold a signed integer setcc with an unsigned integer setcc.
207 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211 // If the N and U bits get set then the resultant comparison DOES suddenly
212 // care about orderedness, and is true when ordered.
213 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000214 Op &= ~16; // Clear the U bit if the N bit is set.
215
216 // Canonicalize illegal integer setcc's.
217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
218 Op = ISD::SETNE;
219
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220 return ISD::CondCode(Op);
221}
222
223/// getSetCCAndOperation - Return the result of a logical AND between different
224/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
225/// function returns zero if it is not possible to represent the resultant
226/// comparison.
227ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
228 bool isInteger) {
229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
230 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232
233 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
235
236 // Canonicalize illegal integer setcc's.
237 if (isInteger) {
238 switch (Result) {
239 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000244 }
245 }
246
247 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248}
249
Chris Lattnerb48da392005-01-23 04:39:44 +0000250const TargetMachine &SelectionDAG::getTarget() const {
251 return TLI.getTargetMachine();
252}
253
Jim Laskey58b968b2005-08-17 20:08:02 +0000254//===----------------------------------------------------------------------===//
255// SelectionDAG Class
256//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000257
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000258/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000259/// SelectionDAG.
260void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 // Create a dummy node (which is not added to allnodes), that adds a reference
262 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000263 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264
Chris Lattner190a4182006-08-04 17:45:20 +0000265 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000266
Chris Lattner190a4182006-08-04 17:45:20 +0000267 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000268 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000269 if (I->use_empty())
270 DeadNodes.push_back(I);
271
272 // Process the worklist, deleting the nodes and adding their uses to the
273 // worklist.
274 while (!DeadNodes.empty()) {
275 SDNode *N = DeadNodes.back();
276 DeadNodes.pop_back();
277
278 // Take the node out of the appropriate CSE map.
279 RemoveNodeFromCSEMaps(N);
280
281 // Next, brutally remove the operand list. This is safe to do, as there are
282 // no cycles in the graph.
283 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
284 SDNode *Operand = I->Val;
285 Operand->removeUser(N);
286
287 // Now that we removed this operand, see if there are no uses of it left.
288 if (Operand->use_empty())
289 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000290 }
Chris Lattner190a4182006-08-04 17:45:20 +0000291 delete[] N->OperandList;
292 N->OperandList = 0;
293 N->NumOperands = 0;
294
295 // Finally, remove N itself.
296 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000297 }
298
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000300 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301}
302
Evan Cheng130a6472006-10-12 20:34:05 +0000303void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
304 SmallVector<SDNode*, 16> DeadNodes;
305 DeadNodes.push_back(N);
306
307 // Process the worklist, deleting the nodes and adding their uses to the
308 // worklist.
309 while (!DeadNodes.empty()) {
310 SDNode *N = DeadNodes.back();
311 DeadNodes.pop_back();
312
313 // Take the node out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
315
316 // Next, brutally remove the operand list. This is safe to do, as there are
317 // no cycles in the graph.
318 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
319 SDNode *Operand = I->Val;
320 Operand->removeUser(N);
321
322 // Now that we removed this operand, see if there are no uses of it left.
323 if (Operand->use_empty())
324 DeadNodes.push_back(Operand);
325 }
326 delete[] N->OperandList;
327 N->OperandList = 0;
328 N->NumOperands = 0;
329
330 // Finally, remove N itself.
331 Deleted.push_back(N);
332 AllNodes.erase(N);
333 }
334}
335
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000336void SelectionDAG::DeleteNode(SDNode *N) {
337 assert(N->use_empty() && "Cannot delete a node that is not dead!");
338
339 // First take this out of the appropriate CSE map.
340 RemoveNodeFromCSEMaps(N);
341
Chris Lattner1e111c72005-09-07 05:37:01 +0000342 // Finally, remove uses due to operands of this node, remove from the
343 // AllNodes list, and delete the node.
344 DeleteNodeNotInCSEMaps(N);
345}
346
347void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
348
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000349 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000350 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000351
352 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000353 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
354 I->Val->removeUser(N);
355 delete[] N->OperandList;
356 N->OperandList = 0;
357 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000358
359 delete N;
360}
361
Chris Lattner149c58c2005-08-16 18:17:10 +0000362/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
363/// correspond to it. This is useful when we're about to delete or repurpose
364/// the node. We don't want future request for structurally identical nodes
365/// to return N anymore.
366void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000367 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000369 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000370 case ISD::STRING:
371 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
372 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000373 case ISD::CONDCODE:
374 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
375 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000376 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000377 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
378 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000379 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000380 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000381 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000382 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000383 Erased =
384 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000385 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000386 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000387 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000388 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
389 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000390 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000391 // Remove it from the CSE Map.
392 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000393 break;
394 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000395#ifndef NDEBUG
396 // Verify that the node was actually in one of the CSE maps, unless it has a
397 // flag result (which cannot be CSE'd) or is one of the special cases that are
398 // not subject to CSE.
399 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000400 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000401 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000402 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000403 assert(0 && "Node is not in map!");
404 }
405#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000406}
407
Chris Lattner8b8749f2005-08-17 19:00:20 +0000408/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
409/// has been taken out and modified in some way. If the specified node already
410/// exists in the CSE maps, do not modify the maps, but return the existing node
411/// instead. If it doesn't exist, add it and return null.
412///
413SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
414 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000415 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000416 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000417
Chris Lattner9f8cc692005-12-19 22:21:21 +0000418 // Check that remaining values produced are not flags.
419 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
420 if (N->getValueType(i) == MVT::Flag)
421 return 0; // Never CSE anything that produces a flag.
422
Chris Lattnera5682852006-08-07 23:03:03 +0000423 SDNode *New = CSEMap.GetOrInsertNode(N);
424 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000425 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000426}
427
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000428/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
429/// were replaced with those specified. If this node is never memoized,
430/// return null, otherwise return a pointer to the slot it would take. If a
431/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000432SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
433 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000434 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000435 return 0; // Never add these nodes.
436
437 // Check that remaining values produced are not flags.
438 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
439 if (N->getValueType(i) == MVT::Flag)
440 return 0; // Never CSE anything that produces a flag.
441
Chris Lattnera5682852006-08-07 23:03:03 +0000442 SelectionDAGCSEMap::NodeID ID;
443 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000444 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000445 ID.SetOperands(Op);
446 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000447}
448
449/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
450/// were replaced with those specified. If this node is never memoized,
451/// return null, otherwise return a pointer to the slot it would take. If a
452/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000453SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
454 SDOperand Op1, SDOperand Op2,
455 void *&InsertPos) {
456 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
457 return 0; // Never add these nodes.
458
459 // Check that remaining values produced are not flags.
460 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
461 if (N->getValueType(i) == MVT::Flag)
462 return 0; // Never CSE anything that produces a flag.
463
464 SelectionDAGCSEMap::NodeID ID;
465 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000466 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000467 ID.SetOperands(Op1, Op2);
468 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
469}
470
471
472/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
473/// were replaced with those specified. If this node is never memoized,
474/// return null, otherwise return a pointer to the slot it would take. If a
475/// node already exists with these operands, the slot will be non-null.
476SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000477 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000478 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000479 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000480 return 0; // Never add these nodes.
481
482 // Check that remaining values produced are not flags.
483 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
484 if (N->getValueType(i) == MVT::Flag)
485 return 0; // Never CSE anything that produces a flag.
486
Chris Lattnera5682852006-08-07 23:03:03 +0000487 SelectionDAGCSEMap::NodeID ID;
488 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000489 ID.SetValueTypes(N->getVTList());
Evan Cheng9629aba2006-10-11 01:47:58 +0000490 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
491 ID.AddInteger(LD->getAddressingMode());
492 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000493 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000494 ID.AddPointer(LD->getSrcValue());
495 ID.AddInteger(LD->getSrcValueOffset());
496 ID.AddInteger(LD->getAlignment());
497 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000498 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
499 ID.AddInteger(ST->getAddressingMode());
500 ID.AddInteger(ST->isTruncatingStore());
501 ID.AddInteger(ST->getStoredVT());
502 ID.AddPointer(ST->getSrcValue());
503 ID.AddInteger(ST->getSrcValueOffset());
504 ID.AddInteger(ST->getAlignment());
505 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000506 }
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000507 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000508 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000509}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000510
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000511
Chris Lattner78ec3112003-08-11 14:57:33 +0000512SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000513 while (!AllNodes.empty()) {
514 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000515 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000516 delete [] N->OperandList;
517 N->OperandList = 0;
518 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000519 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000520 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000521}
522
Chris Lattner0f2287b2005-04-13 02:38:18 +0000523SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000524 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000525 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000526 return getNode(ISD::AND, Op.getValueType(), Op,
527 getConstant(Imm, Op.getValueType()));
528}
529
Chris Lattner36ce6912005-11-29 06:21:05 +0000530SDOperand SelectionDAG::getString(const std::string &Val) {
531 StringSDNode *&N = StringNodes[Val];
532 if (!N) {
533 N = new StringSDNode(Val);
534 AllNodes.push_back(N);
535 }
536 return SDOperand(N, 0);
537}
538
Chris Lattner61b09412006-08-11 21:01:22 +0000539SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000540 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000541 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000542
Chris Lattner61b09412006-08-11 21:01:22 +0000543 // Mask out any bits that are not valid for this constant.
544 Val &= MVT::getIntVTBitMask(VT);
545
546 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000547 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000548 ID.AddInteger(Val);
549 void *IP = 0;
550 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
551 return SDOperand(E, 0);
552 SDNode *N = new ConstantSDNode(isT, Val, VT);
553 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000554 AllNodes.push_back(N);
555 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000556}
557
Chris Lattner61b09412006-08-11 21:01:22 +0000558
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000559SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
560 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000561 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
562 if (VT == MVT::f32)
563 Val = (float)Val; // Mask out extra precision.
564
Chris Lattnerd8658612005-02-17 20:17:32 +0000565 // Do the map lookup using the actual bit pattern for the floating point
566 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
567 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000568 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000569 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000570 ID.AddInteger(DoubleToBits(Val));
571 void *IP = 0;
572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
573 return SDOperand(E, 0);
574 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
575 CSEMap.InsertNode(N, IP);
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;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000584 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000585 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
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000596SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
597 bool isTarget) {
598 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000599 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000600 ID.AddInteger(FI);
601 void *IP = 0;
602 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
603 return SDOperand(E, 0);
604 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
605 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000606 AllNodes.push_back(N);
607 return SDOperand(N, 0);
608}
609
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000610SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
611 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000612 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000613 ID.AddInteger(JTI);
614 void *IP = 0;
615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
616 return SDOperand(E, 0);
617 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
618 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000619 AllNodes.push_back(N);
620 return SDOperand(N, 0);
621}
622
Evan Chengb8973bd2006-01-31 22:23:14 +0000623SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000624 unsigned Alignment, int Offset,
625 bool isTarget) {
626 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000627 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000628 ID.AddInteger(Alignment);
629 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000630 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000631 void *IP = 0;
632 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
633 return SDOperand(E, 0);
634 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
635 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000636 AllNodes.push_back(N);
637 return SDOperand(N, 0);
638}
639
Chris Lattnerc3aae252005-01-07 07:46:32 +0000640
Evan Chengd6594ae2006-09-12 21:00:35 +0000641SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
642 MVT::ValueType VT,
643 unsigned Alignment, int Offset,
644 bool isTarget) {
645 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
646 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
647 ID.AddInteger(Alignment);
648 ID.AddInteger(Offset);
649 C->AddSelectionDAGCSEId(&ID);
650 void *IP = 0;
651 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
652 return SDOperand(E, 0);
653 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
654 CSEMap.InsertNode(N, IP);
655 AllNodes.push_back(N);
656 return SDOperand(N, 0);
657}
658
659
Chris Lattnerc3aae252005-01-07 07:46:32 +0000660SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000661 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000662 ID.AddPointer(MBB);
663 void *IP = 0;
664 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
665 return SDOperand(E, 0);
666 SDNode *N = new BasicBlockSDNode(MBB);
667 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000668 AllNodes.push_back(N);
669 return SDOperand(N, 0);
670}
671
Chris Lattner15e4b012005-07-10 00:07:11 +0000672SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
673 if ((unsigned)VT >= ValueTypeNodes.size())
674 ValueTypeNodes.resize(VT+1);
675 if (ValueTypeNodes[VT] == 0) {
676 ValueTypeNodes[VT] = new VTSDNode(VT);
677 AllNodes.push_back(ValueTypeNodes[VT]);
678 }
679
680 return SDOperand(ValueTypeNodes[VT], 0);
681}
682
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
684 SDNode *&N = ExternalSymbols[Sym];
685 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000686 N = new ExternalSymbolSDNode(false, Sym, VT);
687 AllNodes.push_back(N);
688 return SDOperand(N, 0);
689}
690
Chris Lattner809ec112006-01-28 10:09:25 +0000691SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
692 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000693 SDNode *&N = TargetExternalSymbols[Sym];
694 if (N) return SDOperand(N, 0);
695 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 AllNodes.push_back(N);
697 return SDOperand(N, 0);
698}
699
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000700SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
701 if ((unsigned)Cond >= CondCodeNodes.size())
702 CondCodeNodes.resize(Cond+1);
703
Chris Lattner079a27a2005-08-09 20:40:02 +0000704 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000705 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000706 AllNodes.push_back(CondCodeNodes[Cond]);
707 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000708 return SDOperand(CondCodeNodes[Cond], 0);
709}
710
Chris Lattner0fdd7682005-08-30 22:38:38 +0000711SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000712 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000713 ID.AddInteger(RegNo);
714 void *IP = 0;
715 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
716 return SDOperand(E, 0);
717 SDNode *N = new RegisterSDNode(RegNo, VT);
718 CSEMap.InsertNode(N, IP);
719 AllNodes.push_back(N);
720 return SDOperand(N, 0);
721}
722
723SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
724 assert((!V || isa<PointerType>(V->getType())) &&
725 "SrcValue is not a pointer?");
726
Chris Lattner0b3e5252006-08-15 19:11:05 +0000727 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000728 ID.AddPointer(V);
729 ID.AddInteger(Offset);
730 void *IP = 0;
731 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
732 return SDOperand(E, 0);
733 SDNode *N = new SrcValueSDNode(V, Offset);
734 CSEMap.InsertNode(N, IP);
735 AllNodes.push_back(N);
736 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000737}
738
Chris Lattner51dabfb2006-10-14 00:41:01 +0000739SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
740 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741 // These setcc operations always fold.
742 switch (Cond) {
743 default: break;
744 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000745 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000746 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000747 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000748
749 case ISD::SETOEQ:
750 case ISD::SETOGT:
751 case ISD::SETOGE:
752 case ISD::SETOLT:
753 case ISD::SETOLE:
754 case ISD::SETONE:
755 case ISD::SETO:
756 case ISD::SETUO:
757 case ISD::SETUEQ:
758 case ISD::SETUNE:
759 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
760 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000761 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000762
Chris Lattner67255a12005-04-07 18:14:58 +0000763 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
764 uint64_t C2 = N2C->getValue();
765 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
766 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000767
Chris Lattnerc3aae252005-01-07 07:46:32 +0000768 // Sign extend the operands if required
769 if (ISD::isSignedIntSetCC(Cond)) {
770 C1 = N1C->getSignExtended();
771 C2 = N2C->getSignExtended();
772 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000773
Chris Lattnerc3aae252005-01-07 07:46:32 +0000774 switch (Cond) {
775 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000776 case ISD::SETEQ: return getConstant(C1 == C2, VT);
777 case ISD::SETNE: return getConstant(C1 != C2, VT);
778 case ISD::SETULT: return getConstant(C1 < C2, VT);
779 case ISD::SETUGT: return getConstant(C1 > C2, VT);
780 case ISD::SETULE: return getConstant(C1 <= C2, VT);
781 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
782 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
783 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
784 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
785 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000786 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787 }
Chris Lattner67255a12005-04-07 18:14:58 +0000788 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000789 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
790 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
791 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000792
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793 switch (Cond) {
794 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000795 case ISD::SETEQ: return getConstant(C1 == C2, VT);
796 case ISD::SETNE: return getConstant(C1 != C2, VT);
797 case ISD::SETLT: return getConstant(C1 < C2, VT);
798 case ISD::SETGT: return getConstant(C1 > C2, VT);
799 case ISD::SETLE: return getConstant(C1 <= C2, VT);
800 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 }
802 } else {
803 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000804 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000805 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000806
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000807 // Could not fold it.
808 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000809}
810
Chris Lattner51dabfb2006-10-14 00:41:01 +0000811
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000813///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000815 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000816 void *IP = 0;
817 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
818 return SDOperand(E, 0);
819 SDNode *N = new SDNode(Opcode, VT);
820 CSEMap.InsertNode(N, IP);
821
822 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823 return SDOperand(N, 0);
824}
825
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
827 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000828 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000829 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000830 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
831 uint64_t Val = C->getValue();
832 switch (Opcode) {
833 default: break;
834 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000835 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
837 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000838 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
839 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000840 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000841 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000842 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000843 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000844 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000845 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000846 case ISD::BSWAP:
847 switch(VT) {
848 default: assert(0 && "Invalid bswap!"); break;
849 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
850 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
851 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
852 }
853 break;
854 case ISD::CTPOP:
855 switch(VT) {
856 default: assert(0 && "Invalid ctpop!"); break;
857 case MVT::i1: return getConstant(Val != 0, VT);
858 case MVT::i8:
859 Tmp1 = (unsigned)Val & 0xFF;
860 return getConstant(CountPopulation_32(Tmp1), VT);
861 case MVT::i16:
862 Tmp1 = (unsigned)Val & 0xFFFF;
863 return getConstant(CountPopulation_32(Tmp1), VT);
864 case MVT::i32:
865 return getConstant(CountPopulation_32((unsigned)Val), VT);
866 case MVT::i64:
867 return getConstant(CountPopulation_64(Val), VT);
868 }
869 case ISD::CTLZ:
870 switch(VT) {
871 default: assert(0 && "Invalid ctlz!"); break;
872 case MVT::i1: return getConstant(Val == 0, VT);
873 case MVT::i8:
874 Tmp1 = (unsigned)Val & 0xFF;
875 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
876 case MVT::i16:
877 Tmp1 = (unsigned)Val & 0xFFFF;
878 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
879 case MVT::i32:
880 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
881 case MVT::i64:
882 return getConstant(CountLeadingZeros_64(Val), VT);
883 }
884 case ISD::CTTZ:
885 switch(VT) {
886 default: assert(0 && "Invalid cttz!"); break;
887 case MVT::i1: return getConstant(Val == 0, VT);
888 case MVT::i8:
889 Tmp1 = (unsigned)Val | 0x100;
890 return getConstant(CountTrailingZeros_32(Tmp1), VT);
891 case MVT::i16:
892 Tmp1 = (unsigned)Val | 0x10000;
893 return getConstant(CountTrailingZeros_32(Tmp1), VT);
894 case MVT::i32:
895 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
896 case MVT::i64:
897 return getConstant(CountTrailingZeros_64(Val), VT);
898 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899 }
900 }
901
Chris Lattner94683772005-12-23 05:30:37 +0000902 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000903 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
904 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000905 case ISD::FNEG:
906 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000907 case ISD::FABS:
908 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000909 case ISD::FP_ROUND:
910 case ISD::FP_EXTEND:
911 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000912 case ISD::FP_TO_SINT:
913 return getConstant((int64_t)C->getValue(), VT);
914 case ISD::FP_TO_UINT:
915 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000916 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000917 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +0000918 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000919 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +0000920 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000921 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000922 }
923
924 unsigned OpOpcode = Operand.Val->getOpcode();
925 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000926 case ISD::TokenFactor:
927 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 case ISD::SIGN_EXTEND:
929 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +0000930 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
932 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
933 break;
934 case ISD::ZERO_EXTEND:
935 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +0000936 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +0000937 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000938 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000940 case ISD::ANY_EXTEND:
941 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +0000942 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +0000943 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
944 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
945 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
946 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 case ISD::TRUNCATE:
948 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +0000949 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950 if (OpOpcode == ISD::TRUNCATE)
951 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000952 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
953 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000954 // If the source is smaller than the dest, we still need an extend.
955 if (Operand.Val->getOperand(0).getValueType() < VT)
956 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
957 else if (Operand.Val->getOperand(0).getValueType() > VT)
958 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
959 else
960 return Operand.Val->getOperand(0);
961 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962 break;
Chris Lattner35481892005-12-23 00:16:34 +0000963 case ISD::BIT_CONVERT:
964 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +0000965 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000966 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +0000967 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +0000968 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
969 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +0000970 if (OpOpcode == ISD::UNDEF)
971 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +0000972 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000973 case ISD::SCALAR_TO_VECTOR:
974 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
975 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
976 "Illegal SCALAR_TO_VECTOR node!");
977 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000978 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000979 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
980 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000981 Operand.Val->getOperand(0));
982 if (OpOpcode == ISD::FNEG) // --X -> X
983 return Operand.Val->getOperand(0);
984 break;
985 case ISD::FABS:
986 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
987 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
988 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 }
990
Chris Lattner43247a12005-08-25 19:12:10 +0000991 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000992 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +0000993 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +0000994 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
995 void *IP = 0;
996 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
997 return SDOperand(E, 0);
998 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +0000999 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001000 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001001 } else {
1002 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001003 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001004 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 AllNodes.push_back(N);
1006 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001007}
1008
Chris Lattner36019aa2005-04-18 03:48:41 +00001009
1010
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1012 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001013#ifndef NDEBUG
1014 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001015 case ISD::TokenFactor:
1016 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1017 N2.getValueType() == MVT::Other && "Invalid token factor!");
1018 break;
Chris Lattner76365122005-01-16 02:23:22 +00001019 case ISD::AND:
1020 case ISD::OR:
1021 case ISD::XOR:
1022 case ISD::UDIV:
1023 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001024 case ISD::MULHU:
1025 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001026 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1027 // fall through
1028 case ISD::ADD:
1029 case ISD::SUB:
1030 case ISD::MUL:
1031 case ISD::SDIV:
1032 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001033 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1034 // fall through.
1035 case ISD::FADD:
1036 case ISD::FSUB:
1037 case ISD::FMUL:
1038 case ISD::FDIV:
1039 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001040 assert(N1.getValueType() == N2.getValueType() &&
1041 N1.getValueType() == VT && "Binary operator types must match!");
1042 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001043 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1044 assert(N1.getValueType() == VT &&
1045 MVT::isFloatingPoint(N1.getValueType()) &&
1046 MVT::isFloatingPoint(N2.getValueType()) &&
1047 "Invalid FCOPYSIGN!");
1048 break;
Chris Lattner76365122005-01-16 02:23:22 +00001049 case ISD::SHL:
1050 case ISD::SRA:
1051 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001052 case ISD::ROTL:
1053 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001054 assert(VT == N1.getValueType() &&
1055 "Shift operators return type must be the same as their first arg");
1056 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001057 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001058 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001059 case ISD::FP_ROUND_INREG: {
1060 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1061 assert(VT == N1.getValueType() && "Not an inreg round!");
1062 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1063 "Cannot FP_ROUND_INREG integer types");
1064 assert(EVT <= VT && "Not rounding down!");
1065 break;
1066 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001067 case ISD::AssertSext:
1068 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001069 case ISD::SIGN_EXTEND_INREG: {
1070 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1071 assert(VT == N1.getValueType() && "Not an inreg extend!");
1072 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1073 "Cannot *_EXTEND_INREG FP types");
1074 assert(EVT <= VT && "Not extending!");
1075 }
1076
Chris Lattner76365122005-01-16 02:23:22 +00001077 default: break;
1078 }
1079#endif
1080
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1082 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1083 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001084 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1085 int64_t Val = N1C->getValue();
1086 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1087 Val <<= 64-FromBits;
1088 Val >>= 64-FromBits;
1089 return getConstant(Val, VT);
1090 }
1091
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 if (N2C) {
1093 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1094 switch (Opcode) {
1095 case ISD::ADD: return getConstant(C1 + C2, VT);
1096 case ISD::SUB: return getConstant(C1 - C2, VT);
1097 case ISD::MUL: return getConstant(C1 * C2, VT);
1098 case ISD::UDIV:
1099 if (C2) return getConstant(C1 / C2, VT);
1100 break;
1101 case ISD::UREM :
1102 if (C2) return getConstant(C1 % C2, VT);
1103 break;
1104 case ISD::SDIV :
1105 if (C2) return getConstant(N1C->getSignExtended() /
1106 N2C->getSignExtended(), VT);
1107 break;
1108 case ISD::SREM :
1109 if (C2) return getConstant(N1C->getSignExtended() %
1110 N2C->getSignExtended(), VT);
1111 break;
1112 case ISD::AND : return getConstant(C1 & C2, VT);
1113 case ISD::OR : return getConstant(C1 | C2, VT);
1114 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001115 case ISD::SHL : return getConstant(C1 << C2, VT);
1116 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001117 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001118 case ISD::ROTL :
1119 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1120 VT);
1121 case ISD::ROTR :
1122 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1123 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 default: break;
1125 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 } else { // Cannonicalize constant to RHS if commutative
1127 if (isCommutativeBinOp(Opcode)) {
1128 std::swap(N1C, N2C);
1129 std::swap(N1, N2);
1130 }
1131 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 }
1133
1134 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1135 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001136 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 if (N2CFP) {
1138 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1139 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001140 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1141 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1142 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1143 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 if (C2) return getConstantFP(C1 / C2, VT);
1145 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001146 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 if (C2) return getConstantFP(fmod(C1, C2), VT);
1148 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001149 case ISD::FCOPYSIGN: {
1150 union {
1151 double F;
1152 uint64_t I;
1153 } u1;
1154 union {
1155 double F;
1156 int64_t I;
1157 } u2;
1158 u1.F = C1;
1159 u2.F = C2;
1160 if (u2.I < 0) // Sign bit of RHS set?
1161 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1162 else
1163 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1164 return getConstantFP(u1.F, VT);
1165 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001166 default: break;
1167 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001168 } else { // Cannonicalize constant to RHS if commutative
1169 if (isCommutativeBinOp(Opcode)) {
1170 std::swap(N1CFP, N2CFP);
1171 std::swap(N1, N2);
1172 }
1173 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001174 }
Chris Lattner62b57722006-04-20 05:39:12 +00001175
1176 // Canonicalize an UNDEF to the RHS, even over a constant.
1177 if (N1.getOpcode() == ISD::UNDEF) {
1178 if (isCommutativeBinOp(Opcode)) {
1179 std::swap(N1, N2);
1180 } else {
1181 switch (Opcode) {
1182 case ISD::FP_ROUND_INREG:
1183 case ISD::SIGN_EXTEND_INREG:
1184 case ISD::SUB:
1185 case ISD::FSUB:
1186 case ISD::FDIV:
1187 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001188 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001189 return N1; // fold op(undef, arg2) -> undef
1190 case ISD::UDIV:
1191 case ISD::SDIV:
1192 case ISD::UREM:
1193 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001194 case ISD::SRL:
1195 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001196 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1197 }
1198 }
1199 }
1200
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001201 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001202 if (N2.getOpcode() == ISD::UNDEF) {
1203 switch (Opcode) {
1204 case ISD::ADD:
1205 case ISD::SUB:
1206 case ISD::FADD:
1207 case ISD::FSUB:
1208 case ISD::FMUL:
1209 case ISD::FDIV:
1210 case ISD::FREM:
1211 case ISD::UDIV:
1212 case ISD::SDIV:
1213 case ISD::UREM:
1214 case ISD::SREM:
1215 case ISD::XOR:
1216 return N2; // fold op(arg1, undef) -> undef
1217 case ISD::MUL:
1218 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001219 case ISD::SRL:
1220 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001221 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1222 case ISD::OR:
1223 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001224 case ISD::SRA:
1225 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001226 }
1227 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001228
Chris Lattner51dabfb2006-10-14 00:41:01 +00001229 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230 switch (Opcode) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001231 case ISD::AND:
1232 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1233 // worth handling here.
1234 if (N2C && N2C->getValue() == 0)
1235 return N2;
1236 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001237 case ISD::OR:
1238 case ISD::XOR:
1239 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1240 // worth handling here.
1241 if (N2C && N2C->getValue() == 0)
1242 return N1;
1243 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001244 case ISD::FP_ROUND_INREG:
1245 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1246 break;
1247 case ISD::SIGN_EXTEND_INREG: {
1248 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1249 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001250 break;
1251 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001252 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001253 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1254
Chris Lattner5c6621c2006-09-19 04:51:23 +00001255 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1256 // 64-bit integers into 32-bit parts. Instead of building the extract of
1257 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001258 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001259 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001260
1261 // EXTRACT_ELEMENT of a constant int is also very common.
1262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1263 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1264 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001265 }
1266 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001267
Nate Begemaneea805e2005-04-13 21:23:31 +00001268 // FIXME: figure out how to safely handle things like
1269 // int foo(int x) { return 1 << (x & 255); }
1270 // int bar() { return foo(256); }
1271#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001272 case ISD::SHL:
1273 case ISD::SRL:
1274 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001275 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001276 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001277 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001278 else if (N2.getOpcode() == ISD::AND)
1279 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1280 // If the and is only masking out bits that cannot effect the shift,
1281 // eliminate the and.
1282 unsigned NumBits = MVT::getSizeInBits(VT);
1283 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1284 return getNode(Opcode, VT, N1, N2.getOperand(0));
1285 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001286 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001287#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288 }
1289
Chris Lattner27e9b412005-05-11 18:57:39 +00001290 // Memoize this node if possible.
1291 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001292 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001293 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001294 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1295 void *IP = 0;
1296 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1297 return SDOperand(E, 0);
1298 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001299 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001300 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001301 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001302 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001303 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001304 }
1305
Chris Lattnerc3aae252005-01-07 07:46:32 +00001306 AllNodes.push_back(N);
1307 return SDOperand(N, 0);
1308}
1309
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1311 SDOperand N1, SDOperand N2, SDOperand N3) {
1312 // Perform various simplifications.
1313 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1314 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001315 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001317 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001318 // Use FoldSetCC to simplify SETCC's.
1319 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001320 if (Simp.Val) return Simp;
1321 break;
1322 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001323 case ISD::SELECT:
1324 if (N1C)
1325 if (N1C->getValue())
1326 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001327 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 return N3; // select false, X, Y -> Y
1329
1330 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001331 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001332 case ISD::BRCOND:
1333 if (N2C)
1334 if (N2C->getValue()) // Unconditional branch
1335 return getNode(ISD::BR, MVT::Other, N1, N3);
1336 else
1337 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001338 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001339 case ISD::VECTOR_SHUFFLE:
1340 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1341 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1342 N3.getOpcode() == ISD::BUILD_VECTOR &&
1343 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1344 "Illegal VECTOR_SHUFFLE node!");
1345 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001346 }
1347
Chris Lattner43247a12005-08-25 19:12:10 +00001348 // Memoize node if it doesn't produce a flag.
1349 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001350 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001351 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001352 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1353 void *IP = 0;
1354 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1355 return SDOperand(E, 0);
1356 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001357 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001358 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001359 } else {
1360 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001361 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001362 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001363 AllNodes.push_back(N);
1364 return SDOperand(N, 0);
1365}
1366
1367SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001368 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001369 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001370 SDOperand Ops[] = { N1, N2, N3, N4 };
1371 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001372}
1373
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001374SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1375 SDOperand N1, SDOperand N2, SDOperand N3,
1376 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001377 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1378 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001379}
1380
Evan Cheng7038daf2005-12-10 00:37:58 +00001381SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1382 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001383 const Value *SV, int SVOffset,
1384 bool isVolatile) {
1385 // FIXME: Alignment == 1 for now.
1386 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001387 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001388 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Evan Cheng466685d2006-10-09 20:57:25 +00001389 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1390 ID.AddInteger(ISD::UNINDEXED);
1391 ID.AddInteger(ISD::NON_EXTLOAD);
1392 ID.AddInteger(VT);
1393 ID.AddPointer(SV);
1394 ID.AddInteger(SVOffset);
1395 ID.AddInteger(Alignment);
1396 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001397 void *IP = 0;
1398 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1399 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001400 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED,
1401 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1402 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00001403 N->setValueTypes(VTs);
1404 CSEMap.InsertNode(N, IP);
1405 AllNodes.push_back(N);
1406 return SDOperand(N, 0);
1407}
1408
1409SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1410 SDOperand Chain, SDOperand Ptr, const Value *SV,
1411 int SVOffset, MVT::ValueType EVT,
1412 bool isVolatile) {
1413 // If they are asking for an extending load from/to the same thing, return a
1414 // normal load.
1415 if (VT == EVT)
1416 ExtType = ISD::NON_EXTLOAD;
1417
1418 if (MVT::isVector(VT))
1419 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1420 else
1421 assert(EVT < VT && "Should only be an extending load, not truncating!");
1422 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1423 "Cannot sign/zero extend a FP/Vector load!");
1424 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1425 "Cannot convert from FP to Int or Int -> FP!");
1426
1427 // FIXME: Alignment == 1 for now.
1428 unsigned Alignment = 1;
1429 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001430 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Evan Cheng466685d2006-10-09 20:57:25 +00001431 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1432 ID.AddInteger(ISD::UNINDEXED);
1433 ID.AddInteger(ExtType);
1434 ID.AddInteger(EVT);
1435 ID.AddPointer(SV);
1436 ID.AddInteger(SVOffset);
1437 ID.AddInteger(Alignment);
1438 ID.AddInteger(isVolatile);
1439 void *IP = 0;
1440 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1441 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001442 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED, ExtType, EVT,
1443 SV, SVOffset, Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001444 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001445 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001446 AllNodes.push_back(N);
1447 return SDOperand(N, 0);
1448}
1449
Evan Cheng2caccca2006-10-17 21:14:32 +00001450SDOperand SelectionDAG::getPreIndexedLoad(SDOperand OrigLoad, SDOperand Base) {
1451 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
1452 SDOperand Ptr = LD->getBasePtr();
1453 MVT::ValueType PtrVT = Ptr.getValueType();
1454 unsigned Opc = Ptr.getOpcode();
1455 SDOperand Offset = LD->getOffset();
1456 assert(Offset.getOpcode() == ISD::UNDEF);
1457 assert((Opc == ISD::ADD || Opc == ISD::SUB) &&
1458 "Load address must be <base +/- offset>!");
1459 ISD::MemOpAddrMode AM = (Opc == ISD::ADD) ? ISD::PRE_INC : ISD::PRE_DEC;
1460 if (Ptr.getOperand(0) == Base) {
1461 Offset = Ptr.getOperand(1);
1462 Ptr = Ptr.getOperand(0);
1463 } else {
1464 assert(Ptr.getOperand(1) == Base);
1465 Offset = Ptr.getOperand(0);
1466 Ptr = Ptr.getOperand(1);
1467 }
1468
1469 MVT::ValueType VT = OrigLoad.getValueType();
1470 SDVTList VTs = getVTList(VT, PtrVT, MVT::Other);
1471 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, LD->getChain(), Ptr, Offset);
1472 ID.AddInteger(AM);
1473 ID.AddInteger(LD->getExtensionType());
1474 ID.AddInteger(LD->getLoadedVT());
1475 ID.AddPointer(LD->getSrcValue());
1476 ID.AddInteger(LD->getSrcValueOffset());
1477 ID.AddInteger(LD->getAlignment());
1478 ID.AddInteger(LD->isVolatile());
1479 void *IP = 0;
1480 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1481 return SDOperand(E, 0);
1482 SDNode *N = new LoadSDNode(LD->getChain(), Ptr, Offset, AM,
1483 LD->getExtensionType(), LD->getLoadedVT(),
1484 LD->getSrcValue(), LD->getSrcValueOffset(),
1485 LD->getAlignment(), LD->isVolatile());
1486 N->setValueTypes(VTs);
1487 CSEMap.InsertNode(N, IP);
1488 AllNodes.push_back(N);
1489 return SDOperand(N, 0);
1490}
1491
Evan Cheng7038daf2005-12-10 00:37:58 +00001492SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1493 SDOperand Chain, SDOperand Ptr,
1494 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001495 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1496 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001497 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001498}
1499
Evan Chengad071e12006-10-05 22:57:11 +00001500SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001501 SDOperand Ptr, const Value *SV, int SVOffset,
1502 bool isVolatile) {
1503 MVT::ValueType VT = Value.getValueType();
1504
1505 // FIXME: Alignment == 1 for now.
1506 unsigned Alignment = 1;
Evan Chengad071e12006-10-05 22:57:11 +00001507 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001508 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1509 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Evan Chengad071e12006-10-05 22:57:11 +00001510 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001511 ID.AddInteger(ISD::UNINDEXED);
1512 ID.AddInteger(false);
1513 ID.AddInteger(VT);
1514 ID.AddPointer(SV);
1515 ID.AddInteger(SVOffset);
1516 ID.AddInteger(Alignment);
1517 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001518 void *IP = 0;
1519 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1520 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001521 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, false,
1522 VT, SV, SVOffset, Alignment, isVolatile);
1523 N->setValueTypes(VTs);
1524 CSEMap.InsertNode(N, IP);
1525 AllNodes.push_back(N);
1526 return SDOperand(N, 0);
1527}
1528
1529SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Value,
1530 SDOperand Ptr, const Value *SV,
1531 int SVOffset, MVT::ValueType SVT,
1532 bool isVolatile) {
1533 MVT::ValueType VT = Value.getValueType();
1534 bool isTrunc = VT != SVT;
1535
1536 assert(VT > SVT && "Not a truncation?");
1537 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1538 "Can't do FP-INT conversion!");
1539
1540 // FIXME: Alignment == 1 for now.
1541 unsigned Alignment = 1;
1542 SDVTList VTs = getVTList(MVT::Other);
1543 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1544 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
1545 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1546 ID.AddInteger(ISD::UNINDEXED);
1547 ID.AddInteger(isTrunc);
1548 ID.AddInteger(SVT);
1549 ID.AddPointer(SV);
1550 ID.AddInteger(SVOffset);
1551 ID.AddInteger(Alignment);
1552 ID.AddInteger(isVolatile);
1553 void *IP = 0;
1554 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1555 return SDOperand(E, 0);
1556 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, isTrunc,
1557 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001558 N->setValueTypes(VTs);
1559 CSEMap.InsertNode(N, IP);
1560 AllNodes.push_back(N);
1561 return SDOperand(N, 0);
1562}
1563
Nate Begemanacc398c2006-01-25 18:21:52 +00001564SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1565 SDOperand Chain, SDOperand Ptr,
1566 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001567 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001568 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001569}
1570
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001571SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001572 const SDOperand *Ops, unsigned NumOps) {
1573 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001574 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001575 case 1: return getNode(Opcode, VT, Ops[0]);
1576 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1577 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001578 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001579 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001580
Chris Lattneref847df2005-04-09 03:27:28 +00001581 switch (Opcode) {
1582 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001583 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001584 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001585 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1586 "LHS and RHS of condition must have same type!");
1587 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1588 "True and False arms of SelectCC must have same type!");
1589 assert(Ops[2].getValueType() == VT &&
1590 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001591 break;
1592 }
1593 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001594 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001595 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1596 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001597 break;
1598 }
Chris Lattneref847df2005-04-09 03:27:28 +00001599 }
1600
Chris Lattner385328c2005-05-14 07:42:29 +00001601 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001602 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001603 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001604 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001605 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001606 void *IP = 0;
1607 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1608 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001609 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001610 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001611 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001612 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001613 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001614 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001615 }
Chris Lattneref847df2005-04-09 03:27:28 +00001616 AllNodes.push_back(N);
1617 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001618}
1619
Chris Lattner89c34632005-05-14 06:20:26 +00001620SDOperand SelectionDAG::getNode(unsigned Opcode,
1621 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001622 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001623 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1624 Ops, NumOps);
1625}
1626
1627SDOperand SelectionDAG::getNode(unsigned Opcode,
1628 const MVT::ValueType *VTs, unsigned NumVTs,
1629 const SDOperand *Ops, unsigned NumOps) {
1630 if (NumVTs == 1)
1631 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001632 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1633}
1634
1635SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1636 const SDOperand *Ops, unsigned NumOps) {
1637 if (VTList.NumVTs == 1)
1638 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001639
Chris Lattner5f056bf2005-07-10 01:55:33 +00001640 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001641 // FIXME: figure out how to safely handle things like
1642 // int foo(int x) { return 1 << (x & 255); }
1643 // int bar() { return foo(256); }
1644#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001645 case ISD::SRA_PARTS:
1646 case ISD::SRL_PARTS:
1647 case ISD::SHL_PARTS:
1648 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001649 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001650 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1651 else if (N3.getOpcode() == ISD::AND)
1652 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1653 // If the and is only masking out bits that cannot effect the shift,
1654 // eliminate the and.
1655 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1656 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1657 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1658 }
1659 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001660#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001661 }
Chris Lattner89c34632005-05-14 06:20:26 +00001662
Chris Lattner43247a12005-08-25 19:12:10 +00001663 // Memoize the node unless it returns a flag.
1664 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001665 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001666 SelectionDAGCSEMap::NodeID ID;
1667 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001668 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001669 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001670 void *IP = 0;
1671 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1672 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001673 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001674 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001675 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001676 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001677 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001678 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001679 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001680 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001681 return SDOperand(N, 0);
1682}
1683
Chris Lattner70046e92006-08-15 17:46:01 +00001684SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1685 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001686}
1687
Chris Lattner70046e92006-08-15 17:46:01 +00001688SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001689 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1690 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001691 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001692 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001693 }
1694 std::vector<MVT::ValueType> V;
1695 V.push_back(VT1);
1696 V.push_back(VT2);
1697 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001698 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001699}
Chris Lattner70046e92006-08-15 17:46:01 +00001700SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1701 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001702 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001703 E = VTList.end(); I != E; ++I) {
1704 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1705 (*I)[2] == VT3)
1706 return makeVTList(&(*I)[0], 3);
1707 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001708 std::vector<MVT::ValueType> V;
1709 V.push_back(VT1);
1710 V.push_back(VT2);
1711 V.push_back(VT3);
1712 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001713 return makeVTList(&(*VTList.begin())[0], 3);
1714}
1715
1716SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1717 switch (NumVTs) {
1718 case 0: assert(0 && "Cannot have nodes without results!");
1719 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1720 case 2: return getVTList(VTs[0], VTs[1]);
1721 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1722 default: break;
1723 }
1724
1725 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1726 E = VTList.end(); I != E; ++I) {
1727 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1728
1729 bool NoMatch = false;
1730 for (unsigned i = 2; i != NumVTs; ++i)
1731 if (VTs[i] != (*I)[i]) {
1732 NoMatch = true;
1733 break;
1734 }
1735 if (!NoMatch)
1736 return makeVTList(&*I->begin(), NumVTs);
1737 }
1738
1739 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1740 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001741}
1742
1743
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001744/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1745/// specified operands. If the resultant node already exists in the DAG,
1746/// this does not modify the specified node, instead it returns the node that
1747/// already exists. If the resultant node does not exist in the DAG, the
1748/// input node is returned. As a degenerate case, if you specify the same
1749/// input operands as the node already has, the input node is returned.
1750SDOperand SelectionDAG::
1751UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1752 SDNode *N = InN.Val;
1753 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1754
1755 // Check to see if there is no change.
1756 if (Op == N->getOperand(0)) return InN;
1757
1758 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001759 void *InsertPos = 0;
1760 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1761 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001762
1763 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001764 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001765 RemoveNodeFromCSEMaps(N);
1766
1767 // Now we update the operands.
1768 N->OperandList[0].Val->removeUser(N);
1769 Op.Val->addUser(N);
1770 N->OperandList[0] = Op;
1771
1772 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001773 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001774 return InN;
1775}
1776
1777SDOperand SelectionDAG::
1778UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1779 SDNode *N = InN.Val;
1780 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1781
1782 // Check to see if there is no change.
1783 bool AnyChange = false;
1784 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1785 return InN; // No operands changed, just return the input node.
1786
1787 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001788 void *InsertPos = 0;
1789 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1790 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001791
1792 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001793 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001794 RemoveNodeFromCSEMaps(N);
1795
1796 // Now we update the operands.
1797 if (N->OperandList[0] != Op1) {
1798 N->OperandList[0].Val->removeUser(N);
1799 Op1.Val->addUser(N);
1800 N->OperandList[0] = Op1;
1801 }
1802 if (N->OperandList[1] != Op2) {
1803 N->OperandList[1].Val->removeUser(N);
1804 Op2.Val->addUser(N);
1805 N->OperandList[1] = Op2;
1806 }
1807
1808 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001809 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001810 return InN;
1811}
1812
1813SDOperand SelectionDAG::
1814UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001815 SDOperand Ops[] = { Op1, Op2, Op3 };
1816 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001817}
1818
1819SDOperand SelectionDAG::
1820UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1821 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001822 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1823 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001824}
1825
1826SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001827UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1828 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001829 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1830 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001831}
1832
1833
1834SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001835UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001836 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001837 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001838 "Update with wrong number of operands");
1839
1840 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001841 bool AnyChange = false;
1842 for (unsigned i = 0; i != NumOps; ++i) {
1843 if (Ops[i] != N->getOperand(i)) {
1844 AnyChange = true;
1845 break;
1846 }
1847 }
1848
1849 // No operands changed, just return the input node.
1850 if (!AnyChange) return InN;
1851
1852 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001853 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001854 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001855 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001856
1857 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001858 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001859 RemoveNodeFromCSEMaps(N);
1860
1861 // Now we update the operands.
1862 for (unsigned i = 0; i != NumOps; ++i) {
1863 if (N->OperandList[i] != Ops[i]) {
1864 N->OperandList[i].Val->removeUser(N);
1865 Ops[i].Val->addUser(N);
1866 N->OperandList[i] = Ops[i];
1867 }
1868 }
1869
1870 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001871 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001872 return InN;
1873}
1874
1875
1876
Chris Lattner149c58c2005-08-16 18:17:10 +00001877
1878/// SelectNodeTo - These are used for target selectors to *mutate* the
1879/// specified node to have the specified return type, Target opcode, and
1880/// operands. Note that target opcodes are stored as
1881/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001882///
1883/// Note that SelectNodeTo returns the resultant node. If there is already a
1884/// node of the specified opcode and operands, it returns that node instead of
1885/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00001886SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1887 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001888 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001889 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1890 void *IP = 0;
1891 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001892 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00001893
Chris Lattner7651fa42005-08-24 23:00:29 +00001894 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001895
Chris Lattner7651fa42005-08-24 23:00:29 +00001896 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001897 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001898
Chris Lattner4a283e92006-08-11 18:38:11 +00001899 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001900 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00001901}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001902
Evan Cheng95514ba2006-08-26 08:00:10 +00001903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1904 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001905 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001906 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001907 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1908 void *IP = 0;
1909 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001910 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001911
Chris Lattner149c58c2005-08-16 18:17:10 +00001912 RemoveNodeFromCSEMaps(N);
1913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001914 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001915 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001916 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001917 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001918}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001919
Evan Cheng95514ba2006-08-26 08:00:10 +00001920SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1921 MVT::ValueType VT, SDOperand Op1,
1922 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001923 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001924 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001925 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1926 void *IP = 0;
1927 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001928 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001929
Chris Lattner149c58c2005-08-16 18:17:10 +00001930 RemoveNodeFromCSEMaps(N);
1931 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001932 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001933 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001934
Chris Lattnera5682852006-08-07 23:03:03 +00001935 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001936 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001937}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001938
Evan Cheng95514ba2006-08-26 08:00:10 +00001939SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1940 MVT::ValueType VT, SDOperand Op1,
1941 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001942 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001943 SDVTList VTs = getVTList(VT);
1944 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1945 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001946 void *IP = 0;
1947 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001948 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001949
Chris Lattner149c58c2005-08-16 18:17:10 +00001950 RemoveNodeFromCSEMaps(N);
1951 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001952 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001953 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001954
Chris Lattnera5682852006-08-07 23:03:03 +00001955 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001956 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001957}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001958
Evan Cheng95514ba2006-08-26 08:00:10 +00001959SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00001960 MVT::ValueType VT, const SDOperand *Ops,
1961 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001962 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001963 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001964 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001965 for (unsigned i = 0; i != NumOps; ++i)
1966 ID.AddOperand(Ops[i]);
Chris Lattnera5682852006-08-07 23:03:03 +00001967 void *IP = 0;
1968 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001969 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001970
Chris Lattner6b09a292005-08-21 18:49:33 +00001971 RemoveNodeFromCSEMaps(N);
1972 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001973 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001974 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001975
Chris Lattnera5682852006-08-07 23:03:03 +00001976 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001977 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001978}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001979
Evan Cheng95514ba2006-08-26 08:00:10 +00001980SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1981 MVT::ValueType VT1, MVT::ValueType VT2,
1982 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001983 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00001984 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1985 void *IP = 0;
1986 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001987 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001988
Chris Lattner0fb094f2005-11-19 01:44:53 +00001989 RemoveNodeFromCSEMaps(N);
1990 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001991 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001992 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001993
Chris Lattnera5682852006-08-07 23:03:03 +00001994 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001995 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00001996}
1997
Evan Cheng95514ba2006-08-26 08:00:10 +00001998SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1999 MVT::ValueType VT1, MVT::ValueType VT2,
2000 SDOperand Op1, SDOperand Op2,
2001 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002002 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002003 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002004 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2005 Op1, Op2, Op3);
2006 void *IP = 0;
2007 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002008 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002009
Chris Lattner0fb094f2005-11-19 01:44:53 +00002010 RemoveNodeFromCSEMaps(N);
2011 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002012 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002013 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002014
Chris Lattnera5682852006-08-07 23:03:03 +00002015 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002016 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002017}
2018
Chris Lattner0fb094f2005-11-19 01:44:53 +00002019
Evan Cheng6ae46c42006-02-09 07:15:23 +00002020/// getTargetNode - These are used for target selectors to create a new node
2021/// with specified return type(s), target opcode, and operands.
2022///
2023/// Note that getTargetNode returns the resultant node. If there is already a
2024/// node of the specified opcode and operands, it returns that node instead of
2025/// the current one.
2026SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2027 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2028}
2029SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2030 SDOperand Op1) {
2031 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2032}
2033SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2034 SDOperand Op1, SDOperand Op2) {
2035 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2036}
2037SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2038 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2039 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2040}
2041SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002042 const SDOperand *Ops, unsigned NumOps) {
2043 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002044}
2045SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2046 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002047 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002048 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002049}
2050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002051 MVT::ValueType VT2, SDOperand Op1,
2052 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002053 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002054 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002055 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002056}
2057SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002058 MVT::ValueType VT2, SDOperand Op1,
2059 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002060 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002061 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002062 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002063}
Evan Cheng694481e2006-08-27 08:08:54 +00002064SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2065 MVT::ValueType VT2,
2066 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002067 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002068 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002069}
2070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2071 MVT::ValueType VT2, MVT::ValueType VT3,
2072 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002073 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002074 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002075 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002076}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002077SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002078 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002079 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002080 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2081 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002082}
2083
Evan Cheng99157a02006-08-07 22:13:29 +00002084/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002085/// This can cause recursive merging of nodes in the DAG.
2086///
Chris Lattner8b52f212005-08-26 18:36:28 +00002087/// This version assumes From/To have a single result value.
2088///
Chris Lattner1e111c72005-09-07 05:37:01 +00002089void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2090 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002091 SDNode *From = FromN.Val, *To = ToN.Val;
2092 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2093 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002094 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002095
Chris Lattner8b8749f2005-08-17 19:00:20 +00002096 while (!From->use_empty()) {
2097 // Process users until they are all gone.
2098 SDNode *U = *From->use_begin();
2099
2100 // This node is about to morph, remove its old self from the CSE maps.
2101 RemoveNodeFromCSEMaps(U);
2102
Chris Lattner65113b22005-11-08 22:07:03 +00002103 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2104 I != E; ++I)
2105 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002106 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002107 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002108 To->addUser(U);
2109 }
2110
2111 // Now that we have modified U, add it back to the CSE maps. If it already
2112 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002113 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2114 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002115 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002116 if (Deleted) Deleted->push_back(U);
2117 DeleteNodeNotInCSEMaps(U);
2118 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002119 }
2120}
2121
2122/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2123/// This can cause recursive merging of nodes in the DAG.
2124///
2125/// This version assumes From/To have matching types and numbers of result
2126/// values.
2127///
Chris Lattner1e111c72005-09-07 05:37:01 +00002128void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2129 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002130 assert(From != To && "Cannot replace uses of with self");
2131 assert(From->getNumValues() == To->getNumValues() &&
2132 "Cannot use this version of ReplaceAllUsesWith!");
2133 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002134 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002135 return;
2136 }
2137
2138 while (!From->use_empty()) {
2139 // Process users until they are all gone.
2140 SDNode *U = *From->use_begin();
2141
2142 // This node is about to morph, remove its old self from the CSE maps.
2143 RemoveNodeFromCSEMaps(U);
2144
Chris Lattner65113b22005-11-08 22:07:03 +00002145 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2146 I != E; ++I)
2147 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002148 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002149 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002150 To->addUser(U);
2151 }
2152
2153 // Now that we have modified U, add it back to the CSE maps. If it already
2154 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002155 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2156 ReplaceAllUsesWith(U, Existing, Deleted);
2157 // U is now dead.
2158 if (Deleted) Deleted->push_back(U);
2159 DeleteNodeNotInCSEMaps(U);
2160 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002161 }
2162}
2163
Chris Lattner8b52f212005-08-26 18:36:28 +00002164/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2165/// This can cause recursive merging of nodes in the DAG.
2166///
2167/// This version can replace From with any result values. To must match the
2168/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002169void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002170 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002171 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002172 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002173 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002174 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002175 return;
2176 }
2177
2178 while (!From->use_empty()) {
2179 // Process users until they are all gone.
2180 SDNode *U = *From->use_begin();
2181
2182 // This node is about to morph, remove its old self from the CSE maps.
2183 RemoveNodeFromCSEMaps(U);
2184
Chris Lattner65113b22005-11-08 22:07:03 +00002185 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2186 I != E; ++I)
2187 if (I->Val == From) {
2188 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002189 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002190 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002191 ToOp.Val->addUser(U);
2192 }
2193
2194 // Now that we have modified U, add it back to the CSE maps. If it already
2195 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002196 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2197 ReplaceAllUsesWith(U, Existing, Deleted);
2198 // U is now dead.
2199 if (Deleted) Deleted->push_back(U);
2200 DeleteNodeNotInCSEMaps(U);
2201 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002202 }
2203}
2204
Chris Lattner012f2412006-02-17 21:58:01 +00002205/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2206/// uses of other values produced by From.Val alone. The Deleted vector is
2207/// handled the same was as for ReplaceAllUsesWith.
2208void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2209 std::vector<SDNode*> &Deleted) {
2210 assert(From != To && "Cannot replace a value with itself");
2211 // Handle the simple, trivial, case efficiently.
2212 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2213 ReplaceAllUsesWith(From, To, &Deleted);
2214 return;
2215 }
2216
2217 // Get all of the users in a nice, deterministically ordered, uniqued set.
2218 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2219
2220 while (!Users.empty()) {
2221 // We know that this user uses some value of From. If it is the right
2222 // value, update it.
2223 SDNode *User = Users.back();
2224 Users.pop_back();
2225
2226 for (SDOperand *Op = User->OperandList,
2227 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2228 if (*Op == From) {
2229 // Okay, we know this user needs to be updated. Remove its old self
2230 // from the CSE maps.
2231 RemoveNodeFromCSEMaps(User);
2232
2233 // Update all operands that match "From".
2234 for (; Op != E; ++Op) {
2235 if (*Op == From) {
2236 From.Val->removeUser(User);
2237 *Op = To;
2238 To.Val->addUser(User);
2239 }
2240 }
2241
2242 // Now that we have modified User, add it back to the CSE maps. If it
2243 // already exists there, recursively merge the results together.
2244 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2245 unsigned NumDeleted = Deleted.size();
2246 ReplaceAllUsesWith(User, Existing, &Deleted);
2247
2248 // User is now dead.
2249 Deleted.push_back(User);
2250 DeleteNodeNotInCSEMaps(User);
2251
2252 // We have to be careful here, because ReplaceAllUsesWith could have
2253 // deleted a user of From, which means there may be dangling pointers
2254 // in the "Users" setvector. Scan over the deleted node pointers and
2255 // remove them from the setvector.
2256 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2257 Users.remove(Deleted[i]);
2258 }
2259 break; // Exit the operand scanning loop.
2260 }
2261 }
2262 }
2263}
2264
Chris Lattner7b2880c2005-08-24 22:44:39 +00002265
Evan Chenge6f35d82006-08-01 08:20:41 +00002266/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2267/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002268unsigned SelectionDAG::AssignNodeIds() {
2269 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002270 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2271 SDNode *N = I;
2272 N->setNodeId(Id++);
2273 }
2274 return Id;
2275}
2276
Evan Chenge6f35d82006-08-01 08:20:41 +00002277/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002278/// based on their topological order. It returns the maximum id and a vector
2279/// of the SDNodes* in assigned order by reference.
2280unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002281 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002282 std::vector<unsigned> InDegree(DAGSize);
2283 std::vector<SDNode*> Sources;
2284
2285 // Use a two pass approach to avoid using a std::map which is slow.
2286 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002287 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2288 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002289 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002290 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002291 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002292 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002293 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002294 }
2295
Evan Cheng99157a02006-08-07 22:13:29 +00002296 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002297 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002298 SDNode *N = Sources.back();
2299 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002300 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002301 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2302 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002303 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002304 if (Degree == 0)
2305 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002306 }
2307 }
2308
Evan Chengc384d6c2006-08-02 22:00:34 +00002309 // Second pass, assign the actual topological order as node ids.
2310 Id = 0;
2311 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2312 TI != TE; ++TI)
2313 (*TI)->setNodeId(Id++);
2314
2315 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002316}
2317
2318
Evan Cheng091cba12006-07-27 06:39:06 +00002319
Jim Laskey58b968b2005-08-17 20:08:02 +00002320//===----------------------------------------------------------------------===//
2321// SDNode Class
2322//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002323
Chris Lattner917d2c92006-07-19 00:00:37 +00002324// Out-of-line virtual method to give class a home.
2325void SDNode::ANCHOR() {
2326}
2327
Chris Lattnera3255112005-11-08 23:30:28 +00002328/// getValueTypeList - Return a pointer to the specified value type.
2329///
2330MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2331 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2332 VTs[VT] = VT;
2333 return &VTs[VT];
2334}
Chris Lattnera5682852006-08-07 23:03:03 +00002335
Chris Lattner5c884562005-01-12 18:37:47 +00002336/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2337/// indicated value. This method ignores uses of other values defined by this
2338/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002339bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002340 assert(Value < getNumValues() && "Bad value!");
2341
2342 // If there is only one value, this is easy.
2343 if (getNumValues() == 1)
2344 return use_size() == NUses;
2345 if (Uses.size() < NUses) return false;
2346
Evan Cheng4ee62112006-02-05 06:29:23 +00002347 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002348
2349 std::set<SDNode*> UsersHandled;
2350
Chris Lattnerf83482d2006-08-16 20:59:32 +00002351 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002352 SDNode *User = *UI;
2353 if (User->getNumOperands() == 1 ||
2354 UsersHandled.insert(User).second) // First time we've seen this?
2355 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2356 if (User->getOperand(i) == TheValue) {
2357 if (NUses == 0)
2358 return false; // too many uses
2359 --NUses;
2360 }
2361 }
2362
2363 // Found exactly the right number of uses?
2364 return NUses == 0;
2365}
2366
2367
Evan Cheng4ee62112006-02-05 06:29:23 +00002368// isOnlyUse - Return true if this node is the only use of N.
2369bool SDNode::isOnlyUse(SDNode *N) const {
2370 bool Seen = false;
2371 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2372 SDNode *User = *I;
2373 if (User == this)
2374 Seen = true;
2375 else
2376 return false;
2377 }
2378
2379 return Seen;
2380}
2381
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002382// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002383bool SDOperand::isOperand(SDNode *N) const {
2384 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2385 if (*this == N->getOperand(i))
2386 return true;
2387 return false;
2388}
2389
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002390bool SDNode::isOperand(SDNode *N) const {
2391 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2392 if (this == N->OperandList[i].Val)
2393 return true;
2394 return false;
2395}
Evan Cheng4ee62112006-02-05 06:29:23 +00002396
Evan Chengc5484282006-10-04 00:56:09 +00002397uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2398 assert(Num < NumOperands && "Invalid child # of SDNode!");
2399 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2400}
2401
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002402const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002403 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002404 default:
2405 if (getOpcode() < ISD::BUILTIN_OP_END)
2406 return "<<Unknown DAG Node>>";
2407 else {
Evan Cheng72261582005-12-20 06:22:03 +00002408 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002409 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002410 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2411 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002412
Evan Cheng72261582005-12-20 06:22:03 +00002413 TargetLowering &TLI = G->getTargetLoweringInfo();
2414 const char *Name =
2415 TLI.getTargetNodeName(getOpcode());
2416 if (Name) return Name;
2417 }
2418
2419 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002420 }
2421
Andrew Lenharth95762122005-03-31 21:24:06 +00002422 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002423 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002424 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002425 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002426 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002427 case ISD::AssertSext: return "AssertSext";
2428 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002429
2430 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002431 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002432 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002433 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002434
2435 case ISD::Constant: return "Constant";
2436 case ISD::ConstantFP: return "ConstantFP";
2437 case ISD::GlobalAddress: return "GlobalAddress";
2438 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002439 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002440 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Chris Lattner5839bf22005-08-26 17:15:30 +00002441 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002442 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002443 case ISD::INTRINSIC_WO_CHAIN: {
2444 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2445 return Intrinsic::getName((Intrinsic::ID)IID);
2446 }
2447 case ISD::INTRINSIC_VOID:
2448 case ISD::INTRINSIC_W_CHAIN: {
2449 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002450 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002451 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002452
Chris Lattnerb2827b02006-03-19 00:52:58 +00002453 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002454 case ISD::TargetConstant: return "TargetConstant";
2455 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002456 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2457 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002458 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002459 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002460 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002461
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002462 case ISD::CopyToReg: return "CopyToReg";
2463 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002464 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002465 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002466 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002467 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002468 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002469 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002470
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002471 // Unary operators
2472 case ISD::FABS: return "fabs";
2473 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002474 case ISD::FSQRT: return "fsqrt";
2475 case ISD::FSIN: return "fsin";
2476 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002477 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002478
2479 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002480 case ISD::ADD: return "add";
2481 case ISD::SUB: return "sub";
2482 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002483 case ISD::MULHU: return "mulhu";
2484 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002485 case ISD::SDIV: return "sdiv";
2486 case ISD::UDIV: return "udiv";
2487 case ISD::SREM: return "srem";
2488 case ISD::UREM: return "urem";
2489 case ISD::AND: return "and";
2490 case ISD::OR: return "or";
2491 case ISD::XOR: return "xor";
2492 case ISD::SHL: return "shl";
2493 case ISD::SRA: return "sra";
2494 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002495 case ISD::ROTL: return "rotl";
2496 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002497 case ISD::FADD: return "fadd";
2498 case ISD::FSUB: return "fsub";
2499 case ISD::FMUL: return "fmul";
2500 case ISD::FDIV: return "fdiv";
2501 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002502 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002503 case ISD::VADD: return "vadd";
2504 case ISD::VSUB: return "vsub";
2505 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002506 case ISD::VSDIV: return "vsdiv";
2507 case ISD::VUDIV: return "vudiv";
2508 case ISD::VAND: return "vand";
2509 case ISD::VOR: return "vor";
2510 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002511
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002512 case ISD::SETCC: return "setcc";
2513 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002514 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002515 case ISD::VSELECT: return "vselect";
2516 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2517 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2518 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002519 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002520 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2521 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2522 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2523 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2524 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002525 case ISD::ADDC: return "addc";
2526 case ISD::ADDE: return "adde";
2527 case ISD::SUBC: return "subc";
2528 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002529 case ISD::SHL_PARTS: return "shl_parts";
2530 case ISD::SRA_PARTS: return "sra_parts";
2531 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002532
Chris Lattner7f644642005-04-28 21:44:03 +00002533 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002534 case ISD::SIGN_EXTEND: return "sign_extend";
2535 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002536 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002537 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002538 case ISD::TRUNCATE: return "truncate";
2539 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002540 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002541 case ISD::FP_EXTEND: return "fp_extend";
2542
2543 case ISD::SINT_TO_FP: return "sint_to_fp";
2544 case ISD::UINT_TO_FP: return "uint_to_fp";
2545 case ISD::FP_TO_SINT: return "fp_to_sint";
2546 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002547 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002548
2549 // Control flow instructions
2550 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002551 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002552 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002553 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002554 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002555 case ISD::CALLSEQ_START: return "callseq_start";
2556 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002557
2558 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002559 case ISD::LOAD: return "load";
2560 case ISD::STORE: return "store";
2561 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002562 case ISD::VAARG: return "vaarg";
2563 case ISD::VACOPY: return "vacopy";
2564 case ISD::VAEND: return "vaend";
2565 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002566 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002567 case ISD::EXTRACT_ELEMENT: return "extract_element";
2568 case ISD::BUILD_PAIR: return "build_pair";
2569 case ISD::STACKSAVE: return "stacksave";
2570 case ISD::STACKRESTORE: return "stackrestore";
2571
2572 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002573 case ISD::MEMSET: return "memset";
2574 case ISD::MEMCPY: return "memcpy";
2575 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002576
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002577 // Bit manipulation
2578 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002579 case ISD::CTPOP: return "ctpop";
2580 case ISD::CTTZ: return "cttz";
2581 case ISD::CTLZ: return "ctlz";
2582
Chris Lattner36ce6912005-11-29 06:21:05 +00002583 // Debug info
2584 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002585 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002586 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002587
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002588 case ISD::CONDCODE:
2589 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002590 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002591 case ISD::SETOEQ: return "setoeq";
2592 case ISD::SETOGT: return "setogt";
2593 case ISD::SETOGE: return "setoge";
2594 case ISD::SETOLT: return "setolt";
2595 case ISD::SETOLE: return "setole";
2596 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002597
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002598 case ISD::SETO: return "seto";
2599 case ISD::SETUO: return "setuo";
2600 case ISD::SETUEQ: return "setue";
2601 case ISD::SETUGT: return "setugt";
2602 case ISD::SETUGE: return "setuge";
2603 case ISD::SETULT: return "setult";
2604 case ISD::SETULE: return "setule";
2605 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002606
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002607 case ISD::SETEQ: return "seteq";
2608 case ISD::SETGT: return "setgt";
2609 case ISD::SETGE: return "setge";
2610 case ISD::SETLT: return "setlt";
2611 case ISD::SETLE: return "setle";
2612 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002613 }
2614 }
2615}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002616
Evan Cheng2caccca2006-10-17 21:14:32 +00002617const char *SDNode::getAddressingModeName(ISD::MemOpAddrMode AM) {
2618 switch (AM) {
2619 default:
2620 return "";
2621 case ISD::PRE_INC:
2622 return "<pre-inc>";
2623 case ISD::PRE_DEC:
2624 return "<pre-dec>";
2625 case ISD::POST_INC:
2626 return "<post-inc>";
2627 case ISD::POST_DEC:
2628 return "<post-dec>";
2629 }
2630}
2631
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002632void SDNode::dump() const { dump(0); }
2633void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002634 std::cerr << (void*)this << ": ";
2635
2636 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2637 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002638 if (getValueType(i) == MVT::Other)
2639 std::cerr << "ch";
2640 else
2641 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002642 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002643 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002644
2645 std::cerr << " ";
2646 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2647 if (i) std::cerr << ", ";
2648 std::cerr << (void*)getOperand(i).Val;
2649 if (unsigned RN = getOperand(i).ResNo)
2650 std::cerr << ":" << RN;
2651 }
2652
2653 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2654 std::cerr << "<" << CSDN->getValue() << ">";
2655 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2656 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002657 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002658 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002659 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002660 std::cerr << "<";
2661 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002662 if (offset > 0)
2663 std::cerr << " + " << offset;
2664 else
2665 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002666 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002667 std::cerr << "<" << FIDN->getIndex() << ">";
2668 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002669 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002670 if (CP->isMachineConstantPoolEntry())
2671 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2672 else
2673 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002674 if (offset > 0)
2675 std::cerr << " + " << offset;
2676 else
2677 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002678 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002679 std::cerr << "<";
2680 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2681 if (LBB)
2682 std::cerr << LBB->getName() << " ";
2683 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002684 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002685 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002686 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2687 } else {
2688 std::cerr << " #" << R->getReg();
2689 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002690 } else if (const ExternalSymbolSDNode *ES =
2691 dyn_cast<ExternalSymbolSDNode>(this)) {
2692 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002693 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2694 if (M->getValue())
2695 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2696 else
2697 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002698 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2699 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002700 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2701 bool doExt = true;
2702 switch (LD->getExtensionType()) {
2703 default: doExt = false; break;
2704 case ISD::EXTLOAD:
2705 std::cerr << " <anyext ";
2706 break;
2707 case ISD::SEXTLOAD:
2708 std::cerr << " <sext ";
2709 break;
2710 case ISD::ZEXTLOAD:
2711 std::cerr << " <zext ";
2712 break;
2713 }
2714 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +00002715 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002716
Evan Cheng2caccca2006-10-17 21:14:32 +00002717 const char *AM = getAddressingModeName(LD->getAddressingMode());
2718 if (AM != "")
2719 std::cerr << " " << AM;
2720 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
2721 if (ST->isTruncatingStore())
2722 std::cerr << " <trunc "
2723 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
2724
2725 const char *AM = getAddressingModeName(ST->getAddressingMode());
2726 if (AM != "")
2727 std::cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002728 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002729}
2730
Chris Lattnerde202b32005-11-09 23:47:37 +00002731static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002732 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2733 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002734 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002735 else
2736 std::cerr << "\n" << std::string(indent+2, ' ')
2737 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002738
Chris Lattnerea946cd2005-01-09 20:38:33 +00002739
2740 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002741 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002742}
2743
Chris Lattnerc3aae252005-01-07 07:46:32 +00002744void SelectionDAG::dump() const {
2745 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002746 std::vector<const SDNode*> Nodes;
2747 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2748 I != E; ++I)
2749 Nodes.push_back(I);
2750
Chris Lattner49d24712005-01-09 20:26:36 +00002751 std::sort(Nodes.begin(), Nodes.end());
2752
2753 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002754 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002755 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002756 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002757
Jim Laskey26f7fa72006-10-17 19:33:52 +00002758 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002759
Chris Lattnerc3aae252005-01-07 07:46:32 +00002760 std::cerr << "\n\n";
2761}
2762
Evan Chengd6594ae2006-09-12 21:00:35 +00002763const Type *ConstantPoolSDNode::getType() const {
2764 if (isMachineConstantPoolEntry())
2765 return Val.MachineCPVal->getType();
2766 return Val.ConstVal->getType();
2767}