blob: 05348da6e87703bc622697fd488fbb26bdf1eb3e [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"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.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 Lattnerd48c5e82007-02-04 00:24:41 +000027#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000028#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000029#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000030#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000031#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner0b3e5252006-08-15 19:11:05 +000034/// makeVTList - Return an instance of the SDVTList struct initialized with the
35/// specified members.
36static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
37 SDVTList Res = {VTs, NumVTs};
38 return Res;
39}
40
Jim Laskey58b968b2005-08-17 20:08:02 +000041//===----------------------------------------------------------------------===//
42// ConstantFPSDNode Class
43//===----------------------------------------------------------------------===//
44
45/// isExactlyValue - We don't rely on operator== working on double values, as
46/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
47/// As such, this method can be used to do an exact bit-for-bit comparison of
48/// two floating point values.
49bool ConstantFPSDNode::isExactlyValue(double V) const {
50 return DoubleToBits(V) == DoubleToBits(Value);
51}
52
53//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000054// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000055//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000056
Evan Chenga8df1662006-03-27 06:58:47 +000057/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000058/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000059bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000060 // Look through a bit convert.
61 if (N->getOpcode() == ISD::BIT_CONVERT)
62 N = N->getOperand(0).Val;
63
Evan Chenga8df1662006-03-27 06:58:47 +000064 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000065
66 unsigned i = 0, e = N->getNumOperands();
67
68 // Skip over all of the undef values.
69 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
70 ++i;
71
72 // Do not accept an all-undef vector.
73 if (i == e) return false;
74
75 // Do not accept build_vectors that aren't all constants or which have non-~0
76 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000077 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000078 if (isa<ConstantSDNode>(NotZero)) {
79 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
80 return false;
81 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000082 MVT::ValueType VT = NotZero.getValueType();
83 if (VT== MVT::f64) {
84 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
85 (uint64_t)-1)
86 return false;
87 } else {
88 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
89 (uint32_t)-1)
90 return false;
91 }
Evan Chenga8df1662006-03-27 06:58:47 +000092 } else
Chris Lattner61d43992006-03-25 22:57:01 +000093 return false;
94
95 // Okay, we have at least one ~0 value, check to see if the rest match or are
96 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +000097 for (++i; i != e; ++i)
98 if (N->getOperand(i) != NotZero &&
99 N->getOperand(i).getOpcode() != ISD::UNDEF)
100 return false;
101 return true;
102}
103
104
Evan Cheng4a147842006-03-26 09:50:58 +0000105/// isBuildVectorAllZeros - Return true if the specified node is a
106/// BUILD_VECTOR where all of the elements are 0 or undef.
107bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000108 // Look through a bit convert.
109 if (N->getOpcode() == ISD::BIT_CONVERT)
110 N = N->getOperand(0).Val;
111
Evan Cheng4a147842006-03-26 09:50:58 +0000112 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000113
114 unsigned i = 0, e = N->getNumOperands();
115
116 // Skip over all of the undef values.
117 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
118 ++i;
119
120 // Do not accept an all-undef vector.
121 if (i == e) return false;
122
123 // Do not accept build_vectors that aren't all constants or which have non-~0
124 // elements.
125 SDOperand Zero = N->getOperand(i);
126 if (isa<ConstantSDNode>(Zero)) {
127 if (!cast<ConstantSDNode>(Zero)->isNullValue())
128 return false;
129 } else if (isa<ConstantFPSDNode>(Zero)) {
130 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
131 return false;
132 } else
133 return false;
134
135 // Okay, we have at least one ~0 value, check to see if the rest match or are
136 // undefs.
137 for (++i; i != e; ++i)
138 if (N->getOperand(i) != Zero &&
139 N->getOperand(i).getOpcode() != ISD::UNDEF)
140 return false;
141 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000142}
143
Chris Lattnerc3aae252005-01-07 07:46:32 +0000144/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
145/// when given the operation for (X op Y).
146ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
147 // To perform this operation, we just need to swap the L and G bits of the
148 // operation.
149 unsigned OldL = (Operation >> 2) & 1;
150 unsigned OldG = (Operation >> 1) & 1;
151 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
152 (OldL << 1) | // New G bit
153 (OldG << 2)); // New L bit.
154}
155
156/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
157/// 'op' is a valid SetCC operation.
158ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
159 unsigned Operation = Op;
160 if (isInteger)
161 Operation ^= 7; // Flip L, G, E bits, but not U.
162 else
163 Operation ^= 15; // Flip all of the condition bits.
164 if (Operation > ISD::SETTRUE2)
165 Operation &= ~8; // Don't let N and U bits get set.
166 return ISD::CondCode(Operation);
167}
168
169
170/// isSignedOp - For an integer comparison, return 1 if the comparison is a
171/// signed operation and 2 if the result is an unsigned comparison. Return zero
172/// if the operation does not depend on the sign of the input (setne and seteq).
173static int isSignedOp(ISD::CondCode Opcode) {
174 switch (Opcode) {
175 default: assert(0 && "Illegal integer setcc operation!");
176 case ISD::SETEQ:
177 case ISD::SETNE: return 0;
178 case ISD::SETLT:
179 case ISD::SETLE:
180 case ISD::SETGT:
181 case ISD::SETGE: return 1;
182 case ISD::SETULT:
183 case ISD::SETULE:
184 case ISD::SETUGT:
185 case ISD::SETUGE: return 2;
186 }
187}
188
189/// getSetCCOrOperation - Return the result of a logical OR between different
190/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
191/// returns SETCC_INVALID if it is not possible to represent the resultant
192/// comparison.
193ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
194 bool isInteger) {
195 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
196 // Cannot fold a signed integer setcc with an unsigned integer setcc.
197 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000198
Chris Lattnerc3aae252005-01-07 07:46:32 +0000199 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000200
Chris Lattnerc3aae252005-01-07 07:46:32 +0000201 // If the N and U bits get set then the resultant comparison DOES suddenly
202 // care about orderedness, and is true when ordered.
203 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000204 Op &= ~16; // Clear the U bit if the N bit is set.
205
206 // Canonicalize illegal integer setcc's.
207 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
208 Op = ISD::SETNE;
209
Chris Lattnerc3aae252005-01-07 07:46:32 +0000210 return ISD::CondCode(Op);
211}
212
213/// getSetCCAndOperation - Return the result of a logical AND between different
214/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
215/// function returns zero if it is not possible to represent the resultant
216/// comparison.
217ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
218 bool isInteger) {
219 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
220 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000221 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000222
223 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000224 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
225
226 // Canonicalize illegal integer setcc's.
227 if (isInteger) {
228 switch (Result) {
229 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000230 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
231 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
232 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
233 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000234 }
235 }
236
237 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238}
239
Chris Lattnerb48da392005-01-23 04:39:44 +0000240const TargetMachine &SelectionDAG::getTarget() const {
241 return TLI.getTargetMachine();
242}
243
Jim Laskey58b968b2005-08-17 20:08:02 +0000244//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000245// SDNode Profile Support
246//===----------------------------------------------------------------------===//
247
Jim Laskeydef69b92006-10-27 23:52:51 +0000248/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
249///
Jim Laskey583bd472006-10-27 23:46:08 +0000250static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
251 ID.AddInteger(OpC);
252}
253
254/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
255/// solely with their pointer.
256void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
257 ID.AddPointer(VTList.VTs);
258}
259
Jim Laskeydef69b92006-10-27 23:52:51 +0000260/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
261///
Jim Laskey583bd472006-10-27 23:46:08 +0000262static void AddNodeIDOperands(FoldingSetNodeID &ID,
263 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000264 for (; NumOps; --NumOps, ++Ops) {
265 ID.AddPointer(Ops->Val);
266 ID.AddInteger(Ops->ResNo);
267 }
Jim Laskey583bd472006-10-27 23:46:08 +0000268}
269
Jim Laskey583bd472006-10-27 23:46:08 +0000270static void AddNodeIDNode(FoldingSetNodeID &ID,
271 unsigned short OpC, SDVTList VTList,
272 const SDOperand *OpList, unsigned N) {
273 AddNodeIDOpcode(ID, OpC);
274 AddNodeIDValueTypes(ID, VTList);
275 AddNodeIDOperands(ID, OpList, N);
276}
277
Jim Laskeydef69b92006-10-27 23:52:51 +0000278/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
279/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000280static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
281 AddNodeIDOpcode(ID, N->getOpcode());
282 // Add the return value info.
283 AddNodeIDValueTypes(ID, N->getVTList());
284 // Add the operand info.
285 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
286
287 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000288 switch (N->getOpcode()) {
289 default: break; // Normal nodes don't need extra info.
290 case ISD::TargetConstant:
291 case ISD::Constant:
292 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
293 break;
294 case ISD::TargetConstantFP:
295 case ISD::ConstantFP:
296 ID.AddDouble(cast<ConstantFPSDNode>(N)->getValue());
297 break;
298 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000299 case ISD::GlobalAddress:
300 case ISD::TargetGlobalTLSAddress:
301 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000302 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
303 ID.AddPointer(GA->getGlobal());
304 ID.AddInteger(GA->getOffset());
305 break;
306 }
307 case ISD::BasicBlock:
308 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
309 break;
310 case ISD::Register:
311 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
312 break;
313 case ISD::SRCVALUE: {
314 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
315 ID.AddPointer(SV->getValue());
316 ID.AddInteger(SV->getOffset());
317 break;
318 }
319 case ISD::FrameIndex:
320 case ISD::TargetFrameIndex:
321 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
322 break;
323 case ISD::JumpTable:
324 case ISD::TargetJumpTable:
325 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
326 break;
327 case ISD::ConstantPool:
328 case ISD::TargetConstantPool: {
329 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
330 ID.AddInteger(CP->getAlignment());
331 ID.AddInteger(CP->getOffset());
332 if (CP->isMachineConstantPoolEntry())
333 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
334 else
335 ID.AddPointer(CP->getConstVal());
336 break;
337 }
338 case ISD::LOAD: {
339 LoadSDNode *LD = cast<LoadSDNode>(N);
340 ID.AddInteger(LD->getAddressingMode());
341 ID.AddInteger(LD->getExtensionType());
342 ID.AddInteger(LD->getLoadedVT());
343 ID.AddPointer(LD->getSrcValue());
344 ID.AddInteger(LD->getSrcValueOffset());
345 ID.AddInteger(LD->getAlignment());
346 ID.AddInteger(LD->isVolatile());
347 break;
348 }
349 case ISD::STORE: {
350 StoreSDNode *ST = cast<StoreSDNode>(N);
351 ID.AddInteger(ST->getAddressingMode());
352 ID.AddInteger(ST->isTruncatingStore());
353 ID.AddInteger(ST->getStoredVT());
354 ID.AddPointer(ST->getSrcValue());
355 ID.AddInteger(ST->getSrcValueOffset());
356 ID.AddInteger(ST->getAlignment());
357 ID.AddInteger(ST->isVolatile());
358 break;
359 }
Jim Laskey583bd472006-10-27 23:46:08 +0000360 }
361}
362
363//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000364// SelectionDAG Class
365//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000366
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000367/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000368/// SelectionDAG.
369void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 // Create a dummy node (which is not added to allnodes), that adds a reference
371 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000372 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000373
Chris Lattner190a4182006-08-04 17:45:20 +0000374 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000375
Chris Lattner190a4182006-08-04 17:45:20 +0000376 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000377 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000378 if (I->use_empty())
379 DeadNodes.push_back(I);
380
381 // Process the worklist, deleting the nodes and adding their uses to the
382 // worklist.
383 while (!DeadNodes.empty()) {
384 SDNode *N = DeadNodes.back();
385 DeadNodes.pop_back();
386
387 // Take the node out of the appropriate CSE map.
388 RemoveNodeFromCSEMaps(N);
389
390 // Next, brutally remove the operand list. This is safe to do, as there are
391 // no cycles in the graph.
392 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
393 SDNode *Operand = I->Val;
394 Operand->removeUser(N);
395
396 // Now that we removed this operand, see if there are no uses of it left.
397 if (Operand->use_empty())
398 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000399 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000400 if (N->OperandsNeedDelete)
401 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000402 N->OperandList = 0;
403 N->NumOperands = 0;
404
405 // Finally, remove N itself.
406 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000407 }
408
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000410 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000411}
412
Evan Cheng130a6472006-10-12 20:34:05 +0000413void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
414 SmallVector<SDNode*, 16> DeadNodes;
415 DeadNodes.push_back(N);
416
417 // Process the worklist, deleting the nodes and adding their uses to the
418 // worklist.
419 while (!DeadNodes.empty()) {
420 SDNode *N = DeadNodes.back();
421 DeadNodes.pop_back();
422
423 // Take the node out of the appropriate CSE map.
424 RemoveNodeFromCSEMaps(N);
425
426 // Next, brutally remove the operand list. This is safe to do, as there are
427 // no cycles in the graph.
428 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
429 SDNode *Operand = I->Val;
430 Operand->removeUser(N);
431
432 // Now that we removed this operand, see if there are no uses of it left.
433 if (Operand->use_empty())
434 DeadNodes.push_back(Operand);
435 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000436 if (N->OperandsNeedDelete)
437 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000438 N->OperandList = 0;
439 N->NumOperands = 0;
440
441 // Finally, remove N itself.
442 Deleted.push_back(N);
443 AllNodes.erase(N);
444 }
445}
446
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000447void SelectionDAG::DeleteNode(SDNode *N) {
448 assert(N->use_empty() && "Cannot delete a node that is not dead!");
449
450 // First take this out of the appropriate CSE map.
451 RemoveNodeFromCSEMaps(N);
452
Chris Lattner1e111c72005-09-07 05:37:01 +0000453 // Finally, remove uses due to operands of this node, remove from the
454 // AllNodes list, and delete the node.
455 DeleteNodeNotInCSEMaps(N);
456}
457
458void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
459
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000460 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000461 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000462
463 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000464 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
465 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000466 if (N->OperandsNeedDelete)
467 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000468 N->OperandList = 0;
469 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000470
471 delete N;
472}
473
Chris Lattner149c58c2005-08-16 18:17:10 +0000474/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
475/// correspond to it. This is useful when we're about to delete or repurpose
476/// the node. We don't want future request for structurally identical nodes
477/// to return N anymore.
478void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000479 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000480 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000481 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000482 case ISD::STRING:
483 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
484 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000485 case ISD::CONDCODE:
486 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
487 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000488 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000489 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
490 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000491 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000492 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000493 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000494 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000495 Erased =
496 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000497 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000498 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000499 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000500 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
501 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000502 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000503 // Remove it from the CSE Map.
504 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000505 break;
506 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000507#ifndef NDEBUG
508 // Verify that the node was actually in one of the CSE maps, unless it has a
509 // flag result (which cannot be CSE'd) or is one of the special cases that are
510 // not subject to CSE.
511 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000512 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000513 N->dump();
Bill Wendling832171c2006-12-07 20:04:42 +0000514 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000515 assert(0 && "Node is not in map!");
516 }
517#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000518}
519
Chris Lattner8b8749f2005-08-17 19:00:20 +0000520/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
521/// has been taken out and modified in some way. If the specified node already
522/// exists in the CSE maps, do not modify the maps, but return the existing node
523/// instead. If it doesn't exist, add it and return null.
524///
525SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
526 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000527 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000528 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000529
Chris Lattner9f8cc692005-12-19 22:21:21 +0000530 // Check that remaining values produced are not flags.
531 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
532 if (N->getValueType(i) == MVT::Flag)
533 return 0; // Never CSE anything that produces a flag.
534
Chris Lattnera5682852006-08-07 23:03:03 +0000535 SDNode *New = CSEMap.GetOrInsertNode(N);
536 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000537 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000538}
539
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000540/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
541/// were replaced with those specified. If this node is never memoized,
542/// return null, otherwise return a pointer to the slot it would take. If a
543/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000544SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
545 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000546 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000547 return 0; // Never add these nodes.
548
549 // Check that remaining values produced are not flags.
550 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
551 if (N->getValueType(i) == MVT::Flag)
552 return 0; // Never CSE anything that produces a flag.
553
Chris Lattner63e3f142007-02-04 07:28:00 +0000554 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000555 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000556 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000557 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000558}
559
560/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
561/// were replaced with those specified. If this node is never memoized,
562/// return null, otherwise return a pointer to the slot it would take. If a
563/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000564SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
565 SDOperand Op1, SDOperand Op2,
566 void *&InsertPos) {
567 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
568 return 0; // Never add these nodes.
569
570 // Check that remaining values produced are not flags.
571 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
572 if (N->getValueType(i) == MVT::Flag)
573 return 0; // Never CSE anything that produces a flag.
574
Chris Lattner63e3f142007-02-04 07:28:00 +0000575 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000576 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000577 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000578 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
579}
580
581
582/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
583/// were replaced with those specified. If this node is never memoized,
584/// return null, otherwise return a pointer to the slot it would take. If a
585/// node already exists with these operands, the slot will be non-null.
586SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000587 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000588 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000589 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000590 return 0; // Never add these nodes.
591
592 // Check that remaining values produced are not flags.
593 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
594 if (N->getValueType(i) == MVT::Flag)
595 return 0; // Never CSE anything that produces a flag.
596
Jim Laskey583bd472006-10-27 23:46:08 +0000597 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000598 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), 0, 0);
Jim Laskey583bd472006-10-27 23:46:08 +0000599
Evan Cheng9629aba2006-10-11 01:47:58 +0000600 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
601 ID.AddInteger(LD->getAddressingMode());
602 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000603 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000604 ID.AddPointer(LD->getSrcValue());
605 ID.AddInteger(LD->getSrcValueOffset());
606 ID.AddInteger(LD->getAlignment());
607 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000608 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
609 ID.AddInteger(ST->getAddressingMode());
610 ID.AddInteger(ST->isTruncatingStore());
611 ID.AddInteger(ST->getStoredVT());
612 ID.AddPointer(ST->getSrcValue());
613 ID.AddInteger(ST->getSrcValueOffset());
614 ID.AddInteger(ST->getAlignment());
615 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000616 }
Jim Laskey583bd472006-10-27 23:46:08 +0000617
618 AddNodeIDOperands(ID, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000619 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000620}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000621
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000622
Chris Lattner78ec3112003-08-11 14:57:33 +0000623SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000624 while (!AllNodes.empty()) {
625 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000626 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000627 if (N->OperandsNeedDelete)
628 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000629 N->OperandList = 0;
630 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000631 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000632 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000633}
634
Chris Lattner0f2287b2005-04-13 02:38:18 +0000635SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000636 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000637 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000638 return getNode(ISD::AND, Op.getValueType(), Op,
639 getConstant(Imm, Op.getValueType()));
640}
641
Chris Lattner36ce6912005-11-29 06:21:05 +0000642SDOperand SelectionDAG::getString(const std::string &Val) {
643 StringSDNode *&N = StringNodes[Val];
644 if (!N) {
645 N = new StringSDNode(Val);
646 AllNodes.push_back(N);
647 }
648 return SDOperand(N, 0);
649}
650
Chris Lattner61b09412006-08-11 21:01:22 +0000651SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000652 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000653 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000654
Chris Lattner61b09412006-08-11 21:01:22 +0000655 // Mask out any bits that are not valid for this constant.
656 Val &= MVT::getIntVTBitMask(VT);
657
658 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000659 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000660 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000661 ID.AddInteger(Val);
662 void *IP = 0;
663 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
664 return SDOperand(E, 0);
665 SDNode *N = new ConstantSDNode(isT, Val, VT);
666 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000667 AllNodes.push_back(N);
668 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000669}
670
Chris Lattner61b09412006-08-11 21:01:22 +0000671
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000672SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
673 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000674 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
675 if (VT == MVT::f32)
676 Val = (float)Val; // Mask out extra precision.
677
Chris Lattnerd8658612005-02-17 20:17:32 +0000678 // Do the map lookup using the actual bit pattern for the floating point
679 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
680 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000681 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000682 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000683 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Jim Laskey583bd472006-10-27 23:46:08 +0000684 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000685 void *IP = 0;
686 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
687 return SDOperand(E, 0);
688 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
689 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000690 AllNodes.push_back(N);
691 return SDOperand(N, 0);
692}
693
Chris Lattnerc3aae252005-01-07 07:46:32 +0000694SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000695 MVT::ValueType VT, int Offset,
696 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000697 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
698 unsigned Opc;
699 if (GVar && GVar->isThreadLocal())
700 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
701 else
702 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000703 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000704 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000705 ID.AddPointer(GV);
706 ID.AddInteger(Offset);
707 void *IP = 0;
708 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
709 return SDOperand(E, 0);
710 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
711 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000712 AllNodes.push_back(N);
713 return SDOperand(N, 0);
714}
715
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000716SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
717 bool isTarget) {
718 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000719 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000720 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000721 ID.AddInteger(FI);
722 void *IP = 0;
723 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
724 return SDOperand(E, 0);
725 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
726 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000727 AllNodes.push_back(N);
728 return SDOperand(N, 0);
729}
730
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000731SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
732 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000733 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000734 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000735 ID.AddInteger(JTI);
736 void *IP = 0;
737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
738 return SDOperand(E, 0);
739 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
740 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000741 AllNodes.push_back(N);
742 return SDOperand(N, 0);
743}
744
Evan Chengb8973bd2006-01-31 22:23:14 +0000745SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000746 unsigned Alignment, int Offset,
747 bool isTarget) {
748 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000749 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000750 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000751 ID.AddInteger(Alignment);
752 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000753 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000754 void *IP = 0;
755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
756 return SDOperand(E, 0);
757 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
758 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000759 AllNodes.push_back(N);
760 return SDOperand(N, 0);
761}
762
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763
Evan Chengd6594ae2006-09-12 21:00:35 +0000764SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
765 MVT::ValueType VT,
766 unsigned Alignment, int Offset,
767 bool isTarget) {
768 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000769 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000770 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000771 ID.AddInteger(Alignment);
772 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000773 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000774 void *IP = 0;
775 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
776 return SDOperand(E, 0);
777 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
778 CSEMap.InsertNode(N, IP);
779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
781}
782
783
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000785 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000786 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000787 ID.AddPointer(MBB);
788 void *IP = 0;
789 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
790 return SDOperand(E, 0);
791 SDNode *N = new BasicBlockSDNode(MBB);
792 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793 AllNodes.push_back(N);
794 return SDOperand(N, 0);
795}
796
Chris Lattner15e4b012005-07-10 00:07:11 +0000797SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
798 if ((unsigned)VT >= ValueTypeNodes.size())
799 ValueTypeNodes.resize(VT+1);
800 if (ValueTypeNodes[VT] == 0) {
801 ValueTypeNodes[VT] = new VTSDNode(VT);
802 AllNodes.push_back(ValueTypeNodes[VT]);
803 }
804
805 return SDOperand(ValueTypeNodes[VT], 0);
806}
807
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
809 SDNode *&N = ExternalSymbols[Sym];
810 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000811 N = new ExternalSymbolSDNode(false, Sym, VT);
812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
814}
815
Chris Lattner809ec112006-01-28 10:09:25 +0000816SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
817 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000818 SDNode *&N = TargetExternalSymbols[Sym];
819 if (N) return SDOperand(N, 0);
820 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 AllNodes.push_back(N);
822 return SDOperand(N, 0);
823}
824
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000825SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
826 if ((unsigned)Cond >= CondCodeNodes.size())
827 CondCodeNodes.resize(Cond+1);
828
Chris Lattner079a27a2005-08-09 20:40:02 +0000829 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000830 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000831 AllNodes.push_back(CondCodeNodes[Cond]);
832 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000833 return SDOperand(CondCodeNodes[Cond], 0);
834}
835
Chris Lattner0fdd7682005-08-30 22:38:38 +0000836SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000837 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000838 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000839 ID.AddInteger(RegNo);
840 void *IP = 0;
841 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
842 return SDOperand(E, 0);
843 SDNode *N = new RegisterSDNode(RegNo, VT);
844 CSEMap.InsertNode(N, IP);
845 AllNodes.push_back(N);
846 return SDOperand(N, 0);
847}
848
849SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
850 assert((!V || isa<PointerType>(V->getType())) &&
851 "SrcValue is not a pointer?");
852
Jim Laskey583bd472006-10-27 23:46:08 +0000853 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000854 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000855 ID.AddPointer(V);
856 ID.AddInteger(Offset);
857 void *IP = 0;
858 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
859 return SDOperand(E, 0);
860 SDNode *N = new SrcValueSDNode(V, Offset);
861 CSEMap.InsertNode(N, IP);
862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000864}
865
Chris Lattner51dabfb2006-10-14 00:41:01 +0000866SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
867 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868 // These setcc operations always fold.
869 switch (Cond) {
870 default: break;
871 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000872 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000873 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000874 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000875
876 case ISD::SETOEQ:
877 case ISD::SETOGT:
878 case ISD::SETOGE:
879 case ISD::SETOLT:
880 case ISD::SETOLE:
881 case ISD::SETONE:
882 case ISD::SETO:
883 case ISD::SETUO:
884 case ISD::SETUEQ:
885 case ISD::SETUNE:
886 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
887 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000888 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000889
Chris Lattner67255a12005-04-07 18:14:58 +0000890 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
891 uint64_t C2 = N2C->getValue();
892 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
893 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000894
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895 // Sign extend the operands if required
896 if (ISD::isSignedIntSetCC(Cond)) {
897 C1 = N1C->getSignExtended();
898 C2 = N2C->getSignExtended();
899 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000900
Chris Lattnerc3aae252005-01-07 07:46:32 +0000901 switch (Cond) {
902 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000903 case ISD::SETEQ: return getConstant(C1 == C2, VT);
904 case ISD::SETNE: return getConstant(C1 != C2, VT);
905 case ISD::SETULT: return getConstant(C1 < C2, VT);
906 case ISD::SETUGT: return getConstant(C1 > C2, VT);
907 case ISD::SETULE: return getConstant(C1 <= C2, VT);
908 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
909 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
910 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
911 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
912 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000913 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914 }
Chris Lattner67255a12005-04-07 18:14:58 +0000915 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
917 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
918 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000919
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920 switch (Cond) {
921 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000922 case ISD::SETEQ: return getConstant(C1 == C2, VT);
923 case ISD::SETNE: return getConstant(C1 != C2, VT);
924 case ISD::SETLT: return getConstant(C1 < C2, VT);
925 case ISD::SETGT: return getConstant(C1 > C2, VT);
926 case ISD::SETLE: return getConstant(C1 <= C2, VT);
927 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 }
929 } else {
930 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000931 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000933
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000934 // Could not fold it.
935 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936}
937
Chris Lattner51dabfb2006-10-14 00:41:01 +0000938
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000940///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000942 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000943 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +0000944 void *IP = 0;
945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
946 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +0000947 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000948 CSEMap.InsertNode(N, IP);
949
950 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000951 return SDOperand(N, 0);
952}
953
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
955 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000956 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000957 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
959 uint64_t Val = C->getValue();
960 switch (Opcode) {
961 default: break;
962 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000963 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
965 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000966 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
967 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000968 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000969 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000970 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000971 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000972 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000973 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000974 case ISD::BSWAP:
975 switch(VT) {
976 default: assert(0 && "Invalid bswap!"); break;
977 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
978 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
979 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
980 }
981 break;
982 case ISD::CTPOP:
983 switch(VT) {
984 default: assert(0 && "Invalid ctpop!"); break;
985 case MVT::i1: return getConstant(Val != 0, VT);
986 case MVT::i8:
987 Tmp1 = (unsigned)Val & 0xFF;
988 return getConstant(CountPopulation_32(Tmp1), VT);
989 case MVT::i16:
990 Tmp1 = (unsigned)Val & 0xFFFF;
991 return getConstant(CountPopulation_32(Tmp1), VT);
992 case MVT::i32:
993 return getConstant(CountPopulation_32((unsigned)Val), VT);
994 case MVT::i64:
995 return getConstant(CountPopulation_64(Val), VT);
996 }
997 case ISD::CTLZ:
998 switch(VT) {
999 default: assert(0 && "Invalid ctlz!"); break;
1000 case MVT::i1: return getConstant(Val == 0, VT);
1001 case MVT::i8:
1002 Tmp1 = (unsigned)Val & 0xFF;
1003 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1004 case MVT::i16:
1005 Tmp1 = (unsigned)Val & 0xFFFF;
1006 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1007 case MVT::i32:
1008 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1009 case MVT::i64:
1010 return getConstant(CountLeadingZeros_64(Val), VT);
1011 }
1012 case ISD::CTTZ:
1013 switch(VT) {
1014 default: assert(0 && "Invalid cttz!"); break;
1015 case MVT::i1: return getConstant(Val == 0, VT);
1016 case MVT::i8:
1017 Tmp1 = (unsigned)Val | 0x100;
1018 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1019 case MVT::i16:
1020 Tmp1 = (unsigned)Val | 0x10000;
1021 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1022 case MVT::i32:
1023 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1024 case MVT::i64:
1025 return getConstant(CountTrailingZeros_64(Val), VT);
1026 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001027 }
1028 }
1029
Chris Lattner94683772005-12-23 05:30:37 +00001030 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1032 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001033 case ISD::FNEG:
1034 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001035 case ISD::FABS:
1036 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037 case ISD::FP_ROUND:
1038 case ISD::FP_EXTEND:
1039 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001040 case ISD::FP_TO_SINT:
1041 return getConstant((int64_t)C->getValue(), VT);
1042 case ISD::FP_TO_UINT:
1043 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001044 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001045 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001046 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001047 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001048 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001049 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 }
1051
1052 unsigned OpOpcode = Operand.Val->getOpcode();
1053 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001054 case ISD::TokenFactor:
1055 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001056 case ISD::FP_ROUND:
1057 case ISD::FP_EXTEND:
1058 assert(MVT::isFloatingPoint(VT) &&
1059 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1060 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001062 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1063 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001065 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1067 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1068 break;
1069 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001070 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1071 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001073 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001074 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001075 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001077 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001078 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1079 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001080 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001081 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001082 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1083 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1084 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1085 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001087 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1088 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001090 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 if (OpOpcode == ISD::TRUNCATE)
1092 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001093 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1094 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001095 // If the source is smaller than the dest, we still need an extend.
1096 if (Operand.Val->getOperand(0).getValueType() < VT)
1097 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1098 else if (Operand.Val->getOperand(0).getValueType() > VT)
1099 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1100 else
1101 return Operand.Val->getOperand(0);
1102 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 break;
Chris Lattner35481892005-12-23 00:16:34 +00001104 case ISD::BIT_CONVERT:
1105 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001106 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001107 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001108 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001109 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1110 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001111 if (OpOpcode == ISD::UNDEF)
1112 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001113 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001114 case ISD::SCALAR_TO_VECTOR:
1115 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1116 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1117 "Illegal SCALAR_TO_VECTOR node!");
1118 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001119 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001120 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1121 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001122 Operand.Val->getOperand(0));
1123 if (OpOpcode == ISD::FNEG) // --X -> X
1124 return Operand.Val->getOperand(0);
1125 break;
1126 case ISD::FABS:
1127 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1128 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1129 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 }
1131
Chris Lattner43247a12005-08-25 19:12:10 +00001132 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001133 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001134 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001135 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001136 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001137 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001138 void *IP = 0;
1139 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1140 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001141 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001142 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001143 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001144 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001145 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 AllNodes.push_back(N);
1147 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001148}
1149
Chris Lattner36019aa2005-04-18 03:48:41 +00001150
1151
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1153 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001154#ifndef NDEBUG
1155 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001156 case ISD::TokenFactor:
1157 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1158 N2.getValueType() == MVT::Other && "Invalid token factor!");
1159 break;
Chris Lattner76365122005-01-16 02:23:22 +00001160 case ISD::AND:
1161 case ISD::OR:
1162 case ISD::XOR:
1163 case ISD::UDIV:
1164 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001165 case ISD::MULHU:
1166 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001167 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1168 // fall through
1169 case ISD::ADD:
1170 case ISD::SUB:
1171 case ISD::MUL:
1172 case ISD::SDIV:
1173 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001174 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1175 // fall through.
1176 case ISD::FADD:
1177 case ISD::FSUB:
1178 case ISD::FMUL:
1179 case ISD::FDIV:
1180 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001181 assert(N1.getValueType() == N2.getValueType() &&
1182 N1.getValueType() == VT && "Binary operator types must match!");
1183 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001184 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1185 assert(N1.getValueType() == VT &&
1186 MVT::isFloatingPoint(N1.getValueType()) &&
1187 MVT::isFloatingPoint(N2.getValueType()) &&
1188 "Invalid FCOPYSIGN!");
1189 break;
Chris Lattner76365122005-01-16 02:23:22 +00001190 case ISD::SHL:
1191 case ISD::SRA:
1192 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001193 case ISD::ROTL:
1194 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001195 assert(VT == N1.getValueType() &&
1196 "Shift operators return type must be the same as their first arg");
1197 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001198 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001199 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001200 case ISD::FP_ROUND_INREG: {
1201 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1202 assert(VT == N1.getValueType() && "Not an inreg round!");
1203 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1204 "Cannot FP_ROUND_INREG integer types");
1205 assert(EVT <= VT && "Not rounding down!");
1206 break;
1207 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001208 case ISD::AssertSext:
1209 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001210 case ISD::SIGN_EXTEND_INREG: {
1211 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1212 assert(VT == N1.getValueType() && "Not an inreg extend!");
1213 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1214 "Cannot *_EXTEND_INREG FP types");
1215 assert(EVT <= VT && "Not extending!");
1216 }
1217
Chris Lattner76365122005-01-16 02:23:22 +00001218 default: break;
1219 }
1220#endif
1221
Chris Lattnerc3aae252005-01-07 07:46:32 +00001222 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1223 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1224 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001225 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1226 int64_t Val = N1C->getValue();
1227 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1228 Val <<= 64-FromBits;
1229 Val >>= 64-FromBits;
1230 return getConstant(Val, VT);
1231 }
1232
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 if (N2C) {
1234 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1235 switch (Opcode) {
1236 case ISD::ADD: return getConstant(C1 + C2, VT);
1237 case ISD::SUB: return getConstant(C1 - C2, VT);
1238 case ISD::MUL: return getConstant(C1 * C2, VT);
1239 case ISD::UDIV:
1240 if (C2) return getConstant(C1 / C2, VT);
1241 break;
1242 case ISD::UREM :
1243 if (C2) return getConstant(C1 % C2, VT);
1244 break;
1245 case ISD::SDIV :
1246 if (C2) return getConstant(N1C->getSignExtended() /
1247 N2C->getSignExtended(), VT);
1248 break;
1249 case ISD::SREM :
1250 if (C2) return getConstant(N1C->getSignExtended() %
1251 N2C->getSignExtended(), VT);
1252 break;
1253 case ISD::AND : return getConstant(C1 & C2, VT);
1254 case ISD::OR : return getConstant(C1 | C2, VT);
1255 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001256 case ISD::SHL : return getConstant(C1 << C2, VT);
1257 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001258 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001259 case ISD::ROTL :
1260 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1261 VT);
1262 case ISD::ROTR :
1263 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1264 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 default: break;
1266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 } else { // Cannonicalize constant to RHS if commutative
1268 if (isCommutativeBinOp(Opcode)) {
1269 std::swap(N1C, N2C);
1270 std::swap(N1, N2);
1271 }
1272 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001273 }
1274
1275 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1276 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001277 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001278 if (N2CFP) {
1279 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1280 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001281 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1282 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1283 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1284 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001285 if (C2) return getConstantFP(C1 / C2, VT);
1286 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001287 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288 if (C2) return getConstantFP(fmod(C1, C2), VT);
1289 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001290 case ISD::FCOPYSIGN: {
1291 union {
1292 double F;
1293 uint64_t I;
1294 } u1;
1295 union {
1296 double F;
1297 int64_t I;
1298 } u2;
1299 u1.F = C1;
1300 u2.F = C2;
1301 if (u2.I < 0) // Sign bit of RHS set?
1302 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1303 else
1304 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1305 return getConstantFP(u1.F, VT);
1306 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001307 default: break;
1308 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309 } else { // Cannonicalize constant to RHS if commutative
1310 if (isCommutativeBinOp(Opcode)) {
1311 std::swap(N1CFP, N2CFP);
1312 std::swap(N1, N2);
1313 }
1314 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001315 }
Chris Lattner62b57722006-04-20 05:39:12 +00001316
1317 // Canonicalize an UNDEF to the RHS, even over a constant.
1318 if (N1.getOpcode() == ISD::UNDEF) {
1319 if (isCommutativeBinOp(Opcode)) {
1320 std::swap(N1, N2);
1321 } else {
1322 switch (Opcode) {
1323 case ISD::FP_ROUND_INREG:
1324 case ISD::SIGN_EXTEND_INREG:
1325 case ISD::SUB:
1326 case ISD::FSUB:
1327 case ISD::FDIV:
1328 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001329 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001330 return N1; // fold op(undef, arg2) -> undef
1331 case ISD::UDIV:
1332 case ISD::SDIV:
1333 case ISD::UREM:
1334 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001335 case ISD::SRL:
1336 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001337 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1338 }
1339 }
1340 }
1341
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001342 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001343 if (N2.getOpcode() == ISD::UNDEF) {
1344 switch (Opcode) {
1345 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00001346 case ISD::ADDC:
1347 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00001348 case ISD::SUB:
1349 case ISD::FADD:
1350 case ISD::FSUB:
1351 case ISD::FMUL:
1352 case ISD::FDIV:
1353 case ISD::FREM:
1354 case ISD::UDIV:
1355 case ISD::SDIV:
1356 case ISD::UREM:
1357 case ISD::SREM:
1358 case ISD::XOR:
1359 return N2; // fold op(arg1, undef) -> undef
1360 case ISD::MUL:
1361 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001362 case ISD::SRL:
1363 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001364 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1365 case ISD::OR:
1366 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001367 case ISD::SRA:
1368 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001369 }
1370 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001371
Chris Lattner51dabfb2006-10-14 00:41:01 +00001372 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001373 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00001374 case ISD::TokenFactor:
1375 // Fold trivial token factors.
1376 if (N1.getOpcode() == ISD::EntryToken) return N2;
1377 if (N2.getOpcode() == ISD::EntryToken) return N1;
1378 break;
1379
Chris Lattner51dabfb2006-10-14 00:41:01 +00001380 case ISD::AND:
1381 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1382 // worth handling here.
1383 if (N2C && N2C->getValue() == 0)
1384 return N2;
1385 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001386 case ISD::OR:
1387 case ISD::XOR:
1388 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1389 // worth handling here.
1390 if (N2C && N2C->getValue() == 0)
1391 return N1;
1392 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001393 case ISD::FP_ROUND_INREG:
1394 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1395 break;
1396 case ISD::SIGN_EXTEND_INREG: {
1397 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1398 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001399 break;
1400 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001401 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001402 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1403
Chris Lattner5c6621c2006-09-19 04:51:23 +00001404 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1405 // 64-bit integers into 32-bit parts. Instead of building the extract of
1406 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001407 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001408 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001409
1410 // EXTRACT_ELEMENT of a constant int is also very common.
1411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1412 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1413 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001414 }
1415 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001416
Nate Begemaneea805e2005-04-13 21:23:31 +00001417 // FIXME: figure out how to safely handle things like
1418 // int foo(int x) { return 1 << (x & 255); }
1419 // int bar() { return foo(256); }
1420#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001421 case ISD::SHL:
1422 case ISD::SRL:
1423 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001424 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001425 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001426 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001427 else if (N2.getOpcode() == ISD::AND)
1428 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1429 // If the and is only masking out bits that cannot effect the shift,
1430 // eliminate the and.
1431 unsigned NumBits = MVT::getSizeInBits(VT);
1432 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1433 return getNode(Opcode, VT, N1, N2.getOperand(0));
1434 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001435 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001436#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001437 }
1438
Chris Lattner27e9b412005-05-11 18:57:39 +00001439 // Memoize this node if possible.
1440 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001441 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001442 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001443 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00001444 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001445 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00001446 void *IP = 0;
1447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1448 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001449 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001450 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001451 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001452 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001453 }
1454
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 AllNodes.push_back(N);
1456 return SDOperand(N, 0);
1457}
1458
Chris Lattnerc3aae252005-01-07 07:46:32 +00001459SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1460 SDOperand N1, SDOperand N2, SDOperand N3) {
1461 // Perform various simplifications.
1462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1463 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001465 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001466 // Use FoldSetCC to simplify SETCC's.
1467 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001468 if (Simp.Val) return Simp;
1469 break;
1470 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001471 case ISD::SELECT:
1472 if (N1C)
1473 if (N1C->getValue())
1474 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001475 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001476 return N3; // select false, X, Y -> Y
1477
1478 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001479 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001480 case ISD::BRCOND:
1481 if (N2C)
1482 if (N2C->getValue()) // Unconditional branch
1483 return getNode(ISD::BR, MVT::Other, N1, N3);
1484 else
1485 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001486 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001487 case ISD::VECTOR_SHUFFLE:
1488 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1489 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1490 N3.getOpcode() == ISD::BUILD_VECTOR &&
1491 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1492 "Illegal VECTOR_SHUFFLE node!");
1493 break;
Chris Lattner4829b1c2007-04-12 05:58:43 +00001494 case ISD::VBIT_CONVERT:
1495 // Fold vbit_convert nodes from a type to themselves.
1496 if (N1.getValueType() == MVT::Vector) {
1497 assert(isa<ConstantSDNode>(*(N1.Val->op_end()-2)) &&
1498 isa<VTSDNode>(*(N1.Val->op_end()-1)) && "Malformed vector input!");
1499 if (*(N1.Val->op_end()-2) == N2 && *(N1.Val->op_end()-1) == N3)
1500 return N1;
1501 }
1502 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001503 }
1504
Chris Lattner43247a12005-08-25 19:12:10 +00001505 // Memoize node if it doesn't produce a flag.
1506 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001507 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001508 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001509 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00001510 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001511 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00001512 void *IP = 0;
1513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1514 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001515 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001516 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001517 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001518 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00001519 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001520 AllNodes.push_back(N);
1521 return SDOperand(N, 0);
1522}
1523
1524SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001525 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001526 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001527 SDOperand Ops[] = { N1, N2, N3, N4 };
1528 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001529}
1530
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001531SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1532 SDOperand N1, SDOperand N2, SDOperand N3,
1533 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001534 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1535 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001536}
1537
Evan Cheng7038daf2005-12-10 00:37:58 +00001538SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1539 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001540 const Value *SV, int SVOffset,
Reid Spencerc67bdc22007-04-21 18:36:27 +00001541 bool isVolatile) {
1542 // FIXME: Alignment == 1 for now.
1543 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001544 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001545 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00001546 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001547 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001548 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00001549 ID.AddInteger(ISD::UNINDEXED);
1550 ID.AddInteger(ISD::NON_EXTLOAD);
1551 ID.AddInteger(VT);
1552 ID.AddPointer(SV);
1553 ID.AddInteger(SVOffset);
1554 ID.AddInteger(Alignment);
1555 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001556 void *IP = 0;
1557 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1558 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001559 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001560 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1561 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00001562 CSEMap.InsertNode(N, IP);
1563 AllNodes.push_back(N);
1564 return SDOperand(N, 0);
1565}
1566
1567SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00001568 SDOperand Chain, SDOperand Ptr,
1569 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00001570 int SVOffset, MVT::ValueType EVT,
Reid Spencerc67bdc22007-04-21 18:36:27 +00001571 bool isVolatile) {
Evan Cheng466685d2006-10-09 20:57:25 +00001572 // If they are asking for an extending load from/to the same thing, return a
1573 // normal load.
1574 if (VT == EVT)
1575 ExtType = ISD::NON_EXTLOAD;
1576
1577 if (MVT::isVector(VT))
1578 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1579 else
1580 assert(EVT < VT && "Should only be an extending load, not truncating!");
1581 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1582 "Cannot sign/zero extend a FP/Vector load!");
1583 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1584 "Cannot convert from FP to Int or Int -> FP!");
1585
Reid Spencerc67bdc22007-04-21 18:36:27 +00001586 // FIXME: Alignment == 1 for now.
1587 unsigned Alignment = 1;
Evan Cheng466685d2006-10-09 20:57:25 +00001588 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001589 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00001590 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001591 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001592 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00001593 ID.AddInteger(ISD::UNINDEXED);
1594 ID.AddInteger(ExtType);
1595 ID.AddInteger(EVT);
1596 ID.AddPointer(SV);
1597 ID.AddInteger(SVOffset);
1598 ID.AddInteger(Alignment);
1599 ID.AddInteger(isVolatile);
1600 void *IP = 0;
1601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1602 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001603 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001604 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001605 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001606 AllNodes.push_back(N);
1607 return SDOperand(N, 0);
1608}
1609
Evan Cheng144d8f02006-11-09 17:55:04 +00001610SDOperand
1611SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
1612 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00001613 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00001614 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
1615 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00001616 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00001617 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00001618 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00001619 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001620 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00001621 ID.AddInteger(AM);
1622 ID.AddInteger(LD->getExtensionType());
1623 ID.AddInteger(LD->getLoadedVT());
1624 ID.AddPointer(LD->getSrcValue());
1625 ID.AddInteger(LD->getSrcValueOffset());
1626 ID.AddInteger(LD->getAlignment());
1627 ID.AddInteger(LD->isVolatile());
1628 void *IP = 0;
1629 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1630 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001631 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00001632 LD->getExtensionType(), LD->getLoadedVT(),
1633 LD->getSrcValue(), LD->getSrcValueOffset(),
1634 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00001635 CSEMap.InsertNode(N, IP);
1636 AllNodes.push_back(N);
1637 return SDOperand(N, 0);
1638}
1639
Evan Cheng7038daf2005-12-10 00:37:58 +00001640SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1641 SDOperand Chain, SDOperand Ptr,
1642 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001643 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1644 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001645 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001646}
1647
Jeff Cohend41b30d2006-11-05 19:31:28 +00001648SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001649 SDOperand Ptr, const Value *SV, int SVOffset,
Reid Spencerc67bdc22007-04-21 18:36:27 +00001650 bool isVolatile) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00001651 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001652
Reid Spencerc67bdc22007-04-21 18:36:27 +00001653 // FIXME: Alignment == 1 for now.
1654 unsigned Alignment = 1;
Evan Chengad071e12006-10-05 22:57:11 +00001655 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001656 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00001657 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001658 FoldingSetNodeID ID;
1659 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001660 ID.AddInteger(ISD::UNINDEXED);
1661 ID.AddInteger(false);
1662 ID.AddInteger(VT);
1663 ID.AddPointer(SV);
1664 ID.AddInteger(SVOffset);
1665 ID.AddInteger(Alignment);
1666 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001667 void *IP = 0;
1668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1669 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001670 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001671 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001672 CSEMap.InsertNode(N, IP);
1673 AllNodes.push_back(N);
1674 return SDOperand(N, 0);
1675}
1676
Jeff Cohend41b30d2006-11-05 19:31:28 +00001677SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001678 SDOperand Ptr, const Value *SV,
1679 int SVOffset, MVT::ValueType SVT,
Reid Spencerc67bdc22007-04-21 18:36:27 +00001680 bool isVolatile) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00001681 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001682 bool isTrunc = VT != SVT;
1683
1684 assert(VT > SVT && "Not a truncation?");
1685 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1686 "Can't do FP-INT conversion!");
1687
Reid Spencerc67bdc22007-04-21 18:36:27 +00001688 // FIXME: Alignment == 1 for now.
1689 unsigned Alignment = 1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001690 SDVTList VTs = getVTList(MVT::Other);
1691 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00001692 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001693 FoldingSetNodeID ID;
1694 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001695 ID.AddInteger(ISD::UNINDEXED);
1696 ID.AddInteger(isTrunc);
1697 ID.AddInteger(SVT);
1698 ID.AddPointer(SV);
1699 ID.AddInteger(SVOffset);
1700 ID.AddInteger(Alignment);
1701 ID.AddInteger(isVolatile);
1702 void *IP = 0;
1703 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1704 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001705 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001706 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001707 CSEMap.InsertNode(N, IP);
1708 AllNodes.push_back(N);
1709 return SDOperand(N, 0);
1710}
1711
Evan Cheng144d8f02006-11-09 17:55:04 +00001712SDOperand
1713SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
1714 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00001715 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
1716 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
1717 "Store is already a indexed store!");
1718 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
1719 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
1720 FoldingSetNodeID ID;
1721 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
1722 ID.AddInteger(AM);
1723 ID.AddInteger(ST->isTruncatingStore());
1724 ID.AddInteger(ST->getStoredVT());
1725 ID.AddPointer(ST->getSrcValue());
1726 ID.AddInteger(ST->getSrcValueOffset());
1727 ID.AddInteger(ST->getAlignment());
1728 ID.AddInteger(ST->isVolatile());
1729 void *IP = 0;
1730 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1731 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001732 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00001733 ST->isTruncatingStore(), ST->getStoredVT(),
1734 ST->getSrcValue(), ST->getSrcValueOffset(),
1735 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00001736 CSEMap.InsertNode(N, IP);
1737 AllNodes.push_back(N);
1738 return SDOperand(N, 0);
1739}
1740
Nate Begemanacc398c2006-01-25 18:21:52 +00001741SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1742 SDOperand Chain, SDOperand Ptr,
1743 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001744 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001745 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001746}
1747
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001748SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001749 const SDOperand *Ops, unsigned NumOps) {
1750 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001752 case 1: return getNode(Opcode, VT, Ops[0]);
1753 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1754 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001755 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001756 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001757
Chris Lattneref847df2005-04-09 03:27:28 +00001758 switch (Opcode) {
1759 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001760 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001761 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001762 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1763 "LHS and RHS of condition must have same type!");
1764 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1765 "True and False arms of SelectCC must have same type!");
1766 assert(Ops[2].getValueType() == VT &&
1767 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001768 break;
1769 }
1770 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001771 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001772 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1773 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001774 break;
1775 }
Chris Lattneref847df2005-04-09 03:27:28 +00001776 }
1777
Chris Lattner385328c2005-05-14 07:42:29 +00001778 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001779 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001780 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001781 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001782 FoldingSetNodeID ID;
1783 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001784 void *IP = 0;
1785 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1786 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00001787 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001788 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001789 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00001790 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00001791 }
Chris Lattneref847df2005-04-09 03:27:28 +00001792 AllNodes.push_back(N);
1793 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001794}
1795
Chris Lattner89c34632005-05-14 06:20:26 +00001796SDOperand SelectionDAG::getNode(unsigned Opcode,
1797 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001798 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001799 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1800 Ops, NumOps);
1801}
1802
1803SDOperand SelectionDAG::getNode(unsigned Opcode,
1804 const MVT::ValueType *VTs, unsigned NumVTs,
1805 const SDOperand *Ops, unsigned NumOps) {
1806 if (NumVTs == 1)
1807 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001808 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1809}
1810
1811SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1812 const SDOperand *Ops, unsigned NumOps) {
1813 if (VTList.NumVTs == 1)
1814 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001815
Chris Lattner5f056bf2005-07-10 01:55:33 +00001816 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001817 // FIXME: figure out how to safely handle things like
1818 // int foo(int x) { return 1 << (x & 255); }
1819 // int bar() { return foo(256); }
1820#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001821 case ISD::SRA_PARTS:
1822 case ISD::SRL_PARTS:
1823 case ISD::SHL_PARTS:
1824 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001825 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001826 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1827 else if (N3.getOpcode() == ISD::AND)
1828 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1829 // If the and is only masking out bits that cannot effect the shift,
1830 // eliminate the and.
1831 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1832 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1833 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1834 }
1835 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001836#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001837 }
Chris Lattner89c34632005-05-14 06:20:26 +00001838
Chris Lattner43247a12005-08-25 19:12:10 +00001839 // Memoize the node unless it returns a flag.
1840 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001841 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001842 FoldingSetNodeID ID;
1843 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001844 void *IP = 0;
1845 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1846 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001847 if (NumOps == 1)
1848 N = new UnarySDNode(Opcode, VTList, Ops[0]);
1849 else if (NumOps == 2)
1850 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
1851 else if (NumOps == 3)
1852 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
1853 else
1854 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001855 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001856 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001857 if (NumOps == 1)
1858 N = new UnarySDNode(Opcode, VTList, Ops[0]);
1859 else if (NumOps == 2)
1860 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
1861 else if (NumOps == 3)
1862 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
1863 else
1864 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00001865 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001866 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001867 return SDOperand(N, 0);
1868}
1869
Chris Lattner70046e92006-08-15 17:46:01 +00001870SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1871 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001872}
1873
Chris Lattner70046e92006-08-15 17:46:01 +00001874SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001875 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1876 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001877 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001878 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001879 }
1880 std::vector<MVT::ValueType> V;
1881 V.push_back(VT1);
1882 V.push_back(VT2);
1883 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001884 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001885}
Chris Lattner70046e92006-08-15 17:46:01 +00001886SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1887 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001888 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001889 E = VTList.end(); I != E; ++I) {
1890 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1891 (*I)[2] == VT3)
1892 return makeVTList(&(*I)[0], 3);
1893 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001894 std::vector<MVT::ValueType> V;
1895 V.push_back(VT1);
1896 V.push_back(VT2);
1897 V.push_back(VT3);
1898 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001899 return makeVTList(&(*VTList.begin())[0], 3);
1900}
1901
1902SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1903 switch (NumVTs) {
1904 case 0: assert(0 && "Cannot have nodes without results!");
1905 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1906 case 2: return getVTList(VTs[0], VTs[1]);
1907 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1908 default: break;
1909 }
1910
1911 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1912 E = VTList.end(); I != E; ++I) {
1913 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1914
1915 bool NoMatch = false;
1916 for (unsigned i = 2; i != NumVTs; ++i)
1917 if (VTs[i] != (*I)[i]) {
1918 NoMatch = true;
1919 break;
1920 }
1921 if (!NoMatch)
1922 return makeVTList(&*I->begin(), NumVTs);
1923 }
1924
1925 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1926 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001927}
1928
1929
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001930/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1931/// specified operands. If the resultant node already exists in the DAG,
1932/// this does not modify the specified node, instead it returns the node that
1933/// already exists. If the resultant node does not exist in the DAG, the
1934/// input node is returned. As a degenerate case, if you specify the same
1935/// input operands as the node already has, the input node is returned.
1936SDOperand SelectionDAG::
1937UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1938 SDNode *N = InN.Val;
1939 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1940
1941 // Check to see if there is no change.
1942 if (Op == N->getOperand(0)) return InN;
1943
1944 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001945 void *InsertPos = 0;
1946 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1947 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001948
1949 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001950 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001951 RemoveNodeFromCSEMaps(N);
1952
1953 // Now we update the operands.
1954 N->OperandList[0].Val->removeUser(N);
1955 Op.Val->addUser(N);
1956 N->OperandList[0] = Op;
1957
1958 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001959 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001960 return InN;
1961}
1962
1963SDOperand SelectionDAG::
1964UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1965 SDNode *N = InN.Val;
1966 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1967
1968 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001969 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1970 return InN; // No operands changed, just return the input node.
1971
1972 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001973 void *InsertPos = 0;
1974 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1975 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001976
1977 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001978 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001979 RemoveNodeFromCSEMaps(N);
1980
1981 // Now we update the operands.
1982 if (N->OperandList[0] != Op1) {
1983 N->OperandList[0].Val->removeUser(N);
1984 Op1.Val->addUser(N);
1985 N->OperandList[0] = Op1;
1986 }
1987 if (N->OperandList[1] != Op2) {
1988 N->OperandList[1].Val->removeUser(N);
1989 Op2.Val->addUser(N);
1990 N->OperandList[1] = Op2;
1991 }
1992
1993 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001994 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001995 return InN;
1996}
1997
1998SDOperand SelectionDAG::
1999UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002000 SDOperand Ops[] = { Op1, Op2, Op3 };
2001 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002002}
2003
2004SDOperand SelectionDAG::
2005UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2006 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002007 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2008 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002009}
2010
2011SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002012UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2013 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002014 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2015 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002016}
2017
2018
2019SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002020UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002021 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002022 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002023 "Update with wrong number of operands");
2024
2025 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002026 bool AnyChange = false;
2027 for (unsigned i = 0; i != NumOps; ++i) {
2028 if (Ops[i] != N->getOperand(i)) {
2029 AnyChange = true;
2030 break;
2031 }
2032 }
2033
2034 // No operands changed, just return the input node.
2035 if (!AnyChange) return InN;
2036
2037 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002038 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002039 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002040 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002041
2042 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002043 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002044 RemoveNodeFromCSEMaps(N);
2045
2046 // Now we update the operands.
2047 for (unsigned i = 0; i != NumOps; ++i) {
2048 if (N->OperandList[i] != Ops[i]) {
2049 N->OperandList[i].Val->removeUser(N);
2050 Ops[i].Val->addUser(N);
2051 N->OperandList[i] = Ops[i];
2052 }
2053 }
2054
2055 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002056 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002057 return InN;
2058}
2059
2060
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002061/// MorphNodeTo - This frees the operands of the current node, resets the
2062/// opcode, types, and operands to the specified value. This should only be
2063/// used by the SelectionDAG class.
2064void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2065 const SDOperand *Ops, unsigned NumOps) {
2066 NodeType = Opc;
2067 ValueList = L.VTs;
2068 NumValues = L.NumVTs;
2069
2070 // Clear the operands list, updating used nodes to remove this from their
2071 // use list.
2072 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2073 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002074
2075 // If NumOps is larger than the # of operands we currently have, reallocate
2076 // the operand list.
2077 if (NumOps > NumOperands) {
2078 if (OperandsNeedDelete)
2079 delete [] OperandList;
2080 OperandList = new SDOperand[NumOps];
2081 OperandsNeedDelete = true;
2082 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002083
2084 // Assign the new operands.
2085 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002086
2087 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2088 OperandList[i] = Ops[i];
2089 SDNode *N = OperandList[i].Val;
2090 N->Uses.push_back(this);
2091 }
2092}
Chris Lattner149c58c2005-08-16 18:17:10 +00002093
2094/// SelectNodeTo - These are used for target selectors to *mutate* the
2095/// specified node to have the specified return type, Target opcode, and
2096/// operands. Note that target opcodes are stored as
2097/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002098///
2099/// Note that SelectNodeTo returns the resultant node. If there is already a
2100/// node of the specified opcode and operands, it returns that node instead of
2101/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002102SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2103 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002104 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002105 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002106 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002107 void *IP = 0;
2108 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002109 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002110
Chris Lattner7651fa42005-08-24 23:00:29 +00002111 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002112
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002113 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002114
Chris Lattner4a283e92006-08-11 18:38:11 +00002115 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002116 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002117}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118
Evan Cheng95514ba2006-08-26 08:00:10 +00002119SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2120 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002121 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002122 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002123 SDOperand Ops[] = { Op1 };
2124
Jim Laskey583bd472006-10-27 23:46:08 +00002125 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002126 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002127 void *IP = 0;
2128 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002129 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002130
Chris Lattner149c58c2005-08-16 18:17:10 +00002131 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002132 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002133 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002134 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002135}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002136
Evan Cheng95514ba2006-08-26 08:00:10 +00002137SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2138 MVT::ValueType VT, SDOperand Op1,
2139 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002140 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002141 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002142 SDOperand Ops[] = { Op1, Op2 };
2143
Jim Laskey583bd472006-10-27 23:46:08 +00002144 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002145 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002146 void *IP = 0;
2147 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002148 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002149
Chris Lattner149c58c2005-08-16 18:17:10 +00002150 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002151
Chris Lattner63e3f142007-02-04 07:28:00 +00002152 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002153
Chris Lattnera5682852006-08-07 23:03:03 +00002154 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002155 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002156}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002157
Evan Cheng95514ba2006-08-26 08:00:10 +00002158SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2159 MVT::ValueType VT, SDOperand Op1,
2160 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002161 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002162 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002163 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002164 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002165 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002166 void *IP = 0;
2167 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002168 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002169
Chris Lattner149c58c2005-08-16 18:17:10 +00002170 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002171
Chris Lattner63e3f142007-02-04 07:28:00 +00002172 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002173
Chris Lattnera5682852006-08-07 23:03:03 +00002174 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002175 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002176}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002177
Evan Cheng95514ba2006-08-26 08:00:10 +00002178SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002179 MVT::ValueType VT, const SDOperand *Ops,
2180 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002181 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002182 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002183 FoldingSetNodeID ID;
2184 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002185 void *IP = 0;
2186 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002187 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002188
Chris Lattner6b09a292005-08-21 18:49:33 +00002189 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002190 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002191
Chris Lattnera5682852006-08-07 23:03:03 +00002192 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002193 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002194}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002195
Evan Cheng95514ba2006-08-26 08:00:10 +00002196SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2197 MVT::ValueType VT1, MVT::ValueType VT2,
2198 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002199 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002200 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002201 SDOperand Ops[] = { Op1, Op2 };
2202 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002203 void *IP = 0;
2204 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002205 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002206
Chris Lattner0fb094f2005-11-19 01:44:53 +00002207 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002208 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002209 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002210 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002211}
2212
Evan Cheng95514ba2006-08-26 08:00:10 +00002213SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2214 MVT::ValueType VT1, MVT::ValueType VT2,
2215 SDOperand Op1, SDOperand Op2,
2216 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002217 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002218 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00002219 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002220 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002221 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002222 void *IP = 0;
2223 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002224 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002225
Chris Lattner0fb094f2005-11-19 01:44:53 +00002226 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002227
Chris Lattner63e3f142007-02-04 07:28:00 +00002228 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002229 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002230 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002231}
2232
Chris Lattner0fb094f2005-11-19 01:44:53 +00002233
Evan Cheng6ae46c42006-02-09 07:15:23 +00002234/// getTargetNode - These are used for target selectors to create a new node
2235/// with specified return type(s), target opcode, and operands.
2236///
2237/// Note that getTargetNode returns the resultant node. If there is already a
2238/// node of the specified opcode and operands, it returns that node instead of
2239/// the current one.
2240SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2241 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2242}
2243SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2244 SDOperand Op1) {
2245 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2246}
2247SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2248 SDOperand Op1, SDOperand Op2) {
2249 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2250}
2251SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002252 SDOperand Op1, SDOperand Op2,
2253 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002254 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2255}
2256SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002257 const SDOperand *Ops, unsigned NumOps) {
2258 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002259}
2260SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2261 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002262 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002263 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002264}
2265SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002266 MVT::ValueType VT2, SDOperand Op1,
2267 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002268 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002269 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002270 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002271}
2272SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002273 MVT::ValueType VT2, SDOperand Op1,
2274 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002275 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002276 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002277 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002278}
Evan Cheng694481e2006-08-27 08:08:54 +00002279SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2280 MVT::ValueType VT2,
2281 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002282 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002283 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002284}
2285SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2286 MVT::ValueType VT2, MVT::ValueType VT3,
2287 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002288 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002289 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002290 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002291}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002292SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2293 MVT::ValueType VT2, MVT::ValueType VT3,
2294 SDOperand Op1, SDOperand Op2,
2295 SDOperand Op3) {
2296 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2297 SDOperand Ops[] = { Op1, Op2, Op3 };
2298 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
2299}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002300SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002301 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002302 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002303 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2304 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002305}
2306
Evan Cheng99157a02006-08-07 22:13:29 +00002307/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002308/// This can cause recursive merging of nodes in the DAG.
2309///
Chris Lattner8b52f212005-08-26 18:36:28 +00002310/// This version assumes From/To have a single result value.
2311///
Chris Lattner1e111c72005-09-07 05:37:01 +00002312void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2313 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002314 SDNode *From = FromN.Val, *To = ToN.Val;
2315 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2316 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002317 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002318
Chris Lattner8b8749f2005-08-17 19:00:20 +00002319 while (!From->use_empty()) {
2320 // Process users until they are all gone.
2321 SDNode *U = *From->use_begin();
2322
2323 // This node is about to morph, remove its old self from the CSE maps.
2324 RemoveNodeFromCSEMaps(U);
2325
Chris Lattner65113b22005-11-08 22:07:03 +00002326 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2327 I != E; ++I)
2328 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002329 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002330 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002331 To->addUser(U);
2332 }
2333
2334 // Now that we have modified U, add it back to the CSE maps. If it already
2335 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002336 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2337 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002338 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002339 if (Deleted) Deleted->push_back(U);
2340 DeleteNodeNotInCSEMaps(U);
2341 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002342 }
2343}
2344
2345/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2346/// This can cause recursive merging of nodes in the DAG.
2347///
2348/// This version assumes From/To have matching types and numbers of result
2349/// values.
2350///
Chris Lattner1e111c72005-09-07 05:37:01 +00002351void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2352 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002353 assert(From != To && "Cannot replace uses of with self");
2354 assert(From->getNumValues() == To->getNumValues() &&
2355 "Cannot use this version of ReplaceAllUsesWith!");
2356 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002357 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002358 return;
2359 }
2360
2361 while (!From->use_empty()) {
2362 // Process users until they are all gone.
2363 SDNode *U = *From->use_begin();
2364
2365 // This node is about to morph, remove its old self from the CSE maps.
2366 RemoveNodeFromCSEMaps(U);
2367
Chris Lattner65113b22005-11-08 22:07:03 +00002368 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2369 I != E; ++I)
2370 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002371 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002372 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002373 To->addUser(U);
2374 }
2375
2376 // Now that we have modified U, add it back to the CSE maps. If it already
2377 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002378 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2379 ReplaceAllUsesWith(U, Existing, Deleted);
2380 // U is now dead.
2381 if (Deleted) Deleted->push_back(U);
2382 DeleteNodeNotInCSEMaps(U);
2383 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002384 }
2385}
2386
Chris Lattner8b52f212005-08-26 18:36:28 +00002387/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2388/// This can cause recursive merging of nodes in the DAG.
2389///
2390/// This version can replace From with any result values. To must match the
2391/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002392void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002393 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002394 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002395 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002396 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002397 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002398 return;
2399 }
2400
2401 while (!From->use_empty()) {
2402 // Process users until they are all gone.
2403 SDNode *U = *From->use_begin();
2404
2405 // This node is about to morph, remove its old self from the CSE maps.
2406 RemoveNodeFromCSEMaps(U);
2407
Chris Lattner65113b22005-11-08 22:07:03 +00002408 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2409 I != E; ++I)
2410 if (I->Val == From) {
2411 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002412 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002413 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002414 ToOp.Val->addUser(U);
2415 }
2416
2417 // Now that we have modified U, add it back to the CSE maps. If it already
2418 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002419 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2420 ReplaceAllUsesWith(U, Existing, Deleted);
2421 // U is now dead.
2422 if (Deleted) Deleted->push_back(U);
2423 DeleteNodeNotInCSEMaps(U);
2424 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002425 }
2426}
2427
Chris Lattner012f2412006-02-17 21:58:01 +00002428/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2429/// uses of other values produced by From.Val alone. The Deleted vector is
2430/// handled the same was as for ReplaceAllUsesWith.
2431void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2432 std::vector<SDNode*> &Deleted) {
2433 assert(From != To && "Cannot replace a value with itself");
2434 // Handle the simple, trivial, case efficiently.
2435 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2436 ReplaceAllUsesWith(From, To, &Deleted);
2437 return;
2438 }
2439
Chris Lattnercf5640b2007-02-04 00:14:31 +00002440 // Get all of the users of From.Val. We want these in a nice,
2441 // deterministically ordered and uniqued set, so we use a SmallSetVector.
2442 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00002443
2444 while (!Users.empty()) {
2445 // We know that this user uses some value of From. If it is the right
2446 // value, update it.
2447 SDNode *User = Users.back();
2448 Users.pop_back();
2449
2450 for (SDOperand *Op = User->OperandList,
2451 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2452 if (*Op == From) {
2453 // Okay, we know this user needs to be updated. Remove its old self
2454 // from the CSE maps.
2455 RemoveNodeFromCSEMaps(User);
2456
2457 // Update all operands that match "From".
2458 for (; Op != E; ++Op) {
2459 if (*Op == From) {
2460 From.Val->removeUser(User);
2461 *Op = To;
2462 To.Val->addUser(User);
2463 }
2464 }
2465
2466 // Now that we have modified User, add it back to the CSE maps. If it
2467 // already exists there, recursively merge the results together.
2468 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2469 unsigned NumDeleted = Deleted.size();
2470 ReplaceAllUsesWith(User, Existing, &Deleted);
2471
2472 // User is now dead.
2473 Deleted.push_back(User);
2474 DeleteNodeNotInCSEMaps(User);
2475
2476 // We have to be careful here, because ReplaceAllUsesWith could have
2477 // deleted a user of From, which means there may be dangling pointers
2478 // in the "Users" setvector. Scan over the deleted node pointers and
2479 // remove them from the setvector.
2480 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2481 Users.remove(Deleted[i]);
2482 }
2483 break; // Exit the operand scanning loop.
2484 }
2485 }
2486 }
2487}
2488
Chris Lattner7b2880c2005-08-24 22:44:39 +00002489
Evan Chenge6f35d82006-08-01 08:20:41 +00002490/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2491/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002492unsigned SelectionDAG::AssignNodeIds() {
2493 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002494 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2495 SDNode *N = I;
2496 N->setNodeId(Id++);
2497 }
2498 return Id;
2499}
2500
Evan Chenge6f35d82006-08-01 08:20:41 +00002501/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002502/// based on their topological order. It returns the maximum id and a vector
2503/// of the SDNodes* in assigned order by reference.
2504unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002505 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002506 std::vector<unsigned> InDegree(DAGSize);
2507 std::vector<SDNode*> Sources;
2508
2509 // Use a two pass approach to avoid using a std::map which is slow.
2510 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002511 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2512 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002513 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002514 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002515 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002516 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002517 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002518 }
2519
Evan Cheng99157a02006-08-07 22:13:29 +00002520 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002521 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002522 SDNode *N = Sources.back();
2523 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002524 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002525 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2526 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002527 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002528 if (Degree == 0)
2529 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002530 }
2531 }
2532
Evan Chengc384d6c2006-08-02 22:00:34 +00002533 // Second pass, assign the actual topological order as node ids.
2534 Id = 0;
2535 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2536 TI != TE; ++TI)
2537 (*TI)->setNodeId(Id++);
2538
2539 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002540}
2541
2542
Evan Cheng091cba12006-07-27 06:39:06 +00002543
Jim Laskey58b968b2005-08-17 20:08:02 +00002544//===----------------------------------------------------------------------===//
2545// SDNode Class
2546//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002547
Chris Lattner917d2c92006-07-19 00:00:37 +00002548// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00002549void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00002550void UnarySDNode::ANCHOR() {}
2551void BinarySDNode::ANCHOR() {}
2552void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00002553void HandleSDNode::ANCHOR() {}
2554void StringSDNode::ANCHOR() {}
2555void ConstantSDNode::ANCHOR() {}
2556void ConstantFPSDNode::ANCHOR() {}
2557void GlobalAddressSDNode::ANCHOR() {}
2558void FrameIndexSDNode::ANCHOR() {}
2559void JumpTableSDNode::ANCHOR() {}
2560void ConstantPoolSDNode::ANCHOR() {}
2561void BasicBlockSDNode::ANCHOR() {}
2562void SrcValueSDNode::ANCHOR() {}
2563void RegisterSDNode::ANCHOR() {}
2564void ExternalSymbolSDNode::ANCHOR() {}
2565void CondCodeSDNode::ANCHOR() {}
2566void VTSDNode::ANCHOR() {}
2567void LoadSDNode::ANCHOR() {}
2568void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00002569
Chris Lattner48b85922007-02-04 02:41:42 +00002570HandleSDNode::~HandleSDNode() {
2571 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002572 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00002573}
2574
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00002575GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
2576 MVT::ValueType VT, int o)
2577 : SDNode(isa<GlobalVariable>(GA) &&
2578 dyn_cast<GlobalVariable>(GA)->isThreadLocal() ?
2579 // Thread Local
2580 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
2581 // Non Thread Local
2582 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
2583 getSDVTList(VT)), Offset(o) {
2584 TheGlobal = const_cast<GlobalValue*>(GA);
2585}
Chris Lattner48b85922007-02-04 02:41:42 +00002586
Jim Laskey583bd472006-10-27 23:46:08 +00002587/// Profile - Gather unique data for the node.
2588///
2589void SDNode::Profile(FoldingSetNodeID &ID) {
2590 AddNodeIDNode(ID, this);
2591}
2592
Chris Lattnera3255112005-11-08 23:30:28 +00002593/// getValueTypeList - Return a pointer to the specified value type.
2594///
2595MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2596 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2597 VTs[VT] = VT;
2598 return &VTs[VT];
2599}
Chris Lattnera5682852006-08-07 23:03:03 +00002600
Chris Lattner5c884562005-01-12 18:37:47 +00002601/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2602/// indicated value. This method ignores uses of other values defined by this
2603/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002604bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002605 assert(Value < getNumValues() && "Bad value!");
2606
2607 // If there is only one value, this is easy.
2608 if (getNumValues() == 1)
2609 return use_size() == NUses;
2610 if (Uses.size() < NUses) return false;
2611
Evan Cheng4ee62112006-02-05 06:29:23 +00002612 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002613
Chris Lattnerd48c5e82007-02-04 00:24:41 +00002614 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00002615
Chris Lattnerf83482d2006-08-16 20:59:32 +00002616 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002617 SDNode *User = *UI;
2618 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00002619 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00002620 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2621 if (User->getOperand(i) == TheValue) {
2622 if (NUses == 0)
2623 return false; // too many uses
2624 --NUses;
2625 }
2626 }
2627
2628 // Found exactly the right number of uses?
2629 return NUses == 0;
2630}
2631
2632
Evan Chenge6e97e62006-11-03 07:31:32 +00002633/// isOnlyUse - Return true if this node is the only use of N.
2634///
Evan Cheng4ee62112006-02-05 06:29:23 +00002635bool SDNode::isOnlyUse(SDNode *N) const {
2636 bool Seen = false;
2637 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2638 SDNode *User = *I;
2639 if (User == this)
2640 Seen = true;
2641 else
2642 return false;
2643 }
2644
2645 return Seen;
2646}
2647
Evan Chenge6e97e62006-11-03 07:31:32 +00002648/// isOperand - Return true if this node is an operand of N.
2649///
Evan Chengbfa284f2006-03-03 06:42:32 +00002650bool SDOperand::isOperand(SDNode *N) const {
2651 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2652 if (*this == N->getOperand(i))
2653 return true;
2654 return false;
2655}
2656
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002657bool SDNode::isOperand(SDNode *N) const {
2658 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2659 if (this == N->OperandList[i].Val)
2660 return true;
2661 return false;
2662}
Evan Cheng4ee62112006-02-05 06:29:23 +00002663
Evan Chengc5fc57d2006-11-03 03:05:24 +00002664static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00002665 SmallPtrSet<SDNode *, 32> &Visited) {
2666 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00002667 return;
2668
2669 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
2670 SDNode *Op = N->getOperand(i).Val;
2671 if (Op == P) {
2672 found = true;
2673 return;
2674 }
2675 findPredecessor(Op, P, found, Visited);
2676 }
2677}
2678
Evan Chenge6e97e62006-11-03 07:31:32 +00002679/// isPredecessor - Return true if this node is a predecessor of N. This node
2680/// is either an operand of N or it can be reached by recursively traversing
2681/// up the operands.
2682/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00002683bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00002684 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00002685 bool found = false;
2686 findPredecessor(N, this, found, Visited);
2687 return found;
2688}
2689
Evan Chengc5484282006-10-04 00:56:09 +00002690uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2691 assert(Num < NumOperands && "Invalid child # of SDNode!");
2692 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2693}
2694
Reid Spencer577cc322007-04-01 07:32:19 +00002695std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002696 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002697 default:
2698 if (getOpcode() < ISD::BUILTIN_OP_END)
2699 return "<<Unknown DAG Node>>";
2700 else {
Evan Cheng72261582005-12-20 06:22:03 +00002701 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002702 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002703 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2704 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002705
Evan Cheng72261582005-12-20 06:22:03 +00002706 TargetLowering &TLI = G->getTargetLoweringInfo();
2707 const char *Name =
2708 TLI.getTargetNodeName(getOpcode());
2709 if (Name) return Name;
2710 }
2711
2712 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002713 }
2714
Andrew Lenharth95762122005-03-31 21:24:06 +00002715 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002716 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002717 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002718 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002719 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002720 case ISD::AssertSext: return "AssertSext";
2721 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002722
2723 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002724 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002725 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002726 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002727
2728 case ISD::Constant: return "Constant";
2729 case ISD::ConstantFP: return "ConstantFP";
2730 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002731 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002732 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002733 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002734 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00002735 case ISD::RETURNADDR: return "RETURNADDR";
2736 case ISD::FRAMEADDR: return "FRAMEADDR";
Jim Laskeyb180aa12007-02-21 22:53:45 +00002737 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
2738 case ISD::EHSELECTION: return "EHSELECTION";
Chris Lattner5839bf22005-08-26 17:15:30 +00002739 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002740 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002741 case ISD::INTRINSIC_WO_CHAIN: {
2742 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2743 return Intrinsic::getName((Intrinsic::ID)IID);
2744 }
2745 case ISD::INTRINSIC_VOID:
2746 case ISD::INTRINSIC_W_CHAIN: {
2747 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002748 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002749 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002750
Chris Lattnerb2827b02006-03-19 00:52:58 +00002751 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002752 case ISD::TargetConstant: return "TargetConstant";
2753 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002754 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002755 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002756 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002757 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002758 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002759 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002760
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002761 case ISD::CopyToReg: return "CopyToReg";
2762 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002763 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002764 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002765 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00002766 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002767 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002768 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002769 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002770
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002771 // Unary operators
2772 case ISD::FABS: return "fabs";
2773 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002774 case ISD::FSQRT: return "fsqrt";
2775 case ISD::FSIN: return "fsin";
2776 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002777 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002778
2779 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002780 case ISD::ADD: return "add";
2781 case ISD::SUB: return "sub";
2782 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002783 case ISD::MULHU: return "mulhu";
2784 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002785 case ISD::SDIV: return "sdiv";
2786 case ISD::UDIV: return "udiv";
2787 case ISD::SREM: return "srem";
2788 case ISD::UREM: return "urem";
2789 case ISD::AND: return "and";
2790 case ISD::OR: return "or";
2791 case ISD::XOR: return "xor";
2792 case ISD::SHL: return "shl";
2793 case ISD::SRA: return "sra";
2794 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002795 case ISD::ROTL: return "rotl";
2796 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002797 case ISD::FADD: return "fadd";
2798 case ISD::FSUB: return "fsub";
2799 case ISD::FMUL: return "fmul";
2800 case ISD::FDIV: return "fdiv";
2801 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002802 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002803 case ISD::VADD: return "vadd";
2804 case ISD::VSUB: return "vsub";
2805 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002806 case ISD::VSDIV: return "vsdiv";
2807 case ISD::VUDIV: return "vudiv";
2808 case ISD::VAND: return "vand";
2809 case ISD::VOR: return "vor";
2810 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002811
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002812 case ISD::SETCC: return "setcc";
2813 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002814 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002815 case ISD::VSELECT: return "vselect";
2816 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2817 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2818 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002819 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002820 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2821 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2822 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2823 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2824 case ISD::VBIT_CONVERT: return "vbit_convert";
Chris Lattnerb6541762007-03-04 20:40:38 +00002825 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002826 case ISD::ADDC: return "addc";
2827 case ISD::ADDE: return "adde";
2828 case ISD::SUBC: return "subc";
2829 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002830 case ISD::SHL_PARTS: return "shl_parts";
2831 case ISD::SRA_PARTS: return "sra_parts";
2832 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002833
Chris Lattner7f644642005-04-28 21:44:03 +00002834 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002835 case ISD::SIGN_EXTEND: return "sign_extend";
2836 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002837 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002838 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002839 case ISD::TRUNCATE: return "truncate";
2840 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002841 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002842 case ISD::FP_EXTEND: return "fp_extend";
2843
2844 case ISD::SINT_TO_FP: return "sint_to_fp";
2845 case ISD::UINT_TO_FP: return "uint_to_fp";
2846 case ISD::FP_TO_SINT: return "fp_to_sint";
2847 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002848 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002849
2850 // Control flow instructions
2851 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002852 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00002853 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002854 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002855 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002856 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002857 case ISD::CALLSEQ_START: return "callseq_start";
2858 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002859
2860 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002861 case ISD::LOAD: return "load";
2862 case ISD::STORE: return "store";
2863 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002864 case ISD::VAARG: return "vaarg";
2865 case ISD::VACOPY: return "vacopy";
2866 case ISD::VAEND: return "vaend";
2867 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002868 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002869 case ISD::EXTRACT_ELEMENT: return "extract_element";
2870 case ISD::BUILD_PAIR: return "build_pair";
2871 case ISD::STACKSAVE: return "stacksave";
2872 case ISD::STACKRESTORE: return "stackrestore";
2873
2874 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002875 case ISD::MEMSET: return "memset";
2876 case ISD::MEMCPY: return "memcpy";
2877 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002878
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002879 // Bit manipulation
2880 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002881 case ISD::CTPOP: return "ctpop";
2882 case ISD::CTTZ: return "cttz";
2883 case ISD::CTLZ: return "ctlz";
2884
Chris Lattner36ce6912005-11-29 06:21:05 +00002885 // Debug info
2886 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002887 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00002888
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002889 case ISD::CONDCODE:
2890 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002891 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002892 case ISD::SETOEQ: return "setoeq";
2893 case ISD::SETOGT: return "setogt";
2894 case ISD::SETOGE: return "setoge";
2895 case ISD::SETOLT: return "setolt";
2896 case ISD::SETOLE: return "setole";
2897 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002898
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002899 case ISD::SETO: return "seto";
2900 case ISD::SETUO: return "setuo";
2901 case ISD::SETUEQ: return "setue";
2902 case ISD::SETUGT: return "setugt";
2903 case ISD::SETUGE: return "setuge";
2904 case ISD::SETULT: return "setult";
2905 case ISD::SETULE: return "setule";
2906 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002907
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002908 case ISD::SETEQ: return "seteq";
2909 case ISD::SETGT: return "setgt";
2910 case ISD::SETGE: return "setge";
2911 case ISD::SETLT: return "setlt";
2912 case ISD::SETLE: return "setle";
2913 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002914 }
2915 }
2916}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002917
Evan Cheng144d8f02006-11-09 17:55:04 +00002918const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002919 switch (AM) {
2920 default:
2921 return "";
2922 case ISD::PRE_INC:
2923 return "<pre-inc>";
2924 case ISD::PRE_DEC:
2925 return "<pre-dec>";
2926 case ISD::POST_INC:
2927 return "<post-inc>";
2928 case ISD::POST_DEC:
2929 return "<post-dec>";
2930 }
2931}
2932
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002933void SDNode::dump() const { dump(0); }
2934void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00002935 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002936
2937 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00002938 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002939 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00002940 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00002941 else
Bill Wendling832171c2006-12-07 20:04:42 +00002942 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002943 }
Bill Wendling832171c2006-12-07 20:04:42 +00002944 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002945
Bill Wendling832171c2006-12-07 20:04:42 +00002946 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002947 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00002948 if (i) cerr << ", ";
2949 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002950 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00002951 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002952 }
2953
2954 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002955 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002956 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002957 cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002958 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002959 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002960 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00002961 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00002962 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002963 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00002964 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00002965 else
Bill Wendling832171c2006-12-07 20:04:42 +00002966 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002967 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002968 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00002969 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002970 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002971 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002972 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002973 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00002974 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00002975 else
Bill Wendling832171c2006-12-07 20:04:42 +00002976 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002977 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00002978 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00002979 else
Bill Wendling832171c2006-12-07 20:04:42 +00002980 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002981 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002982 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002983 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2984 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00002985 cerr << LBB->getName() << " ";
2986 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002987 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002988 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00002989 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00002990 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00002991 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00002992 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002993 } else if (const ExternalSymbolSDNode *ES =
2994 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00002995 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002996 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2997 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00002998 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002999 else
Bill Wendling832171c2006-12-07 20:04:42 +00003000 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003001 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003002 cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003003 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3004 bool doExt = true;
3005 switch (LD->getExtensionType()) {
3006 default: doExt = false; break;
3007 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003008 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003009 break;
3010 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003011 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003012 break;
3013 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003014 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003015 break;
3016 }
3017 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003018 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003019
Evan Cheng144d8f02006-11-09 17:55:04 +00003020 const char *AM = getIndexedModeName(LD->getAddressingMode());
Evan Cheng2caccca2006-10-17 21:14:32 +00003021 if (AM != "")
Bill Wendling832171c2006-12-07 20:04:42 +00003022 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003023 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3024 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003025 cerr << " <trunc "
3026 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003027
Evan Cheng144d8f02006-11-09 17:55:04 +00003028 const char *AM = getIndexedModeName(ST->getAddressingMode());
Evan Cheng2caccca2006-10-17 21:14:32 +00003029 if (AM != "")
Bill Wendling832171c2006-12-07 20:04:42 +00003030 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003031 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003032}
3033
Chris Lattnerde202b32005-11-09 23:47:37 +00003034static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003035 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3036 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003037 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003038 else
Bill Wendling832171c2006-12-07 20:04:42 +00003039 cerr << "\n" << std::string(indent+2, ' ')
3040 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003041
Chris Lattnerea946cd2005-01-09 20:38:33 +00003042
Bill Wendling832171c2006-12-07 20:04:42 +00003043 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003044 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003045}
3046
Chris Lattnerc3aae252005-01-07 07:46:32 +00003047void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003048 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003049 std::vector<const SDNode*> Nodes;
3050 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3051 I != E; ++I)
3052 Nodes.push_back(I);
3053
Chris Lattner49d24712005-01-09 20:26:36 +00003054 std::sort(Nodes.begin(), Nodes.end());
3055
3056 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003057 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003058 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003059 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003060
Jim Laskey26f7fa72006-10-17 19:33:52 +00003061 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003062
Bill Wendling832171c2006-12-07 20:04:42 +00003063 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003064}
3065
Evan Chengd6594ae2006-09-12 21:00:35 +00003066const Type *ConstantPoolSDNode::getType() const {
3067 if (isMachineConstantPoolEntry())
3068 return Val.MachineCPVal->getType();
3069 return Val.ConstVal->getType();
3070}