blob: 3322cddaf0add92db8c6a6fcdcc450e421926177 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000023#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000024#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000026#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000029#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000030#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000031#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000032#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000033#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000034#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner0b3e5252006-08-15 19:11:05 +000038/// makeVTList - Return an instance of the SDVTList struct initialized with the
39/// specified members.
40static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
41 SDVTList Res = {VTs, NumVTs};
42 return Res;
43}
44
Jim Laskey58b968b2005-08-17 20:08:02 +000045//===----------------------------------------------------------------------===//
46// ConstantFPSDNode Class
47//===----------------------------------------------------------------------===//
48
49/// isExactlyValue - We don't rely on operator== working on double values, as
50/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
51/// As such, this method can be used to do an exact bit-for-bit comparison of
52/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000053bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000054 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000055}
56
Dale Johannesenf04afdb2007-08-30 00:23:21 +000057bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
58 const APFloat& Val) {
59 // convert modifies in place, so make a copy.
60 APFloat Val2 = APFloat(Val);
61 switch (VT) {
62 default:
63 return false; // These can't be represented as floating point!
64
65 // FIXME rounding mode needs to be more flexible
66 case MVT::f32:
67 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
68 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
69 APFloat::opOK;
70 case MVT::f64:
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 &Val2.getSemantics() == &APFloat::IEEEdouble ||
73 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
74 APFloat::opOK;
75 // TODO: Figure out how to test if we can use a shorter type instead!
76 case MVT::f80:
77 case MVT::f128:
78 case MVT::ppcf128:
79 return true;
80 }
81}
82
Jim Laskey58b968b2005-08-17 20:08:02 +000083//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000084// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000085//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000086
Evan Chenga8df1662006-03-27 06:58:47 +000087/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000088/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000089bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000090 // Look through a bit convert.
91 if (N->getOpcode() == ISD::BIT_CONVERT)
92 N = N->getOperand(0).Val;
93
Evan Chenga8df1662006-03-27 06:58:47 +000094 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000095
96 unsigned i = 0, e = N->getNumOperands();
97
98 // Skip over all of the undef values.
99 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
100 ++i;
101
102 // Do not accept an all-undef vector.
103 if (i == e) return false;
104
105 // Do not accept build_vectors that aren't all constants or which have non-~0
106 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000107 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000108 if (isa<ConstantSDNode>(NotZero)) {
109 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
110 return false;
111 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000112 MVT::ValueType VT = NotZero.getValueType();
113 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000114 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
115 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000116 return false;
117 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000118 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
119 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000120 (uint32_t)-1)
121 return false;
122 }
Evan Chenga8df1662006-03-27 06:58:47 +0000123 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000173}
174
Chris Lattnerc3aae252005-01-07 07:46:32 +0000175/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
176/// when given the operation for (X op Y).
177ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
178 // To perform this operation, we just need to swap the L and G bits of the
179 // operation.
180 unsigned OldL = (Operation >> 2) & 1;
181 unsigned OldG = (Operation >> 1) & 1;
182 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
183 (OldL << 1) | // New G bit
184 (OldG << 2)); // New L bit.
185}
186
187/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
188/// 'op' is a valid SetCC operation.
189ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
190 unsigned Operation = Op;
191 if (isInteger)
192 Operation ^= 7; // Flip L, G, E bits, but not U.
193 else
194 Operation ^= 15; // Flip all of the condition bits.
195 if (Operation > ISD::SETTRUE2)
196 Operation &= ~8; // Don't let N and U bits get set.
197 return ISD::CondCode(Operation);
198}
199
200
201/// isSignedOp - For an integer comparison, return 1 if the comparison is a
202/// signed operation and 2 if the result is an unsigned comparison. Return zero
203/// if the operation does not depend on the sign of the input (setne and seteq).
204static int isSignedOp(ISD::CondCode Opcode) {
205 switch (Opcode) {
206 default: assert(0 && "Illegal integer setcc operation!");
207 case ISD::SETEQ:
208 case ISD::SETNE: return 0;
209 case ISD::SETLT:
210 case ISD::SETLE:
211 case ISD::SETGT:
212 case ISD::SETGE: return 1;
213 case ISD::SETULT:
214 case ISD::SETULE:
215 case ISD::SETUGT:
216 case ISD::SETUGE: return 2;
217 }
218}
219
220/// getSetCCOrOperation - Return the result of a logical OR between different
221/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
222/// returns SETCC_INVALID if it is not possible to represent the resultant
223/// comparison.
224ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
225 bool isInteger) {
226 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
227 // Cannot fold a signed integer setcc with an unsigned integer setcc.
228 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000229
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232 // If the N and U bits get set then the resultant comparison DOES suddenly
233 // care about orderedness, and is true when ordered.
234 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000235 Op &= ~16; // Clear the U bit if the N bit is set.
236
237 // Canonicalize illegal integer setcc's.
238 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
239 Op = ISD::SETNE;
240
Chris Lattnerc3aae252005-01-07 07:46:32 +0000241 return ISD::CondCode(Op);
242}
243
244/// getSetCCAndOperation - Return the result of a logical AND between different
245/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
246/// function returns zero if it is not possible to represent the resultant
247/// comparison.
248ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
249 bool isInteger) {
250 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
251 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000253
254 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000255 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
256
257 // Canonicalize illegal integer setcc's.
258 if (isInteger) {
259 switch (Result) {
260 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000261 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
262 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
263 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
264 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000265 }
266 }
267
268 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269}
270
Chris Lattnerb48da392005-01-23 04:39:44 +0000271const TargetMachine &SelectionDAG::getTarget() const {
272 return TLI.getTargetMachine();
273}
274
Jim Laskey58b968b2005-08-17 20:08:02 +0000275//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000276// SDNode Profile Support
277//===----------------------------------------------------------------------===//
278
Jim Laskeydef69b92006-10-27 23:52:51 +0000279/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
280///
Jim Laskey583bd472006-10-27 23:46:08 +0000281static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
282 ID.AddInteger(OpC);
283}
284
285/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
286/// solely with their pointer.
287void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
288 ID.AddPointer(VTList.VTs);
289}
290
Jim Laskeydef69b92006-10-27 23:52:51 +0000291/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
292///
Jim Laskey583bd472006-10-27 23:46:08 +0000293static void AddNodeIDOperands(FoldingSetNodeID &ID,
294 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000295 for (; NumOps; --NumOps, ++Ops) {
296 ID.AddPointer(Ops->Val);
297 ID.AddInteger(Ops->ResNo);
298 }
Jim Laskey583bd472006-10-27 23:46:08 +0000299}
300
Jim Laskey583bd472006-10-27 23:46:08 +0000301static void AddNodeIDNode(FoldingSetNodeID &ID,
302 unsigned short OpC, SDVTList VTList,
303 const SDOperand *OpList, unsigned N) {
304 AddNodeIDOpcode(ID, OpC);
305 AddNodeIDValueTypes(ID, VTList);
306 AddNodeIDOperands(ID, OpList, N);
307}
308
Jim Laskeydef69b92006-10-27 23:52:51 +0000309/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
310/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000311static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
312 AddNodeIDOpcode(ID, N->getOpcode());
313 // Add the return value info.
314 AddNodeIDValueTypes(ID, N->getVTList());
315 // Add the operand info.
316 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
317
318 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000319 switch (N->getOpcode()) {
320 default: break; // Normal nodes don't need extra info.
321 case ISD::TargetConstant:
322 case ISD::Constant:
323 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
324 break;
325 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000326 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000327 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000328 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000329 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000330 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000331 case ISD::GlobalAddress:
332 case ISD::TargetGlobalTLSAddress:
333 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000334 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
335 ID.AddPointer(GA->getGlobal());
336 ID.AddInteger(GA->getOffset());
337 break;
338 }
339 case ISD::BasicBlock:
340 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
341 break;
342 case ISD::Register:
343 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
344 break;
345 case ISD::SRCVALUE: {
346 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
347 ID.AddPointer(SV->getValue());
348 ID.AddInteger(SV->getOffset());
349 break;
350 }
351 case ISD::FrameIndex:
352 case ISD::TargetFrameIndex:
353 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
354 break;
355 case ISD::JumpTable:
356 case ISD::TargetJumpTable:
357 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
358 break;
359 case ISD::ConstantPool:
360 case ISD::TargetConstantPool: {
361 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
362 ID.AddInteger(CP->getAlignment());
363 ID.AddInteger(CP->getOffset());
364 if (CP->isMachineConstantPoolEntry())
365 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
366 else
367 ID.AddPointer(CP->getConstVal());
368 break;
369 }
370 case ISD::LOAD: {
371 LoadSDNode *LD = cast<LoadSDNode>(N);
372 ID.AddInteger(LD->getAddressingMode());
373 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000374 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 ID.AddPointer(LD->getSrcValue());
376 ID.AddInteger(LD->getSrcValueOffset());
377 ID.AddInteger(LD->getAlignment());
378 ID.AddInteger(LD->isVolatile());
379 break;
380 }
381 case ISD::STORE: {
382 StoreSDNode *ST = cast<StoreSDNode>(N);
383 ID.AddInteger(ST->getAddressingMode());
384 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000385 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000386 ID.AddPointer(ST->getSrcValue());
387 ID.AddInteger(ST->getSrcValueOffset());
388 ID.AddInteger(ST->getAlignment());
389 ID.AddInteger(ST->isVolatile());
390 break;
391 }
Jim Laskey583bd472006-10-27 23:46:08 +0000392 }
393}
394
395//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000396// SelectionDAG Class
397//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000398
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000399/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000400/// SelectionDAG.
401void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 // Create a dummy node (which is not added to allnodes), that adds a reference
403 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000404 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000405
Chris Lattner190a4182006-08-04 17:45:20 +0000406 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000407
Chris Lattner190a4182006-08-04 17:45:20 +0000408 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000409 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000410 if (I->use_empty())
411 DeadNodes.push_back(I);
412
413 // Process the worklist, deleting the nodes and adding their uses to the
414 // worklist.
415 while (!DeadNodes.empty()) {
416 SDNode *N = DeadNodes.back();
417 DeadNodes.pop_back();
418
419 // Take the node out of the appropriate CSE map.
420 RemoveNodeFromCSEMaps(N);
421
422 // Next, brutally remove the operand list. This is safe to do, as there are
423 // no cycles in the graph.
424 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
425 SDNode *Operand = I->Val;
426 Operand->removeUser(N);
427
428 // Now that we removed this operand, see if there are no uses of it left.
429 if (Operand->use_empty())
430 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000431 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000432 if (N->OperandsNeedDelete)
433 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000434 N->OperandList = 0;
435 N->NumOperands = 0;
436
437 // Finally, remove N itself.
438 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000439 }
440
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000442 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000443}
444
Evan Cheng130a6472006-10-12 20:34:05 +0000445void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
446 SmallVector<SDNode*, 16> DeadNodes;
447 DeadNodes.push_back(N);
448
449 // Process the worklist, deleting the nodes and adding their uses to the
450 // worklist.
451 while (!DeadNodes.empty()) {
452 SDNode *N = DeadNodes.back();
453 DeadNodes.pop_back();
454
455 // Take the node out of the appropriate CSE map.
456 RemoveNodeFromCSEMaps(N);
457
458 // Next, brutally remove the operand list. This is safe to do, as there are
459 // no cycles in the graph.
460 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
461 SDNode *Operand = I->Val;
462 Operand->removeUser(N);
463
464 // Now that we removed this operand, see if there are no uses of it left.
465 if (Operand->use_empty())
466 DeadNodes.push_back(Operand);
467 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000468 if (N->OperandsNeedDelete)
469 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000470 N->OperandList = 0;
471 N->NumOperands = 0;
472
473 // Finally, remove N itself.
474 Deleted.push_back(N);
475 AllNodes.erase(N);
476 }
477}
478
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000479void SelectionDAG::DeleteNode(SDNode *N) {
480 assert(N->use_empty() && "Cannot delete a node that is not dead!");
481
482 // First take this out of the appropriate CSE map.
483 RemoveNodeFromCSEMaps(N);
484
Chris Lattner1e111c72005-09-07 05:37:01 +0000485 // Finally, remove uses due to operands of this node, remove from the
486 // AllNodes list, and delete the node.
487 DeleteNodeNotInCSEMaps(N);
488}
489
490void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
491
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000492 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000493 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000494
495 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000496 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
497 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000498 if (N->OperandsNeedDelete)
499 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000500 N->OperandList = 0;
501 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000502
503 delete N;
504}
505
Chris Lattner149c58c2005-08-16 18:17:10 +0000506/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
507/// correspond to it. This is useful when we're about to delete or repurpose
508/// the node. We don't want future request for structurally identical nodes
509/// to return N anymore.
510void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000511 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000512 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000513 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000514 case ISD::STRING:
515 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
516 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000517 case ISD::CONDCODE:
518 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
519 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000520 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000521 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
522 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000523 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000524 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000525 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000526 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000527 Erased =
528 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000529 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000530 case ISD::VALUETYPE: {
531 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
532 if (MVT::isExtendedVT(VT)) {
533 Erased = ExtendedValueTypeNodes.erase(VT);
534 } else {
535 Erased = ValueTypeNodes[VT] != 0;
536 ValueTypeNodes[VT] = 0;
537 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000538 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000539 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000540 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000541 // Remove it from the CSE Map.
542 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000543 break;
544 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000545#ifndef NDEBUG
546 // Verify that the node was actually in one of the CSE maps, unless it has a
547 // flag result (which cannot be CSE'd) or is one of the special cases that are
548 // not subject to CSE.
549 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000550 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000551 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000552 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000553 assert(0 && "Node is not in map!");
554 }
555#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556}
557
Chris Lattner8b8749f2005-08-17 19:00:20 +0000558/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
559/// has been taken out and modified in some way. If the specified node already
560/// exists in the CSE maps, do not modify the maps, but return the existing node
561/// instead. If it doesn't exist, add it and return null.
562///
563SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
564 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000565 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000566 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000567
Chris Lattner9f8cc692005-12-19 22:21:21 +0000568 // Check that remaining values produced are not flags.
569 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
570 if (N->getValueType(i) == MVT::Flag)
571 return 0; // Never CSE anything that produces a flag.
572
Chris Lattnera5682852006-08-07 23:03:03 +0000573 SDNode *New = CSEMap.GetOrInsertNode(N);
574 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000575 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000576}
577
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000578/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
579/// were replaced with those specified. If this node is never memoized,
580/// return null, otherwise return a pointer to the slot it would take. If a
581/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000582SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
583 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000584 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000585 return 0; // Never add these nodes.
586
587 // Check that remaining values produced are not flags.
588 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
589 if (N->getValueType(i) == MVT::Flag)
590 return 0; // Never CSE anything that produces a flag.
591
Chris Lattner63e3f142007-02-04 07:28:00 +0000592 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000593 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000594 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000595 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000596}
597
598/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
599/// were replaced with those specified. If this node is never memoized,
600/// return null, otherwise return a pointer to the slot it would take. If a
601/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000602SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
603 SDOperand Op1, SDOperand Op2,
604 void *&InsertPos) {
605 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
606 return 0; // Never add these nodes.
607
608 // Check that remaining values produced are not flags.
609 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
610 if (N->getValueType(i) == MVT::Flag)
611 return 0; // Never CSE anything that produces a flag.
612
Chris Lattner63e3f142007-02-04 07:28:00 +0000613 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000614 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000615 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000616 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
617}
618
619
620/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
621/// were replaced with those specified. If this node is never memoized,
622/// return null, otherwise return a pointer to the slot it would take. If a
623/// node already exists with these operands, the slot will be non-null.
624SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000625 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000626 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000627 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000628 return 0; // Never add these nodes.
629
630 // Check that remaining values produced are not flags.
631 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
632 if (N->getValueType(i) == MVT::Flag)
633 return 0; // Never CSE anything that produces a flag.
634
Jim Laskey583bd472006-10-27 23:46:08 +0000635 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000636 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000637
Evan Cheng9629aba2006-10-11 01:47:58 +0000638 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
639 ID.AddInteger(LD->getAddressingMode());
640 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000641 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000642 ID.AddPointer(LD->getSrcValue());
643 ID.AddInteger(LD->getSrcValueOffset());
644 ID.AddInteger(LD->getAlignment());
645 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000646 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
647 ID.AddInteger(ST->getAddressingMode());
648 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000649 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000650 ID.AddPointer(ST->getSrcValue());
651 ID.AddInteger(ST->getSrcValueOffset());
652 ID.AddInteger(ST->getAlignment());
653 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000654 }
Jim Laskey583bd472006-10-27 23:46:08 +0000655
Chris Lattnera5682852006-08-07 23:03:03 +0000656 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000657}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000658
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000659
Chris Lattner78ec3112003-08-11 14:57:33 +0000660SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000661 while (!AllNodes.empty()) {
662 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000663 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000664 if (N->OperandsNeedDelete)
665 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000666 N->OperandList = 0;
667 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000668 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000669 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000670}
671
Chris Lattner0f2287b2005-04-13 02:38:18 +0000672SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000673 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000674 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000675 return getNode(ISD::AND, Op.getValueType(), Op,
676 getConstant(Imm, Op.getValueType()));
677}
678
Chris Lattner36ce6912005-11-29 06:21:05 +0000679SDOperand SelectionDAG::getString(const std::string &Val) {
680 StringSDNode *&N = StringNodes[Val];
681 if (!N) {
682 N = new StringSDNode(Val);
683 AllNodes.push_back(N);
684 }
685 return SDOperand(N, 0);
686}
687
Chris Lattner61b09412006-08-11 21:01:22 +0000688SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000689 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000690
691 MVT::ValueType EltVT =
692 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000693
Chris Lattner61b09412006-08-11 21:01:22 +0000694 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000695 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000696
697 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000698 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000699 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000700 ID.AddInteger(Val);
701 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000702 SDNode *N = NULL;
703 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
704 if (!MVT::isVector(VT))
705 return SDOperand(N, 0);
706 if (!N) {
707 N = new ConstantSDNode(isT, Val, EltVT);
708 CSEMap.InsertNode(N, IP);
709 AllNodes.push_back(N);
710 }
711
712 SDOperand Result(N, 0);
713 if (MVT::isVector(VT)) {
714 SmallVector<SDOperand, 8> Ops;
715 Ops.assign(MVT::getVectorNumElements(VT), Result);
716 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
717 }
718 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000719}
720
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000721SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000722 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000723 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000724
Dan Gohman7f321562007-06-25 16:23:39 +0000725 MVT::ValueType EltVT =
726 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727
Chris Lattnerd8658612005-02-17 20:17:32 +0000728 // Do the map lookup using the actual bit pattern for the floating point
729 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
730 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000731 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000732 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000733 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000734 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000735 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000736 SDNode *N = NULL;
737 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
738 if (!MVT::isVector(VT))
739 return SDOperand(N, 0);
740 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000741 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000742 CSEMap.InsertNode(N, IP);
743 AllNodes.push_back(N);
744 }
745
Dan Gohman7f321562007-06-25 16:23:39 +0000746 SDOperand Result(N, 0);
747 if (MVT::isVector(VT)) {
748 SmallVector<SDOperand, 8> Ops;
749 Ops.assign(MVT::getVectorNumElements(VT), Result);
750 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
751 }
752 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000753}
754
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000755SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
756 bool isTarget) {
757 MVT::ValueType EltVT =
758 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
759 if (EltVT==MVT::f32)
760 return getConstantFP(APFloat((float)Val), VT, isTarget);
761 else
762 return getConstantFP(APFloat(Val), VT, isTarget);
763}
764
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000766 MVT::ValueType VT, int Offset,
767 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000768 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
769 unsigned Opc;
770 if (GVar && GVar->isThreadLocal())
771 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
772 else
773 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000774 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000775 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000776 ID.AddPointer(GV);
777 ID.AddInteger(Offset);
778 void *IP = 0;
779 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
780 return SDOperand(E, 0);
781 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
782 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783 AllNodes.push_back(N);
784 return SDOperand(N, 0);
785}
786
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000787SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
788 bool isTarget) {
789 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000790 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000791 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000792 ID.AddInteger(FI);
793 void *IP = 0;
794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
795 return SDOperand(E, 0);
796 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
797 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000798 AllNodes.push_back(N);
799 return SDOperand(N, 0);
800}
801
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000802SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
803 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000804 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000805 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806 ID.AddInteger(JTI);
807 void *IP = 0;
808 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
809 return SDOperand(E, 0);
810 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
811 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
814}
815
Evan Chengb8973bd2006-01-31 22:23:14 +0000816SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000817 unsigned Alignment, int Offset,
818 bool isTarget) {
819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000820 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000822 ID.AddInteger(Alignment);
823 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000824 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000825 void *IP = 0;
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
829 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
832}
833
Chris Lattnerc3aae252005-01-07 07:46:32 +0000834
Evan Chengd6594ae2006-09-12 21:00:35 +0000835SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
836 MVT::ValueType VT,
837 unsigned Alignment, int Offset,
838 bool isTarget) {
839 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000840 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000841 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000842 ID.AddInteger(Alignment);
843 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000844 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000845 void *IP = 0;
846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
847 return SDOperand(E, 0);
848 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
849 CSEMap.InsertNode(N, IP);
850 AllNodes.push_back(N);
851 return SDOperand(N, 0);
852}
853
854
Chris Lattnerc3aae252005-01-07 07:46:32 +0000855SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000856 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000857 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000858 ID.AddPointer(MBB);
859 void *IP = 0;
860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
861 return SDOperand(E, 0);
862 SDNode *N = new BasicBlockSDNode(MBB);
863 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
866}
867
Chris Lattner15e4b012005-07-10 00:07:11 +0000868SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000869 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000870 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000871
Duncan Sandsf411b832007-10-17 13:49:58 +0000872 SDNode *&N = MVT::isExtendedVT(VT) ?
873 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
874
875 if (N) return SDOperand(N, 0);
876 N = new VTSDNode(VT);
877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000879}
880
Chris Lattnerc3aae252005-01-07 07:46:32 +0000881SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
882 SDNode *&N = ExternalSymbols[Sym];
883 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000884 N = new ExternalSymbolSDNode(false, Sym, VT);
885 AllNodes.push_back(N);
886 return SDOperand(N, 0);
887}
888
Chris Lattner809ec112006-01-28 10:09:25 +0000889SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
890 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000891 SDNode *&N = TargetExternalSymbols[Sym];
892 if (N) return SDOperand(N, 0);
893 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000894 AllNodes.push_back(N);
895 return SDOperand(N, 0);
896}
897
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000898SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
899 if ((unsigned)Cond >= CondCodeNodes.size())
900 CondCodeNodes.resize(Cond+1);
901
Chris Lattner079a27a2005-08-09 20:40:02 +0000902 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000903 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000904 AllNodes.push_back(CondCodeNodes[Cond]);
905 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000906 return SDOperand(CondCodeNodes[Cond], 0);
907}
908
Chris Lattner0fdd7682005-08-30 22:38:38 +0000909SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000910 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000911 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000912 ID.AddInteger(RegNo);
913 void *IP = 0;
914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
915 return SDOperand(E, 0);
916 SDNode *N = new RegisterSDNode(RegNo, VT);
917 CSEMap.InsertNode(N, IP);
918 AllNodes.push_back(N);
919 return SDOperand(N, 0);
920}
921
922SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
923 assert((!V || isa<PointerType>(V->getType())) &&
924 "SrcValue is not a pointer?");
925
Jim Laskey583bd472006-10-27 23:46:08 +0000926 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000927 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000928 ID.AddPointer(V);
929 ID.AddInteger(Offset);
930 void *IP = 0;
931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
932 return SDOperand(E, 0);
933 SDNode *N = new SrcValueSDNode(V, Offset);
934 CSEMap.InsertNode(N, IP);
935 AllNodes.push_back(N);
936 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000937}
938
Chris Lattner37ce9df2007-10-15 17:47:20 +0000939/// CreateStackTemporary - Create a stack temporary, suitable for holding the
940/// specified value type.
941SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
942 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
943 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
944 const Type *Ty = MVT::getTypeForValueType(VT);
945 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
946 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
947 return getFrameIndex(FrameIdx, TLI.getPointerTy());
948}
949
950
Chris Lattner51dabfb2006-10-14 00:41:01 +0000951SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
952 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953 // These setcc operations always fold.
954 switch (Cond) {
955 default: break;
956 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000957 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000959 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000960
961 case ISD::SETOEQ:
962 case ISD::SETOGT:
963 case ISD::SETOGE:
964 case ISD::SETOLT:
965 case ISD::SETOLE:
966 case ISD::SETONE:
967 case ISD::SETO:
968 case ISD::SETUO:
969 case ISD::SETUEQ:
970 case ISD::SETUNE:
971 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
972 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000974
Chris Lattner67255a12005-04-07 18:14:58 +0000975 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
976 uint64_t C2 = N2C->getValue();
977 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
978 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000979
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980 // Sign extend the operands if required
981 if (ISD::isSignedIntSetCC(Cond)) {
982 C1 = N1C->getSignExtended();
983 C2 = N2C->getSignExtended();
984 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000985
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 switch (Cond) {
987 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000988 case ISD::SETEQ: return getConstant(C1 == C2, VT);
989 case ISD::SETNE: return getConstant(C1 != C2, VT);
990 case ISD::SETULT: return getConstant(C1 < C2, VT);
991 case ISD::SETUGT: return getConstant(C1 > C2, VT);
992 case ISD::SETULE: return getConstant(C1 <= C2, VT);
993 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
994 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
995 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
996 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
997 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000998 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 }
Chris Lattner67255a12005-04-07 18:14:58 +00001000 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001001 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1002 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001003 // No compile time operations on this type yet.
1004 if (N1C->getValueType(0) == MVT::ppcf128)
1005 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001006
1007 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001009 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001010 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1011 return getNode(ISD::UNDEF, VT);
1012 // fall through
1013 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1014 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1015 return getNode(ISD::UNDEF, VT);
1016 // fall through
1017 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001018 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001019 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1020 return getNode(ISD::UNDEF, VT);
1021 // fall through
1022 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1023 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1024 return getNode(ISD::UNDEF, VT);
1025 // fall through
1026 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1027 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1028 return getNode(ISD::UNDEF, VT);
1029 // fall through
1030 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001031 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001032 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1033 return getNode(ISD::UNDEF, VT);
1034 // fall through
1035 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001036 R==APFloat::cmpEqual, VT);
1037 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1038 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1039 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1040 R==APFloat::cmpEqual, VT);
1041 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1042 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1043 R==APFloat::cmpLessThan, VT);
1044 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1045 R==APFloat::cmpUnordered, VT);
1046 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1047 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 }
1049 } else {
1050 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001051 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001053
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001054 // Could not fold it.
1055 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056}
1057
Dan Gohmanea859be2007-06-22 14:59:07 +00001058/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1059/// this predicate to simplify operations downstream. Mask is known to be zero
1060/// for bits that V cannot have.
1061bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1062 unsigned Depth) const {
1063 // The masks are not wide enough to represent this type! Should use APInt.
1064 if (Op.getValueType() == MVT::i128)
1065 return false;
1066
1067 uint64_t KnownZero, KnownOne;
1068 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1069 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1070 return (KnownZero & Mask) == Mask;
1071}
1072
1073/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1074/// known to be either zero or one and return them in the KnownZero/KnownOne
1075/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1076/// processing.
1077void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1078 uint64_t &KnownZero, uint64_t &KnownOne,
1079 unsigned Depth) const {
1080 KnownZero = KnownOne = 0; // Don't know anything.
1081 if (Depth == 6 || Mask == 0)
1082 return; // Limit search depth.
1083
1084 // The masks are not wide enough to represent this type! Should use APInt.
1085 if (Op.getValueType() == MVT::i128)
1086 return;
1087
1088 uint64_t KnownZero2, KnownOne2;
1089
1090 switch (Op.getOpcode()) {
1091 case ISD::Constant:
1092 // We know all of the bits for a constant!
1093 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1094 KnownZero = ~KnownOne & Mask;
1095 return;
1096 case ISD::AND:
1097 // If either the LHS or the RHS are Zero, the result is zero.
1098 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1099 Mask &= ~KnownZero;
1100 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1101 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1102 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1103
1104 // Output known-1 bits are only known if set in both the LHS & RHS.
1105 KnownOne &= KnownOne2;
1106 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1107 KnownZero |= KnownZero2;
1108 return;
1109 case ISD::OR:
1110 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1111 Mask &= ~KnownOne;
1112 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1113 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1114 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1115
1116 // Output known-0 bits are only known if clear in both the LHS & RHS.
1117 KnownZero &= KnownZero2;
1118 // Output known-1 are known to be set if set in either the LHS | RHS.
1119 KnownOne |= KnownOne2;
1120 return;
1121 case ISD::XOR: {
1122 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1123 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1124 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1125 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1126
1127 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1128 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1129 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1130 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1131 KnownZero = KnownZeroOut;
1132 return;
1133 }
1134 case ISD::SELECT:
1135 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1136 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1137 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1138 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1139
1140 // Only known if known in both the LHS and RHS.
1141 KnownOne &= KnownOne2;
1142 KnownZero &= KnownZero2;
1143 return;
1144 case ISD::SELECT_CC:
1145 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1146 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1147 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1148 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1149
1150 // Only known if known in both the LHS and RHS.
1151 KnownOne &= KnownOne2;
1152 KnownZero &= KnownZero2;
1153 return;
1154 case ISD::SETCC:
1155 // If we know the result of a setcc has the top bits zero, use this info.
1156 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1157 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1158 return;
1159 case ISD::SHL:
1160 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1161 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1162 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1163 KnownZero, KnownOne, Depth+1);
1164 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1165 KnownZero <<= SA->getValue();
1166 KnownOne <<= SA->getValue();
1167 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1168 }
1169 return;
1170 case ISD::SRL:
1171 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1172 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1173 MVT::ValueType VT = Op.getValueType();
1174 unsigned ShAmt = SA->getValue();
1175
1176 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1177 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1178 KnownZero, KnownOne, Depth+1);
1179 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1180 KnownZero &= TypeMask;
1181 KnownOne &= TypeMask;
1182 KnownZero >>= ShAmt;
1183 KnownOne >>= ShAmt;
1184
1185 uint64_t HighBits = (1ULL << ShAmt)-1;
1186 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1187 KnownZero |= HighBits; // High bits known zero.
1188 }
1189 return;
1190 case ISD::SRA:
1191 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1192 MVT::ValueType VT = Op.getValueType();
1193 unsigned ShAmt = SA->getValue();
1194
1195 // Compute the new bits that are at the top now.
1196 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1197
1198 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1199 // If any of the demanded bits are produced by the sign extension, we also
1200 // demand the input sign bit.
1201 uint64_t HighBits = (1ULL << ShAmt)-1;
1202 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1203 if (HighBits & Mask)
1204 InDemandedMask |= MVT::getIntVTSignBit(VT);
1205
1206 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1207 Depth+1);
1208 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1209 KnownZero &= TypeMask;
1210 KnownOne &= TypeMask;
1211 KnownZero >>= ShAmt;
1212 KnownOne >>= ShAmt;
1213
1214 // Handle the sign bits.
1215 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1216 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1217
1218 if (KnownZero & SignBit) {
1219 KnownZero |= HighBits; // New bits are known zero.
1220 } else if (KnownOne & SignBit) {
1221 KnownOne |= HighBits; // New bits are known one.
1222 }
1223 }
1224 return;
1225 case ISD::SIGN_EXTEND_INREG: {
1226 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1227
1228 // Sign extension. Compute the demanded bits in the result that are not
1229 // present in the input.
1230 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1231
1232 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1233 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1234
1235 // If the sign extended bits are demanded, we know that the sign
1236 // bit is demanded.
1237 if (NewBits)
1238 InputDemandedBits |= InSignBit;
1239
1240 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1241 KnownZero, KnownOne, Depth+1);
1242 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1243
1244 // If the sign bit of the input is known set or clear, then we know the
1245 // top bits of the result.
1246 if (KnownZero & InSignBit) { // Input sign bit known clear
1247 KnownZero |= NewBits;
1248 KnownOne &= ~NewBits;
1249 } else if (KnownOne & InSignBit) { // Input sign bit known set
1250 KnownOne |= NewBits;
1251 KnownZero &= ~NewBits;
1252 } else { // Input sign bit unknown
1253 KnownZero &= ~NewBits;
1254 KnownOne &= ~NewBits;
1255 }
1256 return;
1257 }
1258 case ISD::CTTZ:
1259 case ISD::CTLZ:
1260 case ISD::CTPOP: {
1261 MVT::ValueType VT = Op.getValueType();
1262 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1263 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1264 KnownOne = 0;
1265 return;
1266 }
1267 case ISD::LOAD: {
1268 if (ISD::isZEXTLoad(Op.Val)) {
1269 LoadSDNode *LD = cast<LoadSDNode>(Op);
1270 MVT::ValueType VT = LD->getLoadedVT();
1271 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1272 }
1273 return;
1274 }
1275 case ISD::ZERO_EXTEND: {
1276 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1277 uint64_t NewBits = (~InMask) & Mask;
1278 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1279 KnownOne, Depth+1);
1280 KnownZero |= NewBits & Mask;
1281 KnownOne &= ~NewBits;
1282 return;
1283 }
1284 case ISD::SIGN_EXTEND: {
1285 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1286 unsigned InBits = MVT::getSizeInBits(InVT);
1287 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1288 uint64_t InSignBit = 1ULL << (InBits-1);
1289 uint64_t NewBits = (~InMask) & Mask;
1290 uint64_t InDemandedBits = Mask & InMask;
1291
1292 // If any of the sign extended bits are demanded, we know that the sign
1293 // bit is demanded.
1294 if (NewBits & Mask)
1295 InDemandedBits |= InSignBit;
1296
1297 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1298 KnownOne, Depth+1);
1299 // If the sign bit is known zero or one, the top bits match.
1300 if (KnownZero & InSignBit) {
1301 KnownZero |= NewBits;
1302 KnownOne &= ~NewBits;
1303 } else if (KnownOne & InSignBit) {
1304 KnownOne |= NewBits;
1305 KnownZero &= ~NewBits;
1306 } else { // Otherwise, top bits aren't known.
1307 KnownOne &= ~NewBits;
1308 KnownZero &= ~NewBits;
1309 }
1310 return;
1311 }
1312 case ISD::ANY_EXTEND: {
1313 MVT::ValueType VT = Op.getOperand(0).getValueType();
1314 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1315 KnownZero, KnownOne, Depth+1);
1316 return;
1317 }
1318 case ISD::TRUNCATE: {
1319 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1320 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1321 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1322 KnownZero &= OutMask;
1323 KnownOne &= OutMask;
1324 break;
1325 }
1326 case ISD::AssertZext: {
1327 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1328 uint64_t InMask = MVT::getIntVTBitMask(VT);
1329 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1330 KnownOne, Depth+1);
1331 KnownZero |= (~InMask) & Mask;
1332 return;
1333 }
1334 case ISD::ADD: {
1335 // If either the LHS or the RHS are Zero, the result is zero.
1336 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1337 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1338 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1339 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1340
1341 // Output known-0 bits are known if clear or set in both the low clear bits
1342 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1343 // low 3 bits clear.
1344 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1345 CountTrailingZeros_64(~KnownZero2));
1346
1347 KnownZero = (1ULL << KnownZeroOut) - 1;
1348 KnownOne = 0;
1349 return;
1350 }
1351 case ISD::SUB: {
1352 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1353 if (!CLHS) return;
1354
1355 // We know that the top bits of C-X are clear if X contains less bits
1356 // than C (i.e. no wrap-around can happen). For example, 20-X is
1357 // positive if we can prove that X is >= 0 and < 16.
1358 MVT::ValueType VT = CLHS->getValueType(0);
1359 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1360 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1361 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1362 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1363 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1364
1365 // If all of the MaskV bits are known to be zero, then we know the output
1366 // top bits are zero, because we now know that the output is from [0-C].
1367 if ((KnownZero & MaskV) == MaskV) {
1368 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1369 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1370 KnownOne = 0; // No one bits known.
1371 } else {
1372 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1373 }
1374 }
1375 return;
1376 }
1377 default:
1378 // Allow the target to implement this method for its nodes.
1379 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1380 case ISD::INTRINSIC_WO_CHAIN:
1381 case ISD::INTRINSIC_W_CHAIN:
1382 case ISD::INTRINSIC_VOID:
1383 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1384 }
1385 return;
1386 }
1387}
1388
1389/// ComputeNumSignBits - Return the number of times the sign bit of the
1390/// register is replicated into the other bits. We know that at least 1 bit
1391/// is always equal to the sign bit (itself), but other cases can give us
1392/// information. For example, immediately after an "SRA X, 2", we know that
1393/// the top 3 bits are all equal to each other, so we return 3.
1394unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1395 MVT::ValueType VT = Op.getValueType();
1396 assert(MVT::isInteger(VT) && "Invalid VT!");
1397 unsigned VTBits = MVT::getSizeInBits(VT);
1398 unsigned Tmp, Tmp2;
1399
1400 if (Depth == 6)
1401 return 1; // Limit search depth.
1402
1403 switch (Op.getOpcode()) {
1404 default: break;
1405 case ISD::AssertSext:
1406 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1407 return VTBits-Tmp+1;
1408 case ISD::AssertZext:
1409 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1410 return VTBits-Tmp;
1411
1412 case ISD::Constant: {
1413 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1414 // If negative, invert the bits, then look at it.
1415 if (Val & MVT::getIntVTSignBit(VT))
1416 Val = ~Val;
1417
1418 // Shift the bits so they are the leading bits in the int64_t.
1419 Val <<= 64-VTBits;
1420
1421 // Return # leading zeros. We use 'min' here in case Val was zero before
1422 // shifting. We don't want to return '64' as for an i32 "0".
1423 return std::min(VTBits, CountLeadingZeros_64(Val));
1424 }
1425
1426 case ISD::SIGN_EXTEND:
1427 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1428 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1429
1430 case ISD::SIGN_EXTEND_INREG:
1431 // Max of the input and what this extends.
1432 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1433 Tmp = VTBits-Tmp+1;
1434
1435 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1436 return std::max(Tmp, Tmp2);
1437
1438 case ISD::SRA:
1439 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1440 // SRA X, C -> adds C sign bits.
1441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1442 Tmp += C->getValue();
1443 if (Tmp > VTBits) Tmp = VTBits;
1444 }
1445 return Tmp;
1446 case ISD::SHL:
1447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1448 // shl destroys sign bits.
1449 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1450 if (C->getValue() >= VTBits || // Bad shift.
1451 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1452 return Tmp - C->getValue();
1453 }
1454 break;
1455 case ISD::AND:
1456 case ISD::OR:
1457 case ISD::XOR: // NOT is handled here.
1458 // Logical binary ops preserve the number of sign bits.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp == 1) return 1; // Early out.
1461 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1462 return std::min(Tmp, Tmp2);
1463
1464 case ISD::SELECT:
1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1466 if (Tmp == 1) return 1; // Early out.
1467 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1468 return std::min(Tmp, Tmp2);
1469
1470 case ISD::SETCC:
1471 // If setcc returns 0/-1, all bits are sign bits.
1472 if (TLI.getSetCCResultContents() ==
1473 TargetLowering::ZeroOrNegativeOneSetCCResult)
1474 return VTBits;
1475 break;
1476 case ISD::ROTL:
1477 case ISD::ROTR:
1478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1479 unsigned RotAmt = C->getValue() & (VTBits-1);
1480
1481 // Handle rotate right by N like a rotate left by 32-N.
1482 if (Op.getOpcode() == ISD::ROTR)
1483 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1484
1485 // If we aren't rotating out all of the known-in sign bits, return the
1486 // number that are left. This handles rotl(sext(x), 1) for example.
1487 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1488 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1489 }
1490 break;
1491 case ISD::ADD:
1492 // Add can have at most one carry bit. Thus we know that the output
1493 // is, at worst, one more bit than the inputs.
1494 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1495 if (Tmp == 1) return 1; // Early out.
1496
1497 // Special case decrementing a value (ADD X, -1):
1498 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1499 if (CRHS->isAllOnesValue()) {
1500 uint64_t KnownZero, KnownOne;
1501 uint64_t Mask = MVT::getIntVTBitMask(VT);
1502 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1503
1504 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1505 // sign bits set.
1506 if ((KnownZero|1) == Mask)
1507 return VTBits;
1508
1509 // If we are subtracting one from a positive number, there is no carry
1510 // out of the result.
1511 if (KnownZero & MVT::getIntVTSignBit(VT))
1512 return Tmp;
1513 }
1514
1515 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1516 if (Tmp2 == 1) return 1;
1517 return std::min(Tmp, Tmp2)-1;
1518 break;
1519
1520 case ISD::SUB:
1521 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1522 if (Tmp2 == 1) return 1;
1523
1524 // Handle NEG.
1525 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1526 if (CLHS->getValue() == 0) {
1527 uint64_t KnownZero, KnownOne;
1528 uint64_t Mask = MVT::getIntVTBitMask(VT);
1529 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1530 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1531 // sign bits set.
1532 if ((KnownZero|1) == Mask)
1533 return VTBits;
1534
1535 // If the input is known to be positive (the sign bit is known clear),
1536 // the output of the NEG has the same number of sign bits as the input.
1537 if (KnownZero & MVT::getIntVTSignBit(VT))
1538 return Tmp2;
1539
1540 // Otherwise, we treat this like a SUB.
1541 }
1542
1543 // Sub can have at most one carry bit. Thus we know that the output
1544 // is, at worst, one more bit than the inputs.
1545 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1546 if (Tmp == 1) return 1; // Early out.
1547 return std::min(Tmp, Tmp2)-1;
1548 break;
1549 case ISD::TRUNCATE:
1550 // FIXME: it's tricky to do anything useful for this, but it is an important
1551 // case for targets like X86.
1552 break;
1553 }
1554
1555 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1556 if (Op.getOpcode() == ISD::LOAD) {
1557 LoadSDNode *LD = cast<LoadSDNode>(Op);
1558 unsigned ExtType = LD->getExtensionType();
1559 switch (ExtType) {
1560 default: break;
1561 case ISD::SEXTLOAD: // '17' bits known
1562 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1563 return VTBits-Tmp+1;
1564 case ISD::ZEXTLOAD: // '16' bits known
1565 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1566 return VTBits-Tmp;
1567 }
1568 }
1569
1570 // Allow the target to implement this method for its nodes.
1571 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1572 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1573 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1574 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1575 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1576 if (NumBits > 1) return NumBits;
1577 }
1578
1579 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1580 // use this information.
1581 uint64_t KnownZero, KnownOne;
1582 uint64_t Mask = MVT::getIntVTBitMask(VT);
1583 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1584
1585 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1586 if (KnownZero & SignBit) { // SignBit is 0
1587 Mask = KnownZero;
1588 } else if (KnownOne & SignBit) { // SignBit is 1;
1589 Mask = KnownOne;
1590 } else {
1591 // Nothing known.
1592 return 1;
1593 }
1594
1595 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1596 // the number of identical bits in the top of the input value.
1597 Mask ^= ~0ULL;
1598 Mask <<= 64-VTBits;
1599 // Return # leading zeros. We use 'min' here in case Val was zero before
1600 // shifting. We don't want to return '64' as for an i32 "0".
1601 return std::min(VTBits, CountLeadingZeros_64(Mask));
1602}
1603
Chris Lattner51dabfb2006-10-14 00:41:01 +00001604
Chris Lattnerc3aae252005-01-07 07:46:32 +00001605/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001606///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001607SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001608 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001609 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001610 void *IP = 0;
1611 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1612 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001613 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001614 CSEMap.InsertNode(N, IP);
1615
1616 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001617 return SDOperand(N, 0);
1618}
1619
Chris Lattnerc3aae252005-01-07 07:46:32 +00001620SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1621 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001622 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001623 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1625 uint64_t Val = C->getValue();
1626 switch (Opcode) {
1627 default: break;
1628 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001629 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001630 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1631 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001632 case ISD::UINT_TO_FP:
1633 case ISD::SINT_TO_FP: {
1634 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001635 // No compile time operations on this type.
1636 if (VT==MVT::ppcf128)
1637 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001638 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001639 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001640 MVT::getSizeInBits(Operand.getValueType()),
1641 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001642 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001643 return getConstantFP(apf, VT);
1644 }
Chris Lattner94683772005-12-23 05:30:37 +00001645 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001646 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001647 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001648 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001649 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001650 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001651 case ISD::BSWAP:
1652 switch(VT) {
1653 default: assert(0 && "Invalid bswap!"); break;
1654 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1655 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1656 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1657 }
1658 break;
1659 case ISD::CTPOP:
1660 switch(VT) {
1661 default: assert(0 && "Invalid ctpop!"); break;
1662 case MVT::i1: return getConstant(Val != 0, VT);
1663 case MVT::i8:
1664 Tmp1 = (unsigned)Val & 0xFF;
1665 return getConstant(CountPopulation_32(Tmp1), VT);
1666 case MVT::i16:
1667 Tmp1 = (unsigned)Val & 0xFFFF;
1668 return getConstant(CountPopulation_32(Tmp1), VT);
1669 case MVT::i32:
1670 return getConstant(CountPopulation_32((unsigned)Val), VT);
1671 case MVT::i64:
1672 return getConstant(CountPopulation_64(Val), VT);
1673 }
1674 case ISD::CTLZ:
1675 switch(VT) {
1676 default: assert(0 && "Invalid ctlz!"); break;
1677 case MVT::i1: return getConstant(Val == 0, VT);
1678 case MVT::i8:
1679 Tmp1 = (unsigned)Val & 0xFF;
1680 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1681 case MVT::i16:
1682 Tmp1 = (unsigned)Val & 0xFFFF;
1683 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1684 case MVT::i32:
1685 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1686 case MVT::i64:
1687 return getConstant(CountLeadingZeros_64(Val), VT);
1688 }
1689 case ISD::CTTZ:
1690 switch(VT) {
1691 default: assert(0 && "Invalid cttz!"); break;
1692 case MVT::i1: return getConstant(Val == 0, VT);
1693 case MVT::i8:
1694 Tmp1 = (unsigned)Val | 0x100;
1695 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1696 case MVT::i16:
1697 Tmp1 = (unsigned)Val | 0x10000;
1698 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1699 case MVT::i32:
1700 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1701 case MVT::i64:
1702 return getConstant(CountTrailingZeros_64(Val), VT);
1703 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001704 }
1705 }
1706
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001707 // Constant fold unary operations with a floating point constant operand.
1708 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1709 APFloat V = C->getValueAPF(); // make copy
Dale Johannesendb44bf82007-10-16 23:38:29 +00001710 if (VT!=MVT::ppcf128 && Operand.getValueType()!=MVT::ppcf128) {
1711 switch (Opcode) {
1712 case ISD::FNEG:
1713 V.changeSign();
1714 return getConstantFP(V, VT);
1715 case ISD::FABS:
1716 V.clearSign();
1717 return getConstantFP(V, VT);
1718 case ISD::FP_ROUND:
1719 case ISD::FP_EXTEND:
1720 // This can return overflow, underflow, or inexact; we don't care.
1721 // FIXME need to be more flexible about rounding mode.
1722 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1723 VT==MVT::f64 ? APFloat::IEEEdouble :
1724 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1725 VT==MVT::f128 ? APFloat::IEEEquad :
1726 APFloat::Bogus,
1727 APFloat::rmNearestTiesToEven);
1728 return getConstantFP(V, VT);
1729 case ISD::FP_TO_SINT:
1730 case ISD::FP_TO_UINT: {
1731 integerPart x;
1732 assert(integerPartWidth >= 64);
1733 // FIXME need to be more flexible about rounding mode.
1734 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1735 Opcode==ISD::FP_TO_SINT,
1736 APFloat::rmTowardZero);
1737 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1738 break;
1739 return getConstant(x, VT);
1740 }
1741 case ISD::BIT_CONVERT:
1742 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1743 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1744 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1745 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001746 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001747 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001748 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001749 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750
1751 unsigned OpOpcode = Operand.Val->getOpcode();
1752 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001753 case ISD::TokenFactor:
1754 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001755 case ISD::FP_ROUND:
1756 case ISD::FP_EXTEND:
1757 assert(MVT::isFloatingPoint(VT) &&
1758 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1759 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001760 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001761 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1762 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001763 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001764 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1765 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001766 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1767 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1768 break;
1769 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001770 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1771 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001772 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001773 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1774 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001775 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001776 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001777 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001778 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001779 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1780 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001781 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001782 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1783 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001784 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1785 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1786 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1787 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001788 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001789 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1790 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001791 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001792 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1793 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001794 if (OpOpcode == ISD::TRUNCATE)
1795 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001796 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1797 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001798 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001799 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1800 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001801 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001802 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1803 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001804 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1805 else
1806 return Operand.Val->getOperand(0);
1807 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001808 break;
Chris Lattner35481892005-12-23 00:16:34 +00001809 case ISD::BIT_CONVERT:
1810 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001811 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001812 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001813 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001814 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1815 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001816 if (OpOpcode == ISD::UNDEF)
1817 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001818 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001819 case ISD::SCALAR_TO_VECTOR:
1820 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001821 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001822 "Illegal SCALAR_TO_VECTOR node!");
1823 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001824 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001825 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1826 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001827 Operand.Val->getOperand(0));
1828 if (OpOpcode == ISD::FNEG) // --X -> X
1829 return Operand.Val->getOperand(0);
1830 break;
1831 case ISD::FABS:
1832 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1833 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1834 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001835 }
1836
Chris Lattner43247a12005-08-25 19:12:10 +00001837 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001838 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001839 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001840 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001841 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001842 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001843 void *IP = 0;
1844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1845 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001846 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001847 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001848 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001849 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001850 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001851 AllNodes.push_back(N);
1852 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001853}
1854
Chris Lattner36019aa2005-04-18 03:48:41 +00001855
1856
Chris Lattnerc3aae252005-01-07 07:46:32 +00001857SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1858 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001859#ifndef NDEBUG
1860 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001861 case ISD::TokenFactor:
1862 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1863 N2.getValueType() == MVT::Other && "Invalid token factor!");
1864 break;
Chris Lattner76365122005-01-16 02:23:22 +00001865 case ISD::AND:
1866 case ISD::OR:
1867 case ISD::XOR:
1868 case ISD::UDIV:
1869 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001870 case ISD::MULHU:
1871 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001872 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1873 // fall through
1874 case ISD::ADD:
1875 case ISD::SUB:
1876 case ISD::MUL:
1877 case ISD::SDIV:
1878 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001879 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1880 // fall through.
1881 case ISD::FADD:
1882 case ISD::FSUB:
1883 case ISD::FMUL:
1884 case ISD::FDIV:
1885 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001886 assert(N1.getValueType() == N2.getValueType() &&
1887 N1.getValueType() == VT && "Binary operator types must match!");
1888 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001889 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1890 assert(N1.getValueType() == VT &&
1891 MVT::isFloatingPoint(N1.getValueType()) &&
1892 MVT::isFloatingPoint(N2.getValueType()) &&
1893 "Invalid FCOPYSIGN!");
1894 break;
Chris Lattner76365122005-01-16 02:23:22 +00001895 case ISD::SHL:
1896 case ISD::SRA:
1897 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001898 case ISD::ROTL:
1899 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001900 assert(VT == N1.getValueType() &&
1901 "Shift operators return type must be the same as their first arg");
1902 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001903 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001904 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001905 case ISD::FP_ROUND_INREG: {
1906 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1907 assert(VT == N1.getValueType() && "Not an inreg round!");
1908 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1909 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001910 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1911 "Not rounding down!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001912 break;
1913 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001914 case ISD::AssertSext:
1915 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001916 case ISD::SIGN_EXTEND_INREG: {
1917 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1918 assert(VT == N1.getValueType() && "Not an inreg extend!");
1919 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1920 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001921 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1922 "Not extending!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001923 }
1924
Chris Lattner76365122005-01-16 02:23:22 +00001925 default: break;
1926 }
1927#endif
1928
Chris Lattnerc3aae252005-01-07 07:46:32 +00001929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1930 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1931 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001932 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1933 int64_t Val = N1C->getValue();
1934 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1935 Val <<= 64-FromBits;
1936 Val >>= 64-FromBits;
1937 return getConstant(Val, VT);
1938 }
1939
Chris Lattnerc3aae252005-01-07 07:46:32 +00001940 if (N2C) {
1941 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1942 switch (Opcode) {
1943 case ISD::ADD: return getConstant(C1 + C2, VT);
1944 case ISD::SUB: return getConstant(C1 - C2, VT);
1945 case ISD::MUL: return getConstant(C1 * C2, VT);
1946 case ISD::UDIV:
1947 if (C2) return getConstant(C1 / C2, VT);
1948 break;
1949 case ISD::UREM :
1950 if (C2) return getConstant(C1 % C2, VT);
1951 break;
1952 case ISD::SDIV :
1953 if (C2) return getConstant(N1C->getSignExtended() /
1954 N2C->getSignExtended(), VT);
1955 break;
1956 case ISD::SREM :
1957 if (C2) return getConstant(N1C->getSignExtended() %
1958 N2C->getSignExtended(), VT);
1959 break;
1960 case ISD::AND : return getConstant(C1 & C2, VT);
1961 case ISD::OR : return getConstant(C1 | C2, VT);
1962 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001963 case ISD::SHL : return getConstant(C1 << C2, VT);
1964 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001965 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001966 case ISD::ROTL :
1967 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1968 VT);
1969 case ISD::ROTR :
1970 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1971 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001972 default: break;
1973 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001974 } else { // Cannonicalize constant to RHS if commutative
1975 if (isCommutativeBinOp(Opcode)) {
1976 std::swap(N1C, N2C);
1977 std::swap(N1, N2);
1978 }
1979 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001980 }
1981
1982 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1983 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001984 if (N1CFP) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001985 if (N2CFP && VT!=MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001986 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1987 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001989 case ISD::FADD:
1990 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1991 if (s!=APFloat::opInvalidOp)
1992 return getConstantFP(V1, VT);
1993 break;
1994 case ISD::FSUB:
1995 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1996 if (s!=APFloat::opInvalidOp)
1997 return getConstantFP(V1, VT);
1998 break;
1999 case ISD::FMUL:
2000 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2001 if (s!=APFloat::opInvalidOp)
2002 return getConstantFP(V1, VT);
2003 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002004 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002005 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2006 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2007 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002008 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002009 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002010 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2011 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2012 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002013 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002014 case ISD::FCOPYSIGN:
2015 V1.copySign(V2);
2016 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002017 default: break;
2018 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002019 } else { // Cannonicalize constant to RHS if commutative
2020 if (isCommutativeBinOp(Opcode)) {
2021 std::swap(N1CFP, N2CFP);
2022 std::swap(N1, N2);
2023 }
2024 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002025 }
Chris Lattner62b57722006-04-20 05:39:12 +00002026
2027 // Canonicalize an UNDEF to the RHS, even over a constant.
2028 if (N1.getOpcode() == ISD::UNDEF) {
2029 if (isCommutativeBinOp(Opcode)) {
2030 std::swap(N1, N2);
2031 } else {
2032 switch (Opcode) {
2033 case ISD::FP_ROUND_INREG:
2034 case ISD::SIGN_EXTEND_INREG:
2035 case ISD::SUB:
2036 case ISD::FSUB:
2037 case ISD::FDIV:
2038 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002039 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002040 return N1; // fold op(undef, arg2) -> undef
2041 case ISD::UDIV:
2042 case ISD::SDIV:
2043 case ISD::UREM:
2044 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002045 case ISD::SRL:
2046 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002047 if (!MVT::isVector(VT))
2048 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2049 // For vectors, we can't easily build an all zero vector, just return
2050 // the LHS.
2051 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002052 }
2053 }
2054 }
2055
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002056 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002057 if (N2.getOpcode() == ISD::UNDEF) {
2058 switch (Opcode) {
2059 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002060 case ISD::ADDC:
2061 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002062 case ISD::SUB:
2063 case ISD::FADD:
2064 case ISD::FSUB:
2065 case ISD::FMUL:
2066 case ISD::FDIV:
2067 case ISD::FREM:
2068 case ISD::UDIV:
2069 case ISD::SDIV:
2070 case ISD::UREM:
2071 case ISD::SREM:
2072 case ISD::XOR:
2073 return N2; // fold op(arg1, undef) -> undef
2074 case ISD::MUL:
2075 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002076 case ISD::SRL:
2077 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002078 if (!MVT::isVector(VT))
2079 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2080 // For vectors, we can't easily build an all zero vector, just return
2081 // the LHS.
2082 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002083 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002084 if (!MVT::isVector(VT))
2085 return getConstant(MVT::getIntVTBitMask(VT), VT);
2086 // For vectors, we can't easily build an all one vector, just return
2087 // the LHS.
2088 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002089 case ISD::SRA:
2090 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002091 }
2092 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002093
Chris Lattner51dabfb2006-10-14 00:41:01 +00002094 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002095 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002096 case ISD::TokenFactor:
2097 // Fold trivial token factors.
2098 if (N1.getOpcode() == ISD::EntryToken) return N2;
2099 if (N2.getOpcode() == ISD::EntryToken) return N1;
2100 break;
2101
Chris Lattner51dabfb2006-10-14 00:41:01 +00002102 case ISD::AND:
2103 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2104 // worth handling here.
2105 if (N2C && N2C->getValue() == 0)
2106 return N2;
2107 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002108 case ISD::OR:
2109 case ISD::XOR:
2110 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2111 // worth handling here.
2112 if (N2C && N2C->getValue() == 0)
2113 return N1;
2114 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002115 case ISD::FP_ROUND_INREG:
2116 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2117 break;
2118 case ISD::SIGN_EXTEND_INREG: {
2119 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2120 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002121 break;
2122 }
Dan Gohman7f321562007-06-25 16:23:39 +00002123 case ISD::EXTRACT_VECTOR_ELT:
2124 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2125
Dan Gohman743d3a72007-07-10 18:20:44 +00002126 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002127 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002128 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2129 N1.getNumOperands() > 0) {
2130 unsigned Factor =
2131 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2132 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2133 N1.getOperand(N2C->getValue() / Factor),
2134 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002135 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002136
Dan Gohman7f321562007-06-25 16:23:39 +00002137 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2138 // expanding large vector constants.
2139 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2140 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002141
2142 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2143 // operations are lowered to scalars.
2144 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2145 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2146 if (IEC == N2C)
2147 return N1.getOperand(1);
2148 else
2149 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2150 }
Dan Gohman7f321562007-06-25 16:23:39 +00002151 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002152 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002153 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2154
Chris Lattner5c6621c2006-09-19 04:51:23 +00002155 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2156 // 64-bit integers into 32-bit parts. Instead of building the extract of
2157 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002158 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002159 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002160
2161 // EXTRACT_ELEMENT of a constant int is also very common.
2162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2163 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2164 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002165 }
2166 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002167
Nate Begemaneea805e2005-04-13 21:23:31 +00002168 // FIXME: figure out how to safely handle things like
2169 // int foo(int x) { return 1 << (x & 255); }
2170 // int bar() { return foo(256); }
2171#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002172 case ISD::SHL:
2173 case ISD::SRL:
2174 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002175 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002176 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002177 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002178 else if (N2.getOpcode() == ISD::AND)
2179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2180 // If the and is only masking out bits that cannot effect the shift,
2181 // eliminate the and.
2182 unsigned NumBits = MVT::getSizeInBits(VT);
2183 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2184 return getNode(Opcode, VT, N1, N2.getOperand(0));
2185 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002186 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002187#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002188 }
2189
Chris Lattner27e9b412005-05-11 18:57:39 +00002190 // Memoize this node if possible.
2191 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002192 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002193 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002194 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002195 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002196 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002197 void *IP = 0;
2198 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2199 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002200 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002201 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002202 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002203 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002204 }
2205
Chris Lattnerc3aae252005-01-07 07:46:32 +00002206 AllNodes.push_back(N);
2207 return SDOperand(N, 0);
2208}
2209
Chris Lattnerc3aae252005-01-07 07:46:32 +00002210SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2211 SDOperand N1, SDOperand N2, SDOperand N3) {
2212 // Perform various simplifications.
2213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002216 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002217 // Use FoldSetCC to simplify SETCC's.
2218 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002219 if (Simp.Val) return Simp;
2220 break;
2221 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002222 case ISD::SELECT:
2223 if (N1C)
2224 if (N1C->getValue())
2225 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002226 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002227 return N3; // select false, X, Y -> Y
2228
2229 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002230 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002231 case ISD::BRCOND:
2232 if (N2C)
2233 if (N2C->getValue()) // Unconditional branch
2234 return getNode(ISD::BR, MVT::Other, N1, N3);
2235 else
2236 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002237 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002238 case ISD::VECTOR_SHUFFLE:
2239 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2240 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2241 N3.getOpcode() == ISD::BUILD_VECTOR &&
2242 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2243 "Illegal VECTOR_SHUFFLE node!");
2244 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002245 case ISD::BIT_CONVERT:
2246 // Fold bit_convert nodes from a type to themselves.
2247 if (N1.getValueType() == VT)
2248 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002249 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002250 }
2251
Chris Lattner43247a12005-08-25 19:12:10 +00002252 // Memoize node if it doesn't produce a flag.
2253 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002254 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002255 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002256 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002257 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002258 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002259 void *IP = 0;
2260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2261 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002262 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002263 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002264 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002265 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002267 AllNodes.push_back(N);
2268 return SDOperand(N, 0);
2269}
2270
2271SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002272 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002273 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002274 SDOperand Ops[] = { N1, N2, N3, N4 };
2275 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002276}
2277
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002278SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2279 SDOperand N1, SDOperand N2, SDOperand N3,
2280 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002281 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2282 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002283}
2284
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002285SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2286 SDOperand Src, SDOperand Size,
2287 SDOperand Align,
2288 SDOperand AlwaysInline) {
2289 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2290 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2291}
2292
2293SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2294 SDOperand Src, SDOperand Size,
2295 SDOperand Align,
2296 SDOperand AlwaysInline) {
2297 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2298 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2299}
2300
2301SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2302 SDOperand Src, SDOperand Size,
2303 SDOperand Align,
2304 SDOperand AlwaysInline) {
2305 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2306 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2307}
2308
Evan Cheng7038daf2005-12-10 00:37:58 +00002309SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2310 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002311 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002312 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002313 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2314 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002315 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002316 Ty = MVT::getTypeForValueType(VT);
2317 } else if (SV) {
2318 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2319 assert(PT && "Value for load must be a pointer");
2320 Ty = PT->getElementType();
2321 }
2322 assert(Ty && "Could not get type information for load");
2323 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2324 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002325 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002326 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002327 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002328 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002329 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002330 ID.AddInteger(ISD::UNINDEXED);
2331 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002332 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002333 ID.AddPointer(SV);
2334 ID.AddInteger(SVOffset);
2335 ID.AddInteger(Alignment);
2336 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002337 void *IP = 0;
2338 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2339 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002340 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002341 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2342 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002343 CSEMap.InsertNode(N, IP);
2344 AllNodes.push_back(N);
2345 return SDOperand(N, 0);
2346}
2347
2348SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002349 SDOperand Chain, SDOperand Ptr,
2350 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002351 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002352 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002353 // If they are asking for an extending load from/to the same thing, return a
2354 // normal load.
2355 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002356 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002357
2358 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002359 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002360 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002361 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2362 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002363 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2364 "Cannot sign/zero extend a FP/Vector load!");
2365 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2366 "Cannot convert from FP to Int or Int -> FP!");
2367
Dan Gohman575e2f42007-06-04 15:49:41 +00002368 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2369 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002370 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002371 Ty = MVT::getTypeForValueType(VT);
2372 } else if (SV) {
2373 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2374 assert(PT && "Value for load must be a pointer");
2375 Ty = PT->getElementType();
2376 }
2377 assert(Ty && "Could not get type information for load");
2378 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2379 }
Evan Cheng466685d2006-10-09 20:57:25 +00002380 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002381 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002382 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002383 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002384 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002385 ID.AddInteger(ISD::UNINDEXED);
2386 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002387 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002388 ID.AddPointer(SV);
2389 ID.AddInteger(SVOffset);
2390 ID.AddInteger(Alignment);
2391 ID.AddInteger(isVolatile);
2392 void *IP = 0;
2393 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2394 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002395 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002396 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002397 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002398 AllNodes.push_back(N);
2399 return SDOperand(N, 0);
2400}
2401
Evan Cheng144d8f02006-11-09 17:55:04 +00002402SDOperand
2403SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2404 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002405 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002406 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2407 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002408 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002409 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002410 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002411 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002412 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002413 ID.AddInteger(AM);
2414 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002415 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002416 ID.AddPointer(LD->getSrcValue());
2417 ID.AddInteger(LD->getSrcValueOffset());
2418 ID.AddInteger(LD->getAlignment());
2419 ID.AddInteger(LD->isVolatile());
2420 void *IP = 0;
2421 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2422 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002423 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002424 LD->getExtensionType(), LD->getLoadedVT(),
2425 LD->getSrcValue(), LD->getSrcValueOffset(),
2426 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002427 CSEMap.InsertNode(N, IP);
2428 AllNodes.push_back(N);
2429 return SDOperand(N, 0);
2430}
2431
Jeff Cohend41b30d2006-11-05 19:31:28 +00002432SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002433 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002434 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002435 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002436
Dan Gohman575e2f42007-06-04 15:49:41 +00002437 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2438 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002439 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002440 Ty = MVT::getTypeForValueType(VT);
2441 } else if (SV) {
2442 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2443 assert(PT && "Value for store must be a pointer");
2444 Ty = PT->getElementType();
2445 }
2446 assert(Ty && "Could not get type information for store");
2447 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2448 }
Evan Chengad071e12006-10-05 22:57:11 +00002449 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002450 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002451 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002452 FoldingSetNodeID ID;
2453 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002454 ID.AddInteger(ISD::UNINDEXED);
2455 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002456 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002457 ID.AddPointer(SV);
2458 ID.AddInteger(SVOffset);
2459 ID.AddInteger(Alignment);
2460 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002461 void *IP = 0;
2462 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2463 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002464 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002465 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002466 CSEMap.InsertNode(N, IP);
2467 AllNodes.push_back(N);
2468 return SDOperand(N, 0);
2469}
2470
Jeff Cohend41b30d2006-11-05 19:31:28 +00002471SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002472 SDOperand Ptr, const Value *SV,
2473 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002474 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002475 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002476
2477 if (VT == SVT)
2478 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002479
Duncan Sandsaf47b112007-10-16 09:56:48 +00002480 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2481 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002482 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2483 "Can't do FP-INT conversion!");
2484
Dan Gohman575e2f42007-06-04 15:49:41 +00002485 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2486 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002487 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002488 Ty = MVT::getTypeForValueType(VT);
2489 } else if (SV) {
2490 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2491 assert(PT && "Value for store must be a pointer");
2492 Ty = PT->getElementType();
2493 }
2494 assert(Ty && "Could not get type information for store");
2495 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2496 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002497 SDVTList VTs = getVTList(MVT::Other);
2498 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002499 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002500 FoldingSetNodeID ID;
2501 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002502 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002503 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002504 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002505 ID.AddPointer(SV);
2506 ID.AddInteger(SVOffset);
2507 ID.AddInteger(Alignment);
2508 ID.AddInteger(isVolatile);
2509 void *IP = 0;
2510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2511 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002512 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002513 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002514 CSEMap.InsertNode(N, IP);
2515 AllNodes.push_back(N);
2516 return SDOperand(N, 0);
2517}
2518
Evan Cheng144d8f02006-11-09 17:55:04 +00002519SDOperand
2520SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2521 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002522 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2523 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2524 "Store is already a indexed store!");
2525 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2526 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2527 FoldingSetNodeID ID;
2528 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2529 ID.AddInteger(AM);
2530 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002531 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002532 ID.AddPointer(ST->getSrcValue());
2533 ID.AddInteger(ST->getSrcValueOffset());
2534 ID.AddInteger(ST->getAlignment());
2535 ID.AddInteger(ST->isVolatile());
2536 void *IP = 0;
2537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2538 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002539 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002540 ST->isTruncatingStore(), ST->getStoredVT(),
2541 ST->getSrcValue(), ST->getSrcValueOffset(),
2542 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002543 CSEMap.InsertNode(N, IP);
2544 AllNodes.push_back(N);
2545 return SDOperand(N, 0);
2546}
2547
Nate Begemanacc398c2006-01-25 18:21:52 +00002548SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2549 SDOperand Chain, SDOperand Ptr,
2550 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002551 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002552 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002553}
2554
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002555SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002556 const SDOperand *Ops, unsigned NumOps) {
2557 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002558 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002559 case 1: return getNode(Opcode, VT, Ops[0]);
2560 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2561 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002562 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002564
Chris Lattneref847df2005-04-09 03:27:28 +00002565 switch (Opcode) {
2566 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002567 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002568 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002569 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2570 "LHS and RHS of condition must have same type!");
2571 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2572 "True and False arms of SelectCC must have same type!");
2573 assert(Ops[2].getValueType() == VT &&
2574 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002575 break;
2576 }
2577 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002578 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002579 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2580 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002581 break;
2582 }
Chris Lattneref847df2005-04-09 03:27:28 +00002583 }
2584
Chris Lattner385328c2005-05-14 07:42:29 +00002585 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002586 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002587 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002588 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002589 FoldingSetNodeID ID;
2590 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002591 void *IP = 0;
2592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2593 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002594 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002595 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002596 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002597 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002598 }
Chris Lattneref847df2005-04-09 03:27:28 +00002599 AllNodes.push_back(N);
2600 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002601}
2602
Chris Lattner89c34632005-05-14 06:20:26 +00002603SDOperand SelectionDAG::getNode(unsigned Opcode,
2604 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002605 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002606 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2607 Ops, NumOps);
2608}
2609
2610SDOperand SelectionDAG::getNode(unsigned Opcode,
2611 const MVT::ValueType *VTs, unsigned NumVTs,
2612 const SDOperand *Ops, unsigned NumOps) {
2613 if (NumVTs == 1)
2614 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002615 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2616}
2617
2618SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2619 const SDOperand *Ops, unsigned NumOps) {
2620 if (VTList.NumVTs == 1)
2621 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002622
Chris Lattner5f056bf2005-07-10 01:55:33 +00002623 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002624 // FIXME: figure out how to safely handle things like
2625 // int foo(int x) { return 1 << (x & 255); }
2626 // int bar() { return foo(256); }
2627#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002628 case ISD::SRA_PARTS:
2629 case ISD::SRL_PARTS:
2630 case ISD::SHL_PARTS:
2631 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002632 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002633 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2634 else if (N3.getOpcode() == ISD::AND)
2635 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2636 // If the and is only masking out bits that cannot effect the shift,
2637 // eliminate the and.
2638 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2639 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2640 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2641 }
2642 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002643#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002644 }
Chris Lattner89c34632005-05-14 06:20:26 +00002645
Chris Lattner43247a12005-08-25 19:12:10 +00002646 // Memoize the node unless it returns a flag.
2647 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002648 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002649 FoldingSetNodeID ID;
2650 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002651 void *IP = 0;
2652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2653 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002654 if (NumOps == 1)
2655 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2656 else if (NumOps == 2)
2657 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2658 else if (NumOps == 3)
2659 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2660 else
2661 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002662 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002663 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002664 if (NumOps == 1)
2665 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2666 else if (NumOps == 2)
2667 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2668 else if (NumOps == 3)
2669 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2670 else
2671 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002672 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002673 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002674 return SDOperand(N, 0);
2675}
2676
Dan Gohman08ce9762007-10-08 15:49:58 +00002677SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2678 return getNode(Opcode, VTList, 0, 0);
2679}
2680
2681SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2682 SDOperand N1) {
2683 SDOperand Ops[] = { N1 };
2684 return getNode(Opcode, VTList, Ops, 1);
2685}
2686
2687SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2688 SDOperand N1, SDOperand N2) {
2689 SDOperand Ops[] = { N1, N2 };
2690 return getNode(Opcode, VTList, Ops, 2);
2691}
2692
2693SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2694 SDOperand N1, SDOperand N2, SDOperand N3) {
2695 SDOperand Ops[] = { N1, N2, N3 };
2696 return getNode(Opcode, VTList, Ops, 3);
2697}
2698
2699SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2700 SDOperand N1, SDOperand N2, SDOperand N3,
2701 SDOperand N4) {
2702 SDOperand Ops[] = { N1, N2, N3, N4 };
2703 return getNode(Opcode, VTList, Ops, 4);
2704}
2705
2706SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2707 SDOperand N1, SDOperand N2, SDOperand N3,
2708 SDOperand N4, SDOperand N5) {
2709 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2710 return getNode(Opcode, VTList, Ops, 5);
2711}
2712
Chris Lattner70046e92006-08-15 17:46:01 +00002713SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002714 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002715}
2716
Chris Lattner70046e92006-08-15 17:46:01 +00002717SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002718 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2719 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002720 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002721 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002722 }
2723 std::vector<MVT::ValueType> V;
2724 V.push_back(VT1);
2725 V.push_back(VT2);
2726 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002727 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002728}
Chris Lattner70046e92006-08-15 17:46:01 +00002729SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2730 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002731 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002732 E = VTList.end(); I != E; ++I) {
2733 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2734 (*I)[2] == VT3)
2735 return makeVTList(&(*I)[0], 3);
2736 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002737 std::vector<MVT::ValueType> V;
2738 V.push_back(VT1);
2739 V.push_back(VT2);
2740 V.push_back(VT3);
2741 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002742 return makeVTList(&(*VTList.begin())[0], 3);
2743}
2744
2745SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2746 switch (NumVTs) {
2747 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002748 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002749 case 2: return getVTList(VTs[0], VTs[1]);
2750 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2751 default: break;
2752 }
2753
2754 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2755 E = VTList.end(); I != E; ++I) {
2756 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2757
2758 bool NoMatch = false;
2759 for (unsigned i = 2; i != NumVTs; ++i)
2760 if (VTs[i] != (*I)[i]) {
2761 NoMatch = true;
2762 break;
2763 }
2764 if (!NoMatch)
2765 return makeVTList(&*I->begin(), NumVTs);
2766 }
2767
2768 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2769 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002770}
2771
2772
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002773/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2774/// specified operands. If the resultant node already exists in the DAG,
2775/// this does not modify the specified node, instead it returns the node that
2776/// already exists. If the resultant node does not exist in the DAG, the
2777/// input node is returned. As a degenerate case, if you specify the same
2778/// input operands as the node already has, the input node is returned.
2779SDOperand SelectionDAG::
2780UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2781 SDNode *N = InN.Val;
2782 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2783
2784 // Check to see if there is no change.
2785 if (Op == N->getOperand(0)) return InN;
2786
2787 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002788 void *InsertPos = 0;
2789 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2790 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002791
2792 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002793 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002794 RemoveNodeFromCSEMaps(N);
2795
2796 // Now we update the operands.
2797 N->OperandList[0].Val->removeUser(N);
2798 Op.Val->addUser(N);
2799 N->OperandList[0] = Op;
2800
2801 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002802 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002803 return InN;
2804}
2805
2806SDOperand SelectionDAG::
2807UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2808 SDNode *N = InN.Val;
2809 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2810
2811 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002812 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2813 return InN; // No operands changed, just return the input node.
2814
2815 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002816 void *InsertPos = 0;
2817 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2818 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002819
2820 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002821 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002822 RemoveNodeFromCSEMaps(N);
2823
2824 // Now we update the operands.
2825 if (N->OperandList[0] != Op1) {
2826 N->OperandList[0].Val->removeUser(N);
2827 Op1.Val->addUser(N);
2828 N->OperandList[0] = Op1;
2829 }
2830 if (N->OperandList[1] != Op2) {
2831 N->OperandList[1].Val->removeUser(N);
2832 Op2.Val->addUser(N);
2833 N->OperandList[1] = Op2;
2834 }
2835
2836 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002837 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002838 return InN;
2839}
2840
2841SDOperand SelectionDAG::
2842UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002843 SDOperand Ops[] = { Op1, Op2, Op3 };
2844 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002845}
2846
2847SDOperand SelectionDAG::
2848UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2849 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002850 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2851 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002852}
2853
2854SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002855UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2856 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002857 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2858 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002859}
2860
2861
2862SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002863UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002864 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002865 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002866 "Update with wrong number of operands");
2867
2868 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002869 bool AnyChange = false;
2870 for (unsigned i = 0; i != NumOps; ++i) {
2871 if (Ops[i] != N->getOperand(i)) {
2872 AnyChange = true;
2873 break;
2874 }
2875 }
2876
2877 // No operands changed, just return the input node.
2878 if (!AnyChange) return InN;
2879
2880 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002881 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002882 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002883 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002884
2885 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002886 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002887 RemoveNodeFromCSEMaps(N);
2888
2889 // Now we update the operands.
2890 for (unsigned i = 0; i != NumOps; ++i) {
2891 if (N->OperandList[i] != Ops[i]) {
2892 N->OperandList[i].Val->removeUser(N);
2893 Ops[i].Val->addUser(N);
2894 N->OperandList[i] = Ops[i];
2895 }
2896 }
2897
2898 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002899 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002900 return InN;
2901}
2902
2903
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002904/// MorphNodeTo - This frees the operands of the current node, resets the
2905/// opcode, types, and operands to the specified value. This should only be
2906/// used by the SelectionDAG class.
2907void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2908 const SDOperand *Ops, unsigned NumOps) {
2909 NodeType = Opc;
2910 ValueList = L.VTs;
2911 NumValues = L.NumVTs;
2912
2913 // Clear the operands list, updating used nodes to remove this from their
2914 // use list.
2915 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2916 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002917
2918 // If NumOps is larger than the # of operands we currently have, reallocate
2919 // the operand list.
2920 if (NumOps > NumOperands) {
2921 if (OperandsNeedDelete)
2922 delete [] OperandList;
2923 OperandList = new SDOperand[NumOps];
2924 OperandsNeedDelete = true;
2925 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002926
2927 // Assign the new operands.
2928 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002929
2930 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2931 OperandList[i] = Ops[i];
2932 SDNode *N = OperandList[i].Val;
2933 N->Uses.push_back(this);
2934 }
2935}
Chris Lattner149c58c2005-08-16 18:17:10 +00002936
2937/// SelectNodeTo - These are used for target selectors to *mutate* the
2938/// specified node to have the specified return type, Target opcode, and
2939/// operands. Note that target opcodes are stored as
2940/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002941///
2942/// Note that SelectNodeTo returns the resultant node. If there is already a
2943/// node of the specified opcode and operands, it returns that node instead of
2944/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002945SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2946 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002947 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002948 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002949 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002950 void *IP = 0;
2951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002952 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002953
Chris Lattner7651fa42005-08-24 23:00:29 +00002954 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002955
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002956 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002957
Chris Lattner4a283e92006-08-11 18:38:11 +00002958 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002959 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002960}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002961
Evan Cheng95514ba2006-08-26 08:00:10 +00002962SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2963 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002964 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002965 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002966 SDOperand Ops[] = { Op1 };
2967
Jim Laskey583bd472006-10-27 23:46:08 +00002968 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002969 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002970 void *IP = 0;
2971 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002972 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002973
Chris Lattner149c58c2005-08-16 18:17:10 +00002974 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002976 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002977 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002978}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002979
Evan Cheng95514ba2006-08-26 08:00:10 +00002980SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2981 MVT::ValueType VT, SDOperand Op1,
2982 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002983 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002984 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002985 SDOperand Ops[] = { Op1, Op2 };
2986
Jim Laskey583bd472006-10-27 23:46:08 +00002987 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002988 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002989 void *IP = 0;
2990 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002991 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002992
Chris Lattner149c58c2005-08-16 18:17:10 +00002993 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002994
Chris Lattner63e3f142007-02-04 07:28:00 +00002995 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002996
Chris Lattnera5682852006-08-07 23:03:03 +00002997 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002998 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002999}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003000
Evan Cheng95514ba2006-08-26 08:00:10 +00003001SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3002 MVT::ValueType VT, SDOperand Op1,
3003 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003004 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003005 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003006 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003007 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003008 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003009 void *IP = 0;
3010 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003011 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003012
Chris Lattner149c58c2005-08-16 18:17:10 +00003013 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003014
Chris Lattner63e3f142007-02-04 07:28:00 +00003015 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003016
Chris Lattnera5682852006-08-07 23:03:03 +00003017 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003018 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003019}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003020
Evan Cheng95514ba2006-08-26 08:00:10 +00003021SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003022 MVT::ValueType VT, const SDOperand *Ops,
3023 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003024 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003025 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003026 FoldingSetNodeID ID;
3027 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003028 void *IP = 0;
3029 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003030 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003031
Chris Lattner6b09a292005-08-21 18:49:33 +00003032 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003034
Chris Lattnera5682852006-08-07 23:03:03 +00003035 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003036 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003037}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003038
Evan Cheng95514ba2006-08-26 08:00:10 +00003039SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3040 MVT::ValueType VT1, MVT::ValueType VT2,
3041 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003042 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003043 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003044 SDOperand Ops[] = { Op1, Op2 };
3045 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003046 void *IP = 0;
3047 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003048 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003049
Chris Lattner0fb094f2005-11-19 01:44:53 +00003050 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003052 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003053 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003054}
3055
Evan Cheng95514ba2006-08-26 08:00:10 +00003056SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3057 MVT::ValueType VT1, MVT::ValueType VT2,
3058 SDOperand Op1, SDOperand Op2,
3059 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003060 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003061 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003062 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003063 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003064 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003065 void *IP = 0;
3066 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003067 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003068
Chris Lattner0fb094f2005-11-19 01:44:53 +00003069 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003070
Chris Lattner63e3f142007-02-04 07:28:00 +00003071 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003072 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003073 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003074}
3075
Chris Lattner0fb094f2005-11-19 01:44:53 +00003076
Evan Cheng6ae46c42006-02-09 07:15:23 +00003077/// getTargetNode - These are used for target selectors to create a new node
3078/// with specified return type(s), target opcode, and operands.
3079///
3080/// Note that getTargetNode returns the resultant node. If there is already a
3081/// node of the specified opcode and operands, it returns that node instead of
3082/// the current one.
3083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3084 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3085}
3086SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3087 SDOperand Op1) {
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3089}
3090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3091 SDOperand Op1, SDOperand Op2) {
3092 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3093}
3094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003095 SDOperand Op1, SDOperand Op2,
3096 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003097 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3098}
3099SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003100 const SDOperand *Ops, unsigned NumOps) {
3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003102}
3103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003104 MVT::ValueType VT2) {
3105 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3106 SDOperand Op;
3107 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3108}
3109SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003110 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003111 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003112 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003113}
3114SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003115 MVT::ValueType VT2, SDOperand Op1,
3116 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003117 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003118 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003120}
3121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003122 MVT::ValueType VT2, SDOperand Op1,
3123 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003124 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003125 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003126 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003127}
Evan Cheng694481e2006-08-27 08:08:54 +00003128SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3129 MVT::ValueType VT2,
3130 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003131 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003132 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003133}
3134SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3135 MVT::ValueType VT2, MVT::ValueType VT3,
3136 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003137 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003138 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003139 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003140}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003141SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3142 MVT::ValueType VT2, MVT::ValueType VT3,
3143 SDOperand Op1, SDOperand Op2,
3144 SDOperand Op3) {
3145 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3146 SDOperand Ops[] = { Op1, Op2, Op3 };
3147 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3148}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003149SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003150 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003151 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003152 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3153 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003154}
Evan Cheng05e69c12007-09-12 23:39:49 +00003155SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3156 MVT::ValueType VT2, MVT::ValueType VT3,
3157 MVT::ValueType VT4,
3158 const SDOperand *Ops, unsigned NumOps) {
3159 std::vector<MVT::ValueType> VTList;
3160 VTList.push_back(VT1);
3161 VTList.push_back(VT2);
3162 VTList.push_back(VT3);
3163 VTList.push_back(VT4);
3164 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3166}
Evan Cheng39305cf2007-10-05 01:10:49 +00003167SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3168 std::vector<MVT::ValueType> &ResultTys,
3169 const SDOperand *Ops, unsigned NumOps) {
3170 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3171 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3172 Ops, NumOps).Val;
3173}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003174
Evan Cheng99157a02006-08-07 22:13:29 +00003175/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003176/// This can cause recursive merging of nodes in the DAG.
3177///
Chris Lattner8b52f212005-08-26 18:36:28 +00003178/// This version assumes From/To have a single result value.
3179///
Chris Lattner1e111c72005-09-07 05:37:01 +00003180void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3181 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003182 SDNode *From = FromN.Val, *To = ToN.Val;
3183 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3184 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003185 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003186
Chris Lattner8b8749f2005-08-17 19:00:20 +00003187 while (!From->use_empty()) {
3188 // Process users until they are all gone.
3189 SDNode *U = *From->use_begin();
3190
3191 // This node is about to morph, remove its old self from the CSE maps.
3192 RemoveNodeFromCSEMaps(U);
3193
Chris Lattner65113b22005-11-08 22:07:03 +00003194 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3195 I != E; ++I)
3196 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003197 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003198 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003199 To->addUser(U);
3200 }
3201
3202 // Now that we have modified U, add it back to the CSE maps. If it already
3203 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003204 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3205 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003206 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003207 if (Deleted) Deleted->push_back(U);
3208 DeleteNodeNotInCSEMaps(U);
3209 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003210 }
3211}
3212
3213/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3214/// This can cause recursive merging of nodes in the DAG.
3215///
3216/// This version assumes From/To have matching types and numbers of result
3217/// values.
3218///
Chris Lattner1e111c72005-09-07 05:37:01 +00003219void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3220 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003221 assert(From != To && "Cannot replace uses of with self");
3222 assert(From->getNumValues() == To->getNumValues() &&
3223 "Cannot use this version of ReplaceAllUsesWith!");
3224 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003225 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003226 return;
3227 }
3228
3229 while (!From->use_empty()) {
3230 // Process users until they are all gone.
3231 SDNode *U = *From->use_begin();
3232
3233 // This node is about to morph, remove its old self from the CSE maps.
3234 RemoveNodeFromCSEMaps(U);
3235
Chris Lattner65113b22005-11-08 22:07:03 +00003236 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3237 I != E; ++I)
3238 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003239 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003240 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003241 To->addUser(U);
3242 }
3243
3244 // Now that we have modified U, add it back to the CSE maps. If it already
3245 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003246 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3247 ReplaceAllUsesWith(U, Existing, Deleted);
3248 // U is now dead.
3249 if (Deleted) Deleted->push_back(U);
3250 DeleteNodeNotInCSEMaps(U);
3251 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003252 }
3253}
3254
Chris Lattner8b52f212005-08-26 18:36:28 +00003255/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3256/// This can cause recursive merging of nodes in the DAG.
3257///
3258/// This version can replace From with any result values. To must match the
3259/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003260void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003261 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003262 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003263 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003264 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003265 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003266 return;
3267 }
3268
3269 while (!From->use_empty()) {
3270 // Process users until they are all gone.
3271 SDNode *U = *From->use_begin();
3272
3273 // This node is about to morph, remove its old self from the CSE maps.
3274 RemoveNodeFromCSEMaps(U);
3275
Chris Lattner65113b22005-11-08 22:07:03 +00003276 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3277 I != E; ++I)
3278 if (I->Val == From) {
3279 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003280 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003281 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003282 ToOp.Val->addUser(U);
3283 }
3284
3285 // Now that we have modified U, add it back to the CSE maps. If it already
3286 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003287 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3288 ReplaceAllUsesWith(U, Existing, Deleted);
3289 // U is now dead.
3290 if (Deleted) Deleted->push_back(U);
3291 DeleteNodeNotInCSEMaps(U);
3292 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003293 }
3294}
3295
Chris Lattner012f2412006-02-17 21:58:01 +00003296/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3297/// uses of other values produced by From.Val alone. The Deleted vector is
3298/// handled the same was as for ReplaceAllUsesWith.
3299void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003300 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003301 assert(From != To && "Cannot replace a value with itself");
3302 // Handle the simple, trivial, case efficiently.
3303 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003304 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003305 return;
3306 }
3307
Chris Lattnercf5640b2007-02-04 00:14:31 +00003308 // Get all of the users of From.Val. We want these in a nice,
3309 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3310 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003311
Chris Lattner01d029b2007-10-15 06:10:22 +00003312 std::vector<SDNode*> LocalDeletionVector;
3313
3314 // Pick a deletion vector to use. If the user specified one, use theirs,
3315 // otherwise use a local one.
3316 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003317 while (!Users.empty()) {
3318 // We know that this user uses some value of From. If it is the right
3319 // value, update it.
3320 SDNode *User = Users.back();
3321 Users.pop_back();
3322
Chris Lattner01d029b2007-10-15 06:10:22 +00003323 // Scan for an operand that matches From.
3324 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3325 for (; Op != E; ++Op)
3326 if (*Op == From) break;
3327
3328 // If there are no matches, the user must use some other result of From.
3329 if (Op == E) continue;
3330
3331 // Okay, we know this user needs to be updated. Remove its old self
3332 // from the CSE maps.
3333 RemoveNodeFromCSEMaps(User);
3334
3335 // Update all operands that match "From".
3336 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003337 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003338 From.Val->removeUser(User);
3339 *Op = To;
3340 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003341 }
3342 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003343
3344 // Now that we have modified User, add it back to the CSE maps. If it
3345 // already exists there, recursively merge the results together.
3346 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3347 if (!Existing) continue; // Continue on to next user.
3348
3349 // If there was already an existing matching node, use ReplaceAllUsesWith
3350 // to replace the dead one with the existing one. However, this can cause
3351 // recursive merging of other unrelated nodes down the line. The merging
3352 // can cause deletion of nodes that used the old value. In this case,
3353 // we have to be certain to remove them from the Users set.
3354 unsigned NumDeleted = DeleteVector->size();
3355 ReplaceAllUsesWith(User, Existing, DeleteVector);
3356
3357 // User is now dead.
3358 DeleteVector->push_back(User);
3359 DeleteNodeNotInCSEMaps(User);
3360
3361 // We have to be careful here, because ReplaceAllUsesWith could have
3362 // deleted a user of From, which means there may be dangling pointers
3363 // in the "Users" setvector. Scan over the deleted node pointers and
3364 // remove them from the setvector.
3365 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3366 Users.remove((*DeleteVector)[i]);
3367
3368 // If the user doesn't need the set of deleted elements, don't retain them
3369 // to the next loop iteration.
3370 if (Deleted == 0)
3371 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003372 }
3373}
3374
Chris Lattner7b2880c2005-08-24 22:44:39 +00003375
Evan Chenge6f35d82006-08-01 08:20:41 +00003376/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3377/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003378unsigned SelectionDAG::AssignNodeIds() {
3379 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003380 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3381 SDNode *N = I;
3382 N->setNodeId(Id++);
3383 }
3384 return Id;
3385}
3386
Evan Chenge6f35d82006-08-01 08:20:41 +00003387/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003388/// based on their topological order. It returns the maximum id and a vector
3389/// of the SDNodes* in assigned order by reference.
3390unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003391 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003392 std::vector<unsigned> InDegree(DAGSize);
3393 std::vector<SDNode*> Sources;
3394
3395 // Use a two pass approach to avoid using a std::map which is slow.
3396 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003397 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3398 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003399 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003400 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003401 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003402 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003403 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003404 }
3405
Evan Cheng99157a02006-08-07 22:13:29 +00003406 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003407 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003408 SDNode *N = Sources.back();
3409 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003410 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003411 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3412 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003413 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003414 if (Degree == 0)
3415 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003416 }
3417 }
3418
Evan Chengc384d6c2006-08-02 22:00:34 +00003419 // Second pass, assign the actual topological order as node ids.
3420 Id = 0;
3421 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3422 TI != TE; ++TI)
3423 (*TI)->setNodeId(Id++);
3424
3425 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003426}
3427
3428
Evan Cheng091cba12006-07-27 06:39:06 +00003429
Jim Laskey58b968b2005-08-17 20:08:02 +00003430//===----------------------------------------------------------------------===//
3431// SDNode Class
3432//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003433
Chris Lattner917d2c92006-07-19 00:00:37 +00003434// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003435void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003436void UnarySDNode::ANCHOR() {}
3437void BinarySDNode::ANCHOR() {}
3438void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003439void HandleSDNode::ANCHOR() {}
3440void StringSDNode::ANCHOR() {}
3441void ConstantSDNode::ANCHOR() {}
3442void ConstantFPSDNode::ANCHOR() {}
3443void GlobalAddressSDNode::ANCHOR() {}
3444void FrameIndexSDNode::ANCHOR() {}
3445void JumpTableSDNode::ANCHOR() {}
3446void ConstantPoolSDNode::ANCHOR() {}
3447void BasicBlockSDNode::ANCHOR() {}
3448void SrcValueSDNode::ANCHOR() {}
3449void RegisterSDNode::ANCHOR() {}
3450void ExternalSymbolSDNode::ANCHOR() {}
3451void CondCodeSDNode::ANCHOR() {}
3452void VTSDNode::ANCHOR() {}
3453void LoadSDNode::ANCHOR() {}
3454void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003455
Chris Lattner48b85922007-02-04 02:41:42 +00003456HandleSDNode::~HandleSDNode() {
3457 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003458 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003459}
3460
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003461GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3462 MVT::ValueType VT, int o)
3463 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003464 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003465 // Thread Local
3466 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3467 // Non Thread Local
3468 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3469 getSDVTList(VT)), Offset(o) {
3470 TheGlobal = const_cast<GlobalValue*>(GA);
3471}
Chris Lattner48b85922007-02-04 02:41:42 +00003472
Jim Laskey583bd472006-10-27 23:46:08 +00003473/// Profile - Gather unique data for the node.
3474///
3475void SDNode::Profile(FoldingSetNodeID &ID) {
3476 AddNodeIDNode(ID, this);
3477}
3478
Chris Lattnera3255112005-11-08 23:30:28 +00003479/// getValueTypeList - Return a pointer to the specified value type.
3480///
3481MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003482 if (MVT::isExtendedVT(VT)) {
3483 static std::set<MVT::ValueType> EVTs;
3484 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3485 } else {
3486 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3487 VTs[VT] = VT;
3488 return &VTs[VT];
3489 }
Chris Lattnera3255112005-11-08 23:30:28 +00003490}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003491
Chris Lattner5c884562005-01-12 18:37:47 +00003492/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3493/// indicated value. This method ignores uses of other values defined by this
3494/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003495bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003496 assert(Value < getNumValues() && "Bad value!");
3497
3498 // If there is only one value, this is easy.
3499 if (getNumValues() == 1)
3500 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003501 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003502
Evan Cheng4ee62112006-02-05 06:29:23 +00003503 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003504
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003505 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003506
Chris Lattnerf83482d2006-08-16 20:59:32 +00003507 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003508 SDNode *User = *UI;
3509 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003510 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003511 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3512 if (User->getOperand(i) == TheValue) {
3513 if (NUses == 0)
3514 return false; // too many uses
3515 --NUses;
3516 }
3517 }
3518
3519 // Found exactly the right number of uses?
3520 return NUses == 0;
3521}
3522
3523
Evan Cheng33d55952007-08-02 05:29:38 +00003524/// hasAnyUseOfValue - Return true if there are any use of the indicated
3525/// value. This method ignores uses of other values defined by this operation.
3526bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3527 assert(Value < getNumValues() && "Bad value!");
3528
3529 if (use_size() == 0) return false;
3530
3531 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3532
3533 SmallPtrSet<SDNode*, 32> UsersHandled;
3534
3535 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3536 SDNode *User = *UI;
3537 if (User->getNumOperands() == 1 ||
3538 UsersHandled.insert(User)) // First time we've seen this?
3539 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3540 if (User->getOperand(i) == TheValue) {
3541 return true;
3542 }
3543 }
3544
3545 return false;
3546}
3547
3548
Evan Chenge6e97e62006-11-03 07:31:32 +00003549/// isOnlyUse - Return true if this node is the only use of N.
3550///
Evan Cheng4ee62112006-02-05 06:29:23 +00003551bool SDNode::isOnlyUse(SDNode *N) const {
3552 bool Seen = false;
3553 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3554 SDNode *User = *I;
3555 if (User == this)
3556 Seen = true;
3557 else
3558 return false;
3559 }
3560
3561 return Seen;
3562}
3563
Evan Chenge6e97e62006-11-03 07:31:32 +00003564/// isOperand - Return true if this node is an operand of N.
3565///
Evan Chengbfa284f2006-03-03 06:42:32 +00003566bool SDOperand::isOperand(SDNode *N) const {
3567 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3568 if (*this == N->getOperand(i))
3569 return true;
3570 return false;
3571}
3572
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003573bool SDNode::isOperand(SDNode *N) const {
3574 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3575 if (this == N->OperandList[i].Val)
3576 return true;
3577 return false;
3578}
Evan Cheng4ee62112006-02-05 06:29:23 +00003579
Evan Chengc5fc57d2006-11-03 03:05:24 +00003580static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003581 SmallPtrSet<SDNode *, 32> &Visited) {
3582 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003583 return;
3584
3585 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3586 SDNode *Op = N->getOperand(i).Val;
3587 if (Op == P) {
3588 found = true;
3589 return;
3590 }
3591 findPredecessor(Op, P, found, Visited);
3592 }
3593}
3594
Evan Chenge6e97e62006-11-03 07:31:32 +00003595/// isPredecessor - Return true if this node is a predecessor of N. This node
3596/// is either an operand of N or it can be reached by recursively traversing
3597/// up the operands.
3598/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003599bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003600 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003601 bool found = false;
3602 findPredecessor(N, this, found, Visited);
3603 return found;
3604}
3605
Evan Chengc5484282006-10-04 00:56:09 +00003606uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3607 assert(Num < NumOperands && "Invalid child # of SDNode!");
3608 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3609}
3610
Reid Spencer577cc322007-04-01 07:32:19 +00003611std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003612 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003613 default:
3614 if (getOpcode() < ISD::BUILTIN_OP_END)
3615 return "<<Unknown DAG Node>>";
3616 else {
Evan Cheng72261582005-12-20 06:22:03 +00003617 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003618 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003619 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3620 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003621
Evan Cheng72261582005-12-20 06:22:03 +00003622 TargetLowering &TLI = G->getTargetLoweringInfo();
3623 const char *Name =
3624 TLI.getTargetNodeName(getOpcode());
3625 if (Name) return Name;
3626 }
3627
3628 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003629 }
3630
Andrew Lenharth95762122005-03-31 21:24:06 +00003631 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003632 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003633 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003634 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003635 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003636 case ISD::AssertSext: return "AssertSext";
3637 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003638
3639 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003640 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003641 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003642 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003643
3644 case ISD::Constant: return "Constant";
3645 case ISD::ConstantFP: return "ConstantFP";
3646 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003647 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003648 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003649 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003650 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003651 case ISD::RETURNADDR: return "RETURNADDR";
3652 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003653 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003654 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3655 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003656 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003657 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003658 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003659 case ISD::INTRINSIC_WO_CHAIN: {
3660 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3661 return Intrinsic::getName((Intrinsic::ID)IID);
3662 }
3663 case ISD::INTRINSIC_VOID:
3664 case ISD::INTRINSIC_W_CHAIN: {
3665 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003666 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003667 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003668
Chris Lattnerb2827b02006-03-19 00:52:58 +00003669 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003670 case ISD::TargetConstant: return "TargetConstant";
3671 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003672 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003673 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003674 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003675 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003676 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003677 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003678
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003679 case ISD::CopyToReg: return "CopyToReg";
3680 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003681 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003682 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003683 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003684 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003685 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003686 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003687 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003688
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003689 // Unary operators
3690 case ISD::FABS: return "fabs";
3691 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003692 case ISD::FSQRT: return "fsqrt";
3693 case ISD::FSIN: return "fsin";
3694 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003695 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003696 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003697
3698 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003699 case ISD::ADD: return "add";
3700 case ISD::SUB: return "sub";
3701 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003702 case ISD::MULHU: return "mulhu";
3703 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003704 case ISD::SDIV: return "sdiv";
3705 case ISD::UDIV: return "udiv";
3706 case ISD::SREM: return "srem";
3707 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003708 case ISD::SMUL_LOHI: return "smul_lohi";
3709 case ISD::UMUL_LOHI: return "umul_lohi";
3710 case ISD::SDIVREM: return "sdivrem";
3711 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003712 case ISD::AND: return "and";
3713 case ISD::OR: return "or";
3714 case ISD::XOR: return "xor";
3715 case ISD::SHL: return "shl";
3716 case ISD::SRA: return "sra";
3717 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003718 case ISD::ROTL: return "rotl";
3719 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003720 case ISD::FADD: return "fadd";
3721 case ISD::FSUB: return "fsub";
3722 case ISD::FMUL: return "fmul";
3723 case ISD::FDIV: return "fdiv";
3724 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003725 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003726
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003727 case ISD::SETCC: return "setcc";
3728 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003729 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003730 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003731 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003732 case ISD::CONCAT_VECTORS: return "concat_vectors";
3733 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003734 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003735 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003736 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003737 case ISD::ADDC: return "addc";
3738 case ISD::ADDE: return "adde";
3739 case ISD::SUBC: return "subc";
3740 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003741 case ISD::SHL_PARTS: return "shl_parts";
3742 case ISD::SRA_PARTS: return "sra_parts";
3743 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003744
3745 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3746 case ISD::INSERT_SUBREG: return "insert_subreg";
3747
Chris Lattner7f644642005-04-28 21:44:03 +00003748 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003749 case ISD::SIGN_EXTEND: return "sign_extend";
3750 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003751 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003752 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003753 case ISD::TRUNCATE: return "truncate";
3754 case ISD::FP_ROUND: return "fp_round";
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003755 case ISD::FLT_ROUNDS: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003756 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003757 case ISD::FP_EXTEND: return "fp_extend";
3758
3759 case ISD::SINT_TO_FP: return "sint_to_fp";
3760 case ISD::UINT_TO_FP: return "uint_to_fp";
3761 case ISD::FP_TO_SINT: return "fp_to_sint";
3762 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003763 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003764
3765 // Control flow instructions
3766 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003767 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003768 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003769 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003770 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003771 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003772 case ISD::CALLSEQ_START: return "callseq_start";
3773 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003774
3775 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003776 case ISD::LOAD: return "load";
3777 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003778 case ISD::VAARG: return "vaarg";
3779 case ISD::VACOPY: return "vacopy";
3780 case ISD::VAEND: return "vaend";
3781 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003782 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003783 case ISD::EXTRACT_ELEMENT: return "extract_element";
3784 case ISD::BUILD_PAIR: return "build_pair";
3785 case ISD::STACKSAVE: return "stacksave";
3786 case ISD::STACKRESTORE: return "stackrestore";
3787
3788 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003789 case ISD::MEMSET: return "memset";
3790 case ISD::MEMCPY: return "memcpy";
3791 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003792
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003793 // Bit manipulation
3794 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003795 case ISD::CTPOP: return "ctpop";
3796 case ISD::CTTZ: return "cttz";
3797 case ISD::CTLZ: return "ctlz";
3798
Chris Lattner36ce6912005-11-29 06:21:05 +00003799 // Debug info
3800 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003801 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003802
Duncan Sands36397f52007-07-27 12:58:54 +00003803 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003804 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003805
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003806 case ISD::CONDCODE:
3807 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003808 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003809 case ISD::SETOEQ: return "setoeq";
3810 case ISD::SETOGT: return "setogt";
3811 case ISD::SETOGE: return "setoge";
3812 case ISD::SETOLT: return "setolt";
3813 case ISD::SETOLE: return "setole";
3814 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003816 case ISD::SETO: return "seto";
3817 case ISD::SETUO: return "setuo";
3818 case ISD::SETUEQ: return "setue";
3819 case ISD::SETUGT: return "setugt";
3820 case ISD::SETUGE: return "setuge";
3821 case ISD::SETULT: return "setult";
3822 case ISD::SETULE: return "setule";
3823 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003824
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003825 case ISD::SETEQ: return "seteq";
3826 case ISD::SETGT: return "setgt";
3827 case ISD::SETGE: return "setge";
3828 case ISD::SETLT: return "setlt";
3829 case ISD::SETLE: return "setle";
3830 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003831 }
3832 }
3833}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003834
Evan Cheng144d8f02006-11-09 17:55:04 +00003835const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003836 switch (AM) {
3837 default:
3838 return "";
3839 case ISD::PRE_INC:
3840 return "<pre-inc>";
3841 case ISD::PRE_DEC:
3842 return "<pre-dec>";
3843 case ISD::POST_INC:
3844 return "<post-inc>";
3845 case ISD::POST_DEC:
3846 return "<post-dec>";
3847 }
3848}
3849
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003850void SDNode::dump() const { dump(0); }
3851void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003852 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003853
3854 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003855 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003856 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003857 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003858 else
Bill Wendling832171c2006-12-07 20:04:42 +00003859 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003860 }
Bill Wendling832171c2006-12-07 20:04:42 +00003861 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003862
Bill Wendling832171c2006-12-07 20:04:42 +00003863 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003865 if (i) cerr << ", ";
3866 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003867 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003868 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003869 }
3870
Evan Chengce254432007-12-11 02:08:35 +00003871 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3872 SDNode *Mask = getOperand(2).Val;
3873 cerr << "<";
3874 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3875 if (i) cerr << ",";
3876 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3877 cerr << "u";
3878 else
3879 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3880 }
3881 cerr << ">";
3882 }
3883
Chris Lattnerc3aae252005-01-07 07:46:32 +00003884 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003885 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003886 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003887 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3888 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3889 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3890 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3891 else {
3892 cerr << "<APFloat(";
3893 CSDN->getValueAPF().convertToAPInt().dump();
3894 cerr << ")>";
3895 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003896 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003897 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003898 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003899 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003900 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003901 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003902 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003903 else
Bill Wendling832171c2006-12-07 20:04:42 +00003904 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003905 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003906 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003907 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003908 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003909 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003910 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003911 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003912 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003913 else
Bill Wendling832171c2006-12-07 20:04:42 +00003914 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003915 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003916 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003917 else
Bill Wendling832171c2006-12-07 20:04:42 +00003918 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003919 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003920 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003921 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3922 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003923 cerr << LBB->getName() << " ";
3924 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003925 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003926 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003927 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003928 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003929 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003930 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003931 } else if (const ExternalSymbolSDNode *ES =
3932 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003933 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003934 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3935 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003936 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003937 else
Bill Wendling832171c2006-12-07 20:04:42 +00003938 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003939 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003940 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003941 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00003942 const Value *SrcValue = LD->getSrcValue();
3943 int SrcOffset = LD->getSrcValueOffset();
3944 cerr << " <";
3945 if (SrcValue)
3946 cerr << SrcValue;
3947 else
3948 cerr << "null";
3949 cerr << ":" << SrcOffset << ">";
3950
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003951 bool doExt = true;
3952 switch (LD->getExtensionType()) {
3953 default: doExt = false; break;
3954 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003955 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003956 break;
3957 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003958 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003959 break;
3960 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003961 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003962 break;
3963 }
3964 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003965 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003966
Evan Cheng144d8f02006-11-09 17:55:04 +00003967 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003968 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003969 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00003970 if (LD->isVolatile())
3971 cerr << " <volatile>";
3972 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00003973 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00003974 const Value *SrcValue = ST->getSrcValue();
3975 int SrcOffset = ST->getSrcValueOffset();
3976 cerr << " <";
3977 if (SrcValue)
3978 cerr << SrcValue;
3979 else
3980 cerr << "null";
3981 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00003982
3983 if (ST->isTruncatingStore())
3984 cerr << " <trunc "
3985 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3986
3987 const char *AM = getIndexedModeName(ST->getAddressingMode());
3988 if (*AM)
3989 cerr << " " << AM;
3990 if (ST->isVolatile())
3991 cerr << " <volatile>";
3992 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003993 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003994}
3995
Chris Lattnerde202b32005-11-09 23:47:37 +00003996static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003997 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3998 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003999 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004000 else
Bill Wendling832171c2006-12-07 20:04:42 +00004001 cerr << "\n" << std::string(indent+2, ' ')
4002 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004003
Chris Lattnerea946cd2005-01-09 20:38:33 +00004004
Bill Wendling832171c2006-12-07 20:04:42 +00004005 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004006 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004007}
4008
Chris Lattnerc3aae252005-01-07 07:46:32 +00004009void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004010 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004011 std::vector<const SDNode*> Nodes;
4012 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4013 I != E; ++I)
4014 Nodes.push_back(I);
4015
Chris Lattner49d24712005-01-09 20:26:36 +00004016 std::sort(Nodes.begin(), Nodes.end());
4017
4018 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004019 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004020 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004021 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004022
Jim Laskey26f7fa72006-10-17 19:33:52 +00004023 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004024
Bill Wendling832171c2006-12-07 20:04:42 +00004025 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004026}
4027
Evan Chengd6594ae2006-09-12 21:00:35 +00004028const Type *ConstantPoolSDNode::getType() const {
4029 if (isMachineConstantPoolEntry())
4030 return Val.MachineCPVal->getType();
4031 return Val.ConstVal->getType();
4032}