blob: 61f0a49d7a62f77b820471a1a233836ec13d62d1 [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());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000457 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000458 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000459}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000460
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000461
Chris Lattner78ec3112003-08-11 14:57:33 +0000462SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000463 while (!AllNodes.empty()) {
464 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000465 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000466 delete [] N->OperandList;
467 N->OperandList = 0;
468 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000469 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000470 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000471}
472
Chris Lattner0f2287b2005-04-13 02:38:18 +0000473SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000474 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000475 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000476 return getNode(ISD::AND, Op.getValueType(), Op,
477 getConstant(Imm, Op.getValueType()));
478}
479
Chris Lattner36ce6912005-11-29 06:21:05 +0000480SDOperand SelectionDAG::getString(const std::string &Val) {
481 StringSDNode *&N = StringNodes[Val];
482 if (!N) {
483 N = new StringSDNode(Val);
484 AllNodes.push_back(N);
485 }
486 return SDOperand(N, 0);
487}
488
Chris Lattner61b09412006-08-11 21:01:22 +0000489SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000490 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000491 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000492
Chris Lattner61b09412006-08-11 21:01:22 +0000493 // Mask out any bits that are not valid for this constant.
494 Val &= MVT::getIntVTBitMask(VT);
495
496 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000497 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000498 ID.AddInteger(Val);
499 void *IP = 0;
500 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
501 return SDOperand(E, 0);
502 SDNode *N = new ConstantSDNode(isT, Val, VT);
503 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000504 AllNodes.push_back(N);
505 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000506}
507
Chris Lattner61b09412006-08-11 21:01:22 +0000508
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000509SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
510 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000511 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
512 if (VT == MVT::f32)
513 Val = (float)Val; // Mask out extra precision.
514
Chris Lattnerd8658612005-02-17 20:17:32 +0000515 // Do the map lookup using the actual bit pattern for the floating point
516 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
517 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000518 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000519 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000520 ID.AddInteger(DoubleToBits(Val));
521 void *IP = 0;
522 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
523 return SDOperand(E, 0);
524 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
525 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
528}
529
Chris Lattnerc3aae252005-01-07 07:46:32 +0000530SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000531 MVT::ValueType VT, int Offset,
532 bool isTargetGA) {
533 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000534 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000535 ID.AddPointer(GV);
536 ID.AddInteger(Offset);
537 void *IP = 0;
538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
539 return SDOperand(E, 0);
540 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
541 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
544}
545
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000546SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
547 bool isTarget) {
548 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000549 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000550 ID.AddInteger(FI);
551 void *IP = 0;
552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
553 return SDOperand(E, 0);
554 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
555 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
558}
559
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000560SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
561 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000562 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000563 ID.AddInteger(JTI);
564 void *IP = 0;
565 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
566 return SDOperand(E, 0);
567 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
568 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000569 AllNodes.push_back(N);
570 return SDOperand(N, 0);
571}
572
Evan Chengb8973bd2006-01-31 22:23:14 +0000573SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000574 unsigned Alignment, int Offset,
575 bool isTarget) {
576 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000577 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000578 ID.AddInteger(Alignment);
579 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000580 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000581 void *IP = 0;
582 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
583 return SDOperand(E, 0);
584 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
585 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000586 AllNodes.push_back(N);
587 return SDOperand(N, 0);
588}
589
Chris Lattnerc3aae252005-01-07 07:46:32 +0000590
Evan Chengd6594ae2006-09-12 21:00:35 +0000591SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
592 MVT::ValueType VT,
593 unsigned Alignment, int Offset,
594 bool isTarget) {
595 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
596 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
597 ID.AddInteger(Alignment);
598 ID.AddInteger(Offset);
599 C->AddSelectionDAGCSEId(&ID);
600 void *IP = 0;
601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
602 return SDOperand(E, 0);
603 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
604 CSEMap.InsertNode(N, IP);
605 AllNodes.push_back(N);
606 return SDOperand(N, 0);
607}
608
609
Chris Lattnerc3aae252005-01-07 07:46:32 +0000610SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000611 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000612 ID.AddPointer(MBB);
613 void *IP = 0;
614 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
615 return SDOperand(E, 0);
616 SDNode *N = new BasicBlockSDNode(MBB);
617 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000618 AllNodes.push_back(N);
619 return SDOperand(N, 0);
620}
621
Chris Lattner15e4b012005-07-10 00:07:11 +0000622SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
623 if ((unsigned)VT >= ValueTypeNodes.size())
624 ValueTypeNodes.resize(VT+1);
625 if (ValueTypeNodes[VT] == 0) {
626 ValueTypeNodes[VT] = new VTSDNode(VT);
627 AllNodes.push_back(ValueTypeNodes[VT]);
628 }
629
630 return SDOperand(ValueTypeNodes[VT], 0);
631}
632
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
634 SDNode *&N = ExternalSymbols[Sym];
635 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000636 N = new ExternalSymbolSDNode(false, Sym, VT);
637 AllNodes.push_back(N);
638 return SDOperand(N, 0);
639}
640
Chris Lattner809ec112006-01-28 10:09:25 +0000641SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
642 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000643 SDNode *&N = TargetExternalSymbols[Sym];
644 if (N) return SDOperand(N, 0);
645 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
648}
649
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000650SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
651 if ((unsigned)Cond >= CondCodeNodes.size())
652 CondCodeNodes.resize(Cond+1);
653
Chris Lattner079a27a2005-08-09 20:40:02 +0000654 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000655 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000656 AllNodes.push_back(CondCodeNodes[Cond]);
657 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000658 return SDOperand(CondCodeNodes[Cond], 0);
659}
660
Chris Lattner0fdd7682005-08-30 22:38:38 +0000661SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000662 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000663 ID.AddInteger(RegNo);
664 void *IP = 0;
665 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
666 return SDOperand(E, 0);
667 SDNode *N = new RegisterSDNode(RegNo, VT);
668 CSEMap.InsertNode(N, IP);
669 AllNodes.push_back(N);
670 return SDOperand(N, 0);
671}
672
673SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
674 assert((!V || isa<PointerType>(V->getType())) &&
675 "SrcValue is not a pointer?");
676
Chris Lattner0b3e5252006-08-15 19:11:05 +0000677 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000678 ID.AddPointer(V);
679 ID.AddInteger(Offset);
680 void *IP = 0;
681 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
682 return SDOperand(E, 0);
683 SDNode *N = new SrcValueSDNode(V, Offset);
684 CSEMap.InsertNode(N, IP);
685 AllNodes.push_back(N);
686 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000687}
688
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000689SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
690 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000691 // These setcc operations always fold.
692 switch (Cond) {
693 default: break;
694 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000695 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000697 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000698
699 case ISD::SETOEQ:
700 case ISD::SETOGT:
701 case ISD::SETOGE:
702 case ISD::SETOLT:
703 case ISD::SETOLE:
704 case ISD::SETONE:
705 case ISD::SETO:
706 case ISD::SETUO:
707 case ISD::SETUEQ:
708 case ISD::SETUNE:
709 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
710 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000711 }
712
Chris Lattner67255a12005-04-07 18:14:58 +0000713 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
714 uint64_t C2 = N2C->getValue();
715 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
716 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000717
Chris Lattnerc3aae252005-01-07 07:46:32 +0000718 // Sign extend the operands if required
719 if (ISD::isSignedIntSetCC(Cond)) {
720 C1 = N1C->getSignExtended();
721 C2 = N2C->getSignExtended();
722 }
723
724 switch (Cond) {
725 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000726 case ISD::SETEQ: return getConstant(C1 == C2, VT);
727 case ISD::SETNE: return getConstant(C1 != C2, VT);
728 case ISD::SETULT: return getConstant(C1 < C2, VT);
729 case ISD::SETUGT: return getConstant(C1 > C2, VT);
730 case ISD::SETULE: return getConstant(C1 <= C2, VT);
731 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
732 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
733 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
734 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
735 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000736 }
Chris Lattner24673922005-04-07 18:58:54 +0000737 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000738 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000739 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
740 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
741
742 // If the comparison constant has bits in the upper part, the
743 // zero-extended value could never match.
744 if (C2 & (~0ULL << InSize)) {
745 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
746 switch (Cond) {
747 case ISD::SETUGT:
748 case ISD::SETUGE:
749 case ISD::SETEQ: return getConstant(0, VT);
750 case ISD::SETULT:
751 case ISD::SETULE:
752 case ISD::SETNE: return getConstant(1, VT);
753 case ISD::SETGT:
754 case ISD::SETGE:
755 // True if the sign bit of C2 is set.
756 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
757 case ISD::SETLT:
758 case ISD::SETLE:
759 // True if the sign bit of C2 isn't set.
760 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
761 default:
762 break;
763 }
764 }
765
766 // Otherwise, we can perform the comparison with the low bits.
767 switch (Cond) {
768 case ISD::SETEQ:
769 case ISD::SETNE:
770 case ISD::SETUGT:
771 case ISD::SETUGE:
772 case ISD::SETULT:
773 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000774 return getSetCC(VT, N1.getOperand(0),
775 getConstant(C2, N1.getOperand(0).getValueType()),
776 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000777 default:
778 break; // todo, be more careful with signed comparisons
779 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000780 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
781 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
782 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
783 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
784 MVT::ValueType ExtDstTy = N1.getValueType();
785 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000786
Chris Lattner7b2880c2005-08-24 22:44:39 +0000787 // If the extended part has any inconsistent bits, it cannot ever
788 // compare equal. In other words, they have to be all ones or all
789 // zeros.
790 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000791 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000792 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
793 return getConstant(Cond == ISD::SETNE, VT);
794
795 // Otherwise, make this a use of a zext.
796 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000797 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000798 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000799 }
800
Chris Lattner67255a12005-04-07 18:14:58 +0000801 uint64_t MinVal, MaxVal;
802 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
803 if (ISD::isSignedIntSetCC(Cond)) {
804 MinVal = 1ULL << (OperandBitSize-1);
805 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
806 MaxVal = ~0ULL >> (65-OperandBitSize);
807 else
808 MaxVal = 0;
809 } else {
810 MinVal = 0;
811 MaxVal = ~0ULL >> (64-OperandBitSize);
812 }
813
814 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
815 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
816 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
817 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000818 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
819 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000820 }
821
822 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
823 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
824 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000825 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
826 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000827 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000828
Nate Begeman72ea2812005-04-14 08:56:52 +0000829 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
830 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000831
Nate Begeman72ea2812005-04-14 08:56:52 +0000832 // Canonicalize setgt X, Min --> setne X, Min
833 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000834 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000835
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000836 // If we have setult X, 1, turn it into seteq X, 0
837 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000838 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
839 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000840 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000841 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000842 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
843 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000844
845 // If we have "setcc X, C1", check to see if we can shrink the immediate
846 // by changing cc.
847
848 // SETUGT X, SINTMAX -> SETLT X, 0
849 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
850 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000851 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000852
853 // FIXME: Implement the rest of these.
854
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000855
856 // Fold bit comparisons when we can.
857 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
858 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
859 if (ConstantSDNode *AndRHS =
860 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
861 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
862 // Perform the xform if the AND RHS is a single bit.
863 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
864 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000865 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000866 TLI.getShiftAmountTy()));
867 }
868 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
869 // (X & 8) == 8 --> (X & 8) >> 3
870 // Perform the xform if C2 is a single bit.
871 if ((C2 & (C2-1)) == 0) {
872 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000873 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000874 }
875 }
876 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877 }
Chris Lattner67255a12005-04-07 18:14:58 +0000878 } else if (isa<ConstantSDNode>(N1.Val)) {
879 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000881 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882
883 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
884 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
885 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000886
Chris Lattnerc3aae252005-01-07 07:46:32 +0000887 switch (Cond) {
888 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000889 case ISD::SETEQ: return getConstant(C1 == C2, VT);
890 case ISD::SETNE: return getConstant(C1 != C2, VT);
891 case ISD::SETLT: return getConstant(C1 < C2, VT);
892 case ISD::SETGT: return getConstant(C1 > C2, VT);
893 case ISD::SETLE: return getConstant(C1 <= C2, VT);
894 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895 }
896 } else {
897 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000898 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899 }
900
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000901 // Could not fold it.
902 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000903}
904
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000906///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000908 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000909 void *IP = 0;
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
911 return SDOperand(E, 0);
912 SDNode *N = new SDNode(Opcode, VT);
913 CSEMap.InsertNode(N, IP);
914
915 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916 return SDOperand(N, 0);
917}
918
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
920 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000921 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000922 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
924 uint64_t Val = C->getValue();
925 switch (Opcode) {
926 default: break;
927 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000928 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
930 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000931 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
932 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000933 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000934 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000935 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000936 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000937 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000938 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000939 case ISD::BSWAP:
940 switch(VT) {
941 default: assert(0 && "Invalid bswap!"); break;
942 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
943 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
944 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
945 }
946 break;
947 case ISD::CTPOP:
948 switch(VT) {
949 default: assert(0 && "Invalid ctpop!"); break;
950 case MVT::i1: return getConstant(Val != 0, VT);
951 case MVT::i8:
952 Tmp1 = (unsigned)Val & 0xFF;
953 return getConstant(CountPopulation_32(Tmp1), VT);
954 case MVT::i16:
955 Tmp1 = (unsigned)Val & 0xFFFF;
956 return getConstant(CountPopulation_32(Tmp1), VT);
957 case MVT::i32:
958 return getConstant(CountPopulation_32((unsigned)Val), VT);
959 case MVT::i64:
960 return getConstant(CountPopulation_64(Val), VT);
961 }
962 case ISD::CTLZ:
963 switch(VT) {
964 default: assert(0 && "Invalid ctlz!"); break;
965 case MVT::i1: return getConstant(Val == 0, VT);
966 case MVT::i8:
967 Tmp1 = (unsigned)Val & 0xFF;
968 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
969 case MVT::i16:
970 Tmp1 = (unsigned)Val & 0xFFFF;
971 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
972 case MVT::i32:
973 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
974 case MVT::i64:
975 return getConstant(CountLeadingZeros_64(Val), VT);
976 }
977 case ISD::CTTZ:
978 switch(VT) {
979 default: assert(0 && "Invalid cttz!"); break;
980 case MVT::i1: return getConstant(Val == 0, VT);
981 case MVT::i8:
982 Tmp1 = (unsigned)Val | 0x100;
983 return getConstant(CountTrailingZeros_32(Tmp1), VT);
984 case MVT::i16:
985 Tmp1 = (unsigned)Val | 0x10000;
986 return getConstant(CountTrailingZeros_32(Tmp1), VT);
987 case MVT::i32:
988 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
989 case MVT::i64:
990 return getConstant(CountTrailingZeros_64(Val), VT);
991 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000992 }
993 }
994
Chris Lattner94683772005-12-23 05:30:37 +0000995 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
997 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000998 case ISD::FNEG:
999 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001000 case ISD::FABS:
1001 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001002 case ISD::FP_ROUND:
1003 case ISD::FP_EXTEND:
1004 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001005 case ISD::FP_TO_SINT:
1006 return getConstant((int64_t)C->getValue(), VT);
1007 case ISD::FP_TO_UINT:
1008 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001009 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001010 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001011 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001012 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001013 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001014 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015 }
1016
1017 unsigned OpOpcode = Operand.Val->getOpcode();
1018 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001019 case ISD::TokenFactor:
1020 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 case ISD::SIGN_EXTEND:
1022 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001023 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1025 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1026 break;
1027 case ISD::ZERO_EXTEND:
1028 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001029 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001030 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001031 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001033 case ISD::ANY_EXTEND:
1034 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001035 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001036 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1037 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1038 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1039 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 case ISD::TRUNCATE:
1041 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001042 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043 if (OpOpcode == ISD::TRUNCATE)
1044 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001045 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1046 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001047 // If the source is smaller than the dest, we still need an extend.
1048 if (Operand.Val->getOperand(0).getValueType() < VT)
1049 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1050 else if (Operand.Val->getOperand(0).getValueType() > VT)
1051 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1052 else
1053 return Operand.Val->getOperand(0);
1054 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 break;
Chris Lattner35481892005-12-23 00:16:34 +00001056 case ISD::BIT_CONVERT:
1057 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001058 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001059 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001060 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001061 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1062 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001063 if (OpOpcode == ISD::UNDEF)
1064 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001065 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001066 case ISD::SCALAR_TO_VECTOR:
1067 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1068 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1069 "Illegal SCALAR_TO_VECTOR node!");
1070 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001071 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001072 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1073 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001074 Operand.Val->getOperand(0));
1075 if (OpOpcode == ISD::FNEG) // --X -> X
1076 return Operand.Val->getOperand(0);
1077 break;
1078 case ISD::FABS:
1079 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1080 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1081 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 }
1083
Chris Lattner43247a12005-08-25 19:12:10 +00001084 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001085 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001086 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001087 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1088 void *IP = 0;
1089 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1090 return SDOperand(E, 0);
1091 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001092 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001093 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001094 } else {
1095 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001096 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001097 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 AllNodes.push_back(N);
1099 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001100}
1101
Chris Lattner36019aa2005-04-18 03:48:41 +00001102
1103
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1105 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001106#ifndef NDEBUG
1107 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001108 case ISD::TokenFactor:
1109 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1110 N2.getValueType() == MVT::Other && "Invalid token factor!");
1111 break;
Chris Lattner76365122005-01-16 02:23:22 +00001112 case ISD::AND:
1113 case ISD::OR:
1114 case ISD::XOR:
1115 case ISD::UDIV:
1116 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001117 case ISD::MULHU:
1118 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001119 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1120 // fall through
1121 case ISD::ADD:
1122 case ISD::SUB:
1123 case ISD::MUL:
1124 case ISD::SDIV:
1125 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001126 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1127 // fall through.
1128 case ISD::FADD:
1129 case ISD::FSUB:
1130 case ISD::FMUL:
1131 case ISD::FDIV:
1132 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001133 assert(N1.getValueType() == N2.getValueType() &&
1134 N1.getValueType() == VT && "Binary operator types must match!");
1135 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001136 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1137 assert(N1.getValueType() == VT &&
1138 MVT::isFloatingPoint(N1.getValueType()) &&
1139 MVT::isFloatingPoint(N2.getValueType()) &&
1140 "Invalid FCOPYSIGN!");
1141 break;
Chris Lattner76365122005-01-16 02:23:22 +00001142 case ISD::SHL:
1143 case ISD::SRA:
1144 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001145 case ISD::ROTL:
1146 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001147 assert(VT == N1.getValueType() &&
1148 "Shift operators return type must be the same as their first arg");
1149 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001150 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001151 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001152 case ISD::FP_ROUND_INREG: {
1153 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1154 assert(VT == N1.getValueType() && "Not an inreg round!");
1155 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1156 "Cannot FP_ROUND_INREG integer types");
1157 assert(EVT <= VT && "Not rounding down!");
1158 break;
1159 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001160 case ISD::AssertSext:
1161 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001162 case ISD::SIGN_EXTEND_INREG: {
1163 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1164 assert(VT == N1.getValueType() && "Not an inreg extend!");
1165 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1166 "Cannot *_EXTEND_INREG FP types");
1167 assert(EVT <= VT && "Not extending!");
1168 }
1169
Chris Lattner76365122005-01-16 02:23:22 +00001170 default: break;
1171 }
1172#endif
1173
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1175 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1176 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001177 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1178 int64_t Val = N1C->getValue();
1179 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1180 Val <<= 64-FromBits;
1181 Val >>= 64-FromBits;
1182 return getConstant(Val, VT);
1183 }
1184
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 if (N2C) {
1186 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1187 switch (Opcode) {
1188 case ISD::ADD: return getConstant(C1 + C2, VT);
1189 case ISD::SUB: return getConstant(C1 - C2, VT);
1190 case ISD::MUL: return getConstant(C1 * C2, VT);
1191 case ISD::UDIV:
1192 if (C2) return getConstant(C1 / C2, VT);
1193 break;
1194 case ISD::UREM :
1195 if (C2) return getConstant(C1 % C2, VT);
1196 break;
1197 case ISD::SDIV :
1198 if (C2) return getConstant(N1C->getSignExtended() /
1199 N2C->getSignExtended(), VT);
1200 break;
1201 case ISD::SREM :
1202 if (C2) return getConstant(N1C->getSignExtended() %
1203 N2C->getSignExtended(), VT);
1204 break;
1205 case ISD::AND : return getConstant(C1 & C2, VT);
1206 case ISD::OR : return getConstant(C1 | C2, VT);
1207 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001208 case ISD::SHL : return getConstant(C1 << C2, VT);
1209 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001210 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001211 case ISD::ROTL :
1212 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1213 VT);
1214 case ISD::ROTR :
1215 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1216 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001217 default: break;
1218 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 } else { // Cannonicalize constant to RHS if commutative
1220 if (isCommutativeBinOp(Opcode)) {
1221 std::swap(N1C, N2C);
1222 std::swap(N1, N2);
1223 }
1224 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 }
1226
1227 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1228 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001229 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230 if (N2CFP) {
1231 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1232 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001233 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1234 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1235 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1236 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001237 if (C2) return getConstantFP(C1 / C2, VT);
1238 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001239 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001240 if (C2) return getConstantFP(fmod(C1, C2), VT);
1241 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001242 case ISD::FCOPYSIGN: {
1243 union {
1244 double F;
1245 uint64_t I;
1246 } u1;
1247 union {
1248 double F;
1249 int64_t I;
1250 } u2;
1251 u1.F = C1;
1252 u2.F = C2;
1253 if (u2.I < 0) // Sign bit of RHS set?
1254 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1255 else
1256 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1257 return getConstantFP(u1.F, VT);
1258 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 default: break;
1260 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 } else { // Cannonicalize constant to RHS if commutative
1262 if (isCommutativeBinOp(Opcode)) {
1263 std::swap(N1CFP, N2CFP);
1264 std::swap(N1, N2);
1265 }
1266 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001267 }
Chris Lattner62b57722006-04-20 05:39:12 +00001268
1269 // Canonicalize an UNDEF to the RHS, even over a constant.
1270 if (N1.getOpcode() == ISD::UNDEF) {
1271 if (isCommutativeBinOp(Opcode)) {
1272 std::swap(N1, N2);
1273 } else {
1274 switch (Opcode) {
1275 case ISD::FP_ROUND_INREG:
1276 case ISD::SIGN_EXTEND_INREG:
1277 case ISD::SUB:
1278 case ISD::FSUB:
1279 case ISD::FDIV:
1280 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001281 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001282 return N1; // fold op(undef, arg2) -> undef
1283 case ISD::UDIV:
1284 case ISD::SDIV:
1285 case ISD::UREM:
1286 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001287 case ISD::SRL:
1288 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001289 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1290 }
1291 }
1292 }
1293
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001294 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001295 if (N2.getOpcode() == ISD::UNDEF) {
1296 switch (Opcode) {
1297 case ISD::ADD:
1298 case ISD::SUB:
1299 case ISD::FADD:
1300 case ISD::FSUB:
1301 case ISD::FMUL:
1302 case ISD::FDIV:
1303 case ISD::FREM:
1304 case ISD::UDIV:
1305 case ISD::SDIV:
1306 case ISD::UREM:
1307 case ISD::SREM:
1308 case ISD::XOR:
1309 return N2; // fold op(arg1, undef) -> undef
1310 case ISD::MUL:
1311 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001312 case ISD::SRL:
1313 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001314 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1315 case ISD::OR:
1316 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001317 case ISD::SRA:
1318 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001319 }
1320 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001321
Chris Lattnerc3aae252005-01-07 07:46:32 +00001322 // Finally, fold operations that do not require constants.
1323 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001324 case ISD::FP_ROUND_INREG:
1325 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1326 break;
1327 case ISD::SIGN_EXTEND_INREG: {
1328 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1329 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001330 break;
1331 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001332 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001333 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1334
Chris Lattner5c6621c2006-09-19 04:51:23 +00001335 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1336 // 64-bit integers into 32-bit parts. Instead of building the extract of
1337 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001338 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001339 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001340
1341 // EXTRACT_ELEMENT of a constant int is also very common.
1342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1343 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1344 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001345 }
1346 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001347
Nate Begemaneea805e2005-04-13 21:23:31 +00001348 // FIXME: figure out how to safely handle things like
1349 // int foo(int x) { return 1 << (x & 255); }
1350 // int bar() { return foo(256); }
1351#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001352 case ISD::SHL:
1353 case ISD::SRL:
1354 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001355 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001356 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001357 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001358 else if (N2.getOpcode() == ISD::AND)
1359 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1360 // If the and is only masking out bits that cannot effect the shift,
1361 // eliminate the and.
1362 unsigned NumBits = MVT::getSizeInBits(VT);
1363 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1364 return getNode(Opcode, VT, N1, N2.getOperand(0));
1365 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001366 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001367#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001368 }
1369
Chris Lattner27e9b412005-05-11 18:57:39 +00001370 // Memoize this node if possible.
1371 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001372 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001373 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001374 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1375 void *IP = 0;
1376 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1377 return SDOperand(E, 0);
1378 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001379 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001380 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001381 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001382 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001383 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001384 }
1385
Chris Lattnerc3aae252005-01-07 07:46:32 +00001386 AllNodes.push_back(N);
1387 return SDOperand(N, 0);
1388}
1389
Chris Lattnerc3aae252005-01-07 07:46:32 +00001390SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1391 SDOperand N1, SDOperand N2, SDOperand N3) {
1392 // Perform various simplifications.
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1394 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001395 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001396 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001397 case ISD::SETCC: {
1398 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001399 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001400 if (Simp.Val) return Simp;
1401 break;
1402 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001403 case ISD::SELECT:
1404 if (N1C)
1405 if (N1C->getValue())
1406 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001407 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001408 return N3; // select false, X, Y -> Y
1409
1410 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001411 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001412 case ISD::BRCOND:
1413 if (N2C)
1414 if (N2C->getValue()) // Unconditional branch
1415 return getNode(ISD::BR, MVT::Other, N1, N3);
1416 else
1417 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001418 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001419 case ISD::VECTOR_SHUFFLE:
1420 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1421 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1422 N3.getOpcode() == ISD::BUILD_VECTOR &&
1423 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1424 "Illegal VECTOR_SHUFFLE node!");
1425 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001426 }
1427
Chris Lattner43247a12005-08-25 19:12:10 +00001428 // Memoize node if it doesn't produce a flag.
1429 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001430 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001431 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001432 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1433 void *IP = 0;
1434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1435 return SDOperand(E, 0);
1436 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001437 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001438 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001439 } else {
1440 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001441 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001442 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001443 AllNodes.push_back(N);
1444 return SDOperand(N, 0);
1445}
1446
1447SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001448 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001449 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001450 SDOperand Ops[] = { N1, N2, N3, N4 };
1451 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001452}
1453
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001454SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1455 SDOperand N1, SDOperand N2, SDOperand N3,
1456 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001457 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1458 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001459}
1460
Evan Cheng7038daf2005-12-10 00:37:58 +00001461SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1462 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001463 const Value *SV, int SVOffset,
1464 bool isVolatile) {
1465 // FIXME: Alignment == 1 for now.
1466 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001467 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng466685d2006-10-09 20:57:25 +00001468 SDOperand Undef = getNode(ISD::UNDEF, VT);
1469 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1470 ID.AddInteger(ISD::UNINDEXED);
1471 ID.AddInteger(ISD::NON_EXTLOAD);
1472 ID.AddInteger(VT);
1473 ID.AddPointer(SV);
1474 ID.AddInteger(SVOffset);
1475 ID.AddInteger(Alignment);
1476 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001477 void *IP = 0;
1478 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1479 return SDOperand(E, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00001480 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::NON_EXTLOAD, VT,
1481 SV, SVOffset, Alignment, isVolatile);
1482 N->setValueTypes(VTs);
1483 CSEMap.InsertNode(N, IP);
1484 AllNodes.push_back(N);
1485 return SDOperand(N, 0);
1486}
1487
1488SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1489 SDOperand Chain, SDOperand Ptr, const Value *SV,
1490 int SVOffset, MVT::ValueType EVT,
1491 bool isVolatile) {
1492 // If they are asking for an extending load from/to the same thing, return a
1493 // normal load.
1494 if (VT == EVT)
1495 ExtType = ISD::NON_EXTLOAD;
1496
1497 if (MVT::isVector(VT))
1498 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1499 else
1500 assert(EVT < VT && "Should only be an extending load, not truncating!");
1501 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1502 "Cannot sign/zero extend a FP/Vector load!");
1503 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1504 "Cannot convert from FP to Int or Int -> FP!");
1505
1506 // FIXME: Alignment == 1 for now.
1507 unsigned Alignment = 1;
1508 SDVTList VTs = getVTList(VT, MVT::Other);
1509 SDOperand Undef = getNode(ISD::UNDEF, VT);
1510 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1511 ID.AddInteger(ISD::UNINDEXED);
1512 ID.AddInteger(ExtType);
1513 ID.AddInteger(EVT);
1514 ID.AddPointer(SV);
1515 ID.AddInteger(SVOffset);
1516 ID.AddInteger(Alignment);
1517 ID.AddInteger(isVolatile);
1518 void *IP = 0;
1519 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1520 return SDOperand(E, 0);
1521 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ExtType, EVT, SV, SVOffset,
1522 Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001523 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001524 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001525 AllNodes.push_back(N);
1526 return SDOperand(N, 0);
1527}
1528
1529SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1530 SDOperand Chain, SDOperand Ptr,
1531 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001532 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1533 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001534 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001535}
1536
Evan Chengad071e12006-10-05 22:57:11 +00001537SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1538 SDOperand Ptr, SDOperand SV) {
1539 SDVTList VTs = getVTList(MVT::Other);
1540 SDOperand Ops[] = { Chain, Value, Ptr, SV };
1541 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1542 void *IP = 0;
1543 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1544 return SDOperand(E, 0);
1545 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV);
1546 N->setValueTypes(VTs);
1547 CSEMap.InsertNode(N, IP);
1548 AllNodes.push_back(N);
1549 return SDOperand(N, 0);
1550}
1551
Nate Begemanacc398c2006-01-25 18:21:52 +00001552SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1553 SDOperand Chain, SDOperand Ptr,
1554 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001555 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001556 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001557}
1558
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001559SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001560 const SDOperand *Ops, unsigned NumOps) {
1561 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001562 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001563 case 1: return getNode(Opcode, VT, Ops[0]);
1564 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1565 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001566 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001568
Chris Lattneref847df2005-04-09 03:27:28 +00001569 switch (Opcode) {
1570 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001571 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001572 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001573 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1574#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1575 // If this is a truncating store of a constant, convert to the desired type
1576 // and store it instead.
1577 if (isa<Constant>(Ops[0])) {
1578 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1579 if (isa<Constant>(Op))
1580 N1 = Op;
1581 }
1582 // Also for ConstantFP?
1583#endif
1584 if (Ops[0].getValueType() == EVT) // Normal store?
Evan Chengad071e12006-10-05 22:57:11 +00001585 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001586 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1587 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1588 "Can't do FP-INT conversion!");
1589 break;
1590 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001591 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001592 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001593 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1594 "LHS and RHS of condition must have same type!");
1595 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1596 "True and False arms of SelectCC must have same type!");
1597 assert(Ops[2].getValueType() == VT &&
1598 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001599 break;
1600 }
1601 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001602 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001603 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1604 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001605 break;
1606 }
Chris Lattneref847df2005-04-09 03:27:28 +00001607 }
1608
Chris Lattner385328c2005-05-14 07:42:29 +00001609 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001610 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001611 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001612 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001613 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001614 void *IP = 0;
1615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1616 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001617 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001618 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001619 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001620 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001621 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001622 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001623 }
Chris Lattneref847df2005-04-09 03:27:28 +00001624 AllNodes.push_back(N);
1625 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001626}
1627
Chris Lattner89c34632005-05-14 06:20:26 +00001628SDOperand SelectionDAG::getNode(unsigned Opcode,
1629 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001630 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001631 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1632 Ops, NumOps);
1633}
1634
1635SDOperand SelectionDAG::getNode(unsigned Opcode,
1636 const MVT::ValueType *VTs, unsigned NumVTs,
1637 const SDOperand *Ops, unsigned NumOps) {
1638 if (NumVTs == 1)
1639 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001640 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1641}
1642
1643SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1644 const SDOperand *Ops, unsigned NumOps) {
1645 if (VTList.NumVTs == 1)
1646 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001647
Chris Lattner5f056bf2005-07-10 01:55:33 +00001648 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001649 // FIXME: figure out how to safely handle things like
1650 // int foo(int x) { return 1 << (x & 255); }
1651 // int bar() { return foo(256); }
1652#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001653 case ISD::SRA_PARTS:
1654 case ISD::SRL_PARTS:
1655 case ISD::SHL_PARTS:
1656 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001657 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001658 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1659 else if (N3.getOpcode() == ISD::AND)
1660 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1661 // If the and is only masking out bits that cannot effect the shift,
1662 // eliminate the and.
1663 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1664 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1665 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1666 }
1667 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001668#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001669 }
Chris Lattner89c34632005-05-14 06:20:26 +00001670
Chris Lattner43247a12005-08-25 19:12:10 +00001671 // Memoize the node unless it returns a flag.
1672 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001673 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001674 SelectionDAGCSEMap::NodeID ID;
1675 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001676 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001677 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001678 void *IP = 0;
1679 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1680 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001681 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001682 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001683 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001684 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001685 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001686 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001687 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001688 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001689 return SDOperand(N, 0);
1690}
1691
Chris Lattner70046e92006-08-15 17:46:01 +00001692SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1693 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001694}
1695
Chris Lattner70046e92006-08-15 17:46:01 +00001696SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001697 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1698 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001699 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001700 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001701 }
1702 std::vector<MVT::ValueType> V;
1703 V.push_back(VT1);
1704 V.push_back(VT2);
1705 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001706 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001707}
Chris Lattner70046e92006-08-15 17:46:01 +00001708SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1709 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001710 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001711 E = VTList.end(); I != E; ++I) {
1712 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1713 (*I)[2] == VT3)
1714 return makeVTList(&(*I)[0], 3);
1715 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001716 std::vector<MVT::ValueType> V;
1717 V.push_back(VT1);
1718 V.push_back(VT2);
1719 V.push_back(VT3);
1720 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001721 return makeVTList(&(*VTList.begin())[0], 3);
1722}
1723
1724SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1725 switch (NumVTs) {
1726 case 0: assert(0 && "Cannot have nodes without results!");
1727 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1728 case 2: return getVTList(VTs[0], VTs[1]);
1729 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1730 default: break;
1731 }
1732
1733 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1734 E = VTList.end(); I != E; ++I) {
1735 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1736
1737 bool NoMatch = false;
1738 for (unsigned i = 2; i != NumVTs; ++i)
1739 if (VTs[i] != (*I)[i]) {
1740 NoMatch = true;
1741 break;
1742 }
1743 if (!NoMatch)
1744 return makeVTList(&*I->begin(), NumVTs);
1745 }
1746
1747 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1748 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001749}
1750
1751
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001752/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1753/// specified operands. If the resultant node already exists in the DAG,
1754/// this does not modify the specified node, instead it returns the node that
1755/// already exists. If the resultant node does not exist in the DAG, the
1756/// input node is returned. As a degenerate case, if you specify the same
1757/// input operands as the node already has, the input node is returned.
1758SDOperand SelectionDAG::
1759UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1760 SDNode *N = InN.Val;
1761 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1762
1763 // Check to see if there is no change.
1764 if (Op == N->getOperand(0)) return InN;
1765
1766 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001767 void *InsertPos = 0;
1768 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1769 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001770
1771 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001772 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001773 RemoveNodeFromCSEMaps(N);
1774
1775 // Now we update the operands.
1776 N->OperandList[0].Val->removeUser(N);
1777 Op.Val->addUser(N);
1778 N->OperandList[0] = Op;
1779
1780 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001781 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001782 return InN;
1783}
1784
1785SDOperand SelectionDAG::
1786UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1787 SDNode *N = InN.Val;
1788 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1789
1790 // Check to see if there is no change.
1791 bool AnyChange = false;
1792 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1793 return InN; // No operands changed, just return the input node.
1794
1795 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001796 void *InsertPos = 0;
1797 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1798 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001799
1800 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001801 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001802 RemoveNodeFromCSEMaps(N);
1803
1804 // Now we update the operands.
1805 if (N->OperandList[0] != Op1) {
1806 N->OperandList[0].Val->removeUser(N);
1807 Op1.Val->addUser(N);
1808 N->OperandList[0] = Op1;
1809 }
1810 if (N->OperandList[1] != Op2) {
1811 N->OperandList[1].Val->removeUser(N);
1812 Op2.Val->addUser(N);
1813 N->OperandList[1] = Op2;
1814 }
1815
1816 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001817 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001818 return InN;
1819}
1820
1821SDOperand SelectionDAG::
1822UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001823 SDOperand Ops[] = { Op1, Op2, Op3 };
1824 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001825}
1826
1827SDOperand SelectionDAG::
1828UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1829 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001830 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1831 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001832}
1833
1834SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001835UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1836 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001837 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1838 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001839}
1840
1841
1842SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001843UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001844 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001845 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001846 "Update with wrong number of operands");
1847
1848 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001849 bool AnyChange = false;
1850 for (unsigned i = 0; i != NumOps; ++i) {
1851 if (Ops[i] != N->getOperand(i)) {
1852 AnyChange = true;
1853 break;
1854 }
1855 }
1856
1857 // No operands changed, just return the input node.
1858 if (!AnyChange) return InN;
1859
1860 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001861 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001862 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001863 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001864
1865 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001866 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001867 RemoveNodeFromCSEMaps(N);
1868
1869 // Now we update the operands.
1870 for (unsigned i = 0; i != NumOps; ++i) {
1871 if (N->OperandList[i] != Ops[i]) {
1872 N->OperandList[i].Val->removeUser(N);
1873 Ops[i].Val->addUser(N);
1874 N->OperandList[i] = Ops[i];
1875 }
1876 }
1877
1878 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001879 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001880 return InN;
1881}
1882
1883
1884
Chris Lattner149c58c2005-08-16 18:17:10 +00001885
1886/// SelectNodeTo - These are used for target selectors to *mutate* the
1887/// specified node to have the specified return type, Target opcode, and
1888/// operands. Note that target opcodes are stored as
1889/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001890///
1891/// Note that SelectNodeTo returns the resultant node. If there is already a
1892/// node of the specified opcode and operands, it returns that node instead of
1893/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00001894SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1895 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001896 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001897 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1898 void *IP = 0;
1899 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001900 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00001901
Chris Lattner7651fa42005-08-24 23:00:29 +00001902 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001903
Chris Lattner7651fa42005-08-24 23:00:29 +00001904 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001905 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001906
Chris Lattner4a283e92006-08-11 18:38:11 +00001907 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001908 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00001909}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001910
Evan Cheng95514ba2006-08-26 08:00:10 +00001911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1912 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001913 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001914 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001915 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1916 void *IP = 0;
1917 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001918 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001919
Chris Lattner149c58c2005-08-16 18:17:10 +00001920 RemoveNodeFromCSEMaps(N);
1921 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001922 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001923 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001924 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001925 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001926}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001927
Evan Cheng95514ba2006-08-26 08:00:10 +00001928SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1929 MVT::ValueType VT, SDOperand Op1,
1930 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001931 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001932 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001933 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1934 void *IP = 0;
1935 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001936 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001937
Chris Lattner149c58c2005-08-16 18:17:10 +00001938 RemoveNodeFromCSEMaps(N);
1939 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001940 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001941 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001942
Chris Lattnera5682852006-08-07 23:03:03 +00001943 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001944 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001945}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001946
Evan Cheng95514ba2006-08-26 08:00:10 +00001947SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1948 MVT::ValueType VT, SDOperand Op1,
1949 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001950 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001951 SDVTList VTs = getVTList(VT);
1952 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1953 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001954 void *IP = 0;
1955 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001956 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001957
Chris Lattner149c58c2005-08-16 18:17:10 +00001958 RemoveNodeFromCSEMaps(N);
1959 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001960 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001961 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001962
Chris Lattnera5682852006-08-07 23:03:03 +00001963 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001964 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001965}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001966
Evan Cheng95514ba2006-08-26 08:00:10 +00001967SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00001968 MVT::ValueType VT, const SDOperand *Ops,
1969 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001970 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001971 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001972 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001973 for (unsigned i = 0; i != NumOps; ++i)
1974 ID.AddOperand(Ops[i]);
Chris Lattnera5682852006-08-07 23:03:03 +00001975 void *IP = 0;
1976 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001977 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001978
Chris Lattner6b09a292005-08-21 18:49:33 +00001979 RemoveNodeFromCSEMaps(N);
1980 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001981 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001982 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001983
Chris Lattnera5682852006-08-07 23:03:03 +00001984 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001985 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001986}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001987
Evan Cheng95514ba2006-08-26 08:00:10 +00001988SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1989 MVT::ValueType VT1, MVT::ValueType VT2,
1990 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001991 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00001992 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1993 void *IP = 0;
1994 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001995 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001996
Chris Lattner0fb094f2005-11-19 01:44:53 +00001997 RemoveNodeFromCSEMaps(N);
1998 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001999 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002000 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002001
Chris Lattnera5682852006-08-07 23:03:03 +00002002 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002003 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002004}
2005
Evan Cheng95514ba2006-08-26 08:00:10 +00002006SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2007 MVT::ValueType VT1, MVT::ValueType VT2,
2008 SDOperand Op1, SDOperand Op2,
2009 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002010 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002011 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002012 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2013 Op1, Op2, Op3);
2014 void *IP = 0;
2015 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002016 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002017
Chris Lattner0fb094f2005-11-19 01:44:53 +00002018 RemoveNodeFromCSEMaps(N);
2019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002020 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002021 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002022
Chris Lattnera5682852006-08-07 23:03:03 +00002023 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002024 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002025}
2026
Chris Lattner0fb094f2005-11-19 01:44:53 +00002027
Evan Cheng6ae46c42006-02-09 07:15:23 +00002028/// getTargetNode - These are used for target selectors to create a new node
2029/// with specified return type(s), target opcode, and operands.
2030///
2031/// Note that getTargetNode returns the resultant node. If there is already a
2032/// node of the specified opcode and operands, it returns that node instead of
2033/// the current one.
2034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2035 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2036}
2037SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2038 SDOperand Op1) {
2039 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2040}
2041SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2042 SDOperand Op1, SDOperand Op2) {
2043 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2044}
2045SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2046 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2047 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2048}
2049SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002050 const SDOperand *Ops, unsigned NumOps) {
2051 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002052}
2053SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2054 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002055 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002056 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002057}
2058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002059 MVT::ValueType VT2, SDOperand Op1,
2060 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002061 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002062 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002063 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002064}
2065SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002066 MVT::ValueType VT2, SDOperand Op1,
2067 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002068 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002069 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002070 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002071}
Evan Cheng694481e2006-08-27 08:08:54 +00002072SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2073 MVT::ValueType VT2,
2074 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002075 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002076 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002077}
2078SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2079 MVT::ValueType VT2, MVT::ValueType VT3,
2080 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002081 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002082 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002083 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002084}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002085SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002086 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002087 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002088 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2089 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002090}
2091
Evan Cheng99157a02006-08-07 22:13:29 +00002092/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002093/// This can cause recursive merging of nodes in the DAG.
2094///
Chris Lattner8b52f212005-08-26 18:36:28 +00002095/// This version assumes From/To have a single result value.
2096///
Chris Lattner1e111c72005-09-07 05:37:01 +00002097void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2098 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002099 SDNode *From = FromN.Val, *To = ToN.Val;
2100 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2101 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002102 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002103
Chris Lattner8b8749f2005-08-17 19:00:20 +00002104 while (!From->use_empty()) {
2105 // Process users until they are all gone.
2106 SDNode *U = *From->use_begin();
2107
2108 // This node is about to morph, remove its old self from the CSE maps.
2109 RemoveNodeFromCSEMaps(U);
2110
Chris Lattner65113b22005-11-08 22:07:03 +00002111 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2112 I != E; ++I)
2113 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002114 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002115 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002116 To->addUser(U);
2117 }
2118
2119 // Now that we have modified U, add it back to the CSE maps. If it already
2120 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002121 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2122 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002123 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002124 if (Deleted) Deleted->push_back(U);
2125 DeleteNodeNotInCSEMaps(U);
2126 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002127 }
2128}
2129
2130/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2131/// This can cause recursive merging of nodes in the DAG.
2132///
2133/// This version assumes From/To have matching types and numbers of result
2134/// values.
2135///
Chris Lattner1e111c72005-09-07 05:37:01 +00002136void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2137 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002138 assert(From != To && "Cannot replace uses of with self");
2139 assert(From->getNumValues() == To->getNumValues() &&
2140 "Cannot use this version of ReplaceAllUsesWith!");
2141 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002142 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002143 return;
2144 }
2145
2146 while (!From->use_empty()) {
2147 // Process users until they are all gone.
2148 SDNode *U = *From->use_begin();
2149
2150 // This node is about to morph, remove its old self from the CSE maps.
2151 RemoveNodeFromCSEMaps(U);
2152
Chris Lattner65113b22005-11-08 22:07:03 +00002153 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2154 I != E; ++I)
2155 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002156 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002157 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002158 To->addUser(U);
2159 }
2160
2161 // Now that we have modified U, add it back to the CSE maps. If it already
2162 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002163 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2164 ReplaceAllUsesWith(U, Existing, Deleted);
2165 // U is now dead.
2166 if (Deleted) Deleted->push_back(U);
2167 DeleteNodeNotInCSEMaps(U);
2168 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002169 }
2170}
2171
Chris Lattner8b52f212005-08-26 18:36:28 +00002172/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2173/// This can cause recursive merging of nodes in the DAG.
2174///
2175/// This version can replace From with any result values. To must match the
2176/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002177void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002178 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002179 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002180 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002181 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002182 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002183 return;
2184 }
2185
2186 while (!From->use_empty()) {
2187 // Process users until they are all gone.
2188 SDNode *U = *From->use_begin();
2189
2190 // This node is about to morph, remove its old self from the CSE maps.
2191 RemoveNodeFromCSEMaps(U);
2192
Chris Lattner65113b22005-11-08 22:07:03 +00002193 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2194 I != E; ++I)
2195 if (I->Val == From) {
2196 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002197 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002198 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002199 ToOp.Val->addUser(U);
2200 }
2201
2202 // Now that we have modified U, add it back to the CSE maps. If it already
2203 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002204 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2205 ReplaceAllUsesWith(U, Existing, Deleted);
2206 // U is now dead.
2207 if (Deleted) Deleted->push_back(U);
2208 DeleteNodeNotInCSEMaps(U);
2209 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002210 }
2211}
2212
Chris Lattner012f2412006-02-17 21:58:01 +00002213/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2214/// uses of other values produced by From.Val alone. The Deleted vector is
2215/// handled the same was as for ReplaceAllUsesWith.
2216void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2217 std::vector<SDNode*> &Deleted) {
2218 assert(From != To && "Cannot replace a value with itself");
2219 // Handle the simple, trivial, case efficiently.
2220 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2221 ReplaceAllUsesWith(From, To, &Deleted);
2222 return;
2223 }
2224
2225 // Get all of the users in a nice, deterministically ordered, uniqued set.
2226 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2227
2228 while (!Users.empty()) {
2229 // We know that this user uses some value of From. If it is the right
2230 // value, update it.
2231 SDNode *User = Users.back();
2232 Users.pop_back();
2233
2234 for (SDOperand *Op = User->OperandList,
2235 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2236 if (*Op == From) {
2237 // Okay, we know this user needs to be updated. Remove its old self
2238 // from the CSE maps.
2239 RemoveNodeFromCSEMaps(User);
2240
2241 // Update all operands that match "From".
2242 for (; Op != E; ++Op) {
2243 if (*Op == From) {
2244 From.Val->removeUser(User);
2245 *Op = To;
2246 To.Val->addUser(User);
2247 }
2248 }
2249
2250 // Now that we have modified User, add it back to the CSE maps. If it
2251 // already exists there, recursively merge the results together.
2252 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2253 unsigned NumDeleted = Deleted.size();
2254 ReplaceAllUsesWith(User, Existing, &Deleted);
2255
2256 // User is now dead.
2257 Deleted.push_back(User);
2258 DeleteNodeNotInCSEMaps(User);
2259
2260 // We have to be careful here, because ReplaceAllUsesWith could have
2261 // deleted a user of From, which means there may be dangling pointers
2262 // in the "Users" setvector. Scan over the deleted node pointers and
2263 // remove them from the setvector.
2264 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2265 Users.remove(Deleted[i]);
2266 }
2267 break; // Exit the operand scanning loop.
2268 }
2269 }
2270 }
2271}
2272
Chris Lattner7b2880c2005-08-24 22:44:39 +00002273
Evan Chenge6f35d82006-08-01 08:20:41 +00002274/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2275/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002276unsigned SelectionDAG::AssignNodeIds() {
2277 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002278 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2279 SDNode *N = I;
2280 N->setNodeId(Id++);
2281 }
2282 return Id;
2283}
2284
Evan Chenge6f35d82006-08-01 08:20:41 +00002285/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002286/// based on their topological order. It returns the maximum id and a vector
2287/// of the SDNodes* in assigned order by reference.
2288unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002289 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002290 std::vector<unsigned> InDegree(DAGSize);
2291 std::vector<SDNode*> Sources;
2292
2293 // Use a two pass approach to avoid using a std::map which is slow.
2294 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002295 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2296 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002297 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002298 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002299 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002300 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002301 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002302 }
2303
Evan Cheng99157a02006-08-07 22:13:29 +00002304 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002305 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002306 SDNode *N = Sources.back();
2307 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002308 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002309 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2310 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002311 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002312 if (Degree == 0)
2313 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002314 }
2315 }
2316
Evan Chengc384d6c2006-08-02 22:00:34 +00002317 // Second pass, assign the actual topological order as node ids.
2318 Id = 0;
2319 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2320 TI != TE; ++TI)
2321 (*TI)->setNodeId(Id++);
2322
2323 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002324}
2325
2326
Evan Cheng091cba12006-07-27 06:39:06 +00002327
Jim Laskey58b968b2005-08-17 20:08:02 +00002328//===----------------------------------------------------------------------===//
2329// SDNode Class
2330//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002331
Chris Lattner917d2c92006-07-19 00:00:37 +00002332// Out-of-line virtual method to give class a home.
2333void SDNode::ANCHOR() {
2334}
2335
Chris Lattnera3255112005-11-08 23:30:28 +00002336/// getValueTypeList - Return a pointer to the specified value type.
2337///
2338MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2339 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2340 VTs[VT] = VT;
2341 return &VTs[VT];
2342}
Chris Lattnera5682852006-08-07 23:03:03 +00002343
Chris Lattner5c884562005-01-12 18:37:47 +00002344/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2345/// indicated value. This method ignores uses of other values defined by this
2346/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002347bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002348 assert(Value < getNumValues() && "Bad value!");
2349
2350 // If there is only one value, this is easy.
2351 if (getNumValues() == 1)
2352 return use_size() == NUses;
2353 if (Uses.size() < NUses) return false;
2354
Evan Cheng4ee62112006-02-05 06:29:23 +00002355 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002356
2357 std::set<SDNode*> UsersHandled;
2358
Chris Lattnerf83482d2006-08-16 20:59:32 +00002359 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002360 SDNode *User = *UI;
2361 if (User->getNumOperands() == 1 ||
2362 UsersHandled.insert(User).second) // First time we've seen this?
2363 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2364 if (User->getOperand(i) == TheValue) {
2365 if (NUses == 0)
2366 return false; // too many uses
2367 --NUses;
2368 }
2369 }
2370
2371 // Found exactly the right number of uses?
2372 return NUses == 0;
2373}
2374
2375
Evan Cheng4ee62112006-02-05 06:29:23 +00002376// isOnlyUse - Return true if this node is the only use of N.
2377bool SDNode::isOnlyUse(SDNode *N) const {
2378 bool Seen = false;
2379 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2380 SDNode *User = *I;
2381 if (User == this)
2382 Seen = true;
2383 else
2384 return false;
2385 }
2386
2387 return Seen;
2388}
2389
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002390// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002391bool SDOperand::isOperand(SDNode *N) const {
2392 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2393 if (*this == N->getOperand(i))
2394 return true;
2395 return false;
2396}
2397
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002398bool SDNode::isOperand(SDNode *N) const {
2399 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2400 if (this == N->OperandList[i].Val)
2401 return true;
2402 return false;
2403}
Evan Cheng4ee62112006-02-05 06:29:23 +00002404
Evan Chengc5484282006-10-04 00:56:09 +00002405uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2406 assert(Num < NumOperands && "Invalid child # of SDNode!");
2407 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2408}
2409
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002410const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002411 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002412 default:
2413 if (getOpcode() < ISD::BUILTIN_OP_END)
2414 return "<<Unknown DAG Node>>";
2415 else {
Evan Cheng72261582005-12-20 06:22:03 +00002416 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002417 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002418 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2419 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002420
Evan Cheng72261582005-12-20 06:22:03 +00002421 TargetLowering &TLI = G->getTargetLoweringInfo();
2422 const char *Name =
2423 TLI.getTargetNodeName(getOpcode());
2424 if (Name) return Name;
2425 }
2426
2427 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002428 }
2429
Andrew Lenharth95762122005-03-31 21:24:06 +00002430 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002431 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002432 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002433 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002434 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002435 case ISD::AssertSext: return "AssertSext";
2436 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002437
2438 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002439 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002440 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002441 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002442
2443 case ISD::Constant: return "Constant";
2444 case ISD::ConstantFP: return "ConstantFP";
2445 case ISD::GlobalAddress: return "GlobalAddress";
2446 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002447 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth16113432006-09-26 20:02:30 +00002448 case ISD::JumpTableRelocBase: return "JumpTableRelocBase";
Chris Lattner5839bf22005-08-26 17:15:30 +00002449 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002450 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002451 case ISD::INTRINSIC_WO_CHAIN: {
2452 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2453 return Intrinsic::getName((Intrinsic::ID)IID);
2454 }
2455 case ISD::INTRINSIC_VOID:
2456 case ISD::INTRINSIC_W_CHAIN: {
2457 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002458 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002459 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002460
Chris Lattnerb2827b02006-03-19 00:52:58 +00002461 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002462 case ISD::TargetConstant: return "TargetConstant";
2463 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002464 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2465 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002466 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002467 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002468 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002469
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002470 case ISD::CopyToReg: return "CopyToReg";
2471 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002472 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002473 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002474 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002475 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002476 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002477 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002478
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002479 // Unary operators
2480 case ISD::FABS: return "fabs";
2481 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002482 case ISD::FSQRT: return "fsqrt";
2483 case ISD::FSIN: return "fsin";
2484 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002485 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002486
2487 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002488 case ISD::ADD: return "add";
2489 case ISD::SUB: return "sub";
2490 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002491 case ISD::MULHU: return "mulhu";
2492 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002493 case ISD::SDIV: return "sdiv";
2494 case ISD::UDIV: return "udiv";
2495 case ISD::SREM: return "srem";
2496 case ISD::UREM: return "urem";
2497 case ISD::AND: return "and";
2498 case ISD::OR: return "or";
2499 case ISD::XOR: return "xor";
2500 case ISD::SHL: return "shl";
2501 case ISD::SRA: return "sra";
2502 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002503 case ISD::ROTL: return "rotl";
2504 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002505 case ISD::FADD: return "fadd";
2506 case ISD::FSUB: return "fsub";
2507 case ISD::FMUL: return "fmul";
2508 case ISD::FDIV: return "fdiv";
2509 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002510 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002511 case ISD::VADD: return "vadd";
2512 case ISD::VSUB: return "vsub";
2513 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002514 case ISD::VSDIV: return "vsdiv";
2515 case ISD::VUDIV: return "vudiv";
2516 case ISD::VAND: return "vand";
2517 case ISD::VOR: return "vor";
2518 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002519
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002520 case ISD::SETCC: return "setcc";
2521 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002522 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002523 case ISD::VSELECT: return "vselect";
2524 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2525 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2526 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002527 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002528 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2529 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2530 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2531 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2532 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002533 case ISD::ADDC: return "addc";
2534 case ISD::ADDE: return "adde";
2535 case ISD::SUBC: return "subc";
2536 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002537 case ISD::SHL_PARTS: return "shl_parts";
2538 case ISD::SRA_PARTS: return "sra_parts";
2539 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002540
Chris Lattner7f644642005-04-28 21:44:03 +00002541 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002542 case ISD::SIGN_EXTEND: return "sign_extend";
2543 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002544 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002545 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002546 case ISD::TRUNCATE: return "truncate";
2547 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002548 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002549 case ISD::FP_EXTEND: return "fp_extend";
2550
2551 case ISD::SINT_TO_FP: return "sint_to_fp";
2552 case ISD::UINT_TO_FP: return "uint_to_fp";
2553 case ISD::FP_TO_SINT: return "fp_to_sint";
2554 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002555 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002556
2557 // Control flow instructions
2558 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002559 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002560 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002561 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002562 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002563 case ISD::CALLSEQ_START: return "callseq_start";
2564 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002565
2566 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002567 case ISD::LOAD: return "load";
2568 case ISD::STORE: return "store";
2569 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002570 case ISD::TRUNCSTORE: return "truncstore";
2571 case ISD::VAARG: return "vaarg";
2572 case ISD::VACOPY: return "vacopy";
2573 case ISD::VAEND: return "vaend";
2574 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002575 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002576 case ISD::EXTRACT_ELEMENT: return "extract_element";
2577 case ISD::BUILD_PAIR: return "build_pair";
2578 case ISD::STACKSAVE: return "stacksave";
2579 case ISD::STACKRESTORE: return "stackrestore";
2580
2581 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002582 case ISD::MEMSET: return "memset";
2583 case ISD::MEMCPY: return "memcpy";
2584 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002585
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002586 // Bit manipulation
2587 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002588 case ISD::CTPOP: return "ctpop";
2589 case ISD::CTTZ: return "cttz";
2590 case ISD::CTLZ: return "ctlz";
2591
Chris Lattner36ce6912005-11-29 06:21:05 +00002592 // Debug info
2593 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002594 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002595 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002596
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002597 case ISD::CONDCODE:
2598 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002599 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002600 case ISD::SETOEQ: return "setoeq";
2601 case ISD::SETOGT: return "setogt";
2602 case ISD::SETOGE: return "setoge";
2603 case ISD::SETOLT: return "setolt";
2604 case ISD::SETOLE: return "setole";
2605 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002606
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002607 case ISD::SETO: return "seto";
2608 case ISD::SETUO: return "setuo";
2609 case ISD::SETUEQ: return "setue";
2610 case ISD::SETUGT: return "setugt";
2611 case ISD::SETUGE: return "setuge";
2612 case ISD::SETULT: return "setult";
2613 case ISD::SETULE: return "setule";
2614 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002615
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002616 case ISD::SETEQ: return "seteq";
2617 case ISD::SETGT: return "setgt";
2618 case ISD::SETGE: return "setge";
2619 case ISD::SETLT: return "setlt";
2620 case ISD::SETLE: return "setle";
2621 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002622 }
2623 }
2624}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002625
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002626void SDNode::dump() const { dump(0); }
2627void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002628 std::cerr << (void*)this << ": ";
2629
2630 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2631 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002632 if (getValueType(i) == MVT::Other)
2633 std::cerr << "ch";
2634 else
2635 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002636 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002637 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002638
2639 std::cerr << " ";
2640 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2641 if (i) std::cerr << ", ";
2642 std::cerr << (void*)getOperand(i).Val;
2643 if (unsigned RN = getOperand(i).ResNo)
2644 std::cerr << ":" << RN;
2645 }
2646
2647 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2648 std::cerr << "<" << CSDN->getValue() << ">";
2649 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2650 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002651 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002652 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002653 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002654 std::cerr << "<";
2655 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002656 if (offset > 0)
2657 std::cerr << " + " << offset;
2658 else
2659 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002660 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002661 std::cerr << "<" << FIDN->getIndex() << ">";
2662 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002663 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002664 if (CP->isMachineConstantPoolEntry())
2665 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2666 else
2667 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002668 if (offset > 0)
2669 std::cerr << " + " << offset;
2670 else
2671 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002672 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002673 std::cerr << "<";
2674 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2675 if (LBB)
2676 std::cerr << LBB->getName() << " ";
2677 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002678 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002679 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002680 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2681 } else {
2682 std::cerr << " #" << R->getReg();
2683 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002684 } else if (const ExternalSymbolSDNode *ES =
2685 dyn_cast<ExternalSymbolSDNode>(this)) {
2686 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002687 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2688 if (M->getValue())
2689 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2690 else
2691 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002692 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2693 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002694 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2695 bool doExt = true;
2696 switch (LD->getExtensionType()) {
2697 default: doExt = false; break;
2698 case ISD::EXTLOAD:
2699 std::cerr << " <anyext ";
2700 break;
2701 case ISD::SEXTLOAD:
2702 std::cerr << " <sext ";
2703 break;
2704 case ISD::ZEXTLOAD:
2705 std::cerr << " <zext ";
2706 break;
2707 }
2708 if (doExt)
2709 std::cerr << MVT::getValueTypeString(LD->getLoadVT()) << ">";
2710
2711 if (LD->getAddressingMode() == ISD::PRE_INDEXED)
2712 std::cerr << " <pre>";
2713 else if (LD->getAddressingMode() == ISD::POST_INDEXED)
2714 std::cerr << " <post>";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002715 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002716}
2717
Chris Lattnerde202b32005-11-09 23:47:37 +00002718static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002719 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2720 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002721 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002722 else
2723 std::cerr << "\n" << std::string(indent+2, ' ')
2724 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002725
Chris Lattnerea946cd2005-01-09 20:38:33 +00002726
2727 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002728 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002729}
2730
Chris Lattnerc3aae252005-01-07 07:46:32 +00002731void SelectionDAG::dump() const {
2732 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002733 std::vector<const SDNode*> Nodes;
2734 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2735 I != E; ++I)
2736 Nodes.push_back(I);
2737
Chris Lattner49d24712005-01-09 20:26:36 +00002738 std::sort(Nodes.begin(), Nodes.end());
2739
2740 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002741 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002742 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002743 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002744
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002745 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002746
Chris Lattnerc3aae252005-01-07 07:46:32 +00002747 std::cerr << "\n\n";
2748}
2749
Evan Chengd6594ae2006-09-12 21:00:35 +00002750const Type *ConstantPoolSDNode::getType() const {
2751 if (isMachineConstantPoolEntry())
2752 return Val.MachineCPVal->getType();
2753 return Val.ConstVal->getType();
2754}