blob: d2293756bbae1c2696bb3b7a616a69b249f2acf6 [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());
498 }
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000499 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000500 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000501}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000502
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000503
Chris Lattner78ec3112003-08-11 14:57:33 +0000504SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000505 while (!AllNodes.empty()) {
506 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000507 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000508 delete [] N->OperandList;
509 N->OperandList = 0;
510 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000511 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000512 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000513}
514
Chris Lattner0f2287b2005-04-13 02:38:18 +0000515SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000516 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000517 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000518 return getNode(ISD::AND, Op.getValueType(), Op,
519 getConstant(Imm, Op.getValueType()));
520}
521
Chris Lattner36ce6912005-11-29 06:21:05 +0000522SDOperand SelectionDAG::getString(const std::string &Val) {
523 StringSDNode *&N = StringNodes[Val];
524 if (!N) {
525 N = new StringSDNode(Val);
526 AllNodes.push_back(N);
527 }
528 return SDOperand(N, 0);
529}
530
Chris Lattner61b09412006-08-11 21:01:22 +0000531SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000532 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000533 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000534
Chris Lattner61b09412006-08-11 21:01:22 +0000535 // Mask out any bits that are not valid for this constant.
536 Val &= MVT::getIntVTBitMask(VT);
537
538 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000539 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000540 ID.AddInteger(Val);
541 void *IP = 0;
542 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
543 return SDOperand(E, 0);
544 SDNode *N = new ConstantSDNode(isT, Val, VT);
545 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000546 AllNodes.push_back(N);
547 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000548}
549
Chris Lattner61b09412006-08-11 21:01:22 +0000550
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000551SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
552 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000553 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
554 if (VT == MVT::f32)
555 Val = (float)Val; // Mask out extra precision.
556
Chris Lattnerd8658612005-02-17 20:17:32 +0000557 // Do the map lookup using the actual bit pattern for the floating point
558 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
559 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000560 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000561 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000562 ID.AddInteger(DoubleToBits(Val));
563 void *IP = 0;
564 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
565 return SDOperand(E, 0);
566 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
567 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000568 AllNodes.push_back(N);
569 return SDOperand(N, 0);
570}
571
Chris Lattnerc3aae252005-01-07 07:46:32 +0000572SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000573 MVT::ValueType VT, int Offset,
574 bool isTargetGA) {
575 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000576 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000577 ID.AddPointer(GV);
578 ID.AddInteger(Offset);
579 void *IP = 0;
580 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
581 return SDOperand(E, 0);
582 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
583 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000584 AllNodes.push_back(N);
585 return SDOperand(N, 0);
586}
587
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000588SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
589 bool isTarget) {
590 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000591 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000592 ID.AddInteger(FI);
593 void *IP = 0;
594 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
595 return SDOperand(E, 0);
596 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
597 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000598 AllNodes.push_back(N);
599 return SDOperand(N, 0);
600}
601
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000602SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
603 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000604 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000605 ID.AddInteger(JTI);
606 void *IP = 0;
607 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
608 return SDOperand(E, 0);
609 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
610 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000611 AllNodes.push_back(N);
612 return SDOperand(N, 0);
613}
614
Evan Chengb8973bd2006-01-31 22:23:14 +0000615SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000616 unsigned Alignment, int Offset,
617 bool isTarget) {
618 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000619 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000620 ID.AddInteger(Alignment);
621 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000622 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000623 void *IP = 0;
624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
625 return SDOperand(E, 0);
626 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
627 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000628 AllNodes.push_back(N);
629 return SDOperand(N, 0);
630}
631
Chris Lattnerc3aae252005-01-07 07:46:32 +0000632
Evan Chengd6594ae2006-09-12 21:00:35 +0000633SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
634 MVT::ValueType VT,
635 unsigned Alignment, int Offset,
636 bool isTarget) {
637 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
638 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
639 ID.AddInteger(Alignment);
640 ID.AddInteger(Offset);
641 C->AddSelectionDAGCSEId(&ID);
642 void *IP = 0;
643 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
644 return SDOperand(E, 0);
645 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
646 CSEMap.InsertNode(N, IP);
647 AllNodes.push_back(N);
648 return SDOperand(N, 0);
649}
650
651
Chris Lattnerc3aae252005-01-07 07:46:32 +0000652SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000653 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000654 ID.AddPointer(MBB);
655 void *IP = 0;
656 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
657 return SDOperand(E, 0);
658 SDNode *N = new BasicBlockSDNode(MBB);
659 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000660 AllNodes.push_back(N);
661 return SDOperand(N, 0);
662}
663
Chris Lattner15e4b012005-07-10 00:07:11 +0000664SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
665 if ((unsigned)VT >= ValueTypeNodes.size())
666 ValueTypeNodes.resize(VT+1);
667 if (ValueTypeNodes[VT] == 0) {
668 ValueTypeNodes[VT] = new VTSDNode(VT);
669 AllNodes.push_back(ValueTypeNodes[VT]);
670 }
671
672 return SDOperand(ValueTypeNodes[VT], 0);
673}
674
Chris Lattnerc3aae252005-01-07 07:46:32 +0000675SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
676 SDNode *&N = ExternalSymbols[Sym];
677 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000678 N = new ExternalSymbolSDNode(false, Sym, VT);
679 AllNodes.push_back(N);
680 return SDOperand(N, 0);
681}
682
Chris Lattner809ec112006-01-28 10:09:25 +0000683SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
684 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000685 SDNode *&N = TargetExternalSymbols[Sym];
686 if (N) return SDOperand(N, 0);
687 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000688 AllNodes.push_back(N);
689 return SDOperand(N, 0);
690}
691
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000692SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
693 if ((unsigned)Cond >= CondCodeNodes.size())
694 CondCodeNodes.resize(Cond+1);
695
Chris Lattner079a27a2005-08-09 20:40:02 +0000696 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000697 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000698 AllNodes.push_back(CondCodeNodes[Cond]);
699 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000700 return SDOperand(CondCodeNodes[Cond], 0);
701}
702
Chris Lattner0fdd7682005-08-30 22:38:38 +0000703SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000704 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000705 ID.AddInteger(RegNo);
706 void *IP = 0;
707 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
708 return SDOperand(E, 0);
709 SDNode *N = new RegisterSDNode(RegNo, VT);
710 CSEMap.InsertNode(N, IP);
711 AllNodes.push_back(N);
712 return SDOperand(N, 0);
713}
714
715SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
716 assert((!V || isa<PointerType>(V->getType())) &&
717 "SrcValue is not a pointer?");
718
Chris Lattner0b3e5252006-08-15 19:11:05 +0000719 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000720 ID.AddPointer(V);
721 ID.AddInteger(Offset);
722 void *IP = 0;
723 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
724 return SDOperand(E, 0);
725 SDNode *N = new SrcValueSDNode(V, Offset);
726 CSEMap.InsertNode(N, IP);
727 AllNodes.push_back(N);
728 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000729}
730
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000731SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
732 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 // These setcc operations always fold.
734 switch (Cond) {
735 default: break;
736 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000737 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000738 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000739 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000740
741 case ISD::SETOEQ:
742 case ISD::SETOGT:
743 case ISD::SETOGE:
744 case ISD::SETOLT:
745 case ISD::SETOLE:
746 case ISD::SETONE:
747 case ISD::SETO:
748 case ISD::SETUO:
749 case ISD::SETUEQ:
750 case ISD::SETUNE:
751 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
752 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000753 }
754
Chris Lattner67255a12005-04-07 18:14:58 +0000755 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
756 uint64_t C2 = N2C->getValue();
757 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
758 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000759
Chris Lattnerc3aae252005-01-07 07:46:32 +0000760 // Sign extend the operands if required
761 if (ISD::isSignedIntSetCC(Cond)) {
762 C1 = N1C->getSignExtended();
763 C2 = N2C->getSignExtended();
764 }
765
766 switch (Cond) {
767 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000768 case ISD::SETEQ: return getConstant(C1 == C2, VT);
769 case ISD::SETNE: return getConstant(C1 != C2, VT);
770 case ISD::SETULT: return getConstant(C1 < C2, VT);
771 case ISD::SETUGT: return getConstant(C1 > C2, VT);
772 case ISD::SETULE: return getConstant(C1 <= C2, VT);
773 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
774 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
775 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
776 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
777 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000778 }
Chris Lattner24673922005-04-07 18:58:54 +0000779 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000780 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000781 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
782 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
783
784 // If the comparison constant has bits in the upper part, the
785 // zero-extended value could never match.
786 if (C2 & (~0ULL << InSize)) {
787 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
788 switch (Cond) {
789 case ISD::SETUGT:
790 case ISD::SETUGE:
791 case ISD::SETEQ: return getConstant(0, VT);
792 case ISD::SETULT:
793 case ISD::SETULE:
794 case ISD::SETNE: return getConstant(1, VT);
795 case ISD::SETGT:
796 case ISD::SETGE:
797 // True if the sign bit of C2 is set.
798 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
799 case ISD::SETLT:
800 case ISD::SETLE:
801 // True if the sign bit of C2 isn't set.
802 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
803 default:
804 break;
805 }
806 }
807
808 // Otherwise, we can perform the comparison with the low bits.
809 switch (Cond) {
810 case ISD::SETEQ:
811 case ISD::SETNE:
812 case ISD::SETUGT:
813 case ISD::SETUGE:
814 case ISD::SETULT:
815 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000816 return getSetCC(VT, N1.getOperand(0),
817 getConstant(C2, N1.getOperand(0).getValueType()),
818 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000819 default:
820 break; // todo, be more careful with signed comparisons
821 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000822 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
823 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
824 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
825 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
826 MVT::ValueType ExtDstTy = N1.getValueType();
827 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000828
Chris Lattner7b2880c2005-08-24 22:44:39 +0000829 // If the extended part has any inconsistent bits, it cannot ever
830 // compare equal. In other words, they have to be all ones or all
831 // zeros.
832 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000833 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000834 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
835 return getConstant(Cond == ISD::SETNE, VT);
836
837 // Otherwise, make this a use of a zext.
838 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000839 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000840 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000841 }
842
Chris Lattner67255a12005-04-07 18:14:58 +0000843 uint64_t MinVal, MaxVal;
844 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
845 if (ISD::isSignedIntSetCC(Cond)) {
846 MinVal = 1ULL << (OperandBitSize-1);
847 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
848 MaxVal = ~0ULL >> (65-OperandBitSize);
849 else
850 MaxVal = 0;
851 } else {
852 MinVal = 0;
853 MaxVal = ~0ULL >> (64-OperandBitSize);
854 }
855
856 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
857 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
858 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
859 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000860 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
861 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000862 }
863
864 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
865 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
866 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000867 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
868 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000869 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000870
Nate Begeman72ea2812005-04-14 08:56:52 +0000871 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
872 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Nate Begeman72ea2812005-04-14 08:56:52 +0000874 // Canonicalize setgt X, Min --> setne X, Min
875 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000877
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000878 // If we have setult X, 1, turn it into seteq X, 0
879 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
881 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000882 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000883 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000884 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
885 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000886
887 // If we have "setcc X, C1", check to see if we can shrink the immediate
888 // by changing cc.
889
890 // SETUGT X, SINTMAX -> SETLT X, 0
891 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
892 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000893 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000894
895 // FIXME: Implement the rest of these.
896
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000897
898 // Fold bit comparisons when we can.
899 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
900 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
901 if (ConstantSDNode *AndRHS =
902 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
903 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
904 // Perform the xform if the AND RHS is a single bit.
905 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
906 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000907 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000908 TLI.getShiftAmountTy()));
909 }
910 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
911 // (X & 8) == 8 --> (X & 8) >> 3
912 // Perform the xform if C2 is a single bit.
913 if ((C2 & (C2-1)) == 0) {
914 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000915 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000916 }
917 }
918 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919 }
Chris Lattner67255a12005-04-07 18:14:58 +0000920 } else if (isa<ConstantSDNode>(N1.Val)) {
921 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000922 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000923 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000924
925 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
926 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
927 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000928
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 switch (Cond) {
930 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000931 case ISD::SETEQ: return getConstant(C1 == C2, VT);
932 case ISD::SETNE: return getConstant(C1 != C2, VT);
933 case ISD::SETLT: return getConstant(C1 < C2, VT);
934 case ISD::SETGT: return getConstant(C1 > C2, VT);
935 case ISD::SETLE: return getConstant(C1 <= C2, VT);
936 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 }
938 } else {
939 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000940 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 }
942
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000943 // Could not fold it.
944 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945}
946
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000948///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000950 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000951 void *IP = 0;
952 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
953 return SDOperand(E, 0);
954 SDNode *N = new SDNode(Opcode, VT);
955 CSEMap.InsertNode(N, IP);
956
957 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 return SDOperand(N, 0);
959}
960
Chris Lattnerc3aae252005-01-07 07:46:32 +0000961SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
962 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000963 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000964 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
966 uint64_t Val = C->getValue();
967 switch (Opcode) {
968 default: break;
969 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000970 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
972 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000973 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
974 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000975 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000976 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000977 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000978 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000979 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000980 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000981 case ISD::BSWAP:
982 switch(VT) {
983 default: assert(0 && "Invalid bswap!"); break;
984 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
985 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
986 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
987 }
988 break;
989 case ISD::CTPOP:
990 switch(VT) {
991 default: assert(0 && "Invalid ctpop!"); break;
992 case MVT::i1: return getConstant(Val != 0, VT);
993 case MVT::i8:
994 Tmp1 = (unsigned)Val & 0xFF;
995 return getConstant(CountPopulation_32(Tmp1), VT);
996 case MVT::i16:
997 Tmp1 = (unsigned)Val & 0xFFFF;
998 return getConstant(CountPopulation_32(Tmp1), VT);
999 case MVT::i32:
1000 return getConstant(CountPopulation_32((unsigned)Val), VT);
1001 case MVT::i64:
1002 return getConstant(CountPopulation_64(Val), VT);
1003 }
1004 case ISD::CTLZ:
1005 switch(VT) {
1006 default: assert(0 && "Invalid ctlz!"); break;
1007 case MVT::i1: return getConstant(Val == 0, VT);
1008 case MVT::i8:
1009 Tmp1 = (unsigned)Val & 0xFF;
1010 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1011 case MVT::i16:
1012 Tmp1 = (unsigned)Val & 0xFFFF;
1013 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1014 case MVT::i32:
1015 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1016 case MVT::i64:
1017 return getConstant(CountLeadingZeros_64(Val), VT);
1018 }
1019 case ISD::CTTZ:
1020 switch(VT) {
1021 default: assert(0 && "Invalid cttz!"); break;
1022 case MVT::i1: return getConstant(Val == 0, VT);
1023 case MVT::i8:
1024 Tmp1 = (unsigned)Val | 0x100;
1025 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1026 case MVT::i16:
1027 Tmp1 = (unsigned)Val | 0x10000;
1028 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1029 case MVT::i32:
1030 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1031 case MVT::i64:
1032 return getConstant(CountTrailingZeros_64(Val), VT);
1033 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034 }
1035 }
1036
Chris Lattner94683772005-12-23 05:30:37 +00001037 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1039 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001040 case ISD::FNEG:
1041 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001042 case ISD::FABS:
1043 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 case ISD::FP_ROUND:
1045 case ISD::FP_EXTEND:
1046 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001047 case ISD::FP_TO_SINT:
1048 return getConstant((int64_t)C->getValue(), VT);
1049 case ISD::FP_TO_UINT:
1050 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001051 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001052 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001053 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001054 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001055 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001056 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 }
1058
1059 unsigned OpOpcode = Operand.Val->getOpcode();
1060 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001061 case ISD::TokenFactor:
1062 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063 case ISD::SIGN_EXTEND:
1064 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001065 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1067 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1068 break;
1069 case ISD::ZERO_EXTEND:
1070 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001071 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001072 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001073 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001075 case ISD::ANY_EXTEND:
1076 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001077 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001078 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1079 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1080 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1081 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 case ISD::TRUNCATE:
1083 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001084 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001085 if (OpOpcode == ISD::TRUNCATE)
1086 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001087 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1088 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001089 // If the source is smaller than the dest, we still need an extend.
1090 if (Operand.Val->getOperand(0).getValueType() < VT)
1091 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1092 else if (Operand.Val->getOperand(0).getValueType() > VT)
1093 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1094 else
1095 return Operand.Val->getOperand(0);
1096 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 break;
Chris Lattner35481892005-12-23 00:16:34 +00001098 case ISD::BIT_CONVERT:
1099 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001100 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001101 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001102 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001103 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1104 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001105 if (OpOpcode == ISD::UNDEF)
1106 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001107 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001108 case ISD::SCALAR_TO_VECTOR:
1109 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1110 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1111 "Illegal SCALAR_TO_VECTOR node!");
1112 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001113 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001114 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1115 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001116 Operand.Val->getOperand(0));
1117 if (OpOpcode == ISD::FNEG) // --X -> X
1118 return Operand.Val->getOperand(0);
1119 break;
1120 case ISD::FABS:
1121 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1122 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1123 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 }
1125
Chris Lattner43247a12005-08-25 19:12:10 +00001126 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001127 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001128 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001129 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1130 void *IP = 0;
1131 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1132 return SDOperand(E, 0);
1133 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001134 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001135 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001136 } else {
1137 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001138 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001139 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 AllNodes.push_back(N);
1141 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001142}
1143
Chris Lattner36019aa2005-04-18 03:48:41 +00001144
1145
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1147 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001148#ifndef NDEBUG
1149 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001150 case ISD::TokenFactor:
1151 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1152 N2.getValueType() == MVT::Other && "Invalid token factor!");
1153 break;
Chris Lattner76365122005-01-16 02:23:22 +00001154 case ISD::AND:
1155 case ISD::OR:
1156 case ISD::XOR:
1157 case ISD::UDIV:
1158 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001159 case ISD::MULHU:
1160 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001161 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1162 // fall through
1163 case ISD::ADD:
1164 case ISD::SUB:
1165 case ISD::MUL:
1166 case ISD::SDIV:
1167 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001168 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1169 // fall through.
1170 case ISD::FADD:
1171 case ISD::FSUB:
1172 case ISD::FMUL:
1173 case ISD::FDIV:
1174 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001175 assert(N1.getValueType() == N2.getValueType() &&
1176 N1.getValueType() == VT && "Binary operator types must match!");
1177 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001178 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1179 assert(N1.getValueType() == VT &&
1180 MVT::isFloatingPoint(N1.getValueType()) &&
1181 MVT::isFloatingPoint(N2.getValueType()) &&
1182 "Invalid FCOPYSIGN!");
1183 break;
Chris Lattner76365122005-01-16 02:23:22 +00001184 case ISD::SHL:
1185 case ISD::SRA:
1186 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001187 case ISD::ROTL:
1188 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001189 assert(VT == N1.getValueType() &&
1190 "Shift operators return type must be the same as their first arg");
1191 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001192 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001193 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001194 case ISD::FP_ROUND_INREG: {
1195 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1196 assert(VT == N1.getValueType() && "Not an inreg round!");
1197 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1198 "Cannot FP_ROUND_INREG integer types");
1199 assert(EVT <= VT && "Not rounding down!");
1200 break;
1201 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001202 case ISD::AssertSext:
1203 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001204 case ISD::SIGN_EXTEND_INREG: {
1205 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1206 assert(VT == N1.getValueType() && "Not an inreg extend!");
1207 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1208 "Cannot *_EXTEND_INREG FP types");
1209 assert(EVT <= VT && "Not extending!");
1210 }
1211
Chris Lattner76365122005-01-16 02:23:22 +00001212 default: break;
1213 }
1214#endif
1215
Chris Lattnerc3aae252005-01-07 07:46:32 +00001216 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1217 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1218 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001219 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1220 int64_t Val = N1C->getValue();
1221 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1222 Val <<= 64-FromBits;
1223 Val >>= 64-FromBits;
1224 return getConstant(Val, VT);
1225 }
1226
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 if (N2C) {
1228 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1229 switch (Opcode) {
1230 case ISD::ADD: return getConstant(C1 + C2, VT);
1231 case ISD::SUB: return getConstant(C1 - C2, VT);
1232 case ISD::MUL: return getConstant(C1 * C2, VT);
1233 case ISD::UDIV:
1234 if (C2) return getConstant(C1 / C2, VT);
1235 break;
1236 case ISD::UREM :
1237 if (C2) return getConstant(C1 % C2, VT);
1238 break;
1239 case ISD::SDIV :
1240 if (C2) return getConstant(N1C->getSignExtended() /
1241 N2C->getSignExtended(), VT);
1242 break;
1243 case ISD::SREM :
1244 if (C2) return getConstant(N1C->getSignExtended() %
1245 N2C->getSignExtended(), VT);
1246 break;
1247 case ISD::AND : return getConstant(C1 & C2, VT);
1248 case ISD::OR : return getConstant(C1 | C2, VT);
1249 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001250 case ISD::SHL : return getConstant(C1 << C2, VT);
1251 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001252 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001253 case ISD::ROTL :
1254 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1255 VT);
1256 case ISD::ROTR :
1257 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1258 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 default: break;
1260 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 } else { // Cannonicalize constant to RHS if commutative
1262 if (isCommutativeBinOp(Opcode)) {
1263 std::swap(N1C, N2C);
1264 std::swap(N1, N2);
1265 }
1266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 }
1268
1269 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1270 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001271 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001272 if (N2CFP) {
1273 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1274 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001275 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1276 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1277 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1278 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279 if (C2) return getConstantFP(C1 / C2, VT);
1280 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001281 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001282 if (C2) return getConstantFP(fmod(C1, C2), VT);
1283 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001284 case ISD::FCOPYSIGN: {
1285 union {
1286 double F;
1287 uint64_t I;
1288 } u1;
1289 union {
1290 double F;
1291 int64_t I;
1292 } u2;
1293 u1.F = C1;
1294 u2.F = C2;
1295 if (u2.I < 0) // Sign bit of RHS set?
1296 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1297 else
1298 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1299 return getConstantFP(u1.F, VT);
1300 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 default: break;
1302 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001303 } else { // Cannonicalize constant to RHS if commutative
1304 if (isCommutativeBinOp(Opcode)) {
1305 std::swap(N1CFP, N2CFP);
1306 std::swap(N1, N2);
1307 }
1308 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001309 }
Chris Lattner62b57722006-04-20 05:39:12 +00001310
1311 // Canonicalize an UNDEF to the RHS, even over a constant.
1312 if (N1.getOpcode() == ISD::UNDEF) {
1313 if (isCommutativeBinOp(Opcode)) {
1314 std::swap(N1, N2);
1315 } else {
1316 switch (Opcode) {
1317 case ISD::FP_ROUND_INREG:
1318 case ISD::SIGN_EXTEND_INREG:
1319 case ISD::SUB:
1320 case ISD::FSUB:
1321 case ISD::FDIV:
1322 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001323 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001324 return N1; // fold op(undef, arg2) -> undef
1325 case ISD::UDIV:
1326 case ISD::SDIV:
1327 case ISD::UREM:
1328 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001329 case ISD::SRL:
1330 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001331 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1332 }
1333 }
1334 }
1335
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001336 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001337 if (N2.getOpcode() == ISD::UNDEF) {
1338 switch (Opcode) {
1339 case ISD::ADD:
1340 case ISD::SUB:
1341 case ISD::FADD:
1342 case ISD::FSUB:
1343 case ISD::FMUL:
1344 case ISD::FDIV:
1345 case ISD::FREM:
1346 case ISD::UDIV:
1347 case ISD::SDIV:
1348 case ISD::UREM:
1349 case ISD::SREM:
1350 case ISD::XOR:
1351 return N2; // fold op(arg1, undef) -> undef
1352 case ISD::MUL:
1353 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001354 case ISD::SRL:
1355 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001356 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1357 case ISD::OR:
1358 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001359 case ISD::SRA:
1360 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001361 }
1362 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001363
Chris Lattnerc3aae252005-01-07 07:46:32 +00001364 // Finally, fold operations that do not require constants.
1365 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001366 case ISD::FP_ROUND_INREG:
1367 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1368 break;
1369 case ISD::SIGN_EXTEND_INREG: {
1370 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1371 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001372 break;
1373 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001374 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001375 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1376
Chris Lattner5c6621c2006-09-19 04:51:23 +00001377 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1378 // 64-bit integers into 32-bit parts. Instead of building the extract of
1379 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001380 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001381 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001382
1383 // EXTRACT_ELEMENT of a constant int is also very common.
1384 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1385 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1386 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001387 }
1388 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001389
Nate Begemaneea805e2005-04-13 21:23:31 +00001390 // FIXME: figure out how to safely handle things like
1391 // int foo(int x) { return 1 << (x & 255); }
1392 // int bar() { return foo(256); }
1393#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001394 case ISD::SHL:
1395 case ISD::SRL:
1396 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001397 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001398 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001399 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001400 else if (N2.getOpcode() == ISD::AND)
1401 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1402 // If the and is only masking out bits that cannot effect the shift,
1403 // eliminate the and.
1404 unsigned NumBits = MVT::getSizeInBits(VT);
1405 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1406 return getNode(Opcode, VT, N1, N2.getOperand(0));
1407 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001408 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001409#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001410 }
1411
Chris Lattner27e9b412005-05-11 18:57:39 +00001412 // Memoize this node if possible.
1413 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001414 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001415 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001416 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1417 void *IP = 0;
1418 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1419 return SDOperand(E, 0);
1420 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001421 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001422 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001423 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001424 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001425 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001426 }
1427
Chris Lattnerc3aae252005-01-07 07:46:32 +00001428 AllNodes.push_back(N);
1429 return SDOperand(N, 0);
1430}
1431
Chris Lattnerc3aae252005-01-07 07:46:32 +00001432SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1433 SDOperand N1, SDOperand N2, SDOperand N3) {
1434 // Perform various simplifications.
1435 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1436 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001437 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001438 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001439 case ISD::SETCC: {
1440 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001441 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001442 if (Simp.Val) return Simp;
1443 break;
1444 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001445 case ISD::SELECT:
1446 if (N1C)
1447 if (N1C->getValue())
1448 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001449 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001450 return N3; // select false, X, Y -> Y
1451
1452 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001454 case ISD::BRCOND:
1455 if (N2C)
1456 if (N2C->getValue()) // Unconditional branch
1457 return getNode(ISD::BR, MVT::Other, N1, N3);
1458 else
1459 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001460 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001461 case ISD::VECTOR_SHUFFLE:
1462 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1463 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1464 N3.getOpcode() == ISD::BUILD_VECTOR &&
1465 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1466 "Illegal VECTOR_SHUFFLE node!");
1467 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001468 }
1469
Chris Lattner43247a12005-08-25 19:12:10 +00001470 // Memoize node if it doesn't produce a flag.
1471 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001472 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001473 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001474 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1475 void *IP = 0;
1476 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1477 return SDOperand(E, 0);
1478 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001479 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001480 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001481 } else {
1482 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001483 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001484 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001485 AllNodes.push_back(N);
1486 return SDOperand(N, 0);
1487}
1488
1489SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001490 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001491 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001492 SDOperand Ops[] = { N1, N2, N3, N4 };
1493 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001494}
1495
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001496SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1497 SDOperand N1, SDOperand N2, SDOperand N3,
1498 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001499 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1500 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001501}
1502
Evan Cheng7038daf2005-12-10 00:37:58 +00001503SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1504 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001505 const Value *SV, int SVOffset,
1506 bool isVolatile) {
1507 // FIXME: Alignment == 1 for now.
1508 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001509 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng466685d2006-10-09 20:57:25 +00001510 SDOperand Undef = getNode(ISD::UNDEF, VT);
1511 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1512 ID.AddInteger(ISD::UNINDEXED);
1513 ID.AddInteger(ISD::NON_EXTLOAD);
1514 ID.AddInteger(VT);
1515 ID.AddPointer(SV);
1516 ID.AddInteger(SVOffset);
1517 ID.AddInteger(Alignment);
1518 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001519 void *IP = 0;
1520 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1521 return SDOperand(E, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00001522 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::NON_EXTLOAD, VT,
1523 SV, SVOffset, Alignment, isVolatile);
1524 N->setValueTypes(VTs);
1525 CSEMap.InsertNode(N, IP);
1526 AllNodes.push_back(N);
1527 return SDOperand(N, 0);
1528}
1529
1530SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1531 SDOperand Chain, SDOperand Ptr, const Value *SV,
1532 int SVOffset, MVT::ValueType EVT,
1533 bool isVolatile) {
1534 // If they are asking for an extending load from/to the same thing, return a
1535 // normal load.
1536 if (VT == EVT)
1537 ExtType = ISD::NON_EXTLOAD;
1538
1539 if (MVT::isVector(VT))
1540 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1541 else
1542 assert(EVT < VT && "Should only be an extending load, not truncating!");
1543 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1544 "Cannot sign/zero extend a FP/Vector load!");
1545 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1546 "Cannot convert from FP to Int or Int -> FP!");
1547
1548 // FIXME: Alignment == 1 for now.
1549 unsigned Alignment = 1;
1550 SDVTList VTs = getVTList(VT, MVT::Other);
1551 SDOperand Undef = getNode(ISD::UNDEF, VT);
1552 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1553 ID.AddInteger(ISD::UNINDEXED);
1554 ID.AddInteger(ExtType);
1555 ID.AddInteger(EVT);
1556 ID.AddPointer(SV);
1557 ID.AddInteger(SVOffset);
1558 ID.AddInteger(Alignment);
1559 ID.AddInteger(isVolatile);
1560 void *IP = 0;
1561 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1562 return SDOperand(E, 0);
1563 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ExtType, EVT, SV, SVOffset,
1564 Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001565 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001566 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001567 AllNodes.push_back(N);
1568 return SDOperand(N, 0);
1569}
1570
1571SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1572 SDOperand Chain, SDOperand Ptr,
1573 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001574 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1575 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001576 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001577}
1578
Evan Chengad071e12006-10-05 22:57:11 +00001579SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1580 SDOperand Ptr, SDOperand SV) {
1581 SDVTList VTs = getVTList(MVT::Other);
1582 SDOperand Ops[] = { Chain, Value, Ptr, SV };
1583 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1584 void *IP = 0;
1585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1586 return SDOperand(E, 0);
1587 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV);
1588 N->setValueTypes(VTs);
1589 CSEMap.InsertNode(N, IP);
1590 AllNodes.push_back(N);
1591 return SDOperand(N, 0);
1592}
1593
Nate Begemanacc398c2006-01-25 18:21:52 +00001594SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1595 SDOperand Chain, SDOperand Ptr,
1596 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001597 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001598 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001599}
1600
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001601SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001602 const SDOperand *Ops, unsigned NumOps) {
1603 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001604 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001605 case 1: return getNode(Opcode, VT, Ops[0]);
1606 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1607 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001608 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001609 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001610
Chris Lattneref847df2005-04-09 03:27:28 +00001611 switch (Opcode) {
1612 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001613 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001614 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001615 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1616#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1617 // If this is a truncating store of a constant, convert to the desired type
1618 // and store it instead.
1619 if (isa<Constant>(Ops[0])) {
1620 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1621 if (isa<Constant>(Op))
1622 N1 = Op;
1623 }
1624 // Also for ConstantFP?
1625#endif
1626 if (Ops[0].getValueType() == EVT) // Normal store?
Evan Chengad071e12006-10-05 22:57:11 +00001627 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001628 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1629 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1630 "Can't do FP-INT conversion!");
1631 break;
1632 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001633 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001634 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001635 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1636 "LHS and RHS of condition must have same type!");
1637 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1638 "True and False arms of SelectCC must have same type!");
1639 assert(Ops[2].getValueType() == VT &&
1640 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001641 break;
1642 }
1643 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001644 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001645 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1646 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001647 break;
1648 }
Chris Lattneref847df2005-04-09 03:27:28 +00001649 }
1650
Chris Lattner385328c2005-05-14 07:42:29 +00001651 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001652 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001653 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001654 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001655 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001656 void *IP = 0;
1657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1658 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001659 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001660 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001661 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001662 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001663 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001664 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001665 }
Chris Lattneref847df2005-04-09 03:27:28 +00001666 AllNodes.push_back(N);
1667 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001668}
1669
Chris Lattner89c34632005-05-14 06:20:26 +00001670SDOperand SelectionDAG::getNode(unsigned Opcode,
1671 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001672 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001673 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1674 Ops, NumOps);
1675}
1676
1677SDOperand SelectionDAG::getNode(unsigned Opcode,
1678 const MVT::ValueType *VTs, unsigned NumVTs,
1679 const SDOperand *Ops, unsigned NumOps) {
1680 if (NumVTs == 1)
1681 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001682 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1683}
1684
1685SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1686 const SDOperand *Ops, unsigned NumOps) {
1687 if (VTList.NumVTs == 1)
1688 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001689
Chris Lattner5f056bf2005-07-10 01:55:33 +00001690 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001691 // FIXME: figure out how to safely handle things like
1692 // int foo(int x) { return 1 << (x & 255); }
1693 // int bar() { return foo(256); }
1694#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001695 case ISD::SRA_PARTS:
1696 case ISD::SRL_PARTS:
1697 case ISD::SHL_PARTS:
1698 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001699 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001700 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1701 else if (N3.getOpcode() == ISD::AND)
1702 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1703 // If the and is only masking out bits that cannot effect the shift,
1704 // eliminate the and.
1705 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1706 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1707 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1708 }
1709 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001710#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001711 }
Chris Lattner89c34632005-05-14 06:20:26 +00001712
Chris Lattner43247a12005-08-25 19:12:10 +00001713 // Memoize the node unless it returns a flag.
1714 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001715 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001716 SelectionDAGCSEMap::NodeID ID;
1717 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001718 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001719 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001720 void *IP = 0;
1721 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1722 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001723 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001724 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001725 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001726 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001727 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001728 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001729 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001730 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001731 return SDOperand(N, 0);
1732}
1733
Chris Lattner70046e92006-08-15 17:46:01 +00001734SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1735 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001736}
1737
Chris Lattner70046e92006-08-15 17:46:01 +00001738SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001739 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1740 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001741 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001742 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001743 }
1744 std::vector<MVT::ValueType> V;
1745 V.push_back(VT1);
1746 V.push_back(VT2);
1747 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001748 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001749}
Chris Lattner70046e92006-08-15 17:46:01 +00001750SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1751 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001752 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001753 E = VTList.end(); I != E; ++I) {
1754 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1755 (*I)[2] == VT3)
1756 return makeVTList(&(*I)[0], 3);
1757 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001758 std::vector<MVT::ValueType> V;
1759 V.push_back(VT1);
1760 V.push_back(VT2);
1761 V.push_back(VT3);
1762 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001763 return makeVTList(&(*VTList.begin())[0], 3);
1764}
1765
1766SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1767 switch (NumVTs) {
1768 case 0: assert(0 && "Cannot have nodes without results!");
1769 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1770 case 2: return getVTList(VTs[0], VTs[1]);
1771 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1772 default: break;
1773 }
1774
1775 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1776 E = VTList.end(); I != E; ++I) {
1777 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1778
1779 bool NoMatch = false;
1780 for (unsigned i = 2; i != NumVTs; ++i)
1781 if (VTs[i] != (*I)[i]) {
1782 NoMatch = true;
1783 break;
1784 }
1785 if (!NoMatch)
1786 return makeVTList(&*I->begin(), NumVTs);
1787 }
1788
1789 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1790 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001791}
1792
1793
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001794/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1795/// specified operands. If the resultant node already exists in the DAG,
1796/// this does not modify the specified node, instead it returns the node that
1797/// already exists. If the resultant node does not exist in the DAG, the
1798/// input node is returned. As a degenerate case, if you specify the same
1799/// input operands as the node already has, the input node is returned.
1800SDOperand SelectionDAG::
1801UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1802 SDNode *N = InN.Val;
1803 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1804
1805 // Check to see if there is no change.
1806 if (Op == N->getOperand(0)) return InN;
1807
1808 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001809 void *InsertPos = 0;
1810 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1811 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001812
1813 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001814 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001815 RemoveNodeFromCSEMaps(N);
1816
1817 // Now we update the operands.
1818 N->OperandList[0].Val->removeUser(N);
1819 Op.Val->addUser(N);
1820 N->OperandList[0] = Op;
1821
1822 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001823 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001824 return InN;
1825}
1826
1827SDOperand SelectionDAG::
1828UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1829 SDNode *N = InN.Val;
1830 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1831
1832 // Check to see if there is no change.
1833 bool AnyChange = false;
1834 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1835 return InN; // No operands changed, just return the input node.
1836
1837 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001838 void *InsertPos = 0;
1839 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1840 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001841
1842 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001843 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001844 RemoveNodeFromCSEMaps(N);
1845
1846 // Now we update the operands.
1847 if (N->OperandList[0] != Op1) {
1848 N->OperandList[0].Val->removeUser(N);
1849 Op1.Val->addUser(N);
1850 N->OperandList[0] = Op1;
1851 }
1852 if (N->OperandList[1] != Op2) {
1853 N->OperandList[1].Val->removeUser(N);
1854 Op2.Val->addUser(N);
1855 N->OperandList[1] = Op2;
1856 }
1857
1858 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001859 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001860 return InN;
1861}
1862
1863SDOperand SelectionDAG::
1864UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001865 SDOperand Ops[] = { Op1, Op2, Op3 };
1866 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001867}
1868
1869SDOperand SelectionDAG::
1870UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1871 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001872 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1873 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001874}
1875
1876SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001877UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1878 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001879 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1880 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001881}
1882
1883
1884SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001885UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001886 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001887 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001888 "Update with wrong number of operands");
1889
1890 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001891 bool AnyChange = false;
1892 for (unsigned i = 0; i != NumOps; ++i) {
1893 if (Ops[i] != N->getOperand(i)) {
1894 AnyChange = true;
1895 break;
1896 }
1897 }
1898
1899 // No operands changed, just return the input node.
1900 if (!AnyChange) return InN;
1901
1902 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001903 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001904 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001905 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001906
1907 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001908 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001909 RemoveNodeFromCSEMaps(N);
1910
1911 // Now we update the operands.
1912 for (unsigned i = 0; i != NumOps; ++i) {
1913 if (N->OperandList[i] != Ops[i]) {
1914 N->OperandList[i].Val->removeUser(N);
1915 Ops[i].Val->addUser(N);
1916 N->OperandList[i] = Ops[i];
1917 }
1918 }
1919
1920 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001921 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001922 return InN;
1923}
1924
1925
1926
Chris Lattner149c58c2005-08-16 18:17:10 +00001927
1928/// SelectNodeTo - These are used for target selectors to *mutate* the
1929/// specified node to have the specified return type, Target opcode, and
1930/// operands. Note that target opcodes are stored as
1931/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001932///
1933/// Note that SelectNodeTo returns the resultant node. If there is already a
1934/// node of the specified opcode and operands, it returns that node instead of
1935/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00001936SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1937 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001938 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001939 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1940 void *IP = 0;
1941 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001942 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00001943
Chris Lattner7651fa42005-08-24 23:00:29 +00001944 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001945
Chris Lattner7651fa42005-08-24 23:00:29 +00001946 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001947 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001948
Chris Lattner4a283e92006-08-11 18:38:11 +00001949 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001950 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00001951}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001952
Evan Cheng95514ba2006-08-26 08:00:10 +00001953SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1954 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001955 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001956 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001957 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1958 void *IP = 0;
1959 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001960 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001961
Chris Lattner149c58c2005-08-16 18:17:10 +00001962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001964 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001965 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001966 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001967 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001968}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001969
Evan Cheng95514ba2006-08-26 08:00:10 +00001970SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1971 MVT::ValueType VT, SDOperand Op1,
1972 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001973 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001974 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001975 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1976 void *IP = 0;
1977 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001978 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001979
Chris Lattner149c58c2005-08-16 18:17:10 +00001980 RemoveNodeFromCSEMaps(N);
1981 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001982 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001983 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001984
Chris Lattnera5682852006-08-07 23:03:03 +00001985 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001986 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001987}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001988
Evan Cheng95514ba2006-08-26 08:00:10 +00001989SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1990 MVT::ValueType VT, SDOperand Op1,
1991 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001992 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001993 SDVTList VTs = getVTList(VT);
1994 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1995 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001996 void *IP = 0;
1997 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001998 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001999
Chris Lattner149c58c2005-08-16 18:17:10 +00002000 RemoveNodeFromCSEMaps(N);
2001 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002002 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002003 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002004
Chris Lattnera5682852006-08-07 23:03:03 +00002005 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002006 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002007}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002008
Evan Cheng95514ba2006-08-26 08:00:10 +00002009SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002010 MVT::ValueType VT, const SDOperand *Ops,
2011 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002012 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002013 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00002014 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00002015 for (unsigned i = 0; i != NumOps; ++i)
2016 ID.AddOperand(Ops[i]);
Chris Lattnera5682852006-08-07 23:03:03 +00002017 void *IP = 0;
2018 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002019 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002020
Chris Lattner6b09a292005-08-21 18:49:33 +00002021 RemoveNodeFromCSEMaps(N);
2022 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002023 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00002024 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002025
Chris Lattnera5682852006-08-07 23:03:03 +00002026 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002027 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002028}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002029
Evan Cheng95514ba2006-08-26 08:00:10 +00002030SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2031 MVT::ValueType VT1, MVT::ValueType VT2,
2032 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002033 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002034 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2035 void *IP = 0;
2036 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002037 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002038
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039 RemoveNodeFromCSEMaps(N);
2040 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002041 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002042 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002043
Chris Lattnera5682852006-08-07 23:03:03 +00002044 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002045 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002046}
2047
Evan Cheng95514ba2006-08-26 08:00:10 +00002048SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2049 MVT::ValueType VT1, MVT::ValueType VT2,
2050 SDOperand Op1, SDOperand Op2,
2051 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002052 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002053 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002054 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2055 Op1, Op2, Op3);
2056 void *IP = 0;
2057 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002058 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002059
Chris Lattner0fb094f2005-11-19 01:44:53 +00002060 RemoveNodeFromCSEMaps(N);
2061 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002062 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002063 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002064
Chris Lattnera5682852006-08-07 23:03:03 +00002065 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002066 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002067}
2068
Chris Lattner0fb094f2005-11-19 01:44:53 +00002069
Evan Cheng6ae46c42006-02-09 07:15:23 +00002070/// getTargetNode - These are used for target selectors to create a new node
2071/// with specified return type(s), target opcode, and operands.
2072///
2073/// Note that getTargetNode returns the resultant node. If there is already a
2074/// node of the specified opcode and operands, it returns that node instead of
2075/// the current one.
2076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2077 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2078}
2079SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2080 SDOperand Op1) {
2081 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2082}
2083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2084 SDOperand Op1, SDOperand Op2) {
2085 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2086}
2087SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2088 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2089 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2090}
2091SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002092 const SDOperand *Ops, unsigned NumOps) {
2093 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002094}
2095SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2096 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002097 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002098 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002099}
2100SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002101 MVT::ValueType VT2, SDOperand Op1,
2102 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002103 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002104 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002105 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002106}
2107SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002108 MVT::ValueType VT2, SDOperand Op1,
2109 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002110 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002111 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002112 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002113}
Evan Cheng694481e2006-08-27 08:08:54 +00002114SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2115 MVT::ValueType VT2,
2116 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002117 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002118 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002119}
2120SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2121 MVT::ValueType VT2, MVT::ValueType VT3,
2122 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002123 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002124 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002125 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002126}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002127SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002128 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002129 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002130 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002132}
2133
Evan Cheng99157a02006-08-07 22:13:29 +00002134/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002135/// This can cause recursive merging of nodes in the DAG.
2136///
Chris Lattner8b52f212005-08-26 18:36:28 +00002137/// This version assumes From/To have a single result value.
2138///
Chris Lattner1e111c72005-09-07 05:37:01 +00002139void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2140 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002141 SDNode *From = FromN.Val, *To = ToN.Val;
2142 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2143 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002144 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002145
Chris Lattner8b8749f2005-08-17 19:00:20 +00002146 while (!From->use_empty()) {
2147 // Process users until they are all gone.
2148 SDNode *U = *From->use_begin();
2149
2150 // This node is about to morph, remove its old self from the CSE maps.
2151 RemoveNodeFromCSEMaps(U);
2152
Chris Lattner65113b22005-11-08 22:07:03 +00002153 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2154 I != E; ++I)
2155 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002156 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002157 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002158 To->addUser(U);
2159 }
2160
2161 // Now that we have modified U, add it back to the CSE maps. If it already
2162 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002163 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2164 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002165 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002166 if (Deleted) Deleted->push_back(U);
2167 DeleteNodeNotInCSEMaps(U);
2168 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002169 }
2170}
2171
2172/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2173/// This can cause recursive merging of nodes in the DAG.
2174///
2175/// This version assumes From/To have matching types and numbers of result
2176/// values.
2177///
Chris Lattner1e111c72005-09-07 05:37:01 +00002178void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2179 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002180 assert(From != To && "Cannot replace uses of with self");
2181 assert(From->getNumValues() == To->getNumValues() &&
2182 "Cannot use this version of ReplaceAllUsesWith!");
2183 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002184 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002185 return;
2186 }
2187
2188 while (!From->use_empty()) {
2189 // Process users until they are all gone.
2190 SDNode *U = *From->use_begin();
2191
2192 // This node is about to morph, remove its old self from the CSE maps.
2193 RemoveNodeFromCSEMaps(U);
2194
Chris Lattner65113b22005-11-08 22:07:03 +00002195 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2196 I != E; ++I)
2197 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002198 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002199 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002200 To->addUser(U);
2201 }
2202
2203 // Now that we have modified U, add it back to the CSE maps. If it already
2204 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002205 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2206 ReplaceAllUsesWith(U, Existing, Deleted);
2207 // U is now dead.
2208 if (Deleted) Deleted->push_back(U);
2209 DeleteNodeNotInCSEMaps(U);
2210 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002211 }
2212}
2213
Chris Lattner8b52f212005-08-26 18:36:28 +00002214/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2215/// This can cause recursive merging of nodes in the DAG.
2216///
2217/// This version can replace From with any result values. To must match the
2218/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002219void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002220 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002221 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002222 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002223 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002224 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002225 return;
2226 }
2227
2228 while (!From->use_empty()) {
2229 // Process users until they are all gone.
2230 SDNode *U = *From->use_begin();
2231
2232 // This node is about to morph, remove its old self from the CSE maps.
2233 RemoveNodeFromCSEMaps(U);
2234
Chris Lattner65113b22005-11-08 22:07:03 +00002235 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2236 I != E; ++I)
2237 if (I->Val == From) {
2238 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002239 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002240 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002241 ToOp.Val->addUser(U);
2242 }
2243
2244 // Now that we have modified U, add it back to the CSE maps. If it already
2245 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002246 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2247 ReplaceAllUsesWith(U, Existing, Deleted);
2248 // U is now dead.
2249 if (Deleted) Deleted->push_back(U);
2250 DeleteNodeNotInCSEMaps(U);
2251 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002252 }
2253}
2254
Chris Lattner012f2412006-02-17 21:58:01 +00002255/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2256/// uses of other values produced by From.Val alone. The Deleted vector is
2257/// handled the same was as for ReplaceAllUsesWith.
2258void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2259 std::vector<SDNode*> &Deleted) {
2260 assert(From != To && "Cannot replace a value with itself");
2261 // Handle the simple, trivial, case efficiently.
2262 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2263 ReplaceAllUsesWith(From, To, &Deleted);
2264 return;
2265 }
2266
2267 // Get all of the users in a nice, deterministically ordered, uniqued set.
2268 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2269
2270 while (!Users.empty()) {
2271 // We know that this user uses some value of From. If it is the right
2272 // value, update it.
2273 SDNode *User = Users.back();
2274 Users.pop_back();
2275
2276 for (SDOperand *Op = User->OperandList,
2277 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2278 if (*Op == From) {
2279 // Okay, we know this user needs to be updated. Remove its old self
2280 // from the CSE maps.
2281 RemoveNodeFromCSEMaps(User);
2282
2283 // Update all operands that match "From".
2284 for (; Op != E; ++Op) {
2285 if (*Op == From) {
2286 From.Val->removeUser(User);
2287 *Op = To;
2288 To.Val->addUser(User);
2289 }
2290 }
2291
2292 // Now that we have modified User, add it back to the CSE maps. If it
2293 // already exists there, recursively merge the results together.
2294 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2295 unsigned NumDeleted = Deleted.size();
2296 ReplaceAllUsesWith(User, Existing, &Deleted);
2297
2298 // User is now dead.
2299 Deleted.push_back(User);
2300 DeleteNodeNotInCSEMaps(User);
2301
2302 // We have to be careful here, because ReplaceAllUsesWith could have
2303 // deleted a user of From, which means there may be dangling pointers
2304 // in the "Users" setvector. Scan over the deleted node pointers and
2305 // remove them from the setvector.
2306 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2307 Users.remove(Deleted[i]);
2308 }
2309 break; // Exit the operand scanning loop.
2310 }
2311 }
2312 }
2313}
2314
Chris Lattner7b2880c2005-08-24 22:44:39 +00002315
Evan Chenge6f35d82006-08-01 08:20:41 +00002316/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2317/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002318unsigned SelectionDAG::AssignNodeIds() {
2319 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002320 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2321 SDNode *N = I;
2322 N->setNodeId(Id++);
2323 }
2324 return Id;
2325}
2326
Evan Chenge6f35d82006-08-01 08:20:41 +00002327/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002328/// based on their topological order. It returns the maximum id and a vector
2329/// of the SDNodes* in assigned order by reference.
2330unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002331 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002332 std::vector<unsigned> InDegree(DAGSize);
2333 std::vector<SDNode*> Sources;
2334
2335 // Use a two pass approach to avoid using a std::map which is slow.
2336 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002337 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2338 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002339 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002340 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002341 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002342 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002343 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002344 }
2345
Evan Cheng99157a02006-08-07 22:13:29 +00002346 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002347 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002348 SDNode *N = Sources.back();
2349 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002350 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002351 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2352 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002353 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002354 if (Degree == 0)
2355 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002356 }
2357 }
2358
Evan Chengc384d6c2006-08-02 22:00:34 +00002359 // Second pass, assign the actual topological order as node ids.
2360 Id = 0;
2361 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2362 TI != TE; ++TI)
2363 (*TI)->setNodeId(Id++);
2364
2365 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002366}
2367
2368
Evan Cheng091cba12006-07-27 06:39:06 +00002369
Jim Laskey58b968b2005-08-17 20:08:02 +00002370//===----------------------------------------------------------------------===//
2371// SDNode Class
2372//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002373
Chris Lattner917d2c92006-07-19 00:00:37 +00002374// Out-of-line virtual method to give class a home.
2375void SDNode::ANCHOR() {
2376}
2377
Chris Lattnera3255112005-11-08 23:30:28 +00002378/// getValueTypeList - Return a pointer to the specified value type.
2379///
2380MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2381 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2382 VTs[VT] = VT;
2383 return &VTs[VT];
2384}
Chris Lattnera5682852006-08-07 23:03:03 +00002385
Chris Lattner5c884562005-01-12 18:37:47 +00002386/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2387/// indicated value. This method ignores uses of other values defined by this
2388/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002389bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002390 assert(Value < getNumValues() && "Bad value!");
2391
2392 // If there is only one value, this is easy.
2393 if (getNumValues() == 1)
2394 return use_size() == NUses;
2395 if (Uses.size() < NUses) return false;
2396
Evan Cheng4ee62112006-02-05 06:29:23 +00002397 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002398
2399 std::set<SDNode*> UsersHandled;
2400
Chris Lattnerf83482d2006-08-16 20:59:32 +00002401 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002402 SDNode *User = *UI;
2403 if (User->getNumOperands() == 1 ||
2404 UsersHandled.insert(User).second) // First time we've seen this?
2405 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2406 if (User->getOperand(i) == TheValue) {
2407 if (NUses == 0)
2408 return false; // too many uses
2409 --NUses;
2410 }
2411 }
2412
2413 // Found exactly the right number of uses?
2414 return NUses == 0;
2415}
2416
2417
Evan Cheng4ee62112006-02-05 06:29:23 +00002418// isOnlyUse - Return true if this node is the only use of N.
2419bool SDNode::isOnlyUse(SDNode *N) const {
2420 bool Seen = false;
2421 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2422 SDNode *User = *I;
2423 if (User == this)
2424 Seen = true;
2425 else
2426 return false;
2427 }
2428
2429 return Seen;
2430}
2431
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002432// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002433bool SDOperand::isOperand(SDNode *N) const {
2434 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2435 if (*this == N->getOperand(i))
2436 return true;
2437 return false;
2438}
2439
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002440bool SDNode::isOperand(SDNode *N) const {
2441 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2442 if (this == N->OperandList[i].Val)
2443 return true;
2444 return false;
2445}
Evan Cheng4ee62112006-02-05 06:29:23 +00002446
Evan Chengc5484282006-10-04 00:56:09 +00002447uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2448 assert(Num < NumOperands && "Invalid child # of SDNode!");
2449 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2450}
2451
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002452const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002453 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002454 default:
2455 if (getOpcode() < ISD::BUILTIN_OP_END)
2456 return "<<Unknown DAG Node>>";
2457 else {
Evan Cheng72261582005-12-20 06:22:03 +00002458 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002459 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002460 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2461 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002462
Evan Cheng72261582005-12-20 06:22:03 +00002463 TargetLowering &TLI = G->getTargetLoweringInfo();
2464 const char *Name =
2465 TLI.getTargetNodeName(getOpcode());
2466 if (Name) return Name;
2467 }
2468
2469 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002470 }
2471
Andrew Lenharth95762122005-03-31 21:24:06 +00002472 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002473 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002474 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002475 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002476 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002477 case ISD::AssertSext: return "AssertSext";
2478 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002479
2480 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002481 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002482 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002483 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002484
2485 case ISD::Constant: return "Constant";
2486 case ISD::ConstantFP: return "ConstantFP";
2487 case ISD::GlobalAddress: return "GlobalAddress";
2488 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002489 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002490 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Chris Lattner5839bf22005-08-26 17:15:30 +00002491 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002492 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002493 case ISD::INTRINSIC_WO_CHAIN: {
2494 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2495 return Intrinsic::getName((Intrinsic::ID)IID);
2496 }
2497 case ISD::INTRINSIC_VOID:
2498 case ISD::INTRINSIC_W_CHAIN: {
2499 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002500 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002501 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002502
Chris Lattnerb2827b02006-03-19 00:52:58 +00002503 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002504 case ISD::TargetConstant: return "TargetConstant";
2505 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002506 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2507 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002508 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002509 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002510 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002511
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002512 case ISD::CopyToReg: return "CopyToReg";
2513 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002514 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002515 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002516 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002517 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002518 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002519 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002520
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002521 // Unary operators
2522 case ISD::FABS: return "fabs";
2523 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002524 case ISD::FSQRT: return "fsqrt";
2525 case ISD::FSIN: return "fsin";
2526 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002527 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002528
2529 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002530 case ISD::ADD: return "add";
2531 case ISD::SUB: return "sub";
2532 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002533 case ISD::MULHU: return "mulhu";
2534 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002535 case ISD::SDIV: return "sdiv";
2536 case ISD::UDIV: return "udiv";
2537 case ISD::SREM: return "srem";
2538 case ISD::UREM: return "urem";
2539 case ISD::AND: return "and";
2540 case ISD::OR: return "or";
2541 case ISD::XOR: return "xor";
2542 case ISD::SHL: return "shl";
2543 case ISD::SRA: return "sra";
2544 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002545 case ISD::ROTL: return "rotl";
2546 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002547 case ISD::FADD: return "fadd";
2548 case ISD::FSUB: return "fsub";
2549 case ISD::FMUL: return "fmul";
2550 case ISD::FDIV: return "fdiv";
2551 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002552 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002553 case ISD::VADD: return "vadd";
2554 case ISD::VSUB: return "vsub";
2555 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002556 case ISD::VSDIV: return "vsdiv";
2557 case ISD::VUDIV: return "vudiv";
2558 case ISD::VAND: return "vand";
2559 case ISD::VOR: return "vor";
2560 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002561
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002562 case ISD::SETCC: return "setcc";
2563 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002564 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002565 case ISD::VSELECT: return "vselect";
2566 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2567 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2568 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002569 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002570 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2571 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2572 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2573 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2574 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002575 case ISD::ADDC: return "addc";
2576 case ISD::ADDE: return "adde";
2577 case ISD::SUBC: return "subc";
2578 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002579 case ISD::SHL_PARTS: return "shl_parts";
2580 case ISD::SRA_PARTS: return "sra_parts";
2581 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002582
Chris Lattner7f644642005-04-28 21:44:03 +00002583 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002584 case ISD::SIGN_EXTEND: return "sign_extend";
2585 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002586 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002587 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002588 case ISD::TRUNCATE: return "truncate";
2589 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002590 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002591 case ISD::FP_EXTEND: return "fp_extend";
2592
2593 case ISD::SINT_TO_FP: return "sint_to_fp";
2594 case ISD::UINT_TO_FP: return "uint_to_fp";
2595 case ISD::FP_TO_SINT: return "fp_to_sint";
2596 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002597 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002598
2599 // Control flow instructions
2600 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002601 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002602 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002603 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002604 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002605 case ISD::CALLSEQ_START: return "callseq_start";
2606 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002607
2608 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002609 case ISD::LOAD: return "load";
2610 case ISD::STORE: return "store";
2611 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002612 case ISD::TRUNCSTORE: return "truncstore";
2613 case ISD::VAARG: return "vaarg";
2614 case ISD::VACOPY: return "vacopy";
2615 case ISD::VAEND: return "vaend";
2616 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002617 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002618 case ISD::EXTRACT_ELEMENT: return "extract_element";
2619 case ISD::BUILD_PAIR: return "build_pair";
2620 case ISD::STACKSAVE: return "stacksave";
2621 case ISD::STACKRESTORE: return "stackrestore";
2622
2623 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002624 case ISD::MEMSET: return "memset";
2625 case ISD::MEMCPY: return "memcpy";
2626 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002627
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002628 // Bit manipulation
2629 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002630 case ISD::CTPOP: return "ctpop";
2631 case ISD::CTTZ: return "cttz";
2632 case ISD::CTLZ: return "ctlz";
2633
Chris Lattner36ce6912005-11-29 06:21:05 +00002634 // Debug info
2635 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002636 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002637 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002638
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002639 case ISD::CONDCODE:
2640 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002641 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002642 case ISD::SETOEQ: return "setoeq";
2643 case ISD::SETOGT: return "setogt";
2644 case ISD::SETOGE: return "setoge";
2645 case ISD::SETOLT: return "setolt";
2646 case ISD::SETOLE: return "setole";
2647 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002648
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002649 case ISD::SETO: return "seto";
2650 case ISD::SETUO: return "setuo";
2651 case ISD::SETUEQ: return "setue";
2652 case ISD::SETUGT: return "setugt";
2653 case ISD::SETUGE: return "setuge";
2654 case ISD::SETULT: return "setult";
2655 case ISD::SETULE: return "setule";
2656 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002657
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002658 case ISD::SETEQ: return "seteq";
2659 case ISD::SETGT: return "setgt";
2660 case ISD::SETGE: return "setge";
2661 case ISD::SETLT: return "setlt";
2662 case ISD::SETLE: return "setle";
2663 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002664 }
2665 }
2666}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002667
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002668void SDNode::dump() const { dump(0); }
2669void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002670 std::cerr << (void*)this << ": ";
2671
2672 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2673 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002674 if (getValueType(i) == MVT::Other)
2675 std::cerr << "ch";
2676 else
2677 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002678 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002679 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002680
2681 std::cerr << " ";
2682 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2683 if (i) std::cerr << ", ";
2684 std::cerr << (void*)getOperand(i).Val;
2685 if (unsigned RN = getOperand(i).ResNo)
2686 std::cerr << ":" << RN;
2687 }
2688
2689 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2690 std::cerr << "<" << CSDN->getValue() << ">";
2691 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2692 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002693 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002694 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002695 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002696 std::cerr << "<";
2697 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002698 if (offset > 0)
2699 std::cerr << " + " << offset;
2700 else
2701 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002702 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002703 std::cerr << "<" << FIDN->getIndex() << ">";
2704 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002705 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002706 if (CP->isMachineConstantPoolEntry())
2707 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2708 else
2709 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002710 if (offset > 0)
2711 std::cerr << " + " << offset;
2712 else
2713 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002714 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002715 std::cerr << "<";
2716 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2717 if (LBB)
2718 std::cerr << LBB->getName() << " ";
2719 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002720 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002721 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002722 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2723 } else {
2724 std::cerr << " #" << R->getReg();
2725 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002726 } else if (const ExternalSymbolSDNode *ES =
2727 dyn_cast<ExternalSymbolSDNode>(this)) {
2728 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002729 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2730 if (M->getValue())
2731 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2732 else
2733 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002734 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2735 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002736 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2737 bool doExt = true;
2738 switch (LD->getExtensionType()) {
2739 default: doExt = false; break;
2740 case ISD::EXTLOAD:
2741 std::cerr << " <anyext ";
2742 break;
2743 case ISD::SEXTLOAD:
2744 std::cerr << " <sext ";
2745 break;
2746 case ISD::ZEXTLOAD:
2747 std::cerr << " <zext ";
2748 break;
2749 }
2750 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +00002751 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002752
2753 if (LD->getAddressingMode() == ISD::PRE_INDEXED)
2754 std::cerr << " <pre>";
2755 else if (LD->getAddressingMode() == ISD::POST_INDEXED)
2756 std::cerr << " <post>";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002757 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002758}
2759
Chris Lattnerde202b32005-11-09 23:47:37 +00002760static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002761 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2762 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002763 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002764 else
2765 std::cerr << "\n" << std::string(indent+2, ' ')
2766 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002767
Chris Lattnerea946cd2005-01-09 20:38:33 +00002768
2769 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002770 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002771}
2772
Chris Lattnerc3aae252005-01-07 07:46:32 +00002773void SelectionDAG::dump() const {
2774 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002775 std::vector<const SDNode*> Nodes;
2776 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2777 I != E; ++I)
2778 Nodes.push_back(I);
2779
Chris Lattner49d24712005-01-09 20:26:36 +00002780 std::sort(Nodes.begin(), Nodes.end());
2781
2782 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002783 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002784 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002785 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002786
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002787 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002788
Chris Lattnerc3aae252005-01-07 07:46:32 +00002789 std::cerr << "\n\n";
2790}
2791
Evan Chengd6594ae2006-09-12 21:00:35 +00002792const Type *ConstantPoolSDNode::getType() const {
2793 if (isMachineConstantPoolEntry())
2794 return Val.MachineCPVal->getType();
2795 return Val.ConstVal->getType();
2796}