blob: 6b1a1e15daa2b5ae3960e9e40b42b21759972aa6 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000021#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000022#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000024#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000026#include "llvm/ADT/SetVector.h"
Chris Lattner190a4182006-08-04 17:45:20 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000028#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000029#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000030#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000031#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000032#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner0b3e5252006-08-15 19:11:05 +000035/// makeVTList - Return an instance of the SDVTList struct initialized with the
36/// specified members.
37static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
38 SDVTList Res = {VTs, NumVTs};
39 return Res;
40}
41
Chris Lattner5cdcc582005-01-09 20:52:51 +000042// isInvertibleForFree - Return true if there is no cost to emitting the logical
43// inverse of this node.
44static bool isInvertibleForFree(SDOperand N) {
45 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000046 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000047 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000048 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000049}
50
Jim Laskey58b968b2005-08-17 20:08:02 +000051//===----------------------------------------------------------------------===//
52// ConstantFPSDNode Class
53//===----------------------------------------------------------------------===//
54
55/// isExactlyValue - We don't rely on operator== working on double values, as
56/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
57/// As such, this method can be used to do an exact bit-for-bit comparison of
58/// two floating point values.
59bool ConstantFPSDNode::isExactlyValue(double V) const {
60 return DoubleToBits(V) == DoubleToBits(Value);
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000064// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000066
Evan Chenga8df1662006-03-27 06:58:47 +000067/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000068/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000069bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000070 // Look through a bit convert.
71 if (N->getOpcode() == ISD::BIT_CONVERT)
72 N = N->getOperand(0).Val;
73
Evan Chenga8df1662006-03-27 06:58:47 +000074 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000075
76 unsigned i = 0, e = N->getNumOperands();
77
78 // Skip over all of the undef values.
79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
80 ++i;
81
82 // Do not accept an all-undef vector.
83 if (i == e) return false;
84
85 // Do not accept build_vectors that aren't all constants or which have non-~0
86 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000087 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000088 if (isa<ConstantSDNode>(NotZero)) {
89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
90 return false;
91 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000092 MVT::ValueType VT = NotZero.getValueType();
93 if (VT== MVT::f64) {
94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
95 (uint64_t)-1)
96 return false;
97 } else {
98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
99 (uint32_t)-1)
100 return false;
101 }
Evan Chenga8df1662006-03-27 06:58:47 +0000102 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000103 return false;
104
105 // Okay, we have at least one ~0 value, check to see if the rest match or are
106 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000107 for (++i; i != e; ++i)
108 if (N->getOperand(i) != NotZero &&
109 N->getOperand(i).getOpcode() != ISD::UNDEF)
110 return false;
111 return true;
112}
113
114
Evan Cheng4a147842006-03-26 09:50:58 +0000115/// isBuildVectorAllZeros - Return true if the specified node is a
116/// BUILD_VECTOR where all of the elements are 0 or undef.
117bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000118 // Look through a bit convert.
119 if (N->getOpcode() == ISD::BIT_CONVERT)
120 N = N->getOperand(0).Val;
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123
124 unsigned i = 0, e = N->getNumOperands();
125
126 // Skip over all of the undef values.
127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
128 ++i;
129
130 // Do not accept an all-undef vector.
131 if (i == e) return false;
132
133 // Do not accept build_vectors that aren't all constants or which have non-~0
134 // elements.
135 SDOperand Zero = N->getOperand(i);
136 if (isa<ConstantSDNode>(Zero)) {
137 if (!cast<ConstantSDNode>(Zero)->isNullValue())
138 return false;
139 } else if (isa<ConstantFPSDNode>(Zero)) {
140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
141 return false;
142 } else
143 return false;
144
145 // Okay, we have at least one ~0 value, check to see if the rest match or are
146 // undefs.
147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != Zero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
150 return false;
151 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000152}
153
Chris Lattnerc3aae252005-01-07 07:46:32 +0000154/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
155/// when given the operation for (X op Y).
156ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
157 // To perform this operation, we just need to swap the L and G bits of the
158 // operation.
159 unsigned OldL = (Operation >> 2) & 1;
160 unsigned OldG = (Operation >> 1) & 1;
161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
162 (OldL << 1) | // New G bit
163 (OldG << 2)); // New L bit.
164}
165
166/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
167/// 'op' is a valid SetCC operation.
168ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
169 unsigned Operation = Op;
170 if (isInteger)
171 Operation ^= 7; // Flip L, G, E bits, but not U.
172 else
173 Operation ^= 15; // Flip all of the condition bits.
174 if (Operation > ISD::SETTRUE2)
175 Operation &= ~8; // Don't let N and U bits get set.
176 return ISD::CondCode(Operation);
177}
178
179
180/// isSignedOp - For an integer comparison, return 1 if the comparison is a
181/// signed operation and 2 if the result is an unsigned comparison. Return zero
182/// if the operation does not depend on the sign of the input (setne and seteq).
183static int isSignedOp(ISD::CondCode Opcode) {
184 switch (Opcode) {
185 default: assert(0 && "Illegal integer setcc operation!");
186 case ISD::SETEQ:
187 case ISD::SETNE: return 0;
188 case ISD::SETLT:
189 case ISD::SETLE:
190 case ISD::SETGT:
191 case ISD::SETGE: return 1;
192 case ISD::SETULT:
193 case ISD::SETULE:
194 case ISD::SETUGT:
195 case ISD::SETUGE: return 2;
196 }
197}
198
199/// getSetCCOrOperation - Return the result of a logical OR between different
200/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
201/// returns SETCC_INVALID if it is not possible to represent the resultant
202/// comparison.
203ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
204 bool isInteger) {
205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
206 // Cannot fold a signed integer setcc with an unsigned integer setcc.
207 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211 // If the N and U bits get set then the resultant comparison DOES suddenly
212 // care about orderedness, and is true when ordered.
213 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000214 Op &= ~16; // Clear the U bit if the N bit is set.
215
216 // Canonicalize illegal integer setcc's.
217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
218 Op = ISD::SETNE;
219
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220 return ISD::CondCode(Op);
221}
222
223/// getSetCCAndOperation - Return the result of a logical AND between different
224/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
225/// function returns zero if it is not possible to represent the resultant
226/// comparison.
227ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
228 bool isInteger) {
229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
230 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232
233 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
235
236 // Canonicalize illegal integer setcc's.
237 if (isInteger) {
238 switch (Result) {
239 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000244 }
245 }
246
247 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248}
249
Chris Lattnerb48da392005-01-23 04:39:44 +0000250const TargetMachine &SelectionDAG::getTarget() const {
251 return TLI.getTargetMachine();
252}
253
Jim Laskey58b968b2005-08-17 20:08:02 +0000254//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000255// SDNode Profile Support
256//===----------------------------------------------------------------------===//
257
258/// getNodeIDOpcode - Return the opcode that has been set for this NodeID.
259///
260static unsigned getNodeIDOpcode(FoldingSetNodeID &ID) {
261 return ID.getRawData(0);
262}
Jim Laskeydef69b92006-10-27 23:52:51 +0000263
264/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
265///
Jim Laskey583bd472006-10-27 23:46:08 +0000266static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
267 ID.AddInteger(OpC);
268}
269
270/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
271/// solely with their pointer.
272void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
273 ID.AddPointer(VTList.VTs);
274}
275
Jim Laskeydef69b92006-10-27 23:52:51 +0000276/// AddNodeIDOperand - Add an operands data to the NodeID data.
277///
Jim Laskey583bd472006-10-27 23:46:08 +0000278static void AddNodeIDOperand(FoldingSetNodeID &ID, SDOperand Op) {
279 ID.AddPointer(Op.Val);
280 ID.AddInteger(Op.ResNo);
281}
282
Jim Laskeydef69b92006-10-27 23:52:51 +0000283/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
284///
Jim Laskey583bd472006-10-27 23:46:08 +0000285static void AddNodeIDOperands(FoldingSetNodeID &ID) {
286}
Jim Laskeydef69b92006-10-27 23:52:51 +0000287static void AddNodeIDOperands(FoldingSetNodeID &ID, SDOperand Op) {
Jim Laskey583bd472006-10-27 23:46:08 +0000288 AddNodeIDOperand(ID, Op);
289}
290static void AddNodeIDOperands(FoldingSetNodeID &ID,
291 SDOperand Op1, SDOperand Op2) {
292 AddNodeIDOperand(ID, Op1);
293 AddNodeIDOperand(ID, Op2);
294}
295static void AddNodeIDOperands(FoldingSetNodeID &ID,
296 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
297 AddNodeIDOperand(ID, Op1);
298 AddNodeIDOperand(ID, Op2);
299 AddNodeIDOperand(ID, Op3);
300}
301static void AddNodeIDOperands(FoldingSetNodeID &ID,
302 const SDOperand *Ops, unsigned NumOps) {
303 for (; NumOps; --NumOps, ++Ops)
304 AddNodeIDOperand(ID, *Ops);
305}
306
Jim Laskeydef69b92006-10-27 23:52:51 +0000307/// AddNodeIDOperands - Various routines for adding node info to the NodeID
308/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000309static void AddNodeIDNode(FoldingSetNodeID &ID,
310 unsigned short OpC, SDVTList VTList) {
311 AddNodeIDOpcode(ID, OpC);
312 AddNodeIDValueTypes(ID, VTList);
313 AddNodeIDOperands(ID);
314}
315static void AddNodeIDNode(FoldingSetNodeID &ID,
316 unsigned short OpC, SDVTList VTList,
317 SDOperand Op) {
318 AddNodeIDOpcode(ID, OpC);
319 AddNodeIDValueTypes(ID, VTList);
320 AddNodeIDOperands(ID, Op);
321}
322static void AddNodeIDNode(FoldingSetNodeID &ID,
323 unsigned short OpC, SDVTList VTList,
324 SDOperand Op1, SDOperand Op2) {
325 AddNodeIDOpcode(ID, OpC);
326 AddNodeIDValueTypes(ID, VTList);
327 AddNodeIDOperands(ID, Op1, Op2);
328}
329static void AddNodeIDNode(FoldingSetNodeID &ID,
330 unsigned short OpC, SDVTList VTList,
331 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
332 AddNodeIDOpcode(ID, OpC);
333 AddNodeIDValueTypes(ID, VTList);
334 AddNodeIDOperands(ID, Op1, Op2);
335}
336static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
338 const SDOperand *OpList, unsigned N) {
339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
342}
343
Jim Laskeydef69b92006-10-27 23:52:51 +0000344/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
345/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000346static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
347 AddNodeIDOpcode(ID, N->getOpcode());
348 // Add the return value info.
349 AddNodeIDValueTypes(ID, N->getVTList());
350 // Add the operand info.
351 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
352
353 // Handle SDNode leafs with special info.
354 if (N->getNumOperands() == 0) {
355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
357 case ISD::TargetConstant:
358 case ISD::Constant:
359 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
360 break;
361 case ISD::TargetConstantFP:
362 case ISD::ConstantFP:
363 ID.AddDouble(cast<ConstantFPSDNode>(N)->getValue());
364 break;
365 case ISD::TargetGlobalAddress:
366 case ISD::GlobalAddress:
367 ID.AddPointer(cast<GlobalAddressSDNode>(N)->getGlobal());
368 ID.AddInteger(cast<GlobalAddressSDNode>(N)->getOffset());
369 break;
370 case ISD::BasicBlock:
371 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
372 break;
373 case ISD::Register:
374 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
375 break;
376 case ISD::SRCVALUE:
377 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
378 ID.AddInteger(cast<SrcValueSDNode>(N)->getOffset());
379 break;
380 case ISD::FrameIndex:
381 case ISD::TargetFrameIndex:
382 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
383 break;
384 case ISD::JumpTable:
385 case ISD::TargetJumpTable:
386 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
387 break;
388 case ISD::ConstantPool:
389 case ISD::TargetConstantPool:
390 ID.AddInteger(cast<ConstantPoolSDNode>(N)->getAlignment());
391 ID.AddInteger(cast<ConstantPoolSDNode>(N)->getOffset());
392 if (cast<ConstantPoolSDNode>(N)->isMachineConstantPoolEntry())
393 cast<ConstantPoolSDNode>(N)->getMachineCPVal()->
394 AddSelectionDAGCSEId(ID);
395 else
396 ID.AddPointer(cast<ConstantPoolSDNode>(N)->getConstVal());
397 break;
398 }
399 }
400}
401
402//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000403// SelectionDAG Class
404//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000405
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000406/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000407/// SelectionDAG.
408void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409 // Create a dummy node (which is not added to allnodes), that adds a reference
410 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000411 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000412
Chris Lattner190a4182006-08-04 17:45:20 +0000413 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000414
Chris Lattner190a4182006-08-04 17:45:20 +0000415 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000416 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000417 if (I->use_empty())
418 DeadNodes.push_back(I);
419
420 // Process the worklist, deleting the nodes and adding their uses to the
421 // worklist.
422 while (!DeadNodes.empty()) {
423 SDNode *N = DeadNodes.back();
424 DeadNodes.pop_back();
425
426 // Take the node out of the appropriate CSE map.
427 RemoveNodeFromCSEMaps(N);
428
429 // Next, brutally remove the operand list. This is safe to do, as there are
430 // no cycles in the graph.
431 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
432 SDNode *Operand = I->Val;
433 Operand->removeUser(N);
434
435 // Now that we removed this operand, see if there are no uses of it left.
436 if (Operand->use_empty())
437 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000438 }
Chris Lattner190a4182006-08-04 17:45:20 +0000439 delete[] N->OperandList;
440 N->OperandList = 0;
441 N->NumOperands = 0;
442
443 // Finally, remove N itself.
444 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000445 }
446
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000447 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000448 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000449}
450
Evan Cheng130a6472006-10-12 20:34:05 +0000451void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
452 SmallVector<SDNode*, 16> DeadNodes;
453 DeadNodes.push_back(N);
454
455 // Process the worklist, deleting the nodes and adding their uses to the
456 // worklist.
457 while (!DeadNodes.empty()) {
458 SDNode *N = DeadNodes.back();
459 DeadNodes.pop_back();
460
461 // Take the node out of the appropriate CSE map.
462 RemoveNodeFromCSEMaps(N);
463
464 // Next, brutally remove the operand list. This is safe to do, as there are
465 // no cycles in the graph.
466 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
467 SDNode *Operand = I->Val;
468 Operand->removeUser(N);
469
470 // Now that we removed this operand, see if there are no uses of it left.
471 if (Operand->use_empty())
472 DeadNodes.push_back(Operand);
473 }
474 delete[] N->OperandList;
475 N->OperandList = 0;
476 N->NumOperands = 0;
477
478 // Finally, remove N itself.
479 Deleted.push_back(N);
480 AllNodes.erase(N);
481 }
482}
483
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000484void SelectionDAG::DeleteNode(SDNode *N) {
485 assert(N->use_empty() && "Cannot delete a node that is not dead!");
486
487 // First take this out of the appropriate CSE map.
488 RemoveNodeFromCSEMaps(N);
489
Chris Lattner1e111c72005-09-07 05:37:01 +0000490 // Finally, remove uses due to operands of this node, remove from the
491 // AllNodes list, and delete the node.
492 DeleteNodeNotInCSEMaps(N);
493}
494
495void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
496
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000497 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000498 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000499
500 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000501 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
502 I->Val->removeUser(N);
503 delete[] N->OperandList;
504 N->OperandList = 0;
505 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000506
507 delete N;
508}
509
Chris Lattner149c58c2005-08-16 18:17:10 +0000510/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
511/// correspond to it. This is useful when we're about to delete or repurpose
512/// the node. We don't want future request for structurally identical nodes
513/// to return N anymore.
514void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000515 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000516 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000517 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000518 case ISD::STRING:
519 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
520 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000521 case ISD::CONDCODE:
522 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
523 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000524 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000525 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
526 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000527 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000528 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000529 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000530 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000531 Erased =
532 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000533 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000534 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000535 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000536 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
537 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000538 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000539 // Remove it from the CSE Map.
540 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000541 break;
542 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000543#ifndef NDEBUG
544 // Verify that the node was actually in one of the CSE maps, unless it has a
545 // flag result (which cannot be CSE'd) or is one of the special cases that are
546 // not subject to CSE.
547 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000548 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000549 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000550 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000551 assert(0 && "Node is not in map!");
552 }
553#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000554}
555
Chris Lattner8b8749f2005-08-17 19:00:20 +0000556/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
557/// has been taken out and modified in some way. If the specified node already
558/// exists in the CSE maps, do not modify the maps, but return the existing node
559/// instead. If it doesn't exist, add it and return null.
560///
561SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
562 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000563 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000564 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000565
Chris Lattner9f8cc692005-12-19 22:21:21 +0000566 // Check that remaining values produced are not flags.
567 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
568 if (N->getValueType(i) == MVT::Flag)
569 return 0; // Never CSE anything that produces a flag.
570
Chris Lattnera5682852006-08-07 23:03:03 +0000571 SDNode *New = CSEMap.GetOrInsertNode(N);
572 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000573 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000574}
575
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000576/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
577/// were replaced with those specified. If this node is never memoized,
578/// return null, otherwise return a pointer to the slot it would take. If a
579/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000580SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
581 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000582 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000583 return 0; // Never add these nodes.
584
585 // Check that remaining values produced are not flags.
586 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
587 if (N->getValueType(i) == MVT::Flag)
588 return 0; // Never CSE anything that produces a flag.
589
Jim Laskey583bd472006-10-27 23:46:08 +0000590 FoldingSetNodeID ID;
591 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op);
Chris Lattnera5682852006-08-07 23:03:03 +0000592 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000593}
594
595/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
596/// were replaced with those specified. If this node is never memoized,
597/// return null, otherwise return a pointer to the slot it would take. If a
598/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000599SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
600 SDOperand Op1, SDOperand Op2,
601 void *&InsertPos) {
602 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
603 return 0; // Never add these nodes.
604
605 // Check that remaining values produced are not flags.
606 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
607 if (N->getValueType(i) == MVT::Flag)
608 return 0; // Never CSE anything that produces a flag.
609
Jim Laskey583bd472006-10-27 23:46:08 +0000610 FoldingSetNodeID ID;
611 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +0000612 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
613}
614
615
616/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
617/// were replaced with those specified. If this node is never memoized,
618/// return null, otherwise return a pointer to the slot it would take. If a
619/// node already exists with these operands, the slot will be non-null.
620SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000621 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000622 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000623 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000624 return 0; // Never add these nodes.
625
626 // Check that remaining values produced are not flags.
627 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
628 if (N->getValueType(i) == MVT::Flag)
629 return 0; // Never CSE anything that produces a flag.
630
Jim Laskey583bd472006-10-27 23:46:08 +0000631 FoldingSetNodeID ID;
632 AddNodeIDNode(ID, N->getOpcode(), N->getVTList());
633
Evan Cheng9629aba2006-10-11 01:47:58 +0000634 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
635 ID.AddInteger(LD->getAddressingMode());
636 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000637 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000638 ID.AddPointer(LD->getSrcValue());
639 ID.AddInteger(LD->getSrcValueOffset());
640 ID.AddInteger(LD->getAlignment());
641 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000642 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
643 ID.AddInteger(ST->getAddressingMode());
644 ID.AddInteger(ST->isTruncatingStore());
645 ID.AddInteger(ST->getStoredVT());
646 ID.AddPointer(ST->getSrcValue());
647 ID.AddInteger(ST->getSrcValueOffset());
648 ID.AddInteger(ST->getAlignment());
649 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000650 }
Jim Laskey583bd472006-10-27 23:46:08 +0000651
652 AddNodeIDOperands(ID, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000653 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000654}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000655
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000656
Chris Lattner78ec3112003-08-11 14:57:33 +0000657SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000658 while (!AllNodes.empty()) {
659 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000660 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000661 delete [] N->OperandList;
662 N->OperandList = 0;
663 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000664 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000665 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000666}
667
Chris Lattner0f2287b2005-04-13 02:38:18 +0000668SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000669 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000670 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000671 return getNode(ISD::AND, Op.getValueType(), Op,
672 getConstant(Imm, Op.getValueType()));
673}
674
Chris Lattner36ce6912005-11-29 06:21:05 +0000675SDOperand SelectionDAG::getString(const std::string &Val) {
676 StringSDNode *&N = StringNodes[Val];
677 if (!N) {
678 N = new StringSDNode(Val);
679 AllNodes.push_back(N);
680 }
681 return SDOperand(N, 0);
682}
683
Chris Lattner61b09412006-08-11 21:01:22 +0000684SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000685 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000686 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000687
Chris Lattner61b09412006-08-11 21:01:22 +0000688 // Mask out any bits that are not valid for this constant.
689 Val &= MVT::getIntVTBitMask(VT);
690
691 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000692 FoldingSetNodeID ID;
693 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000694 ID.AddInteger(Val);
695 void *IP = 0;
696 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
697 return SDOperand(E, 0);
698 SDNode *N = new ConstantSDNode(isT, Val, VT);
699 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000700 AllNodes.push_back(N);
701 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000702}
703
Chris Lattner61b09412006-08-11 21:01:22 +0000704
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000705SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
706 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000707 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
708 if (VT == MVT::f32)
709 Val = (float)Val; // Mask out extra precision.
710
Chris Lattnerd8658612005-02-17 20:17:32 +0000711 // Do the map lookup using the actual bit pattern for the floating point
712 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
713 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000714 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000715 FoldingSetNodeID ID;
716 AddNodeIDNode(ID, Opc, getVTList(VT));
717 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000718 void *IP = 0;
719 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
720 return SDOperand(E, 0);
721 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
722 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
725}
726
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000728 MVT::ValueType VT, int Offset,
729 bool isTargetGA) {
730 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000731 FoldingSetNodeID ID;
732 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000733 ID.AddPointer(GV);
734 ID.AddInteger(Offset);
735 void *IP = 0;
736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
737 return SDOperand(E, 0);
738 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
739 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740 AllNodes.push_back(N);
741 return SDOperand(N, 0);
742}
743
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000744SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
745 bool isTarget) {
746 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000747 FoldingSetNodeID ID;
748 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000749 ID.AddInteger(FI);
750 void *IP = 0;
751 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
752 return SDOperand(E, 0);
753 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
754 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000755 AllNodes.push_back(N);
756 return SDOperand(N, 0);
757}
758
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000759SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
760 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000761 FoldingSetNodeID ID;
762 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000763 ID.AddInteger(JTI);
764 void *IP = 0;
765 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
766 return SDOperand(E, 0);
767 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
768 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000769 AllNodes.push_back(N);
770 return SDOperand(N, 0);
771}
772
Evan Chengb8973bd2006-01-31 22:23:14 +0000773SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000774 unsigned Alignment, int Offset,
775 bool isTarget) {
776 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000777 FoldingSetNodeID ID;
778 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000779 ID.AddInteger(Alignment);
780 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000781 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000782 void *IP = 0;
783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
784 return SDOperand(E, 0);
785 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
786 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
789}
790
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791
Evan Chengd6594ae2006-09-12 21:00:35 +0000792SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
793 MVT::ValueType VT,
794 unsigned Alignment, int Offset,
795 bool isTarget) {
796 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000797 FoldingSetNodeID ID;
798 AddNodeIDNode(ID, Opc, getVTList(VT));
Evan Chengd6594ae2006-09-12 21:00:35 +0000799 ID.AddInteger(Alignment);
800 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000801 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000802 void *IP = 0;
803 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
804 return SDOperand(E, 0);
805 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
806 CSEMap.InsertNode(N, IP);
807 AllNodes.push_back(N);
808 return SDOperand(N, 0);
809}
810
811
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000813 FoldingSetNodeID ID;
814 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000815 ID.AddPointer(MBB);
816 void *IP = 0;
817 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
818 return SDOperand(E, 0);
819 SDNode *N = new BasicBlockSDNode(MBB);
820 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 AllNodes.push_back(N);
822 return SDOperand(N, 0);
823}
824
Chris Lattner15e4b012005-07-10 00:07:11 +0000825SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
826 if ((unsigned)VT >= ValueTypeNodes.size())
827 ValueTypeNodes.resize(VT+1);
828 if (ValueTypeNodes[VT] == 0) {
829 ValueTypeNodes[VT] = new VTSDNode(VT);
830 AllNodes.push_back(ValueTypeNodes[VT]);
831 }
832
833 return SDOperand(ValueTypeNodes[VT], 0);
834}
835
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
837 SDNode *&N = ExternalSymbols[Sym];
838 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000839 N = new ExternalSymbolSDNode(false, Sym, VT);
840 AllNodes.push_back(N);
841 return SDOperand(N, 0);
842}
843
Chris Lattner809ec112006-01-28 10:09:25 +0000844SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
845 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000846 SDNode *&N = TargetExternalSymbols[Sym];
847 if (N) return SDOperand(N, 0);
848 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 AllNodes.push_back(N);
850 return SDOperand(N, 0);
851}
852
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000853SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
854 if ((unsigned)Cond >= CondCodeNodes.size())
855 CondCodeNodes.resize(Cond+1);
856
Chris Lattner079a27a2005-08-09 20:40:02 +0000857 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000858 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000859 AllNodes.push_back(CondCodeNodes[Cond]);
860 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000861 return SDOperand(CondCodeNodes[Cond], 0);
862}
863
Chris Lattner0fdd7682005-08-30 22:38:38 +0000864SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000865 FoldingSetNodeID ID;
866 AddNodeIDNode(ID, ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000867 ID.AddInteger(RegNo);
868 void *IP = 0;
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new RegisterSDNode(RegNo, VT);
872 CSEMap.InsertNode(N, IP);
873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
877SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
878 assert((!V || isa<PointerType>(V->getType())) &&
879 "SrcValue is not a pointer?");
880
Jim Laskey583bd472006-10-27 23:46:08 +0000881 FoldingSetNodeID ID;
882 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000883 ID.AddPointer(V);
884 ID.AddInteger(Offset);
885 void *IP = 0;
886 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
887 return SDOperand(E, 0);
888 SDNode *N = new SrcValueSDNode(V, Offset);
889 CSEMap.InsertNode(N, IP);
890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000892}
893
Chris Lattner51dabfb2006-10-14 00:41:01 +0000894SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
895 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896 // These setcc operations always fold.
897 switch (Cond) {
898 default: break;
899 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000900 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000901 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000902 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000903
904 case ISD::SETOEQ:
905 case ISD::SETOGT:
906 case ISD::SETOGE:
907 case ISD::SETOLT:
908 case ISD::SETOLE:
909 case ISD::SETONE:
910 case ISD::SETO:
911 case ISD::SETUO:
912 case ISD::SETUEQ:
913 case ISD::SETUNE:
914 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
915 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000917
Chris Lattner67255a12005-04-07 18:14:58 +0000918 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
919 uint64_t C2 = N2C->getValue();
920 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
921 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000922
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 // Sign extend the operands if required
924 if (ISD::isSignedIntSetCC(Cond)) {
925 C1 = N1C->getSignExtended();
926 C2 = N2C->getSignExtended();
927 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000928
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 switch (Cond) {
930 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000931 case ISD::SETEQ: return getConstant(C1 == C2, VT);
932 case ISD::SETNE: return getConstant(C1 != C2, VT);
933 case ISD::SETULT: return getConstant(C1 < C2, VT);
934 case ISD::SETUGT: return getConstant(C1 > C2, VT);
935 case ISD::SETULE: return getConstant(C1 <= C2, VT);
936 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
937 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
938 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
939 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
940 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000942 }
Chris Lattner67255a12005-04-07 18:14:58 +0000943 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
945 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
946 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000947
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 switch (Cond) {
949 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000950 case ISD::SETEQ: return getConstant(C1 == C2, VT);
951 case ISD::SETNE: return getConstant(C1 != C2, VT);
952 case ISD::SETLT: return getConstant(C1 < C2, VT);
953 case ISD::SETGT: return getConstant(C1 > C2, VT);
954 case ISD::SETLE: return getConstant(C1 <= C2, VT);
955 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 }
957 } else {
958 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000959 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000961
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000962 // Could not fold it.
963 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964}
965
Chris Lattner51dabfb2006-10-14 00:41:01 +0000966
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000968///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000970 FoldingSetNodeID ID;
971 AddNodeIDNode(ID, Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000972 void *IP = 0;
973 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
974 return SDOperand(E, 0);
975 SDNode *N = new SDNode(Opcode, VT);
976 CSEMap.InsertNode(N, IP);
977
978 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 return SDOperand(N, 0);
980}
981
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
983 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000984 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000985 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
987 uint64_t Val = C->getValue();
988 switch (Opcode) {
989 default: break;
990 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000991 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000992 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
993 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000994 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
995 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000996 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000997 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000998 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000999 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001000 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001001 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001002 case ISD::BSWAP:
1003 switch(VT) {
1004 default: assert(0 && "Invalid bswap!"); break;
1005 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1006 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1007 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1008 }
1009 break;
1010 case ISD::CTPOP:
1011 switch(VT) {
1012 default: assert(0 && "Invalid ctpop!"); break;
1013 case MVT::i1: return getConstant(Val != 0, VT);
1014 case MVT::i8:
1015 Tmp1 = (unsigned)Val & 0xFF;
1016 return getConstant(CountPopulation_32(Tmp1), VT);
1017 case MVT::i16:
1018 Tmp1 = (unsigned)Val & 0xFFFF;
1019 return getConstant(CountPopulation_32(Tmp1), VT);
1020 case MVT::i32:
1021 return getConstant(CountPopulation_32((unsigned)Val), VT);
1022 case MVT::i64:
1023 return getConstant(CountPopulation_64(Val), VT);
1024 }
1025 case ISD::CTLZ:
1026 switch(VT) {
1027 default: assert(0 && "Invalid ctlz!"); break;
1028 case MVT::i1: return getConstant(Val == 0, VT);
1029 case MVT::i8:
1030 Tmp1 = (unsigned)Val & 0xFF;
1031 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1032 case MVT::i16:
1033 Tmp1 = (unsigned)Val & 0xFFFF;
1034 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1035 case MVT::i32:
1036 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1037 case MVT::i64:
1038 return getConstant(CountLeadingZeros_64(Val), VT);
1039 }
1040 case ISD::CTTZ:
1041 switch(VT) {
1042 default: assert(0 && "Invalid cttz!"); break;
1043 case MVT::i1: return getConstant(Val == 0, VT);
1044 case MVT::i8:
1045 Tmp1 = (unsigned)Val | 0x100;
1046 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1047 case MVT::i16:
1048 Tmp1 = (unsigned)Val | 0x10000;
1049 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1050 case MVT::i32:
1051 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1052 case MVT::i64:
1053 return getConstant(CountTrailingZeros_64(Val), VT);
1054 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 }
1056 }
1057
Chris Lattner94683772005-12-23 05:30:37 +00001058 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1060 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001061 case ISD::FNEG:
1062 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001063 case ISD::FABS:
1064 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 case ISD::FP_ROUND:
1066 case ISD::FP_EXTEND:
1067 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001068 case ISD::FP_TO_SINT:
1069 return getConstant((int64_t)C->getValue(), VT);
1070 case ISD::FP_TO_UINT:
1071 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001072 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001073 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001074 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001075 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001076 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001077 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 }
1079
1080 unsigned OpOpcode = Operand.Val->getOpcode();
1081 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001082 case ISD::TokenFactor:
1083 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084 case ISD::SIGN_EXTEND:
1085 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001086 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1088 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1089 break;
1090 case ISD::ZERO_EXTEND:
1091 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001092 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001093 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001094 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001096 case ISD::ANY_EXTEND:
1097 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001098 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001099 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1100 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1101 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1102 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 case ISD::TRUNCATE:
1104 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001105 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 if (OpOpcode == ISD::TRUNCATE)
1107 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001108 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1109 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001110 // If the source is smaller than the dest, we still need an extend.
1111 if (Operand.Val->getOperand(0).getValueType() < VT)
1112 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1113 else if (Operand.Val->getOperand(0).getValueType() > VT)
1114 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1115 else
1116 return Operand.Val->getOperand(0);
1117 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001118 break;
Chris Lattner35481892005-12-23 00:16:34 +00001119 case ISD::BIT_CONVERT:
1120 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001121 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001122 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001123 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001124 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1125 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001126 if (OpOpcode == ISD::UNDEF)
1127 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001128 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001129 case ISD::SCALAR_TO_VECTOR:
1130 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1131 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1132 "Illegal SCALAR_TO_VECTOR node!");
1133 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001134 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001135 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1136 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001137 Operand.Val->getOperand(0));
1138 if (OpOpcode == ISD::FNEG) // --X -> X
1139 return Operand.Val->getOperand(0);
1140 break;
1141 case ISD::FABS:
1142 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1143 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1144 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 }
1146
Chris Lattner43247a12005-08-25 19:12:10 +00001147 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001148 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001149 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001150 FoldingSetNodeID ID;
1151 AddNodeIDNode(ID, Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001152 void *IP = 0;
1153 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1154 return SDOperand(E, 0);
1155 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001156 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001157 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001158 } else {
1159 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001160 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001161 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 AllNodes.push_back(N);
1163 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001164}
1165
Chris Lattner36019aa2005-04-18 03:48:41 +00001166
1167
Chris Lattnerc3aae252005-01-07 07:46:32 +00001168SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1169 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001170#ifndef NDEBUG
1171 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001172 case ISD::TokenFactor:
1173 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1174 N2.getValueType() == MVT::Other && "Invalid token factor!");
1175 break;
Chris Lattner76365122005-01-16 02:23:22 +00001176 case ISD::AND:
1177 case ISD::OR:
1178 case ISD::XOR:
1179 case ISD::UDIV:
1180 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001181 case ISD::MULHU:
1182 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001183 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1184 // fall through
1185 case ISD::ADD:
1186 case ISD::SUB:
1187 case ISD::MUL:
1188 case ISD::SDIV:
1189 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001190 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1191 // fall through.
1192 case ISD::FADD:
1193 case ISD::FSUB:
1194 case ISD::FMUL:
1195 case ISD::FDIV:
1196 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001197 assert(N1.getValueType() == N2.getValueType() &&
1198 N1.getValueType() == VT && "Binary operator types must match!");
1199 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001200 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1201 assert(N1.getValueType() == VT &&
1202 MVT::isFloatingPoint(N1.getValueType()) &&
1203 MVT::isFloatingPoint(N2.getValueType()) &&
1204 "Invalid FCOPYSIGN!");
1205 break;
Chris Lattner76365122005-01-16 02:23:22 +00001206 case ISD::SHL:
1207 case ISD::SRA:
1208 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001209 case ISD::ROTL:
1210 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001211 assert(VT == N1.getValueType() &&
1212 "Shift operators return type must be the same as their first arg");
1213 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001214 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001215 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001216 case ISD::FP_ROUND_INREG: {
1217 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1218 assert(VT == N1.getValueType() && "Not an inreg round!");
1219 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1220 "Cannot FP_ROUND_INREG integer types");
1221 assert(EVT <= VT && "Not rounding down!");
1222 break;
1223 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001224 case ISD::AssertSext:
1225 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001226 case ISD::SIGN_EXTEND_INREG: {
1227 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1228 assert(VT == N1.getValueType() && "Not an inreg extend!");
1229 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1230 "Cannot *_EXTEND_INREG FP types");
1231 assert(EVT <= VT && "Not extending!");
1232 }
1233
Chris Lattner76365122005-01-16 02:23:22 +00001234 default: break;
1235 }
1236#endif
1237
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1239 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1240 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001241 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1242 int64_t Val = N1C->getValue();
1243 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1244 Val <<= 64-FromBits;
1245 Val >>= 64-FromBits;
1246 return getConstant(Val, VT);
1247 }
1248
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 if (N2C) {
1250 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1251 switch (Opcode) {
1252 case ISD::ADD: return getConstant(C1 + C2, VT);
1253 case ISD::SUB: return getConstant(C1 - C2, VT);
1254 case ISD::MUL: return getConstant(C1 * C2, VT);
1255 case ISD::UDIV:
1256 if (C2) return getConstant(C1 / C2, VT);
1257 break;
1258 case ISD::UREM :
1259 if (C2) return getConstant(C1 % C2, VT);
1260 break;
1261 case ISD::SDIV :
1262 if (C2) return getConstant(N1C->getSignExtended() /
1263 N2C->getSignExtended(), VT);
1264 break;
1265 case ISD::SREM :
1266 if (C2) return getConstant(N1C->getSignExtended() %
1267 N2C->getSignExtended(), VT);
1268 break;
1269 case ISD::AND : return getConstant(C1 & C2, VT);
1270 case ISD::OR : return getConstant(C1 | C2, VT);
1271 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001272 case ISD::SHL : return getConstant(C1 << C2, VT);
1273 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001274 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001275 case ISD::ROTL :
1276 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1277 VT);
1278 case ISD::ROTR :
1279 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1280 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001281 default: break;
1282 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001283 } else { // Cannonicalize constant to RHS if commutative
1284 if (isCommutativeBinOp(Opcode)) {
1285 std::swap(N1C, N2C);
1286 std::swap(N1, N2);
1287 }
1288 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001289 }
1290
1291 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1292 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001293 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001294 if (N2CFP) {
1295 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1296 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001297 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1298 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1299 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1300 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 if (C2) return getConstantFP(C1 / C2, VT);
1302 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001303 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001304 if (C2) return getConstantFP(fmod(C1, C2), VT);
1305 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001306 case ISD::FCOPYSIGN: {
1307 union {
1308 double F;
1309 uint64_t I;
1310 } u1;
1311 union {
1312 double F;
1313 int64_t I;
1314 } u2;
1315 u1.F = C1;
1316 u2.F = C2;
1317 if (u2.I < 0) // Sign bit of RHS set?
1318 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1319 else
1320 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1321 return getConstantFP(u1.F, VT);
1322 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001323 default: break;
1324 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001325 } else { // Cannonicalize constant to RHS if commutative
1326 if (isCommutativeBinOp(Opcode)) {
1327 std::swap(N1CFP, N2CFP);
1328 std::swap(N1, N2);
1329 }
1330 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001331 }
Chris Lattner62b57722006-04-20 05:39:12 +00001332
1333 // Canonicalize an UNDEF to the RHS, even over a constant.
1334 if (N1.getOpcode() == ISD::UNDEF) {
1335 if (isCommutativeBinOp(Opcode)) {
1336 std::swap(N1, N2);
1337 } else {
1338 switch (Opcode) {
1339 case ISD::FP_ROUND_INREG:
1340 case ISD::SIGN_EXTEND_INREG:
1341 case ISD::SUB:
1342 case ISD::FSUB:
1343 case ISD::FDIV:
1344 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001345 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001346 return N1; // fold op(undef, arg2) -> undef
1347 case ISD::UDIV:
1348 case ISD::SDIV:
1349 case ISD::UREM:
1350 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001351 case ISD::SRL:
1352 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001353 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1354 }
1355 }
1356 }
1357
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001358 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001359 if (N2.getOpcode() == ISD::UNDEF) {
1360 switch (Opcode) {
1361 case ISD::ADD:
1362 case ISD::SUB:
1363 case ISD::FADD:
1364 case ISD::FSUB:
1365 case ISD::FMUL:
1366 case ISD::FDIV:
1367 case ISD::FREM:
1368 case ISD::UDIV:
1369 case ISD::SDIV:
1370 case ISD::UREM:
1371 case ISD::SREM:
1372 case ISD::XOR:
1373 return N2; // fold op(arg1, undef) -> undef
1374 case ISD::MUL:
1375 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001376 case ISD::SRL:
1377 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001378 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1379 case ISD::OR:
1380 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001381 case ISD::SRA:
1382 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001383 }
1384 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001385
Chris Lattner51dabfb2006-10-14 00:41:01 +00001386 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001387 switch (Opcode) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001388 case ISD::AND:
1389 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1390 // worth handling here.
1391 if (N2C && N2C->getValue() == 0)
1392 return N2;
1393 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001394 case ISD::OR:
1395 case ISD::XOR:
1396 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1397 // worth handling here.
1398 if (N2C && N2C->getValue() == 0)
1399 return N1;
1400 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001401 case ISD::FP_ROUND_INREG:
1402 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1403 break;
1404 case ISD::SIGN_EXTEND_INREG: {
1405 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1406 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001407 break;
1408 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001409 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001410 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1411
Chris Lattner5c6621c2006-09-19 04:51:23 +00001412 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1413 // 64-bit integers into 32-bit parts. Instead of building the extract of
1414 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001415 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001416 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001417
1418 // EXTRACT_ELEMENT of a constant int is also very common.
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1420 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1421 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001422 }
1423 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001424
Nate Begemaneea805e2005-04-13 21:23:31 +00001425 // FIXME: figure out how to safely handle things like
1426 // int foo(int x) { return 1 << (x & 255); }
1427 // int bar() { return foo(256); }
1428#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001429 case ISD::SHL:
1430 case ISD::SRL:
1431 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001432 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001433 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001434 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001435 else if (N2.getOpcode() == ISD::AND)
1436 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1437 // If the and is only masking out bits that cannot effect the shift,
1438 // eliminate the and.
1439 unsigned NumBits = MVT::getSizeInBits(VT);
1440 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1441 return getNode(Opcode, VT, N1, N2.getOperand(0));
1442 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001443 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001444#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001445 }
1446
Chris Lattner27e9b412005-05-11 18:57:39 +00001447 // Memoize this node if possible.
1448 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001449 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001450 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001451 FoldingSetNodeID ID;
1452 AddNodeIDNode(ID, Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001453 void *IP = 0;
1454 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1455 return SDOperand(E, 0);
1456 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001457 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001458 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001459 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001460 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001461 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001462 }
1463
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 AllNodes.push_back(N);
1465 return SDOperand(N, 0);
1466}
1467
Chris Lattnerc3aae252005-01-07 07:46:32 +00001468SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1469 SDOperand N1, SDOperand N2, SDOperand N3) {
1470 // Perform various simplifications.
1471 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1472 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001473 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001474 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001475 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001476 // Use FoldSetCC to simplify SETCC's.
1477 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001478 if (Simp.Val) return Simp;
1479 break;
1480 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001481 case ISD::SELECT:
1482 if (N1C)
1483 if (N1C->getValue())
1484 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001485 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001486 return N3; // select false, X, Y -> Y
1487
1488 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001489 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001490 case ISD::BRCOND:
1491 if (N2C)
1492 if (N2C->getValue()) // Unconditional branch
1493 return getNode(ISD::BR, MVT::Other, N1, N3);
1494 else
1495 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001496 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001497 case ISD::VECTOR_SHUFFLE:
1498 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1499 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1500 N3.getOpcode() == ISD::BUILD_VECTOR &&
1501 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1502 "Illegal VECTOR_SHUFFLE node!");
1503 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001504 }
1505
Chris Lattner43247a12005-08-25 19:12:10 +00001506 // Memoize node if it doesn't produce a flag.
1507 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001508 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001509 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001510 FoldingSetNodeID ID;
1511 AddNodeIDNode(ID, Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001512 void *IP = 0;
1513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1514 return SDOperand(E, 0);
1515 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001516 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001517 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001518 } else {
1519 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001520 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001521 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001522 AllNodes.push_back(N);
1523 return SDOperand(N, 0);
1524}
1525
1526SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001527 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001528 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001529 SDOperand Ops[] = { N1, N2, N3, N4 };
1530 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001531}
1532
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001533SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1534 SDOperand N1, SDOperand N2, SDOperand N3,
1535 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001536 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1537 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001538}
1539
Evan Cheng7038daf2005-12-10 00:37:58 +00001540SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1541 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001542 const Value *SV, int SVOffset,
1543 bool isVolatile) {
1544 // FIXME: Alignment == 1 for now.
1545 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001546 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001547 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001548 FoldingSetNodeID ID;
1549 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
Evan Cheng466685d2006-10-09 20:57:25 +00001550 ID.AddInteger(ISD::UNINDEXED);
1551 ID.AddInteger(ISD::NON_EXTLOAD);
1552 ID.AddInteger(VT);
1553 ID.AddPointer(SV);
1554 ID.AddInteger(SVOffset);
1555 ID.AddInteger(Alignment);
1556 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001557 void *IP = 0;
1558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1559 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001560 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED,
1561 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1562 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00001563 N->setValueTypes(VTs);
1564 CSEMap.InsertNode(N, IP);
1565 AllNodes.push_back(N);
1566 return SDOperand(N, 0);
1567}
1568
1569SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1570 SDOperand Chain, SDOperand Ptr, const Value *SV,
1571 int SVOffset, MVT::ValueType EVT,
1572 bool isVolatile) {
1573 // If they are asking for an extending load from/to the same thing, return a
1574 // normal load.
1575 if (VT == EVT)
1576 ExtType = ISD::NON_EXTLOAD;
1577
1578 if (MVT::isVector(VT))
1579 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1580 else
1581 assert(EVT < VT && "Should only be an extending load, not truncating!");
1582 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1583 "Cannot sign/zero extend a FP/Vector load!");
1584 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1585 "Cannot convert from FP to Int or Int -> FP!");
1586
1587 // FIXME: Alignment == 1 for now.
1588 unsigned Alignment = 1;
1589 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001590 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001591 FoldingSetNodeID ID;
1592 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
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);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001603 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED, ExtType, EVT,
1604 SV, SVOffset, Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001605 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001606 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001607 AllNodes.push_back(N);
1608 return SDOperand(N, 0);
1609}
1610
Evan Cheng5270cf12006-10-26 21:53:40 +00001611SDOperand SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
1612 SDOperand Offset, ISD::MemOpAddrMode 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);
Jim Laskey583bd472006-10-27 23:46:08 +00001618 FoldingSetNodeID ID;
1619 AddNodeIDNode(ID, ISD::LOAD, VTs, LD->getChain(), Base, Offset);
Evan Cheng2caccca2006-10-17 21:14:32 +00001620 ID.AddInteger(AM);
1621 ID.AddInteger(LD->getExtensionType());
1622 ID.AddInteger(LD->getLoadedVT());
1623 ID.AddPointer(LD->getSrcValue());
1624 ID.AddInteger(LD->getSrcValueOffset());
1625 ID.AddInteger(LD->getAlignment());
1626 ID.AddInteger(LD->isVolatile());
1627 void *IP = 0;
1628 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1629 return SDOperand(E, 0);
Evan Cheng5270cf12006-10-26 21:53:40 +00001630 SDNode *N = new LoadSDNode(LD->getChain(), Base, Offset, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00001631 LD->getExtensionType(), LD->getLoadedVT(),
1632 LD->getSrcValue(), LD->getSrcValueOffset(),
1633 LD->getAlignment(), LD->isVolatile());
1634 N->setValueTypes(VTs);
1635 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
Evan Chengad071e12006-10-05 22:57:11 +00001648SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001649 SDOperand Ptr, const Value *SV, int SVOffset,
1650 bool isVolatile) {
1651 MVT::ValueType VT = Value.getValueType();
1652
1653 // 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());
1657 SDOperand Ops[] = { Chain, Value, 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);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001670 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, false,
1671 VT, SV, SVOffset, Alignment, isVolatile);
1672 N->setValueTypes(VTs);
1673 CSEMap.InsertNode(N, IP);
1674 AllNodes.push_back(N);
1675 return SDOperand(N, 0);
1676}
1677
1678SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Value,
1679 SDOperand Ptr, const Value *SV,
1680 int SVOffset, MVT::ValueType SVT,
1681 bool isVolatile) {
1682 MVT::ValueType VT = Value.getValueType();
1683 bool isTrunc = VT != SVT;
1684
1685 assert(VT > SVT && "Not a truncation?");
1686 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1687 "Can't do FP-INT conversion!");
1688
1689 // FIXME: Alignment == 1 for now.
1690 unsigned Alignment = 1;
1691 SDVTList VTs = getVTList(MVT::Other);
1692 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1693 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001694 FoldingSetNodeID ID;
1695 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001696 ID.AddInteger(ISD::UNINDEXED);
1697 ID.AddInteger(isTrunc);
1698 ID.AddInteger(SVT);
1699 ID.AddPointer(SV);
1700 ID.AddInteger(SVOffset);
1701 ID.AddInteger(Alignment);
1702 ID.AddInteger(isVolatile);
1703 void *IP = 0;
1704 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1705 return SDOperand(E, 0);
1706 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, isTrunc,
1707 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001708 N->setValueTypes(VTs);
1709 CSEMap.InsertNode(N, IP);
1710 AllNodes.push_back(N);
1711 return SDOperand(N, 0);
1712}
1713
Nate Begemanacc398c2006-01-25 18:21:52 +00001714SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1715 SDOperand Chain, SDOperand Ptr,
1716 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001717 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001718 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001719}
1720
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001721SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001722 const SDOperand *Ops, unsigned NumOps) {
1723 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001724 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001725 case 1: return getNode(Opcode, VT, Ops[0]);
1726 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1727 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001728 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001729 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001730
Chris Lattneref847df2005-04-09 03:27:28 +00001731 switch (Opcode) {
1732 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001733 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001734 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001735 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1736 "LHS and RHS of condition must have same type!");
1737 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1738 "True and False arms of SelectCC must have same type!");
1739 assert(Ops[2].getValueType() == VT &&
1740 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001741 break;
1742 }
1743 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001744 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001745 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1746 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001747 break;
1748 }
Chris Lattneref847df2005-04-09 03:27:28 +00001749 }
1750
Chris Lattner385328c2005-05-14 07:42:29 +00001751 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001752 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001753 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001754 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001755 FoldingSetNodeID ID;
1756 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001757 void *IP = 0;
1758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1759 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001760 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001761 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001762 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001763 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001764 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001765 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001766 }
Chris Lattneref847df2005-04-09 03:27:28 +00001767 AllNodes.push_back(N);
1768 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001769}
1770
Chris Lattner89c34632005-05-14 06:20:26 +00001771SDOperand SelectionDAG::getNode(unsigned Opcode,
1772 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001773 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001774 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1775 Ops, NumOps);
1776}
1777
1778SDOperand SelectionDAG::getNode(unsigned Opcode,
1779 const MVT::ValueType *VTs, unsigned NumVTs,
1780 const SDOperand *Ops, unsigned NumOps) {
1781 if (NumVTs == 1)
1782 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001783 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1784}
1785
1786SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1787 const SDOperand *Ops, unsigned NumOps) {
1788 if (VTList.NumVTs == 1)
1789 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001790
Chris Lattner5f056bf2005-07-10 01:55:33 +00001791 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001792 // FIXME: figure out how to safely handle things like
1793 // int foo(int x) { return 1 << (x & 255); }
1794 // int bar() { return foo(256); }
1795#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001796 case ISD::SRA_PARTS:
1797 case ISD::SRL_PARTS:
1798 case ISD::SHL_PARTS:
1799 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001800 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001801 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1802 else if (N3.getOpcode() == ISD::AND)
1803 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1804 // If the and is only masking out bits that cannot effect the shift,
1805 // eliminate the and.
1806 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1807 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1808 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1809 }
1810 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001811#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001812 }
Chris Lattner89c34632005-05-14 06:20:26 +00001813
Chris Lattner43247a12005-08-25 19:12:10 +00001814 // Memoize the node unless it returns a flag.
1815 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001816 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001817 FoldingSetNodeID ID;
1818 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001819 void *IP = 0;
1820 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1821 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001822 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001823 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001824 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001825 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001826 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001827 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001828 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001829 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001830 return SDOperand(N, 0);
1831}
1832
Chris Lattner70046e92006-08-15 17:46:01 +00001833SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1834 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001835}
1836
Chris Lattner70046e92006-08-15 17:46:01 +00001837SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001838 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1839 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001840 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001841 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001842 }
1843 std::vector<MVT::ValueType> V;
1844 V.push_back(VT1);
1845 V.push_back(VT2);
1846 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001847 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001848}
Chris Lattner70046e92006-08-15 17:46:01 +00001849SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1850 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001851 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001852 E = VTList.end(); I != E; ++I) {
1853 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1854 (*I)[2] == VT3)
1855 return makeVTList(&(*I)[0], 3);
1856 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001857 std::vector<MVT::ValueType> V;
1858 V.push_back(VT1);
1859 V.push_back(VT2);
1860 V.push_back(VT3);
1861 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001862 return makeVTList(&(*VTList.begin())[0], 3);
1863}
1864
1865SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1866 switch (NumVTs) {
1867 case 0: assert(0 && "Cannot have nodes without results!");
1868 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1869 case 2: return getVTList(VTs[0], VTs[1]);
1870 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1871 default: break;
1872 }
1873
1874 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1875 E = VTList.end(); I != E; ++I) {
1876 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1877
1878 bool NoMatch = false;
1879 for (unsigned i = 2; i != NumVTs; ++i)
1880 if (VTs[i] != (*I)[i]) {
1881 NoMatch = true;
1882 break;
1883 }
1884 if (!NoMatch)
1885 return makeVTList(&*I->begin(), NumVTs);
1886 }
1887
1888 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1889 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001890}
1891
1892
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001893/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1894/// specified operands. If the resultant node already exists in the DAG,
1895/// this does not modify the specified node, instead it returns the node that
1896/// already exists. If the resultant node does not exist in the DAG, the
1897/// input node is returned. As a degenerate case, if you specify the same
1898/// input operands as the node already has, the input node is returned.
1899SDOperand SelectionDAG::
1900UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1901 SDNode *N = InN.Val;
1902 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1903
1904 // Check to see if there is no change.
1905 if (Op == N->getOperand(0)) return InN;
1906
1907 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001908 void *InsertPos = 0;
1909 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1910 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001911
1912 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001913 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001914 RemoveNodeFromCSEMaps(N);
1915
1916 // Now we update the operands.
1917 N->OperandList[0].Val->removeUser(N);
1918 Op.Val->addUser(N);
1919 N->OperandList[0] = Op;
1920
1921 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001922 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001923 return InN;
1924}
1925
1926SDOperand SelectionDAG::
1927UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1928 SDNode *N = InN.Val;
1929 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1930
1931 // Check to see if there is no change.
1932 bool AnyChange = false;
1933 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1934 return InN; // No operands changed, just return the input node.
1935
1936 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001937 void *InsertPos = 0;
1938 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1939 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001940
1941 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001942 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001943 RemoveNodeFromCSEMaps(N);
1944
1945 // Now we update the operands.
1946 if (N->OperandList[0] != Op1) {
1947 N->OperandList[0].Val->removeUser(N);
1948 Op1.Val->addUser(N);
1949 N->OperandList[0] = Op1;
1950 }
1951 if (N->OperandList[1] != Op2) {
1952 N->OperandList[1].Val->removeUser(N);
1953 Op2.Val->addUser(N);
1954 N->OperandList[1] = Op2;
1955 }
1956
1957 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001958 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001959 return InN;
1960}
1961
1962SDOperand SelectionDAG::
1963UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001964 SDOperand Ops[] = { Op1, Op2, Op3 };
1965 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001966}
1967
1968SDOperand SelectionDAG::
1969UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1970 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001971 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1972 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001973}
1974
1975SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001976UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1977 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001978 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1979 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001980}
1981
1982
1983SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001984UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001985 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001986 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001987 "Update with wrong number of operands");
1988
1989 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001990 bool AnyChange = false;
1991 for (unsigned i = 0; i != NumOps; ++i) {
1992 if (Ops[i] != N->getOperand(i)) {
1993 AnyChange = true;
1994 break;
1995 }
1996 }
1997
1998 // No operands changed, just return the input node.
1999 if (!AnyChange) return InN;
2000
2001 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002002 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002003 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002004 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002005
2006 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002007 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002008 RemoveNodeFromCSEMaps(N);
2009
2010 // Now we update the operands.
2011 for (unsigned i = 0; i != NumOps; ++i) {
2012 if (N->OperandList[i] != Ops[i]) {
2013 N->OperandList[i].Val->removeUser(N);
2014 Ops[i].Val->addUser(N);
2015 N->OperandList[i] = Ops[i];
2016 }
2017 }
2018
2019 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002020 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002021 return InN;
2022}
2023
2024
2025
Chris Lattner149c58c2005-08-16 18:17:10 +00002026
2027/// SelectNodeTo - These are used for target selectors to *mutate* the
2028/// specified node to have the specified return type, Target opcode, and
2029/// operands. Note that target opcodes are stored as
2030/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002031///
2032/// Note that SelectNodeTo returns the resultant node. If there is already a
2033/// node of the specified opcode and operands, it returns that node instead of
2034/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002035SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2036 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002037 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002038 FoldingSetNodeID ID;
2039 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs);
Chris Lattner4a283e92006-08-11 18:38:11 +00002040 void *IP = 0;
2041 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002042 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002043
Chris Lattner7651fa42005-08-24 23:00:29 +00002044 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002045
Chris Lattner7651fa42005-08-24 23:00:29 +00002046 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002047 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002048
Chris Lattner4a283e92006-08-11 18:38:11 +00002049 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002050 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002051}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002052
Evan Cheng95514ba2006-08-26 08:00:10 +00002053SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2054 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002055 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002056 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002057 FoldingSetNodeID ID;
2058 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002059 void *IP = 0;
2060 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002061 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002062
Chris Lattner149c58c2005-08-16 18:17:10 +00002063 RemoveNodeFromCSEMaps(N);
2064 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002065 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002066 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002067 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002068 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002069}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002070
Evan Cheng95514ba2006-08-26 08:00:10 +00002071SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2072 MVT::ValueType VT, SDOperand Op1,
2073 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002074 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002075 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002076 FoldingSetNodeID ID;
2077 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002078 void *IP = 0;
2079 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002080 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002081
Chris Lattner149c58c2005-08-16 18:17:10 +00002082 RemoveNodeFromCSEMaps(N);
2083 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002084 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002085 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002086
Chris Lattnera5682852006-08-07 23:03:03 +00002087 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002088 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002089}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002090
Evan Cheng95514ba2006-08-26 08:00:10 +00002091SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2092 MVT::ValueType VT, SDOperand Op1,
2093 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002094 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002095 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002096 FoldingSetNodeID ID;
2097 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002098 void *IP = 0;
2099 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002100 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002101
Chris Lattner149c58c2005-08-16 18:17:10 +00002102 RemoveNodeFromCSEMaps(N);
2103 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002104 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002105 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002106
Chris Lattnera5682852006-08-07 23:03:03 +00002107 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002108 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002109}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002110
Evan Cheng95514ba2006-08-26 08:00:10 +00002111SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002112 MVT::ValueType VT, const SDOperand *Ops,
2113 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002114 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002115 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002116 FoldingSetNodeID ID;
2117 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002118 void *IP = 0;
2119 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002120 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002121
Chris Lattner6b09a292005-08-21 18:49:33 +00002122 RemoveNodeFromCSEMaps(N);
2123 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002124 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00002125 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002126
Chris Lattnera5682852006-08-07 23:03:03 +00002127 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002128 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002129}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002130
Evan Cheng95514ba2006-08-26 08:00:10 +00002131SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2132 MVT::ValueType VT1, MVT::ValueType VT2,
2133 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002134 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002135 FoldingSetNodeID ID;
2136 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002137 void *IP = 0;
2138 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002139 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002140
Chris Lattner0fb094f2005-11-19 01:44:53 +00002141 RemoveNodeFromCSEMaps(N);
2142 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002143 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002144 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002145
Chris Lattnera5682852006-08-07 23:03:03 +00002146 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002147 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002148}
2149
Evan Cheng95514ba2006-08-26 08:00:10 +00002150SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2151 MVT::ValueType VT1, MVT::ValueType VT2,
2152 SDOperand Op1, SDOperand Op2,
2153 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002154 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002155 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002156 FoldingSetNodeID ID;
2157 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002158 void *IP = 0;
2159 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002160 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002161
Chris Lattner0fb094f2005-11-19 01:44:53 +00002162 RemoveNodeFromCSEMaps(N);
2163 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002164 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002165 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002166
Chris Lattnera5682852006-08-07 23:03:03 +00002167 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002168 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002169}
2170
Chris Lattner0fb094f2005-11-19 01:44:53 +00002171
Evan Cheng6ae46c42006-02-09 07:15:23 +00002172/// getTargetNode - These are used for target selectors to create a new node
2173/// with specified return type(s), target opcode, and operands.
2174///
2175/// Note that getTargetNode returns the resultant node. If there is already a
2176/// node of the specified opcode and operands, it returns that node instead of
2177/// the current one.
2178SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2179 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2180}
2181SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2182 SDOperand Op1) {
2183 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2184}
2185SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2186 SDOperand Op1, SDOperand Op2) {
2187 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2188}
2189SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2190 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2191 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2192}
2193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002194 const SDOperand *Ops, unsigned NumOps) {
2195 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002196}
2197SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2198 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002199 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002200 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002201}
2202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002203 MVT::ValueType VT2, SDOperand Op1,
2204 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002205 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002206 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002207 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002208}
2209SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002210 MVT::ValueType VT2, SDOperand Op1,
2211 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002212 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002213 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002214 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002215}
Evan Cheng694481e2006-08-27 08:08:54 +00002216SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2217 MVT::ValueType VT2,
2218 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002219 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002220 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2223 MVT::ValueType VT2, MVT::ValueType VT3,
2224 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002225 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002226 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002227 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002228}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002229SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002230 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002231 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002232 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2233 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002234}
2235
Evan Cheng99157a02006-08-07 22:13:29 +00002236/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002237/// This can cause recursive merging of nodes in the DAG.
2238///
Chris Lattner8b52f212005-08-26 18:36:28 +00002239/// This version assumes From/To have a single result value.
2240///
Chris Lattner1e111c72005-09-07 05:37:01 +00002241void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2242 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002243 SDNode *From = FromN.Val, *To = ToN.Val;
2244 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2245 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002246 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002247
Chris Lattner8b8749f2005-08-17 19:00:20 +00002248 while (!From->use_empty()) {
2249 // Process users until they are all gone.
2250 SDNode *U = *From->use_begin();
2251
2252 // This node is about to morph, remove its old self from the CSE maps.
2253 RemoveNodeFromCSEMaps(U);
2254
Chris Lattner65113b22005-11-08 22:07:03 +00002255 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2256 I != E; ++I)
2257 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002258 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002259 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002260 To->addUser(U);
2261 }
2262
2263 // Now that we have modified U, add it back to the CSE maps. If it already
2264 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002265 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2266 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002267 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002268 if (Deleted) Deleted->push_back(U);
2269 DeleteNodeNotInCSEMaps(U);
2270 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002271 }
2272}
2273
2274/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2275/// This can cause recursive merging of nodes in the DAG.
2276///
2277/// This version assumes From/To have matching types and numbers of result
2278/// values.
2279///
Chris Lattner1e111c72005-09-07 05:37:01 +00002280void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2281 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002282 assert(From != To && "Cannot replace uses of with self");
2283 assert(From->getNumValues() == To->getNumValues() &&
2284 "Cannot use this version of ReplaceAllUsesWith!");
2285 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002286 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002287 return;
2288 }
2289
2290 while (!From->use_empty()) {
2291 // Process users until they are all gone.
2292 SDNode *U = *From->use_begin();
2293
2294 // This node is about to morph, remove its old self from the CSE maps.
2295 RemoveNodeFromCSEMaps(U);
2296
Chris Lattner65113b22005-11-08 22:07:03 +00002297 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2298 I != E; ++I)
2299 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002300 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002301 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002302 To->addUser(U);
2303 }
2304
2305 // Now that we have modified U, add it back to the CSE maps. If it already
2306 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002307 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2308 ReplaceAllUsesWith(U, Existing, Deleted);
2309 // U is now dead.
2310 if (Deleted) Deleted->push_back(U);
2311 DeleteNodeNotInCSEMaps(U);
2312 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002313 }
2314}
2315
Chris Lattner8b52f212005-08-26 18:36:28 +00002316/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2317/// This can cause recursive merging of nodes in the DAG.
2318///
2319/// This version can replace From with any result values. To must match the
2320/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002321void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002322 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002323 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002324 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002325 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002326 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002327 return;
2328 }
2329
2330 while (!From->use_empty()) {
2331 // Process users until they are all gone.
2332 SDNode *U = *From->use_begin();
2333
2334 // This node is about to morph, remove its old self from the CSE maps.
2335 RemoveNodeFromCSEMaps(U);
2336
Chris Lattner65113b22005-11-08 22:07:03 +00002337 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2338 I != E; ++I)
2339 if (I->Val == From) {
2340 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002341 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002342 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002343 ToOp.Val->addUser(U);
2344 }
2345
2346 // Now that we have modified U, add it back to the CSE maps. If it already
2347 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002348 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2349 ReplaceAllUsesWith(U, Existing, Deleted);
2350 // U is now dead.
2351 if (Deleted) Deleted->push_back(U);
2352 DeleteNodeNotInCSEMaps(U);
2353 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002354 }
2355}
2356
Chris Lattner012f2412006-02-17 21:58:01 +00002357/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2358/// uses of other values produced by From.Val alone. The Deleted vector is
2359/// handled the same was as for ReplaceAllUsesWith.
2360void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2361 std::vector<SDNode*> &Deleted) {
2362 assert(From != To && "Cannot replace a value with itself");
2363 // Handle the simple, trivial, case efficiently.
2364 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2365 ReplaceAllUsesWith(From, To, &Deleted);
2366 return;
2367 }
2368
2369 // Get all of the users in a nice, deterministically ordered, uniqued set.
2370 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2371
2372 while (!Users.empty()) {
2373 // We know that this user uses some value of From. If it is the right
2374 // value, update it.
2375 SDNode *User = Users.back();
2376 Users.pop_back();
2377
2378 for (SDOperand *Op = User->OperandList,
2379 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2380 if (*Op == From) {
2381 // Okay, we know this user needs to be updated. Remove its old self
2382 // from the CSE maps.
2383 RemoveNodeFromCSEMaps(User);
2384
2385 // Update all operands that match "From".
2386 for (; Op != E; ++Op) {
2387 if (*Op == From) {
2388 From.Val->removeUser(User);
2389 *Op = To;
2390 To.Val->addUser(User);
2391 }
2392 }
2393
2394 // Now that we have modified User, add it back to the CSE maps. If it
2395 // already exists there, recursively merge the results together.
2396 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2397 unsigned NumDeleted = Deleted.size();
2398 ReplaceAllUsesWith(User, Existing, &Deleted);
2399
2400 // User is now dead.
2401 Deleted.push_back(User);
2402 DeleteNodeNotInCSEMaps(User);
2403
2404 // We have to be careful here, because ReplaceAllUsesWith could have
2405 // deleted a user of From, which means there may be dangling pointers
2406 // in the "Users" setvector. Scan over the deleted node pointers and
2407 // remove them from the setvector.
2408 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2409 Users.remove(Deleted[i]);
2410 }
2411 break; // Exit the operand scanning loop.
2412 }
2413 }
2414 }
2415}
2416
Chris Lattner7b2880c2005-08-24 22:44:39 +00002417
Evan Chenge6f35d82006-08-01 08:20:41 +00002418/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2419/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002420unsigned SelectionDAG::AssignNodeIds() {
2421 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002422 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2423 SDNode *N = I;
2424 N->setNodeId(Id++);
2425 }
2426 return Id;
2427}
2428
Evan Chenge6f35d82006-08-01 08:20:41 +00002429/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002430/// based on their topological order. It returns the maximum id and a vector
2431/// of the SDNodes* in assigned order by reference.
2432unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002433 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002434 std::vector<unsigned> InDegree(DAGSize);
2435 std::vector<SDNode*> Sources;
2436
2437 // Use a two pass approach to avoid using a std::map which is slow.
2438 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002439 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2440 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002441 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002442 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002443 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002444 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002445 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002446 }
2447
Evan Cheng99157a02006-08-07 22:13:29 +00002448 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002449 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002450 SDNode *N = Sources.back();
2451 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002452 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002453 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2454 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002455 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002456 if (Degree == 0)
2457 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002458 }
2459 }
2460
Evan Chengc384d6c2006-08-02 22:00:34 +00002461 // Second pass, assign the actual topological order as node ids.
2462 Id = 0;
2463 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2464 TI != TE; ++TI)
2465 (*TI)->setNodeId(Id++);
2466
2467 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002468}
2469
2470
Evan Cheng091cba12006-07-27 06:39:06 +00002471
Jim Laskey58b968b2005-08-17 20:08:02 +00002472//===----------------------------------------------------------------------===//
2473// SDNode Class
2474//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002475
Chris Lattner917d2c92006-07-19 00:00:37 +00002476// Out-of-line virtual method to give class a home.
2477void SDNode::ANCHOR() {
2478}
2479
Jim Laskey583bd472006-10-27 23:46:08 +00002480/// Profile - Gather unique data for the node.
2481///
2482void SDNode::Profile(FoldingSetNodeID &ID) {
2483 AddNodeIDNode(ID, this);
2484}
2485
Chris Lattnera3255112005-11-08 23:30:28 +00002486/// getValueTypeList - Return a pointer to the specified value type.
2487///
2488MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2489 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2490 VTs[VT] = VT;
2491 return &VTs[VT];
2492}
Chris Lattnera5682852006-08-07 23:03:03 +00002493
Chris Lattner5c884562005-01-12 18:37:47 +00002494/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2495/// indicated value. This method ignores uses of other values defined by this
2496/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002497bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002498 assert(Value < getNumValues() && "Bad value!");
2499
2500 // If there is only one value, this is easy.
2501 if (getNumValues() == 1)
2502 return use_size() == NUses;
2503 if (Uses.size() < NUses) return false;
2504
Evan Cheng4ee62112006-02-05 06:29:23 +00002505 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002506
2507 std::set<SDNode*> UsersHandled;
2508
Chris Lattnerf83482d2006-08-16 20:59:32 +00002509 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002510 SDNode *User = *UI;
2511 if (User->getNumOperands() == 1 ||
2512 UsersHandled.insert(User).second) // First time we've seen this?
2513 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2514 if (User->getOperand(i) == TheValue) {
2515 if (NUses == 0)
2516 return false; // too many uses
2517 --NUses;
2518 }
2519 }
2520
2521 // Found exactly the right number of uses?
2522 return NUses == 0;
2523}
2524
2525
Evan Cheng4ee62112006-02-05 06:29:23 +00002526// isOnlyUse - Return true if this node is the only use of N.
2527bool SDNode::isOnlyUse(SDNode *N) const {
2528 bool Seen = false;
2529 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2530 SDNode *User = *I;
2531 if (User == this)
2532 Seen = true;
2533 else
2534 return false;
2535 }
2536
2537 return Seen;
2538}
2539
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002540// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002541bool SDOperand::isOperand(SDNode *N) const {
2542 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2543 if (*this == N->getOperand(i))
2544 return true;
2545 return false;
2546}
2547
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002548bool SDNode::isOperand(SDNode *N) const {
2549 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2550 if (this == N->OperandList[i].Val)
2551 return true;
2552 return false;
2553}
Evan Cheng4ee62112006-02-05 06:29:23 +00002554
Evan Chengc5484282006-10-04 00:56:09 +00002555uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2556 assert(Num < NumOperands && "Invalid child # of SDNode!");
2557 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2558}
2559
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002560const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002561 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002562 default:
2563 if (getOpcode() < ISD::BUILTIN_OP_END)
2564 return "<<Unknown DAG Node>>";
2565 else {
Evan Cheng72261582005-12-20 06:22:03 +00002566 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002567 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002568 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2569 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002570
Evan Cheng72261582005-12-20 06:22:03 +00002571 TargetLowering &TLI = G->getTargetLoweringInfo();
2572 const char *Name =
2573 TLI.getTargetNodeName(getOpcode());
2574 if (Name) return Name;
2575 }
2576
2577 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002578 }
2579
Andrew Lenharth95762122005-03-31 21:24:06 +00002580 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002581 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002582 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002583 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002584 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002585 case ISD::AssertSext: return "AssertSext";
2586 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002587
2588 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002589 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002590 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002591 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002592
2593 case ISD::Constant: return "Constant";
2594 case ISD::ConstantFP: return "ConstantFP";
2595 case ISD::GlobalAddress: return "GlobalAddress";
2596 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002597 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002598 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Chris Lattner5839bf22005-08-26 17:15:30 +00002599 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002600 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002601 case ISD::INTRINSIC_WO_CHAIN: {
2602 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2603 return Intrinsic::getName((Intrinsic::ID)IID);
2604 }
2605 case ISD::INTRINSIC_VOID:
2606 case ISD::INTRINSIC_W_CHAIN: {
2607 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002608 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002609 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002610
Chris Lattnerb2827b02006-03-19 00:52:58 +00002611 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002612 case ISD::TargetConstant: return "TargetConstant";
2613 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002614 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2615 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002616 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002617 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002618 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002619
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002620 case ISD::CopyToReg: return "CopyToReg";
2621 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002622 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002623 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002624 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002625 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002626 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002627 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002628
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002629 // Unary operators
2630 case ISD::FABS: return "fabs";
2631 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002632 case ISD::FSQRT: return "fsqrt";
2633 case ISD::FSIN: return "fsin";
2634 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002635 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002636
2637 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002638 case ISD::ADD: return "add";
2639 case ISD::SUB: return "sub";
2640 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002641 case ISD::MULHU: return "mulhu";
2642 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002643 case ISD::SDIV: return "sdiv";
2644 case ISD::UDIV: return "udiv";
2645 case ISD::SREM: return "srem";
2646 case ISD::UREM: return "urem";
2647 case ISD::AND: return "and";
2648 case ISD::OR: return "or";
2649 case ISD::XOR: return "xor";
2650 case ISD::SHL: return "shl";
2651 case ISD::SRA: return "sra";
2652 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002653 case ISD::ROTL: return "rotl";
2654 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002655 case ISD::FADD: return "fadd";
2656 case ISD::FSUB: return "fsub";
2657 case ISD::FMUL: return "fmul";
2658 case ISD::FDIV: return "fdiv";
2659 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002660 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002661 case ISD::VADD: return "vadd";
2662 case ISD::VSUB: return "vsub";
2663 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002664 case ISD::VSDIV: return "vsdiv";
2665 case ISD::VUDIV: return "vudiv";
2666 case ISD::VAND: return "vand";
2667 case ISD::VOR: return "vor";
2668 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002669
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002670 case ISD::SETCC: return "setcc";
2671 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002672 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002673 case ISD::VSELECT: return "vselect";
2674 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2675 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2676 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002677 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002678 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2679 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2680 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2681 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2682 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002683 case ISD::ADDC: return "addc";
2684 case ISD::ADDE: return "adde";
2685 case ISD::SUBC: return "subc";
2686 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002687 case ISD::SHL_PARTS: return "shl_parts";
2688 case ISD::SRA_PARTS: return "sra_parts";
2689 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002690
Chris Lattner7f644642005-04-28 21:44:03 +00002691 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002692 case ISD::SIGN_EXTEND: return "sign_extend";
2693 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002694 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002695 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002696 case ISD::TRUNCATE: return "truncate";
2697 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002698 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002699 case ISD::FP_EXTEND: return "fp_extend";
2700
2701 case ISD::SINT_TO_FP: return "sint_to_fp";
2702 case ISD::UINT_TO_FP: return "uint_to_fp";
2703 case ISD::FP_TO_SINT: return "fp_to_sint";
2704 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002705 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002706
2707 // Control flow instructions
2708 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002709 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002710 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002711 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002712 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002713 case ISD::CALLSEQ_START: return "callseq_start";
2714 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002715
2716 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002717 case ISD::LOAD: return "load";
2718 case ISD::STORE: return "store";
2719 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002720 case ISD::VAARG: return "vaarg";
2721 case ISD::VACOPY: return "vacopy";
2722 case ISD::VAEND: return "vaend";
2723 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002724 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002725 case ISD::EXTRACT_ELEMENT: return "extract_element";
2726 case ISD::BUILD_PAIR: return "build_pair";
2727 case ISD::STACKSAVE: return "stacksave";
2728 case ISD::STACKRESTORE: return "stackrestore";
2729
2730 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002731 case ISD::MEMSET: return "memset";
2732 case ISD::MEMCPY: return "memcpy";
2733 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002734
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002735 // Bit manipulation
2736 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002737 case ISD::CTPOP: return "ctpop";
2738 case ISD::CTTZ: return "cttz";
2739 case ISD::CTLZ: return "ctlz";
2740
Chris Lattner36ce6912005-11-29 06:21:05 +00002741 // Debug info
2742 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002743 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002744 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002745
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002746 case ISD::CONDCODE:
2747 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002748 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002749 case ISD::SETOEQ: return "setoeq";
2750 case ISD::SETOGT: return "setogt";
2751 case ISD::SETOGE: return "setoge";
2752 case ISD::SETOLT: return "setolt";
2753 case ISD::SETOLE: return "setole";
2754 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002755
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002756 case ISD::SETO: return "seto";
2757 case ISD::SETUO: return "setuo";
2758 case ISD::SETUEQ: return "setue";
2759 case ISD::SETUGT: return "setugt";
2760 case ISD::SETUGE: return "setuge";
2761 case ISD::SETULT: return "setult";
2762 case ISD::SETULE: return "setule";
2763 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002764
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002765 case ISD::SETEQ: return "seteq";
2766 case ISD::SETGT: return "setgt";
2767 case ISD::SETGE: return "setge";
2768 case ISD::SETLT: return "setlt";
2769 case ISD::SETLE: return "setle";
2770 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002771 }
2772 }
2773}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002774
Evan Cheng2caccca2006-10-17 21:14:32 +00002775const char *SDNode::getAddressingModeName(ISD::MemOpAddrMode AM) {
2776 switch (AM) {
2777 default:
2778 return "";
2779 case ISD::PRE_INC:
2780 return "<pre-inc>";
2781 case ISD::PRE_DEC:
2782 return "<pre-dec>";
2783 case ISD::POST_INC:
2784 return "<post-inc>";
2785 case ISD::POST_DEC:
2786 return "<post-dec>";
2787 }
2788}
2789
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002790void SDNode::dump() const { dump(0); }
2791void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002792 std::cerr << (void*)this << ": ";
2793
2794 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2795 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002796 if (getValueType(i) == MVT::Other)
2797 std::cerr << "ch";
2798 else
2799 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002800 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002801 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002802
2803 std::cerr << " ";
2804 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2805 if (i) std::cerr << ", ";
2806 std::cerr << (void*)getOperand(i).Val;
2807 if (unsigned RN = getOperand(i).ResNo)
2808 std::cerr << ":" << RN;
2809 }
2810
2811 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2812 std::cerr << "<" << CSDN->getValue() << ">";
2813 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2814 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002815 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002816 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002817 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002818 std::cerr << "<";
2819 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002820 if (offset > 0)
2821 std::cerr << " + " << offset;
2822 else
2823 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002824 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825 std::cerr << "<" << FIDN->getIndex() << ">";
2826 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002827 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002828 if (CP->isMachineConstantPoolEntry())
2829 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2830 else
2831 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002832 if (offset > 0)
2833 std::cerr << " + " << offset;
2834 else
2835 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002836 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002837 std::cerr << "<";
2838 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2839 if (LBB)
2840 std::cerr << LBB->getName() << " ";
2841 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002842 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002843 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002844 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2845 } else {
2846 std::cerr << " #" << R->getReg();
2847 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002848 } else if (const ExternalSymbolSDNode *ES =
2849 dyn_cast<ExternalSymbolSDNode>(this)) {
2850 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002851 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2852 if (M->getValue())
2853 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2854 else
2855 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002856 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2857 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002858 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2859 bool doExt = true;
2860 switch (LD->getExtensionType()) {
2861 default: doExt = false; break;
2862 case ISD::EXTLOAD:
2863 std::cerr << " <anyext ";
2864 break;
2865 case ISD::SEXTLOAD:
2866 std::cerr << " <sext ";
2867 break;
2868 case ISD::ZEXTLOAD:
2869 std::cerr << " <zext ";
2870 break;
2871 }
2872 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +00002873 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002874
Evan Cheng2caccca2006-10-17 21:14:32 +00002875 const char *AM = getAddressingModeName(LD->getAddressingMode());
2876 if (AM != "")
2877 std::cerr << " " << AM;
2878 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
2879 if (ST->isTruncatingStore())
2880 std::cerr << " <trunc "
2881 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
2882
2883 const char *AM = getAddressingModeName(ST->getAddressingMode());
2884 if (AM != "")
2885 std::cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002886 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002887}
2888
Chris Lattnerde202b32005-11-09 23:47:37 +00002889static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002890 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2891 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002892 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002893 else
2894 std::cerr << "\n" << std::string(indent+2, ' ')
2895 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002896
Chris Lattnerea946cd2005-01-09 20:38:33 +00002897
2898 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002899 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002900}
2901
Chris Lattnerc3aae252005-01-07 07:46:32 +00002902void SelectionDAG::dump() const {
2903 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002904 std::vector<const SDNode*> Nodes;
2905 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2906 I != E; ++I)
2907 Nodes.push_back(I);
2908
Chris Lattner49d24712005-01-09 20:26:36 +00002909 std::sort(Nodes.begin(), Nodes.end());
2910
2911 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002912 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002913 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002914 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002915
Jim Laskey26f7fa72006-10-17 19:33:52 +00002916 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002917
Chris Lattnerc3aae252005-01-07 07:46:32 +00002918 std::cerr << "\n\n";
2919}
2920
Evan Chengd6594ae2006-09-12 21:00:35 +00002921const Type *ConstantPoolSDNode::getType() const {
2922 if (isMachineConstantPoolEntry())
2923 return Val.MachineCPVal->getType();
2924 return Val.ConstVal->getType();
2925}