blob: 895ae47f99bc0676a813fa196c54fdc3a49d928f [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
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000303void SelectionDAG::DeleteNode(SDNode *N) {
304 assert(N->use_empty() && "Cannot delete a node that is not dead!");
305
306 // First take this out of the appropriate CSE map.
307 RemoveNodeFromCSEMaps(N);
308
Chris Lattner1e111c72005-09-07 05:37:01 +0000309 // Finally, remove uses due to operands of this node, remove from the
310 // AllNodes list, and delete the node.
311 DeleteNodeNotInCSEMaps(N);
312}
313
314void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
315
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000316 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000317 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000318
319 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000320 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
321 I->Val->removeUser(N);
322 delete[] N->OperandList;
323 N->OperandList = 0;
324 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000325
326 delete N;
327}
328
Chris Lattner149c58c2005-08-16 18:17:10 +0000329/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
330/// correspond to it. This is useful when we're about to delete or repurpose
331/// the node. We don't want future request for structurally identical nodes
332/// to return N anymore.
333void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000336 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000337 case ISD::STRING:
338 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
339 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000340 case ISD::CONDCODE:
341 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
342 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000343 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000344 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
345 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000346 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000347 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000348 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000349 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000350 Erased =
351 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000352 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000353 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000354 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000355 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
356 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000357 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000358 // Remove it from the CSE Map.
359 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000360 break;
361 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000362#ifndef NDEBUG
363 // Verify that the node was actually in one of the CSE maps, unless it has a
364 // flag result (which cannot be CSE'd) or is one of the special cases that are
365 // not subject to CSE.
366 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000367 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000368 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000369 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000370 assert(0 && "Node is not in map!");
371 }
372#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000373}
374
Chris Lattner8b8749f2005-08-17 19:00:20 +0000375/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
376/// has been taken out and modified in some way. If the specified node already
377/// exists in the CSE maps, do not modify the maps, but return the existing node
378/// instead. If it doesn't exist, add it and return null.
379///
380SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
381 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000382 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000383 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000384
Chris Lattner9f8cc692005-12-19 22:21:21 +0000385 // Check that remaining values produced are not flags.
386 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
387 if (N->getValueType(i) == MVT::Flag)
388 return 0; // Never CSE anything that produces a flag.
389
Chris Lattnera5682852006-08-07 23:03:03 +0000390 SDNode *New = CSEMap.GetOrInsertNode(N);
391 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000392 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000393}
394
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000395/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
396/// were replaced with those specified. If this node is never memoized,
397/// return null, otherwise return a pointer to the slot it would take. If a
398/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000399SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
400 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000401 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000402 return 0; // Never add these nodes.
403
404 // Check that remaining values produced are not flags.
405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
406 if (N->getValueType(i) == MVT::Flag)
407 return 0; // Never CSE anything that produces a flag.
408
Chris Lattnera5682852006-08-07 23:03:03 +0000409 SelectionDAGCSEMap::NodeID ID;
410 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000411 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000412 ID.SetOperands(Op);
413 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000414}
415
416/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
417/// were replaced with those specified. If this node is never memoized,
418/// return null, otherwise return a pointer to the slot it would take. If a
419/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000420SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
421 SDOperand Op1, SDOperand Op2,
422 void *&InsertPos) {
423 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
424 return 0; // Never add these nodes.
425
426 // Check that remaining values produced are not flags.
427 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
428 if (N->getValueType(i) == MVT::Flag)
429 return 0; // Never CSE anything that produces a flag.
430
431 SelectionDAGCSEMap::NodeID ID;
432 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000433 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000434 ID.SetOperands(Op1, Op2);
435 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
436}
437
438
439/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
440/// were replaced with those specified. If this node is never memoized,
441/// return null, otherwise return a pointer to the slot it would take. If a
442/// node already exists with these operands, the slot will be non-null.
443SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000444 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000445 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000446 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000447 return 0; // Never add these nodes.
448
449 // Check that remaining values produced are not flags.
450 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
451 if (N->getValueType(i) == MVT::Flag)
452 return 0; // Never CSE anything that produces a flag.
453
Chris Lattnera5682852006-08-07 23:03:03 +0000454 SelectionDAGCSEMap::NodeID ID;
455 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000456 ID.SetValueTypes(N->getVTList());
Evan Cheng9629aba2006-10-11 01:47:58 +0000457 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
458 ID.AddInteger(LD->getAddressingMode());
459 ID.AddInteger(LD->getExtensionType());
460 ID.AddInteger(LD->getLoadVT());
461 ID.AddPointer(LD->getSrcValue());
462 ID.AddInteger(LD->getSrcValueOffset());
463 ID.AddInteger(LD->getAlignment());
464 ID.AddInteger(LD->isVolatile());
465 }
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000466 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000467 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000468}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000469
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000470
Chris Lattner78ec3112003-08-11 14:57:33 +0000471SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000472 while (!AllNodes.empty()) {
473 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000474 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000475 delete [] N->OperandList;
476 N->OperandList = 0;
477 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000478 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000479 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000480}
481
Chris Lattner0f2287b2005-04-13 02:38:18 +0000482SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000483 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000484 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000485 return getNode(ISD::AND, Op.getValueType(), Op,
486 getConstant(Imm, Op.getValueType()));
487}
488
Chris Lattner36ce6912005-11-29 06:21:05 +0000489SDOperand SelectionDAG::getString(const std::string &Val) {
490 StringSDNode *&N = StringNodes[Val];
491 if (!N) {
492 N = new StringSDNode(Val);
493 AllNodes.push_back(N);
494 }
495 return SDOperand(N, 0);
496}
497
Chris Lattner61b09412006-08-11 21:01:22 +0000498SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000499 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000500 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000501
Chris Lattner61b09412006-08-11 21:01:22 +0000502 // Mask out any bits that are not valid for this constant.
503 Val &= MVT::getIntVTBitMask(VT);
504
505 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000506 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000507 ID.AddInteger(Val);
508 void *IP = 0;
509 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
510 return SDOperand(E, 0);
511 SDNode *N = new ConstantSDNode(isT, Val, VT);
512 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000513 AllNodes.push_back(N);
514 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000515}
516
Chris Lattner61b09412006-08-11 21:01:22 +0000517
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000518SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
519 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000520 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
521 if (VT == MVT::f32)
522 Val = (float)Val; // Mask out extra precision.
523
Chris Lattnerd8658612005-02-17 20:17:32 +0000524 // Do the map lookup using the actual bit pattern for the floating point
525 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
526 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000527 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000528 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000529 ID.AddInteger(DoubleToBits(Val));
530 void *IP = 0;
531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
532 return SDOperand(E, 0);
533 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
534 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000535 AllNodes.push_back(N);
536 return SDOperand(N, 0);
537}
538
Chris Lattnerc3aae252005-01-07 07:46:32 +0000539SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000540 MVT::ValueType VT, int Offset,
541 bool isTargetGA) {
542 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000543 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000544 ID.AddPointer(GV);
545 ID.AddInteger(Offset);
546 void *IP = 0;
547 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
548 return SDOperand(E, 0);
549 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
550 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000551 AllNodes.push_back(N);
552 return SDOperand(N, 0);
553}
554
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000555SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
556 bool isTarget) {
557 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000558 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000559 ID.AddInteger(FI);
560 void *IP = 0;
561 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
562 return SDOperand(E, 0);
563 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
564 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000565 AllNodes.push_back(N);
566 return SDOperand(N, 0);
567}
568
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000569SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
570 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000571 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000572 ID.AddInteger(JTI);
573 void *IP = 0;
574 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
575 return SDOperand(E, 0);
576 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
577 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000578 AllNodes.push_back(N);
579 return SDOperand(N, 0);
580}
581
Evan Chengb8973bd2006-01-31 22:23:14 +0000582SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000583 unsigned Alignment, int Offset,
584 bool isTarget) {
585 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000586 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000587 ID.AddInteger(Alignment);
588 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000589 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000590 void *IP = 0;
591 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
592 return SDOperand(E, 0);
593 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
594 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000595 AllNodes.push_back(N);
596 return SDOperand(N, 0);
597}
598
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599
Evan Chengd6594ae2006-09-12 21:00:35 +0000600SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
601 MVT::ValueType VT,
602 unsigned Alignment, int Offset,
603 bool isTarget) {
604 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
605 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
606 ID.AddInteger(Alignment);
607 ID.AddInteger(Offset);
608 C->AddSelectionDAGCSEId(&ID);
609 void *IP = 0;
610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
611 return SDOperand(E, 0);
612 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
613 CSEMap.InsertNode(N, IP);
614 AllNodes.push_back(N);
615 return SDOperand(N, 0);
616}
617
618
Chris Lattnerc3aae252005-01-07 07:46:32 +0000619SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000620 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000621 ID.AddPointer(MBB);
622 void *IP = 0;
623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
624 return SDOperand(E, 0);
625 SDNode *N = new BasicBlockSDNode(MBB);
626 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
629}
630
Chris Lattner15e4b012005-07-10 00:07:11 +0000631SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
632 if ((unsigned)VT >= ValueTypeNodes.size())
633 ValueTypeNodes.resize(VT+1);
634 if (ValueTypeNodes[VT] == 0) {
635 ValueTypeNodes[VT] = new VTSDNode(VT);
636 AllNodes.push_back(ValueTypeNodes[VT]);
637 }
638
639 return SDOperand(ValueTypeNodes[VT], 0);
640}
641
Chris Lattnerc3aae252005-01-07 07:46:32 +0000642SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
643 SDNode *&N = ExternalSymbols[Sym];
644 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000645 N = new ExternalSymbolSDNode(false, Sym, VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
648}
649
Chris Lattner809ec112006-01-28 10:09:25 +0000650SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
651 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000652 SDNode *&N = TargetExternalSymbols[Sym];
653 if (N) return SDOperand(N, 0);
654 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 AllNodes.push_back(N);
656 return SDOperand(N, 0);
657}
658
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000659SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
660 if ((unsigned)Cond >= CondCodeNodes.size())
661 CondCodeNodes.resize(Cond+1);
662
Chris Lattner079a27a2005-08-09 20:40:02 +0000663 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000664 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000665 AllNodes.push_back(CondCodeNodes[Cond]);
666 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000667 return SDOperand(CondCodeNodes[Cond], 0);
668}
669
Chris Lattner0fdd7682005-08-30 22:38:38 +0000670SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000671 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000672 ID.AddInteger(RegNo);
673 void *IP = 0;
674 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
675 return SDOperand(E, 0);
676 SDNode *N = new RegisterSDNode(RegNo, VT);
677 CSEMap.InsertNode(N, IP);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
680}
681
682SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
683 assert((!V || isa<PointerType>(V->getType())) &&
684 "SrcValue is not a pointer?");
685
Chris Lattner0b3e5252006-08-15 19:11:05 +0000686 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000687 ID.AddPointer(V);
688 ID.AddInteger(Offset);
689 void *IP = 0;
690 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
691 return SDOperand(E, 0);
692 SDNode *N = new SrcValueSDNode(V, Offset);
693 CSEMap.InsertNode(N, IP);
694 AllNodes.push_back(N);
695 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000696}
697
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000698SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
699 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000700 // These setcc operations always fold.
701 switch (Cond) {
702 default: break;
703 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000704 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000706 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000707
708 case ISD::SETOEQ:
709 case ISD::SETOGT:
710 case ISD::SETOGE:
711 case ISD::SETOLT:
712 case ISD::SETOLE:
713 case ISD::SETONE:
714 case ISD::SETO:
715 case ISD::SETUO:
716 case ISD::SETUEQ:
717 case ISD::SETUNE:
718 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
719 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000720 }
721
Chris Lattner67255a12005-04-07 18:14:58 +0000722 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
723 uint64_t C2 = N2C->getValue();
724 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
725 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000726
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 // Sign extend the operands if required
728 if (ISD::isSignedIntSetCC(Cond)) {
729 C1 = N1C->getSignExtended();
730 C2 = N2C->getSignExtended();
731 }
732
733 switch (Cond) {
734 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000735 case ISD::SETEQ: return getConstant(C1 == C2, VT);
736 case ISD::SETNE: return getConstant(C1 != C2, VT);
737 case ISD::SETULT: return getConstant(C1 < C2, VT);
738 case ISD::SETUGT: return getConstant(C1 > C2, VT);
739 case ISD::SETULE: return getConstant(C1 <= C2, VT);
740 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
741 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
742 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
743 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
744 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745 }
Chris Lattner24673922005-04-07 18:58:54 +0000746 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000747 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000748 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
749 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
750
751 // If the comparison constant has bits in the upper part, the
752 // zero-extended value could never match.
753 if (C2 & (~0ULL << InSize)) {
754 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
755 switch (Cond) {
756 case ISD::SETUGT:
757 case ISD::SETUGE:
758 case ISD::SETEQ: return getConstant(0, VT);
759 case ISD::SETULT:
760 case ISD::SETULE:
761 case ISD::SETNE: return getConstant(1, VT);
762 case ISD::SETGT:
763 case ISD::SETGE:
764 // True if the sign bit of C2 is set.
765 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
766 case ISD::SETLT:
767 case ISD::SETLE:
768 // True if the sign bit of C2 isn't set.
769 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
770 default:
771 break;
772 }
773 }
774
775 // Otherwise, we can perform the comparison with the low bits.
776 switch (Cond) {
777 case ISD::SETEQ:
778 case ISD::SETNE:
779 case ISD::SETUGT:
780 case ISD::SETUGE:
781 case ISD::SETULT:
782 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000783 return getSetCC(VT, N1.getOperand(0),
784 getConstant(C2, N1.getOperand(0).getValueType()),
785 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000786 default:
787 break; // todo, be more careful with signed comparisons
788 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000789 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
790 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
791 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
792 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
793 MVT::ValueType ExtDstTy = N1.getValueType();
794 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000795
Chris Lattner7b2880c2005-08-24 22:44:39 +0000796 // If the extended part has any inconsistent bits, it cannot ever
797 // compare equal. In other words, they have to be all ones or all
798 // zeros.
799 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000800 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000801 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
802 return getConstant(Cond == ISD::SETNE, VT);
803
804 // Otherwise, make this a use of a zext.
805 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000806 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000807 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000808 }
809
Chris Lattner67255a12005-04-07 18:14:58 +0000810 uint64_t MinVal, MaxVal;
811 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
812 if (ISD::isSignedIntSetCC(Cond)) {
813 MinVal = 1ULL << (OperandBitSize-1);
814 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
815 MaxVal = ~0ULL >> (65-OperandBitSize);
816 else
817 MaxVal = 0;
818 } else {
819 MinVal = 0;
820 MaxVal = ~0ULL >> (64-OperandBitSize);
821 }
822
823 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
824 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
825 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
826 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000827 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
828 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000829 }
830
831 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
832 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
833 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000834 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
835 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000836 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000837
Nate Begeman72ea2812005-04-14 08:56:52 +0000838 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
839 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000840
Nate Begeman72ea2812005-04-14 08:56:52 +0000841 // Canonicalize setgt X, Min --> setne X, Min
842 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000843 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000844
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000845 // If we have setult X, 1, turn it into seteq X, 0
846 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000847 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
848 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000849 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000850 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000851 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
852 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000853
854 // If we have "setcc X, C1", check to see if we can shrink the immediate
855 // by changing cc.
856
857 // SETUGT X, SINTMAX -> SETLT X, 0
858 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
859 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000860 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000861
862 // FIXME: Implement the rest of these.
863
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000864
865 // Fold bit comparisons when we can.
866 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
867 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
868 if (ConstantSDNode *AndRHS =
869 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
870 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
871 // Perform the xform if the AND RHS is a single bit.
872 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
873 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000874 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000875 TLI.getShiftAmountTy()));
876 }
877 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
878 // (X & 8) == 8 --> (X & 8) >> 3
879 // Perform the xform if C2 is a single bit.
880 if ((C2 & (C2-1)) == 0) {
881 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000882 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000883 }
884 }
885 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000886 }
Chris Lattner67255a12005-04-07 18:14:58 +0000887 } else if (isa<ConstantSDNode>(N1.Val)) {
888 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000889 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000890 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000891
892 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
893 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
894 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000895
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896 switch (Cond) {
897 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000898 case ISD::SETEQ: return getConstant(C1 == C2, VT);
899 case ISD::SETNE: return getConstant(C1 != C2, VT);
900 case ISD::SETLT: return getConstant(C1 < C2, VT);
901 case ISD::SETGT: return getConstant(C1 > C2, VT);
902 case ISD::SETLE: return getConstant(C1 <= C2, VT);
903 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000904 }
905 } else {
906 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000907 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000908 }
909
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000910 // Could not fold it.
911 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912}
913
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000915///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000917 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000918 void *IP = 0;
919 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
920 return SDOperand(E, 0);
921 SDNode *N = new SDNode(Opcode, VT);
922 CSEMap.InsertNode(N, IP);
923
924 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 return SDOperand(N, 0);
926}
927
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
929 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000930 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000931 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
933 uint64_t Val = C->getValue();
934 switch (Opcode) {
935 default: break;
936 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000937 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
939 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000940 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
941 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000942 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000943 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000944 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000945 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000946 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000947 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000948 case ISD::BSWAP:
949 switch(VT) {
950 default: assert(0 && "Invalid bswap!"); break;
951 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
952 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
953 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
954 }
955 break;
956 case ISD::CTPOP:
957 switch(VT) {
958 default: assert(0 && "Invalid ctpop!"); break;
959 case MVT::i1: return getConstant(Val != 0, VT);
960 case MVT::i8:
961 Tmp1 = (unsigned)Val & 0xFF;
962 return getConstant(CountPopulation_32(Tmp1), VT);
963 case MVT::i16:
964 Tmp1 = (unsigned)Val & 0xFFFF;
965 return getConstant(CountPopulation_32(Tmp1), VT);
966 case MVT::i32:
967 return getConstant(CountPopulation_32((unsigned)Val), VT);
968 case MVT::i64:
969 return getConstant(CountPopulation_64(Val), VT);
970 }
971 case ISD::CTLZ:
972 switch(VT) {
973 default: assert(0 && "Invalid ctlz!"); break;
974 case MVT::i1: return getConstant(Val == 0, VT);
975 case MVT::i8:
976 Tmp1 = (unsigned)Val & 0xFF;
977 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
978 case MVT::i16:
979 Tmp1 = (unsigned)Val & 0xFFFF;
980 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
981 case MVT::i32:
982 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
983 case MVT::i64:
984 return getConstant(CountLeadingZeros_64(Val), VT);
985 }
986 case ISD::CTTZ:
987 switch(VT) {
988 default: assert(0 && "Invalid cttz!"); break;
989 case MVT::i1: return getConstant(Val == 0, VT);
990 case MVT::i8:
991 Tmp1 = (unsigned)Val | 0x100;
992 return getConstant(CountTrailingZeros_32(Tmp1), VT);
993 case MVT::i16:
994 Tmp1 = (unsigned)Val | 0x10000;
995 return getConstant(CountTrailingZeros_32(Tmp1), VT);
996 case MVT::i32:
997 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
998 case MVT::i64:
999 return getConstant(CountTrailingZeros_64(Val), VT);
1000 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001001 }
1002 }
1003
Chris Lattner94683772005-12-23 05:30:37 +00001004 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1006 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001007 case ISD::FNEG:
1008 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001009 case ISD::FABS:
1010 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 case ISD::FP_ROUND:
1012 case ISD::FP_EXTEND:
1013 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001014 case ISD::FP_TO_SINT:
1015 return getConstant((int64_t)C->getValue(), VT);
1016 case ISD::FP_TO_UINT:
1017 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001018 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001019 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001020 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001021 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001022 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001023 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 }
1025
1026 unsigned OpOpcode = Operand.Val->getOpcode();
1027 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001028 case ISD::TokenFactor:
1029 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 case ISD::SIGN_EXTEND:
1031 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001032 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001033 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1034 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1035 break;
1036 case ISD::ZERO_EXTEND:
1037 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001038 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001039 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001040 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001041 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001042 case ISD::ANY_EXTEND:
1043 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001044 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001045 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1046 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1047 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1048 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 case ISD::TRUNCATE:
1050 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001051 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 if (OpOpcode == ISD::TRUNCATE)
1053 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001054 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1055 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001056 // If the source is smaller than the dest, we still need an extend.
1057 if (Operand.Val->getOperand(0).getValueType() < VT)
1058 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1059 else if (Operand.Val->getOperand(0).getValueType() > VT)
1060 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1061 else
1062 return Operand.Val->getOperand(0);
1063 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 break;
Chris Lattner35481892005-12-23 00:16:34 +00001065 case ISD::BIT_CONVERT:
1066 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001067 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001068 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001069 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001070 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1071 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001072 if (OpOpcode == ISD::UNDEF)
1073 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001074 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001075 case ISD::SCALAR_TO_VECTOR:
1076 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1077 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1078 "Illegal SCALAR_TO_VECTOR node!");
1079 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001080 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001081 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1082 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001083 Operand.Val->getOperand(0));
1084 if (OpOpcode == ISD::FNEG) // --X -> X
1085 return Operand.Val->getOperand(0);
1086 break;
1087 case ISD::FABS:
1088 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1089 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1090 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 }
1092
Chris Lattner43247a12005-08-25 19:12:10 +00001093 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001094 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001095 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001096 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1097 void *IP = 0;
1098 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1099 return SDOperand(E, 0);
1100 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001101 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001102 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001103 } else {
1104 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001105 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001106 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107 AllNodes.push_back(N);
1108 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001109}
1110
Chris Lattner36019aa2005-04-18 03:48:41 +00001111
1112
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1114 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001115#ifndef NDEBUG
1116 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001117 case ISD::TokenFactor:
1118 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1119 N2.getValueType() == MVT::Other && "Invalid token factor!");
1120 break;
Chris Lattner76365122005-01-16 02:23:22 +00001121 case ISD::AND:
1122 case ISD::OR:
1123 case ISD::XOR:
1124 case ISD::UDIV:
1125 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001126 case ISD::MULHU:
1127 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001128 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1129 // fall through
1130 case ISD::ADD:
1131 case ISD::SUB:
1132 case ISD::MUL:
1133 case ISD::SDIV:
1134 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001135 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1136 // fall through.
1137 case ISD::FADD:
1138 case ISD::FSUB:
1139 case ISD::FMUL:
1140 case ISD::FDIV:
1141 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001142 assert(N1.getValueType() == N2.getValueType() &&
1143 N1.getValueType() == VT && "Binary operator types must match!");
1144 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001145 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1146 assert(N1.getValueType() == VT &&
1147 MVT::isFloatingPoint(N1.getValueType()) &&
1148 MVT::isFloatingPoint(N2.getValueType()) &&
1149 "Invalid FCOPYSIGN!");
1150 break;
Chris Lattner76365122005-01-16 02:23:22 +00001151 case ISD::SHL:
1152 case ISD::SRA:
1153 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001154 case ISD::ROTL:
1155 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001156 assert(VT == N1.getValueType() &&
1157 "Shift operators return type must be the same as their first arg");
1158 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001159 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001160 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001161 case ISD::FP_ROUND_INREG: {
1162 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1163 assert(VT == N1.getValueType() && "Not an inreg round!");
1164 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1165 "Cannot FP_ROUND_INREG integer types");
1166 assert(EVT <= VT && "Not rounding down!");
1167 break;
1168 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001169 case ISD::AssertSext:
1170 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001171 case ISD::SIGN_EXTEND_INREG: {
1172 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1173 assert(VT == N1.getValueType() && "Not an inreg extend!");
1174 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1175 "Cannot *_EXTEND_INREG FP types");
1176 assert(EVT <= VT && "Not extending!");
1177 }
1178
Chris Lattner76365122005-01-16 02:23:22 +00001179 default: break;
1180 }
1181#endif
1182
Chris Lattnerc3aae252005-01-07 07:46:32 +00001183 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1184 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1185 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001186 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1187 int64_t Val = N1C->getValue();
1188 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1189 Val <<= 64-FromBits;
1190 Val >>= 64-FromBits;
1191 return getConstant(Val, VT);
1192 }
1193
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 if (N2C) {
1195 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1196 switch (Opcode) {
1197 case ISD::ADD: return getConstant(C1 + C2, VT);
1198 case ISD::SUB: return getConstant(C1 - C2, VT);
1199 case ISD::MUL: return getConstant(C1 * C2, VT);
1200 case ISD::UDIV:
1201 if (C2) return getConstant(C1 / C2, VT);
1202 break;
1203 case ISD::UREM :
1204 if (C2) return getConstant(C1 % C2, VT);
1205 break;
1206 case ISD::SDIV :
1207 if (C2) return getConstant(N1C->getSignExtended() /
1208 N2C->getSignExtended(), VT);
1209 break;
1210 case ISD::SREM :
1211 if (C2) return getConstant(N1C->getSignExtended() %
1212 N2C->getSignExtended(), VT);
1213 break;
1214 case ISD::AND : return getConstant(C1 & C2, VT);
1215 case ISD::OR : return getConstant(C1 | C2, VT);
1216 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001217 case ISD::SHL : return getConstant(C1 << C2, VT);
1218 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001219 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001220 case ISD::ROTL :
1221 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1222 VT);
1223 case ISD::ROTR :
1224 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1225 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 default: break;
1227 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 } else { // Cannonicalize constant to RHS if commutative
1229 if (isCommutativeBinOp(Opcode)) {
1230 std::swap(N1C, N2C);
1231 std::swap(N1, N2);
1232 }
1233 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 }
1235
1236 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1237 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001238 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 if (N2CFP) {
1240 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1241 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001242 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1243 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1244 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1245 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 if (C2) return getConstantFP(C1 / C2, VT);
1247 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001248 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 if (C2) return getConstantFP(fmod(C1, C2), VT);
1250 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001251 case ISD::FCOPYSIGN: {
1252 union {
1253 double F;
1254 uint64_t I;
1255 } u1;
1256 union {
1257 double F;
1258 int64_t I;
1259 } u2;
1260 u1.F = C1;
1261 u2.F = C2;
1262 if (u2.I < 0) // Sign bit of RHS set?
1263 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1264 else
1265 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1266 return getConstantFP(u1.F, VT);
1267 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268 default: break;
1269 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001270 } else { // Cannonicalize constant to RHS if commutative
1271 if (isCommutativeBinOp(Opcode)) {
1272 std::swap(N1CFP, N2CFP);
1273 std::swap(N1, N2);
1274 }
1275 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001276 }
Chris Lattner62b57722006-04-20 05:39:12 +00001277
1278 // Canonicalize an UNDEF to the RHS, even over a constant.
1279 if (N1.getOpcode() == ISD::UNDEF) {
1280 if (isCommutativeBinOp(Opcode)) {
1281 std::swap(N1, N2);
1282 } else {
1283 switch (Opcode) {
1284 case ISD::FP_ROUND_INREG:
1285 case ISD::SIGN_EXTEND_INREG:
1286 case ISD::SUB:
1287 case ISD::FSUB:
1288 case ISD::FDIV:
1289 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001290 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001291 return N1; // fold op(undef, arg2) -> undef
1292 case ISD::UDIV:
1293 case ISD::SDIV:
1294 case ISD::UREM:
1295 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001296 case ISD::SRL:
1297 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001298 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1299 }
1300 }
1301 }
1302
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001303 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001304 if (N2.getOpcode() == ISD::UNDEF) {
1305 switch (Opcode) {
1306 case ISD::ADD:
1307 case ISD::SUB:
1308 case ISD::FADD:
1309 case ISD::FSUB:
1310 case ISD::FMUL:
1311 case ISD::FDIV:
1312 case ISD::FREM:
1313 case ISD::UDIV:
1314 case ISD::SDIV:
1315 case ISD::UREM:
1316 case ISD::SREM:
1317 case ISD::XOR:
1318 return N2; // fold op(arg1, undef) -> undef
1319 case ISD::MUL:
1320 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001321 case ISD::SRL:
1322 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001323 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1324 case ISD::OR:
1325 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001326 case ISD::SRA:
1327 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001328 }
1329 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001330
Chris Lattnerc3aae252005-01-07 07:46:32 +00001331 // Finally, fold operations that do not require constants.
1332 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001333 case ISD::FP_ROUND_INREG:
1334 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1335 break;
1336 case ISD::SIGN_EXTEND_INREG: {
1337 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1338 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001339 break;
1340 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001341 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001342 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1343
Chris Lattner5c6621c2006-09-19 04:51:23 +00001344 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1345 // 64-bit integers into 32-bit parts. Instead of building the extract of
1346 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001347 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001348 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001349
1350 // EXTRACT_ELEMENT of a constant int is also very common.
1351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1352 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1353 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001354 }
1355 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001356
Nate Begemaneea805e2005-04-13 21:23:31 +00001357 // FIXME: figure out how to safely handle things like
1358 // int foo(int x) { return 1 << (x & 255); }
1359 // int bar() { return foo(256); }
1360#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001361 case ISD::SHL:
1362 case ISD::SRL:
1363 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001364 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001365 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001366 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001367 else if (N2.getOpcode() == ISD::AND)
1368 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1369 // If the and is only masking out bits that cannot effect the shift,
1370 // eliminate the and.
1371 unsigned NumBits = MVT::getSizeInBits(VT);
1372 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1373 return getNode(Opcode, VT, N1, N2.getOperand(0));
1374 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001375 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001376#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 }
1378
Chris Lattner27e9b412005-05-11 18:57:39 +00001379 // Memoize this node if possible.
1380 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001381 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001382 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001383 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1384 void *IP = 0;
1385 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1386 return SDOperand(E, 0);
1387 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001388 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001389 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001390 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001391 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001392 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001393 }
1394
Chris Lattnerc3aae252005-01-07 07:46:32 +00001395 AllNodes.push_back(N);
1396 return SDOperand(N, 0);
1397}
1398
Chris Lattnerc3aae252005-01-07 07:46:32 +00001399SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1400 SDOperand N1, SDOperand N2, SDOperand N3) {
1401 // Perform various simplifications.
1402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1403 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001404 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001405 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001406 case ISD::SETCC: {
1407 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001408 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001409 if (Simp.Val) return Simp;
1410 break;
1411 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001412 case ISD::SELECT:
1413 if (N1C)
1414 if (N1C->getValue())
1415 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001416 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 return N3; // select false, X, Y -> Y
1418
1419 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001420 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001421 case ISD::BRCOND:
1422 if (N2C)
1423 if (N2C->getValue()) // Unconditional branch
1424 return getNode(ISD::BR, MVT::Other, N1, N3);
1425 else
1426 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001427 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001428 case ISD::VECTOR_SHUFFLE:
1429 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1430 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1431 N3.getOpcode() == ISD::BUILD_VECTOR &&
1432 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1433 "Illegal VECTOR_SHUFFLE node!");
1434 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001435 }
1436
Chris Lattner43247a12005-08-25 19:12:10 +00001437 // Memoize node if it doesn't produce a flag.
1438 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001439 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001440 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001441 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1442 void *IP = 0;
1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1444 return SDOperand(E, 0);
1445 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001446 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001447 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001448 } else {
1449 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001450 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001451 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001452 AllNodes.push_back(N);
1453 return SDOperand(N, 0);
1454}
1455
1456SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001457 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001458 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001459 SDOperand Ops[] = { N1, N2, N3, N4 };
1460 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001461}
1462
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001463SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1464 SDOperand N1, SDOperand N2, SDOperand N3,
1465 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001466 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1467 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001468}
1469
Evan Cheng7038daf2005-12-10 00:37:58 +00001470SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1471 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001472 const Value *SV, int SVOffset,
1473 bool isVolatile) {
1474 // FIXME: Alignment == 1 for now.
1475 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001476 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng466685d2006-10-09 20:57:25 +00001477 SDOperand Undef = getNode(ISD::UNDEF, VT);
1478 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1479 ID.AddInteger(ISD::UNINDEXED);
1480 ID.AddInteger(ISD::NON_EXTLOAD);
1481 ID.AddInteger(VT);
1482 ID.AddPointer(SV);
1483 ID.AddInteger(SVOffset);
1484 ID.AddInteger(Alignment);
1485 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001486 void *IP = 0;
1487 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1488 return SDOperand(E, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00001489 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::NON_EXTLOAD, VT,
1490 SV, SVOffset, Alignment, isVolatile);
1491 N->setValueTypes(VTs);
1492 CSEMap.InsertNode(N, IP);
1493 AllNodes.push_back(N);
1494 return SDOperand(N, 0);
1495}
1496
1497SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1498 SDOperand Chain, SDOperand Ptr, const Value *SV,
1499 int SVOffset, MVT::ValueType EVT,
1500 bool isVolatile) {
1501 // If they are asking for an extending load from/to the same thing, return a
1502 // normal load.
1503 if (VT == EVT)
1504 ExtType = ISD::NON_EXTLOAD;
1505
1506 if (MVT::isVector(VT))
1507 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1508 else
1509 assert(EVT < VT && "Should only be an extending load, not truncating!");
1510 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1511 "Cannot sign/zero extend a FP/Vector load!");
1512 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1513 "Cannot convert from FP to Int or Int -> FP!");
1514
1515 // FIXME: Alignment == 1 for now.
1516 unsigned Alignment = 1;
1517 SDVTList VTs = getVTList(VT, MVT::Other);
1518 SDOperand Undef = getNode(ISD::UNDEF, VT);
1519 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1520 ID.AddInteger(ISD::UNINDEXED);
1521 ID.AddInteger(ExtType);
1522 ID.AddInteger(EVT);
1523 ID.AddPointer(SV);
1524 ID.AddInteger(SVOffset);
1525 ID.AddInteger(Alignment);
1526 ID.AddInteger(isVolatile);
1527 void *IP = 0;
1528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1529 return SDOperand(E, 0);
1530 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ExtType, EVT, SV, SVOffset,
1531 Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001532 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001533 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001534 AllNodes.push_back(N);
1535 return SDOperand(N, 0);
1536}
1537
1538SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1539 SDOperand Chain, SDOperand Ptr,
1540 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001541 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1542 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001543 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001544}
1545
Evan Chengad071e12006-10-05 22:57:11 +00001546SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1547 SDOperand Ptr, SDOperand SV) {
1548 SDVTList VTs = getVTList(MVT::Other);
1549 SDOperand Ops[] = { Chain, Value, Ptr, SV };
1550 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1551 void *IP = 0;
1552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1553 return SDOperand(E, 0);
1554 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV);
1555 N->setValueTypes(VTs);
1556 CSEMap.InsertNode(N, IP);
1557 AllNodes.push_back(N);
1558 return SDOperand(N, 0);
1559}
1560
Nate Begemanacc398c2006-01-25 18:21:52 +00001561SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1562 SDOperand Chain, SDOperand Ptr,
1563 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001564 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001565 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001566}
1567
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001568SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001569 const SDOperand *Ops, unsigned NumOps) {
1570 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001571 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001572 case 1: return getNode(Opcode, VT, Ops[0]);
1573 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1574 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001575 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001576 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001577
Chris Lattneref847df2005-04-09 03:27:28 +00001578 switch (Opcode) {
1579 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001580 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001581 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001582 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1583#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1584 // If this is a truncating store of a constant, convert to the desired type
1585 // and store it instead.
1586 if (isa<Constant>(Ops[0])) {
1587 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1588 if (isa<Constant>(Op))
1589 N1 = Op;
1590 }
1591 // Also for ConstantFP?
1592#endif
1593 if (Ops[0].getValueType() == EVT) // Normal store?
Evan Chengad071e12006-10-05 22:57:11 +00001594 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001595 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1596 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1597 "Can't do FP-INT conversion!");
1598 break;
1599 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001600 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001601 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001602 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1603 "LHS and RHS of condition must have same type!");
1604 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1605 "True and False arms of SelectCC must have same type!");
1606 assert(Ops[2].getValueType() == VT &&
1607 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001608 break;
1609 }
1610 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001611 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001612 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1613 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001614 break;
1615 }
Chris Lattneref847df2005-04-09 03:27:28 +00001616 }
1617
Chris Lattner385328c2005-05-14 07:42:29 +00001618 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001619 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001620 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001621 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001622 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001623 void *IP = 0;
1624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1625 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001626 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001627 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001628 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001629 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001630 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001631 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001632 }
Chris Lattneref847df2005-04-09 03:27:28 +00001633 AllNodes.push_back(N);
1634 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001635}
1636
Chris Lattner89c34632005-05-14 06:20:26 +00001637SDOperand SelectionDAG::getNode(unsigned Opcode,
1638 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001639 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001640 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1641 Ops, NumOps);
1642}
1643
1644SDOperand SelectionDAG::getNode(unsigned Opcode,
1645 const MVT::ValueType *VTs, unsigned NumVTs,
1646 const SDOperand *Ops, unsigned NumOps) {
1647 if (NumVTs == 1)
1648 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001649 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1650}
1651
1652SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1653 const SDOperand *Ops, unsigned NumOps) {
1654 if (VTList.NumVTs == 1)
1655 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001656
Chris Lattner5f056bf2005-07-10 01:55:33 +00001657 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001658 // FIXME: figure out how to safely handle things like
1659 // int foo(int x) { return 1 << (x & 255); }
1660 // int bar() { return foo(256); }
1661#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001662 case ISD::SRA_PARTS:
1663 case ISD::SRL_PARTS:
1664 case ISD::SHL_PARTS:
1665 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001666 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001667 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1668 else if (N3.getOpcode() == ISD::AND)
1669 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1670 // If the and is only masking out bits that cannot effect the shift,
1671 // eliminate the and.
1672 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1673 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1674 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1675 }
1676 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001677#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001678 }
Chris Lattner89c34632005-05-14 06:20:26 +00001679
Chris Lattner43247a12005-08-25 19:12:10 +00001680 // Memoize the node unless it returns a flag.
1681 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001682 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001683 SelectionDAGCSEMap::NodeID ID;
1684 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001685 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001686 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001687 void *IP = 0;
1688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1689 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001690 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001691 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001692 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001693 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001694 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001695 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001696 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001697 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001698 return SDOperand(N, 0);
1699}
1700
Chris Lattner70046e92006-08-15 17:46:01 +00001701SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1702 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001703}
1704
Chris Lattner70046e92006-08-15 17:46:01 +00001705SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001706 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1707 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001708 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001709 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001710 }
1711 std::vector<MVT::ValueType> V;
1712 V.push_back(VT1);
1713 V.push_back(VT2);
1714 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001715 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001716}
Chris Lattner70046e92006-08-15 17:46:01 +00001717SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1718 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001719 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001720 E = VTList.end(); I != E; ++I) {
1721 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1722 (*I)[2] == VT3)
1723 return makeVTList(&(*I)[0], 3);
1724 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001725 std::vector<MVT::ValueType> V;
1726 V.push_back(VT1);
1727 V.push_back(VT2);
1728 V.push_back(VT3);
1729 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001730 return makeVTList(&(*VTList.begin())[0], 3);
1731}
1732
1733SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1734 switch (NumVTs) {
1735 case 0: assert(0 && "Cannot have nodes without results!");
1736 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1737 case 2: return getVTList(VTs[0], VTs[1]);
1738 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1739 default: break;
1740 }
1741
1742 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1743 E = VTList.end(); I != E; ++I) {
1744 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1745
1746 bool NoMatch = false;
1747 for (unsigned i = 2; i != NumVTs; ++i)
1748 if (VTs[i] != (*I)[i]) {
1749 NoMatch = true;
1750 break;
1751 }
1752 if (!NoMatch)
1753 return makeVTList(&*I->begin(), NumVTs);
1754 }
1755
1756 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1757 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001758}
1759
1760
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001761/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1762/// specified operands. If the resultant node already exists in the DAG,
1763/// this does not modify the specified node, instead it returns the node that
1764/// already exists. If the resultant node does not exist in the DAG, the
1765/// input node is returned. As a degenerate case, if you specify the same
1766/// input operands as the node already has, the input node is returned.
1767SDOperand SelectionDAG::
1768UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1769 SDNode *N = InN.Val;
1770 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1771
1772 // Check to see if there is no change.
1773 if (Op == N->getOperand(0)) return InN;
1774
1775 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001776 void *InsertPos = 0;
1777 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1778 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001779
1780 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001781 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001782 RemoveNodeFromCSEMaps(N);
1783
1784 // Now we update the operands.
1785 N->OperandList[0].Val->removeUser(N);
1786 Op.Val->addUser(N);
1787 N->OperandList[0] = Op;
1788
1789 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001790 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001791 return InN;
1792}
1793
1794SDOperand SelectionDAG::
1795UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1796 SDNode *N = InN.Val;
1797 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1798
1799 // Check to see if there is no change.
1800 bool AnyChange = false;
1801 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1802 return InN; // No operands changed, just return the input node.
1803
1804 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001805 void *InsertPos = 0;
1806 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1807 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001808
1809 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001810 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001811 RemoveNodeFromCSEMaps(N);
1812
1813 // Now we update the operands.
1814 if (N->OperandList[0] != Op1) {
1815 N->OperandList[0].Val->removeUser(N);
1816 Op1.Val->addUser(N);
1817 N->OperandList[0] = Op1;
1818 }
1819 if (N->OperandList[1] != Op2) {
1820 N->OperandList[1].Val->removeUser(N);
1821 Op2.Val->addUser(N);
1822 N->OperandList[1] = Op2;
1823 }
1824
1825 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001826 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001827 return InN;
1828}
1829
1830SDOperand SelectionDAG::
1831UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001832 SDOperand Ops[] = { Op1, Op2, Op3 };
1833 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001834}
1835
1836SDOperand SelectionDAG::
1837UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1838 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001839 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1840 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001841}
1842
1843SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001844UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1845 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001846 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1847 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001848}
1849
1850
1851SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001852UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001853 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001854 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001855 "Update with wrong number of operands");
1856
1857 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001858 bool AnyChange = false;
1859 for (unsigned i = 0; i != NumOps; ++i) {
1860 if (Ops[i] != N->getOperand(i)) {
1861 AnyChange = true;
1862 break;
1863 }
1864 }
1865
1866 // No operands changed, just return the input node.
1867 if (!AnyChange) return InN;
1868
1869 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001870 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001871 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001872 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001873
1874 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001875 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001876 RemoveNodeFromCSEMaps(N);
1877
1878 // Now we update the operands.
1879 for (unsigned i = 0; i != NumOps; ++i) {
1880 if (N->OperandList[i] != Ops[i]) {
1881 N->OperandList[i].Val->removeUser(N);
1882 Ops[i].Val->addUser(N);
1883 N->OperandList[i] = Ops[i];
1884 }
1885 }
1886
1887 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001888 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001889 return InN;
1890}
1891
1892
1893
Chris Lattner149c58c2005-08-16 18:17:10 +00001894
1895/// SelectNodeTo - These are used for target selectors to *mutate* the
1896/// specified node to have the specified return type, Target opcode, and
1897/// operands. Note that target opcodes are stored as
1898/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001899///
1900/// Note that SelectNodeTo returns the resultant node. If there is already a
1901/// node of the specified opcode and operands, it returns that node instead of
1902/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00001903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1904 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001905 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001906 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1907 void *IP = 0;
1908 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001909 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00001910
Chris Lattner7651fa42005-08-24 23:00:29 +00001911 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001912
Chris Lattner7651fa42005-08-24 23:00:29 +00001913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001914 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001915
Chris Lattner4a283e92006-08-11 18:38:11 +00001916 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001917 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00001918}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001919
Evan Cheng95514ba2006-08-26 08:00:10 +00001920SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1921 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001922 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001923 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001924 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1925 void *IP = 0;
1926 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001927 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001928
Chris Lattner149c58c2005-08-16 18:17:10 +00001929 RemoveNodeFromCSEMaps(N);
1930 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001931 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001932 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001933 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001934 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001935}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001936
Evan Cheng95514ba2006-08-26 08:00:10 +00001937SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1938 MVT::ValueType VT, SDOperand Op1,
1939 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001940 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001941 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001942 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1943 void *IP = 0;
1944 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001945 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001946
Chris Lattner149c58c2005-08-16 18:17:10 +00001947 RemoveNodeFromCSEMaps(N);
1948 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001949 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001950 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001951
Chris Lattnera5682852006-08-07 23:03:03 +00001952 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001953 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001954}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001955
Evan Cheng95514ba2006-08-26 08:00:10 +00001956SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1957 MVT::ValueType VT, SDOperand Op1,
1958 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001959 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001960 SDVTList VTs = getVTList(VT);
1961 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1962 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001963 void *IP = 0;
1964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001965 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001966
Chris Lattner149c58c2005-08-16 18:17:10 +00001967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001969 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001970 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971
Chris Lattnera5682852006-08-07 23:03:03 +00001972 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001973 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001974}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001975
Evan Cheng95514ba2006-08-26 08:00:10 +00001976SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00001977 MVT::ValueType VT, const SDOperand *Ops,
1978 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001979 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001980 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001981 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001982 for (unsigned i = 0; i != NumOps; ++i)
1983 ID.AddOperand(Ops[i]);
Chris Lattnera5682852006-08-07 23:03:03 +00001984 void *IP = 0;
1985 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001986 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001987
Chris Lattner6b09a292005-08-21 18:49:33 +00001988 RemoveNodeFromCSEMaps(N);
1989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001990 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001991 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001992
Chris Lattnera5682852006-08-07 23:03:03 +00001993 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001994 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001995}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001996
Evan Cheng95514ba2006-08-26 08:00:10 +00001997SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1998 MVT::ValueType VT1, MVT::ValueType VT2,
1999 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002000 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002001 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2002 void *IP = 0;
2003 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002004 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002005
Chris Lattner0fb094f2005-11-19 01:44:53 +00002006 RemoveNodeFromCSEMaps(N);
2007 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002008 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002009 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002010
Chris Lattnera5682852006-08-07 23:03:03 +00002011 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002012 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002013}
2014
Evan Cheng95514ba2006-08-26 08:00:10 +00002015SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2016 MVT::ValueType VT1, MVT::ValueType VT2,
2017 SDOperand Op1, SDOperand Op2,
2018 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002019 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002020 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002021 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2022 Op1, Op2, Op3);
2023 void *IP = 0;
2024 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002025 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026
Chris Lattner0fb094f2005-11-19 01:44:53 +00002027 RemoveNodeFromCSEMaps(N);
2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002029 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002030 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002031
Chris Lattnera5682852006-08-07 23:03:03 +00002032 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002033 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002034}
2035
Chris Lattner0fb094f2005-11-19 01:44:53 +00002036
Evan Cheng6ae46c42006-02-09 07:15:23 +00002037/// getTargetNode - These are used for target selectors to create a new node
2038/// with specified return type(s), target opcode, and operands.
2039///
2040/// Note that getTargetNode returns the resultant node. If there is already a
2041/// node of the specified opcode and operands, it returns that node instead of
2042/// the current one.
2043SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2044 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2045}
2046SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2047 SDOperand Op1) {
2048 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2049}
2050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2051 SDOperand Op1, SDOperand Op2) {
2052 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2053}
2054SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2055 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2056 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2057}
2058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002059 const SDOperand *Ops, unsigned NumOps) {
2060 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002061}
2062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2063 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002064 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002065 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002066}
2067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002068 MVT::ValueType VT2, SDOperand Op1,
2069 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002070 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002071 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002072 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002073}
2074SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002075 MVT::ValueType VT2, SDOperand Op1,
2076 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002077 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002078 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002079 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002080}
Evan Cheng694481e2006-08-27 08:08:54 +00002081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2082 MVT::ValueType VT2,
2083 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002084 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002085 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002086}
2087SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2088 MVT::ValueType VT2, MVT::ValueType VT3,
2089 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002090 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002091 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002092 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002093}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002095 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002096 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002097 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2098 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002099}
2100
Evan Cheng99157a02006-08-07 22:13:29 +00002101/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002102/// This can cause recursive merging of nodes in the DAG.
2103///
Chris Lattner8b52f212005-08-26 18:36:28 +00002104/// This version assumes From/To have a single result value.
2105///
Chris Lattner1e111c72005-09-07 05:37:01 +00002106void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2107 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002108 SDNode *From = FromN.Val, *To = ToN.Val;
2109 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2110 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002111 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002112
Chris Lattner8b8749f2005-08-17 19:00:20 +00002113 while (!From->use_empty()) {
2114 // Process users until they are all gone.
2115 SDNode *U = *From->use_begin();
2116
2117 // This node is about to morph, remove its old self from the CSE maps.
2118 RemoveNodeFromCSEMaps(U);
2119
Chris Lattner65113b22005-11-08 22:07:03 +00002120 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2121 I != E; ++I)
2122 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002123 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002124 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002125 To->addUser(U);
2126 }
2127
2128 // Now that we have modified U, add it back to the CSE maps. If it already
2129 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002130 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2131 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002132 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002133 if (Deleted) Deleted->push_back(U);
2134 DeleteNodeNotInCSEMaps(U);
2135 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002136 }
2137}
2138
2139/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2140/// This can cause recursive merging of nodes in the DAG.
2141///
2142/// This version assumes From/To have matching types and numbers of result
2143/// values.
2144///
Chris Lattner1e111c72005-09-07 05:37:01 +00002145void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2146 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002147 assert(From != To && "Cannot replace uses of with self");
2148 assert(From->getNumValues() == To->getNumValues() &&
2149 "Cannot use this version of ReplaceAllUsesWith!");
2150 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002151 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002152 return;
2153 }
2154
2155 while (!From->use_empty()) {
2156 // Process users until they are all gone.
2157 SDNode *U = *From->use_begin();
2158
2159 // This node is about to morph, remove its old self from the CSE maps.
2160 RemoveNodeFromCSEMaps(U);
2161
Chris Lattner65113b22005-11-08 22:07:03 +00002162 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2163 I != E; ++I)
2164 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002165 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002166 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002167 To->addUser(U);
2168 }
2169
2170 // Now that we have modified U, add it back to the CSE maps. If it already
2171 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002172 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2173 ReplaceAllUsesWith(U, Existing, Deleted);
2174 // U is now dead.
2175 if (Deleted) Deleted->push_back(U);
2176 DeleteNodeNotInCSEMaps(U);
2177 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002178 }
2179}
2180
Chris Lattner8b52f212005-08-26 18:36:28 +00002181/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2182/// This can cause recursive merging of nodes in the DAG.
2183///
2184/// This version can replace From with any result values. To must match the
2185/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002186void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002187 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002188 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002189 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002190 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002191 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002192 return;
2193 }
2194
2195 while (!From->use_empty()) {
2196 // Process users until they are all gone.
2197 SDNode *U = *From->use_begin();
2198
2199 // This node is about to morph, remove its old self from the CSE maps.
2200 RemoveNodeFromCSEMaps(U);
2201
Chris Lattner65113b22005-11-08 22:07:03 +00002202 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2203 I != E; ++I)
2204 if (I->Val == From) {
2205 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002206 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002207 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002208 ToOp.Val->addUser(U);
2209 }
2210
2211 // Now that we have modified U, add it back to the CSE maps. If it already
2212 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002213 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2214 ReplaceAllUsesWith(U, Existing, Deleted);
2215 // U is now dead.
2216 if (Deleted) Deleted->push_back(U);
2217 DeleteNodeNotInCSEMaps(U);
2218 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002219 }
2220}
2221
Chris Lattner012f2412006-02-17 21:58:01 +00002222/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2223/// uses of other values produced by From.Val alone. The Deleted vector is
2224/// handled the same was as for ReplaceAllUsesWith.
2225void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2226 std::vector<SDNode*> &Deleted) {
2227 assert(From != To && "Cannot replace a value with itself");
2228 // Handle the simple, trivial, case efficiently.
2229 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2230 ReplaceAllUsesWith(From, To, &Deleted);
2231 return;
2232 }
2233
2234 // Get all of the users in a nice, deterministically ordered, uniqued set.
2235 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2236
2237 while (!Users.empty()) {
2238 // We know that this user uses some value of From. If it is the right
2239 // value, update it.
2240 SDNode *User = Users.back();
2241 Users.pop_back();
2242
2243 for (SDOperand *Op = User->OperandList,
2244 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2245 if (*Op == From) {
2246 // Okay, we know this user needs to be updated. Remove its old self
2247 // from the CSE maps.
2248 RemoveNodeFromCSEMaps(User);
2249
2250 // Update all operands that match "From".
2251 for (; Op != E; ++Op) {
2252 if (*Op == From) {
2253 From.Val->removeUser(User);
2254 *Op = To;
2255 To.Val->addUser(User);
2256 }
2257 }
2258
2259 // Now that we have modified User, add it back to the CSE maps. If it
2260 // already exists there, recursively merge the results together.
2261 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2262 unsigned NumDeleted = Deleted.size();
2263 ReplaceAllUsesWith(User, Existing, &Deleted);
2264
2265 // User is now dead.
2266 Deleted.push_back(User);
2267 DeleteNodeNotInCSEMaps(User);
2268
2269 // We have to be careful here, because ReplaceAllUsesWith could have
2270 // deleted a user of From, which means there may be dangling pointers
2271 // in the "Users" setvector. Scan over the deleted node pointers and
2272 // remove them from the setvector.
2273 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2274 Users.remove(Deleted[i]);
2275 }
2276 break; // Exit the operand scanning loop.
2277 }
2278 }
2279 }
2280}
2281
Chris Lattner7b2880c2005-08-24 22:44:39 +00002282
Evan Chenge6f35d82006-08-01 08:20:41 +00002283/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2284/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002285unsigned SelectionDAG::AssignNodeIds() {
2286 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002287 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2288 SDNode *N = I;
2289 N->setNodeId(Id++);
2290 }
2291 return Id;
2292}
2293
Evan Chenge6f35d82006-08-01 08:20:41 +00002294/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002295/// based on their topological order. It returns the maximum id and a vector
2296/// of the SDNodes* in assigned order by reference.
2297unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002298 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002299 std::vector<unsigned> InDegree(DAGSize);
2300 std::vector<SDNode*> Sources;
2301
2302 // Use a two pass approach to avoid using a std::map which is slow.
2303 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002304 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2305 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002306 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002307 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002308 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002309 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002310 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002311 }
2312
Evan Cheng99157a02006-08-07 22:13:29 +00002313 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002314 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002315 SDNode *N = Sources.back();
2316 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002317 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002318 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2319 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002320 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002321 if (Degree == 0)
2322 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002323 }
2324 }
2325
Evan Chengc384d6c2006-08-02 22:00:34 +00002326 // Second pass, assign the actual topological order as node ids.
2327 Id = 0;
2328 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2329 TI != TE; ++TI)
2330 (*TI)->setNodeId(Id++);
2331
2332 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002333}
2334
2335
Evan Cheng091cba12006-07-27 06:39:06 +00002336
Jim Laskey58b968b2005-08-17 20:08:02 +00002337//===----------------------------------------------------------------------===//
2338// SDNode Class
2339//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002340
Chris Lattner917d2c92006-07-19 00:00:37 +00002341// Out-of-line virtual method to give class a home.
2342void SDNode::ANCHOR() {
2343}
2344
Chris Lattnera3255112005-11-08 23:30:28 +00002345/// getValueTypeList - Return a pointer to the specified value type.
2346///
2347MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2348 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2349 VTs[VT] = VT;
2350 return &VTs[VT];
2351}
Chris Lattnera5682852006-08-07 23:03:03 +00002352
Chris Lattner5c884562005-01-12 18:37:47 +00002353/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2354/// indicated value. This method ignores uses of other values defined by this
2355/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002356bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002357 assert(Value < getNumValues() && "Bad value!");
2358
2359 // If there is only one value, this is easy.
2360 if (getNumValues() == 1)
2361 return use_size() == NUses;
2362 if (Uses.size() < NUses) return false;
2363
Evan Cheng4ee62112006-02-05 06:29:23 +00002364 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002365
2366 std::set<SDNode*> UsersHandled;
2367
Chris Lattnerf83482d2006-08-16 20:59:32 +00002368 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002369 SDNode *User = *UI;
2370 if (User->getNumOperands() == 1 ||
2371 UsersHandled.insert(User).second) // First time we've seen this?
2372 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2373 if (User->getOperand(i) == TheValue) {
2374 if (NUses == 0)
2375 return false; // too many uses
2376 --NUses;
2377 }
2378 }
2379
2380 // Found exactly the right number of uses?
2381 return NUses == 0;
2382}
2383
2384
Evan Cheng4ee62112006-02-05 06:29:23 +00002385// isOnlyUse - Return true if this node is the only use of N.
2386bool SDNode::isOnlyUse(SDNode *N) const {
2387 bool Seen = false;
2388 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2389 SDNode *User = *I;
2390 if (User == this)
2391 Seen = true;
2392 else
2393 return false;
2394 }
2395
2396 return Seen;
2397}
2398
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002399// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002400bool SDOperand::isOperand(SDNode *N) const {
2401 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2402 if (*this == N->getOperand(i))
2403 return true;
2404 return false;
2405}
2406
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002407bool SDNode::isOperand(SDNode *N) const {
2408 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2409 if (this == N->OperandList[i].Val)
2410 return true;
2411 return false;
2412}
Evan Cheng4ee62112006-02-05 06:29:23 +00002413
Evan Chengc5484282006-10-04 00:56:09 +00002414uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2415 assert(Num < NumOperands && "Invalid child # of SDNode!");
2416 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2417}
2418
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002419const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002420 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002421 default:
2422 if (getOpcode() < ISD::BUILTIN_OP_END)
2423 return "<<Unknown DAG Node>>";
2424 else {
Evan Cheng72261582005-12-20 06:22:03 +00002425 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002426 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002427 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2428 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002429
Evan Cheng72261582005-12-20 06:22:03 +00002430 TargetLowering &TLI = G->getTargetLoweringInfo();
2431 const char *Name =
2432 TLI.getTargetNodeName(getOpcode());
2433 if (Name) return Name;
2434 }
2435
2436 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002437 }
2438
Andrew Lenharth95762122005-03-31 21:24:06 +00002439 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002440 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002441 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002442 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002443 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002444 case ISD::AssertSext: return "AssertSext";
2445 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002446
2447 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002448 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002449 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002450 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002451
2452 case ISD::Constant: return "Constant";
2453 case ISD::ConstantFP: return "ConstantFP";
2454 case ISD::GlobalAddress: return "GlobalAddress";
2455 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002456 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth16113432006-09-26 20:02:30 +00002457 case ISD::JumpTableRelocBase: return "JumpTableRelocBase";
Chris Lattner5839bf22005-08-26 17:15:30 +00002458 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002459 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002460 case ISD::INTRINSIC_WO_CHAIN: {
2461 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2462 return Intrinsic::getName((Intrinsic::ID)IID);
2463 }
2464 case ISD::INTRINSIC_VOID:
2465 case ISD::INTRINSIC_W_CHAIN: {
2466 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002467 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002468 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002469
Chris Lattnerb2827b02006-03-19 00:52:58 +00002470 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002471 case ISD::TargetConstant: return "TargetConstant";
2472 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002473 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2474 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002475 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002476 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002477 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002478
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002479 case ISD::CopyToReg: return "CopyToReg";
2480 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002481 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002482 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002483 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002484 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002485 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002486 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002487
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002488 // Unary operators
2489 case ISD::FABS: return "fabs";
2490 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002491 case ISD::FSQRT: return "fsqrt";
2492 case ISD::FSIN: return "fsin";
2493 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002494 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002495
2496 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002497 case ISD::ADD: return "add";
2498 case ISD::SUB: return "sub";
2499 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002500 case ISD::MULHU: return "mulhu";
2501 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002502 case ISD::SDIV: return "sdiv";
2503 case ISD::UDIV: return "udiv";
2504 case ISD::SREM: return "srem";
2505 case ISD::UREM: return "urem";
2506 case ISD::AND: return "and";
2507 case ISD::OR: return "or";
2508 case ISD::XOR: return "xor";
2509 case ISD::SHL: return "shl";
2510 case ISD::SRA: return "sra";
2511 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002512 case ISD::ROTL: return "rotl";
2513 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002514 case ISD::FADD: return "fadd";
2515 case ISD::FSUB: return "fsub";
2516 case ISD::FMUL: return "fmul";
2517 case ISD::FDIV: return "fdiv";
2518 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002519 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002520 case ISD::VADD: return "vadd";
2521 case ISD::VSUB: return "vsub";
2522 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002523 case ISD::VSDIV: return "vsdiv";
2524 case ISD::VUDIV: return "vudiv";
2525 case ISD::VAND: return "vand";
2526 case ISD::VOR: return "vor";
2527 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002528
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002529 case ISD::SETCC: return "setcc";
2530 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002531 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002532 case ISD::VSELECT: return "vselect";
2533 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2534 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2535 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002536 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002537 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2538 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2539 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2540 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2541 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002542 case ISD::ADDC: return "addc";
2543 case ISD::ADDE: return "adde";
2544 case ISD::SUBC: return "subc";
2545 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002546 case ISD::SHL_PARTS: return "shl_parts";
2547 case ISD::SRA_PARTS: return "sra_parts";
2548 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002549
Chris Lattner7f644642005-04-28 21:44:03 +00002550 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002551 case ISD::SIGN_EXTEND: return "sign_extend";
2552 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002553 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002554 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002555 case ISD::TRUNCATE: return "truncate";
2556 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002557 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002558 case ISD::FP_EXTEND: return "fp_extend";
2559
2560 case ISD::SINT_TO_FP: return "sint_to_fp";
2561 case ISD::UINT_TO_FP: return "uint_to_fp";
2562 case ISD::FP_TO_SINT: return "fp_to_sint";
2563 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002564 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002565
2566 // Control flow instructions
2567 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002568 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002569 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002570 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002571 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002572 case ISD::CALLSEQ_START: return "callseq_start";
2573 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002574
2575 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002576 case ISD::LOAD: return "load";
2577 case ISD::STORE: return "store";
2578 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002579 case ISD::TRUNCSTORE: return "truncstore";
2580 case ISD::VAARG: return "vaarg";
2581 case ISD::VACOPY: return "vacopy";
2582 case ISD::VAEND: return "vaend";
2583 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002584 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002585 case ISD::EXTRACT_ELEMENT: return "extract_element";
2586 case ISD::BUILD_PAIR: return "build_pair";
2587 case ISD::STACKSAVE: return "stacksave";
2588 case ISD::STACKRESTORE: return "stackrestore";
2589
2590 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002591 case ISD::MEMSET: return "memset";
2592 case ISD::MEMCPY: return "memcpy";
2593 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002594
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002595 // Bit manipulation
2596 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002597 case ISD::CTPOP: return "ctpop";
2598 case ISD::CTTZ: return "cttz";
2599 case ISD::CTLZ: return "ctlz";
2600
Chris Lattner36ce6912005-11-29 06:21:05 +00002601 // Debug info
2602 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002603 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002604 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002605
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002606 case ISD::CONDCODE:
2607 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002608 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002609 case ISD::SETOEQ: return "setoeq";
2610 case ISD::SETOGT: return "setogt";
2611 case ISD::SETOGE: return "setoge";
2612 case ISD::SETOLT: return "setolt";
2613 case ISD::SETOLE: return "setole";
2614 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002615
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002616 case ISD::SETO: return "seto";
2617 case ISD::SETUO: return "setuo";
2618 case ISD::SETUEQ: return "setue";
2619 case ISD::SETUGT: return "setugt";
2620 case ISD::SETUGE: return "setuge";
2621 case ISD::SETULT: return "setult";
2622 case ISD::SETULE: return "setule";
2623 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002624
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002625 case ISD::SETEQ: return "seteq";
2626 case ISD::SETGT: return "setgt";
2627 case ISD::SETGE: return "setge";
2628 case ISD::SETLT: return "setlt";
2629 case ISD::SETLE: return "setle";
2630 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002631 }
2632 }
2633}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002634
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002635void SDNode::dump() const { dump(0); }
2636void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002637 std::cerr << (void*)this << ": ";
2638
2639 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2640 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002641 if (getValueType(i) == MVT::Other)
2642 std::cerr << "ch";
2643 else
2644 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002645 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002646 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002647
2648 std::cerr << " ";
2649 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2650 if (i) std::cerr << ", ";
2651 std::cerr << (void*)getOperand(i).Val;
2652 if (unsigned RN = getOperand(i).ResNo)
2653 std::cerr << ":" << RN;
2654 }
2655
2656 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2657 std::cerr << "<" << CSDN->getValue() << ">";
2658 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2659 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002660 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002661 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002662 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002663 std::cerr << "<";
2664 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002665 if (offset > 0)
2666 std::cerr << " + " << offset;
2667 else
2668 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002669 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002670 std::cerr << "<" << FIDN->getIndex() << ">";
2671 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002672 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002673 if (CP->isMachineConstantPoolEntry())
2674 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2675 else
2676 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002677 if (offset > 0)
2678 std::cerr << " + " << offset;
2679 else
2680 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002681 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002682 std::cerr << "<";
2683 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2684 if (LBB)
2685 std::cerr << LBB->getName() << " ";
2686 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002687 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002688 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002689 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2690 } else {
2691 std::cerr << " #" << R->getReg();
2692 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002693 } else if (const ExternalSymbolSDNode *ES =
2694 dyn_cast<ExternalSymbolSDNode>(this)) {
2695 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002696 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2697 if (M->getValue())
2698 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2699 else
2700 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002701 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2702 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002703 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2704 bool doExt = true;
2705 switch (LD->getExtensionType()) {
2706 default: doExt = false; break;
2707 case ISD::EXTLOAD:
2708 std::cerr << " <anyext ";
2709 break;
2710 case ISD::SEXTLOAD:
2711 std::cerr << " <sext ";
2712 break;
2713 case ISD::ZEXTLOAD:
2714 std::cerr << " <zext ";
2715 break;
2716 }
2717 if (doExt)
2718 std::cerr << MVT::getValueTypeString(LD->getLoadVT()) << ">";
2719
2720 if (LD->getAddressingMode() == ISD::PRE_INDEXED)
2721 std::cerr << " <pre>";
2722 else if (LD->getAddressingMode() == ISD::POST_INDEXED)
2723 std::cerr << " <post>";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002724 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002725}
2726
Chris Lattnerde202b32005-11-09 23:47:37 +00002727static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002728 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2729 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002730 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002731 else
2732 std::cerr << "\n" << std::string(indent+2, ' ')
2733 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002734
Chris Lattnerea946cd2005-01-09 20:38:33 +00002735
2736 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002737 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002738}
2739
Chris Lattnerc3aae252005-01-07 07:46:32 +00002740void SelectionDAG::dump() const {
2741 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002742 std::vector<const SDNode*> Nodes;
2743 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2744 I != E; ++I)
2745 Nodes.push_back(I);
2746
Chris Lattner49d24712005-01-09 20:26:36 +00002747 std::sort(Nodes.begin(), Nodes.end());
2748
2749 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002750 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002751 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002752 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002753
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002754 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002755
Chris Lattnerc3aae252005-01-07 07:46:32 +00002756 std::cerr << "\n\n";
2757}
2758
Evan Chengd6594ae2006-09-12 21:00:35 +00002759const Type *ConstantPoolSDNode::getType() const {
2760 if (isMachineConstantPoolEntry())
2761 return Val.MachineCPVal->getType();
2762 return Val.ConstVal->getType();
2763}