blob: cbeda67727adb887ccf44004c3a006bbe60bfecb [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.AddInteger(LD->getAlignment());
376 ID.AddInteger(LD->isVolatile());
377 break;
378 }
379 case ISD::STORE: {
380 StoreSDNode *ST = cast<StoreSDNode>(N);
381 ID.AddInteger(ST->getAddressingMode());
382 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000383 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000384 ID.AddInteger(ST->getAlignment());
385 ID.AddInteger(ST->isVolatile());
386 break;
387 }
Jim Laskey583bd472006-10-27 23:46:08 +0000388 }
389}
390
391//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000392// SelectionDAG Class
393//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000394
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000395/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000396/// SelectionDAG.
397void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000398 // Create a dummy node (which is not added to allnodes), that adds a reference
399 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000400 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000401
Chris Lattner190a4182006-08-04 17:45:20 +0000402 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000403
Chris Lattner190a4182006-08-04 17:45:20 +0000404 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000405 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000406 if (I->use_empty())
407 DeadNodes.push_back(I);
408
409 // Process the worklist, deleting the nodes and adding their uses to the
410 // worklist.
411 while (!DeadNodes.empty()) {
412 SDNode *N = DeadNodes.back();
413 DeadNodes.pop_back();
414
415 // Take the node out of the appropriate CSE map.
416 RemoveNodeFromCSEMaps(N);
417
418 // Next, brutally remove the operand list. This is safe to do, as there are
419 // no cycles in the graph.
420 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
421 SDNode *Operand = I->Val;
422 Operand->removeUser(N);
423
424 // Now that we removed this operand, see if there are no uses of it left.
425 if (Operand->use_empty())
426 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000427 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000428 if (N->OperandsNeedDelete)
429 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000430 N->OperandList = 0;
431 N->NumOperands = 0;
432
433 // Finally, remove N itself.
434 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000435 }
436
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000437 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000438 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000439}
440
Evan Cheng130a6472006-10-12 20:34:05 +0000441void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
442 SmallVector<SDNode*, 16> DeadNodes;
443 DeadNodes.push_back(N);
444
445 // Process the worklist, deleting the nodes and adding their uses to the
446 // worklist.
447 while (!DeadNodes.empty()) {
448 SDNode *N = DeadNodes.back();
449 DeadNodes.pop_back();
450
451 // Take the node out of the appropriate CSE map.
452 RemoveNodeFromCSEMaps(N);
453
454 // Next, brutally remove the operand list. This is safe to do, as there are
455 // no cycles in the graph.
456 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
457 SDNode *Operand = I->Val;
458 Operand->removeUser(N);
459
460 // Now that we removed this operand, see if there are no uses of it left.
461 if (Operand->use_empty())
462 DeadNodes.push_back(Operand);
463 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000464 if (N->OperandsNeedDelete)
465 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000466 N->OperandList = 0;
467 N->NumOperands = 0;
468
469 // Finally, remove N itself.
470 Deleted.push_back(N);
471 AllNodes.erase(N);
472 }
473}
474
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000475void SelectionDAG::DeleteNode(SDNode *N) {
476 assert(N->use_empty() && "Cannot delete a node that is not dead!");
477
478 // First take this out of the appropriate CSE map.
479 RemoveNodeFromCSEMaps(N);
480
Chris Lattner1e111c72005-09-07 05:37:01 +0000481 // Finally, remove uses due to operands of this node, remove from the
482 // AllNodes list, and delete the node.
483 DeleteNodeNotInCSEMaps(N);
484}
485
486void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
487
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000488 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000489 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000490
491 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000492 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
493 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000494 if (N->OperandsNeedDelete)
495 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000496 N->OperandList = 0;
497 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000498
499 delete N;
500}
501
Chris Lattner149c58c2005-08-16 18:17:10 +0000502/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
503/// correspond to it. This is useful when we're about to delete or repurpose
504/// the node. We don't want future request for structurally identical nodes
505/// to return N anymore.
506void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000507 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000509 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000510 case ISD::STRING:
511 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
512 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000513 case ISD::CONDCODE:
514 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
515 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000516 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000517 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
518 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000520 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000521 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000522 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000523 Erased =
524 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000525 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000526 case ISD::VALUETYPE: {
527 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
528 if (MVT::isExtendedVT(VT)) {
529 Erased = ExtendedValueTypeNodes.erase(VT);
530 } else {
531 Erased = ValueTypeNodes[VT] != 0;
532 ValueTypeNodes[VT] = 0;
533 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000534 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000535 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000537 // Remove it from the CSE Map.
538 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000539 break;
540 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000541#ifndef NDEBUG
542 // Verify that the node was actually in one of the CSE maps, unless it has a
543 // flag result (which cannot be CSE'd) or is one of the special cases that are
544 // not subject to CSE.
545 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000546 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000547 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000548 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000549 assert(0 && "Node is not in map!");
550 }
551#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000552}
553
Chris Lattner8b8749f2005-08-17 19:00:20 +0000554/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
555/// has been taken out and modified in some way. If the specified node already
556/// exists in the CSE maps, do not modify the maps, but return the existing node
557/// instead. If it doesn't exist, add it and return null.
558///
559SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
560 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000561 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000562 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000563
Chris Lattner9f8cc692005-12-19 22:21:21 +0000564 // Check that remaining values produced are not flags.
565 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
566 if (N->getValueType(i) == MVT::Flag)
567 return 0; // Never CSE anything that produces a flag.
568
Chris Lattnera5682852006-08-07 23:03:03 +0000569 SDNode *New = CSEMap.GetOrInsertNode(N);
570 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000571 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000572}
573
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000574/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
575/// were replaced with those specified. If this node is never memoized,
576/// return null, otherwise return a pointer to the slot it would take. If a
577/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000578SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
579 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000580 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000581 return 0; // Never add these nodes.
582
583 // Check that remaining values produced are not flags.
584 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
585 if (N->getValueType(i) == MVT::Flag)
586 return 0; // Never CSE anything that produces a flag.
587
Chris Lattner63e3f142007-02-04 07:28:00 +0000588 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000589 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000590 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000591 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000592}
593
594/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
595/// were replaced with those specified. If this node is never memoized,
596/// return null, otherwise return a pointer to the slot it would take. If a
597/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000598SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
599 SDOperand Op1, SDOperand Op2,
600 void *&InsertPos) {
601 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
602 return 0; // Never add these nodes.
603
604 // Check that remaining values produced are not flags.
605 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
606 if (N->getValueType(i) == MVT::Flag)
607 return 0; // Never CSE anything that produces a flag.
608
Chris Lattner63e3f142007-02-04 07:28:00 +0000609 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000610 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000611 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000612 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
613}
614
615
616/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
617/// were replaced with those specified. If this node is never memoized,
618/// return null, otherwise return a pointer to the slot it would take. If a
619/// node already exists with these operands, the slot will be non-null.
620SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000621 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000622 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000623 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000624 return 0; // Never add these nodes.
625
626 // Check that remaining values produced are not flags.
627 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
628 if (N->getValueType(i) == MVT::Flag)
629 return 0; // Never CSE anything that produces a flag.
630
Jim Laskey583bd472006-10-27 23:46:08 +0000631 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000632 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000633
Evan Cheng9629aba2006-10-11 01:47:58 +0000634 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
635 ID.AddInteger(LD->getAddressingMode());
636 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000637 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000638 ID.AddInteger(LD->getAlignment());
639 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000640 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
641 ID.AddInteger(ST->getAddressingMode());
642 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000643 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000644 ID.AddInteger(ST->getAlignment());
645 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000646 }
Jim Laskey583bd472006-10-27 23:46:08 +0000647
Chris Lattnera5682852006-08-07 23:03:03 +0000648 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000650
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000651
Chris Lattner78ec3112003-08-11 14:57:33 +0000652SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000655 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000656 if (N->OperandsNeedDelete)
657 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000658 N->OperandList = 0;
659 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000660 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000661 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000662}
663
Chris Lattner0f2287b2005-04-13 02:38:18 +0000664SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000665 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000666 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000667 return getNode(ISD::AND, Op.getValueType(), Op,
668 getConstant(Imm, Op.getValueType()));
669}
670
Chris Lattner36ce6912005-11-29 06:21:05 +0000671SDOperand SelectionDAG::getString(const std::string &Val) {
672 StringSDNode *&N = StringNodes[Val];
673 if (!N) {
674 N = new StringSDNode(Val);
675 AllNodes.push_back(N);
676 }
677 return SDOperand(N, 0);
678}
679
Chris Lattner61b09412006-08-11 21:01:22 +0000680SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000681 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000682
683 MVT::ValueType EltVT =
684 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000685
Chris Lattner61b09412006-08-11 21:01:22 +0000686 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000687 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000688
689 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000690 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000691 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000692 ID.AddInteger(Val);
693 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000694 SDNode *N = NULL;
695 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
696 if (!MVT::isVector(VT))
697 return SDOperand(N, 0);
698 if (!N) {
699 N = new ConstantSDNode(isT, Val, EltVT);
700 CSEMap.InsertNode(N, IP);
701 AllNodes.push_back(N);
702 }
703
704 SDOperand Result(N, 0);
705 if (MVT::isVector(VT)) {
706 SmallVector<SDOperand, 8> Ops;
707 Ops.assign(MVT::getVectorNumElements(VT), Result);
708 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
709 }
710 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000711}
712
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000713SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000714 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000716
Dan Gohman7f321562007-06-25 16:23:39 +0000717 MVT::ValueType EltVT =
718 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000719
Chris Lattnerd8658612005-02-17 20:17:32 +0000720 // Do the map lookup using the actual bit pattern for the floating point
721 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
722 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000723 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000724 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000725 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000726 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000727 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000728 SDNode *N = NULL;
729 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
730 if (!MVT::isVector(VT))
731 return SDOperand(N, 0);
732 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000733 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000734 CSEMap.InsertNode(N, IP);
735 AllNodes.push_back(N);
736 }
737
Dan Gohman7f321562007-06-25 16:23:39 +0000738 SDOperand Result(N, 0);
739 if (MVT::isVector(VT)) {
740 SmallVector<SDOperand, 8> Ops;
741 Ops.assign(MVT::getVectorNumElements(VT), Result);
742 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
743 }
744 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745}
746
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000747SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
748 bool isTarget) {
749 MVT::ValueType EltVT =
750 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
751 if (EltVT==MVT::f32)
752 return getConstantFP(APFloat((float)Val), VT, isTarget);
753 else
754 return getConstantFP(APFloat(Val), VT, isTarget);
755}
756
Chris Lattnerc3aae252005-01-07 07:46:32 +0000757SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000758 MVT::ValueType VT, int Offset,
759 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000760 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
761 unsigned Opc;
762 if (GVar && GVar->isThreadLocal())
763 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
764 else
765 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000766 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000767 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000768 ID.AddPointer(GV);
769 ID.AddInteger(Offset);
770 void *IP = 0;
771 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
772 return SDOperand(E, 0);
773 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
774 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000775 AllNodes.push_back(N);
776 return SDOperand(N, 0);
777}
778
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000779SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
780 bool isTarget) {
781 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000782 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000783 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000784 ID.AddInteger(FI);
785 void *IP = 0;
786 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
787 return SDOperand(E, 0);
788 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
789 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000790 AllNodes.push_back(N);
791 return SDOperand(N, 0);
792}
793
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000794SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
795 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000796 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000797 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000798 ID.AddInteger(JTI);
799 void *IP = 0;
800 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
801 return SDOperand(E, 0);
802 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
803 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000804 AllNodes.push_back(N);
805 return SDOperand(N, 0);
806}
807
Evan Chengb8973bd2006-01-31 22:23:14 +0000808SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000809 unsigned Alignment, int Offset,
810 bool isTarget) {
811 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000812 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000813 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000814 ID.AddInteger(Alignment);
815 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000816 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000817 void *IP = 0;
818 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
819 return SDOperand(E, 0);
820 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
821 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000822 AllNodes.push_back(N);
823 return SDOperand(N, 0);
824}
825
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826
Evan Chengd6594ae2006-09-12 21:00:35 +0000827SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
828 MVT::ValueType VT,
829 unsigned Alignment, int Offset,
830 bool isTarget) {
831 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000832 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000833 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000834 ID.AddInteger(Alignment);
835 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000836 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000837 void *IP = 0;
838 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
839 return SDOperand(E, 0);
840 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
841 CSEMap.InsertNode(N, IP);
842 AllNodes.push_back(N);
843 return SDOperand(N, 0);
844}
845
846
Chris Lattnerc3aae252005-01-07 07:46:32 +0000847SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000848 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000849 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000850 ID.AddPointer(MBB);
851 void *IP = 0;
852 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
853 return SDOperand(E, 0);
854 SDNode *N = new BasicBlockSDNode(MBB);
855 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000856 AllNodes.push_back(N);
857 return SDOperand(N, 0);
858}
859
Chris Lattner15e4b012005-07-10 00:07:11 +0000860SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000861 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000862 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000863
Duncan Sandsf411b832007-10-17 13:49:58 +0000864 SDNode *&N = MVT::isExtendedVT(VT) ?
865 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
866
867 if (N) return SDOperand(N, 0);
868 N = new VTSDNode(VT);
869 AllNodes.push_back(N);
870 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000871}
872
Chris Lattnerc3aae252005-01-07 07:46:32 +0000873SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
874 SDNode *&N = ExternalSymbols[Sym];
875 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000876 N = new ExternalSymbolSDNode(false, Sym, VT);
877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
879}
880
Chris Lattner809ec112006-01-28 10:09:25 +0000881SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
882 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000883 SDNode *&N = TargetExternalSymbols[Sym];
884 if (N) return SDOperand(N, 0);
885 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000886 AllNodes.push_back(N);
887 return SDOperand(N, 0);
888}
889
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000890SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
891 if ((unsigned)Cond >= CondCodeNodes.size())
892 CondCodeNodes.resize(Cond+1);
893
Chris Lattner079a27a2005-08-09 20:40:02 +0000894 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000895 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000896 AllNodes.push_back(CondCodeNodes[Cond]);
897 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000898 return SDOperand(CondCodeNodes[Cond], 0);
899}
900
Chris Lattner0fdd7682005-08-30 22:38:38 +0000901SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000902 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000903 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000904 ID.AddInteger(RegNo);
905 void *IP = 0;
906 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
907 return SDOperand(E, 0);
908 SDNode *N = new RegisterSDNode(RegNo, VT);
909 CSEMap.InsertNode(N, IP);
910 AllNodes.push_back(N);
911 return SDOperand(N, 0);
912}
913
914SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
915 assert((!V || isa<PointerType>(V->getType())) &&
916 "SrcValue is not a pointer?");
917
Jim Laskey583bd472006-10-27 23:46:08 +0000918 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000919 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000920 ID.AddPointer(V);
921 ID.AddInteger(Offset);
922 void *IP = 0;
923 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
924 return SDOperand(E, 0);
925 SDNode *N = new SrcValueSDNode(V, Offset);
926 CSEMap.InsertNode(N, IP);
927 AllNodes.push_back(N);
928 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000929}
930
Chris Lattner37ce9df2007-10-15 17:47:20 +0000931/// CreateStackTemporary - Create a stack temporary, suitable for holding the
932/// specified value type.
933SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
934 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
935 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
936 const Type *Ty = MVT::getTypeForValueType(VT);
937 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
938 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
939 return getFrameIndex(FrameIdx, TLI.getPointerTy());
940}
941
942
Chris Lattner51dabfb2006-10-14 00:41:01 +0000943SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
944 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 // These setcc operations always fold.
946 switch (Cond) {
947 default: break;
948 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000949 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000951 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000952
953 case ISD::SETOEQ:
954 case ISD::SETOGT:
955 case ISD::SETOGE:
956 case ISD::SETOLT:
957 case ISD::SETOLE:
958 case ISD::SETONE:
959 case ISD::SETO:
960 case ISD::SETUO:
961 case ISD::SETUEQ:
962 case ISD::SETUNE:
963 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
964 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000966
Chris Lattner67255a12005-04-07 18:14:58 +0000967 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
968 uint64_t C2 = N2C->getValue();
969 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
970 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000971
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 // Sign extend the operands if required
973 if (ISD::isSignedIntSetCC(Cond)) {
974 C1 = N1C->getSignExtended();
975 C2 = N2C->getSignExtended();
976 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000977
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 switch (Cond) {
979 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000980 case ISD::SETEQ: return getConstant(C1 == C2, VT);
981 case ISD::SETNE: return getConstant(C1 != C2, VT);
982 case ISD::SETULT: return getConstant(C1 < C2, VT);
983 case ISD::SETUGT: return getConstant(C1 > C2, VT);
984 case ISD::SETULE: return getConstant(C1 <= C2, VT);
985 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
986 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
987 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
988 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
989 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000990 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991 }
Chris Lattner67255a12005-04-07 18:14:58 +0000992 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000993 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
994 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +0000995 // No compile time operations on this type yet.
996 if (N1C->getValueType(0) == MVT::ppcf128)
997 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +0000998
999 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001001 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001002 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1003 return getNode(ISD::UNDEF, VT);
1004 // fall through
1005 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1006 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1007 return getNode(ISD::UNDEF, VT);
1008 // fall through
1009 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001010 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001011 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1012 return getNode(ISD::UNDEF, VT);
1013 // fall through
1014 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1015 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1016 return getNode(ISD::UNDEF, VT);
1017 // fall through
1018 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1019 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1020 return getNode(ISD::UNDEF, VT);
1021 // fall through
1022 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001023 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001024 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1025 return getNode(ISD::UNDEF, VT);
1026 // fall through
1027 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001028 R==APFloat::cmpEqual, VT);
1029 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1030 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1031 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1032 R==APFloat::cmpEqual, VT);
1033 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1034 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1035 R==APFloat::cmpLessThan, VT);
1036 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1037 R==APFloat::cmpUnordered, VT);
1038 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1039 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 }
1041 } else {
1042 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001043 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001045
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001046 // Could not fold it.
1047 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048}
1049
Dan Gohmanea859be2007-06-22 14:59:07 +00001050/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1051/// this predicate to simplify operations downstream. Mask is known to be zero
1052/// for bits that V cannot have.
1053bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1054 unsigned Depth) const {
1055 // The masks are not wide enough to represent this type! Should use APInt.
1056 if (Op.getValueType() == MVT::i128)
1057 return false;
1058
1059 uint64_t KnownZero, KnownOne;
1060 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1061 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1062 return (KnownZero & Mask) == Mask;
1063}
1064
1065/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1066/// known to be either zero or one and return them in the KnownZero/KnownOne
1067/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1068/// processing.
1069void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1070 uint64_t &KnownZero, uint64_t &KnownOne,
1071 unsigned Depth) const {
1072 KnownZero = KnownOne = 0; // Don't know anything.
1073 if (Depth == 6 || Mask == 0)
1074 return; // Limit search depth.
1075
1076 // The masks are not wide enough to represent this type! Should use APInt.
1077 if (Op.getValueType() == MVT::i128)
1078 return;
1079
1080 uint64_t KnownZero2, KnownOne2;
1081
1082 switch (Op.getOpcode()) {
1083 case ISD::Constant:
1084 // We know all of the bits for a constant!
1085 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1086 KnownZero = ~KnownOne & Mask;
1087 return;
1088 case ISD::AND:
1089 // If either the LHS or the RHS are Zero, the result is zero.
1090 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1091 Mask &= ~KnownZero;
1092 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1093 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1094 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1095
1096 // Output known-1 bits are only known if set in both the LHS & RHS.
1097 KnownOne &= KnownOne2;
1098 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1099 KnownZero |= KnownZero2;
1100 return;
1101 case ISD::OR:
1102 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1103 Mask &= ~KnownOne;
1104 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1105 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1106 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1107
1108 // Output known-0 bits are only known if clear in both the LHS & RHS.
1109 KnownZero &= KnownZero2;
1110 // Output known-1 are known to be set if set in either the LHS | RHS.
1111 KnownOne |= KnownOne2;
1112 return;
1113 case ISD::XOR: {
1114 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1115 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1116 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1117 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1118
1119 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1120 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1121 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1122 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1123 KnownZero = KnownZeroOut;
1124 return;
1125 }
1126 case ISD::SELECT:
1127 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1128 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1129 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1130 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1131
1132 // Only known if known in both the LHS and RHS.
1133 KnownOne &= KnownOne2;
1134 KnownZero &= KnownZero2;
1135 return;
1136 case ISD::SELECT_CC:
1137 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1138 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1139 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1140 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1141
1142 // Only known if known in both the LHS and RHS.
1143 KnownOne &= KnownOne2;
1144 KnownZero &= KnownZero2;
1145 return;
1146 case ISD::SETCC:
1147 // If we know the result of a setcc has the top bits zero, use this info.
1148 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1149 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1150 return;
1151 case ISD::SHL:
1152 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1153 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1154 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1155 KnownZero, KnownOne, Depth+1);
1156 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1157 KnownZero <<= SA->getValue();
1158 KnownOne <<= SA->getValue();
1159 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1160 }
1161 return;
1162 case ISD::SRL:
1163 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1164 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1165 MVT::ValueType VT = Op.getValueType();
1166 unsigned ShAmt = SA->getValue();
1167
1168 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1169 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1170 KnownZero, KnownOne, Depth+1);
1171 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1172 KnownZero &= TypeMask;
1173 KnownOne &= TypeMask;
1174 KnownZero >>= ShAmt;
1175 KnownOne >>= ShAmt;
1176
1177 uint64_t HighBits = (1ULL << ShAmt)-1;
1178 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1179 KnownZero |= HighBits; // High bits known zero.
1180 }
1181 return;
1182 case ISD::SRA:
1183 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1184 MVT::ValueType VT = Op.getValueType();
1185 unsigned ShAmt = SA->getValue();
1186
1187 // Compute the new bits that are at the top now.
1188 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1189
1190 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1191 // If any of the demanded bits are produced by the sign extension, we also
1192 // demand the input sign bit.
1193 uint64_t HighBits = (1ULL << ShAmt)-1;
1194 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1195 if (HighBits & Mask)
1196 InDemandedMask |= MVT::getIntVTSignBit(VT);
1197
1198 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1199 Depth+1);
1200 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1201 KnownZero &= TypeMask;
1202 KnownOne &= TypeMask;
1203 KnownZero >>= ShAmt;
1204 KnownOne >>= ShAmt;
1205
1206 // Handle the sign bits.
1207 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1208 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1209
1210 if (KnownZero & SignBit) {
1211 KnownZero |= HighBits; // New bits are known zero.
1212 } else if (KnownOne & SignBit) {
1213 KnownOne |= HighBits; // New bits are known one.
1214 }
1215 }
1216 return;
1217 case ISD::SIGN_EXTEND_INREG: {
1218 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1219
1220 // Sign extension. Compute the demanded bits in the result that are not
1221 // present in the input.
1222 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1223
1224 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1225 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1226
1227 // If the sign extended bits are demanded, we know that the sign
1228 // bit is demanded.
1229 if (NewBits)
1230 InputDemandedBits |= InSignBit;
1231
1232 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1233 KnownZero, KnownOne, Depth+1);
1234 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1235
1236 // If the sign bit of the input is known set or clear, then we know the
1237 // top bits of the result.
1238 if (KnownZero & InSignBit) { // Input sign bit known clear
1239 KnownZero |= NewBits;
1240 KnownOne &= ~NewBits;
1241 } else if (KnownOne & InSignBit) { // Input sign bit known set
1242 KnownOne |= NewBits;
1243 KnownZero &= ~NewBits;
1244 } else { // Input sign bit unknown
1245 KnownZero &= ~NewBits;
1246 KnownOne &= ~NewBits;
1247 }
1248 return;
1249 }
1250 case ISD::CTTZ:
1251 case ISD::CTLZ:
1252 case ISD::CTPOP: {
1253 MVT::ValueType VT = Op.getValueType();
1254 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1255 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1256 KnownOne = 0;
1257 return;
1258 }
1259 case ISD::LOAD: {
1260 if (ISD::isZEXTLoad(Op.Val)) {
1261 LoadSDNode *LD = cast<LoadSDNode>(Op);
1262 MVT::ValueType VT = LD->getLoadedVT();
1263 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1264 }
1265 return;
1266 }
1267 case ISD::ZERO_EXTEND: {
1268 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1269 uint64_t NewBits = (~InMask) & Mask;
1270 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1271 KnownOne, Depth+1);
1272 KnownZero |= NewBits & Mask;
1273 KnownOne &= ~NewBits;
1274 return;
1275 }
1276 case ISD::SIGN_EXTEND: {
1277 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1278 unsigned InBits = MVT::getSizeInBits(InVT);
1279 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1280 uint64_t InSignBit = 1ULL << (InBits-1);
1281 uint64_t NewBits = (~InMask) & Mask;
1282 uint64_t InDemandedBits = Mask & InMask;
1283
1284 // If any of the sign extended bits are demanded, we know that the sign
1285 // bit is demanded.
1286 if (NewBits & Mask)
1287 InDemandedBits |= InSignBit;
1288
1289 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1290 KnownOne, Depth+1);
1291 // If the sign bit is known zero or one, the top bits match.
1292 if (KnownZero & InSignBit) {
1293 KnownZero |= NewBits;
1294 KnownOne &= ~NewBits;
1295 } else if (KnownOne & InSignBit) {
1296 KnownOne |= NewBits;
1297 KnownZero &= ~NewBits;
1298 } else { // Otherwise, top bits aren't known.
1299 KnownOne &= ~NewBits;
1300 KnownZero &= ~NewBits;
1301 }
1302 return;
1303 }
1304 case ISD::ANY_EXTEND: {
1305 MVT::ValueType VT = Op.getOperand(0).getValueType();
1306 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1307 KnownZero, KnownOne, Depth+1);
1308 return;
1309 }
1310 case ISD::TRUNCATE: {
1311 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1312 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1313 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1314 KnownZero &= OutMask;
1315 KnownOne &= OutMask;
1316 break;
1317 }
1318 case ISD::AssertZext: {
1319 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1320 uint64_t InMask = MVT::getIntVTBitMask(VT);
1321 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1322 KnownOne, Depth+1);
1323 KnownZero |= (~InMask) & Mask;
1324 return;
1325 }
1326 case ISD::ADD: {
1327 // If either the LHS or the RHS are Zero, the result is zero.
1328 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1329 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1330 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1331 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1332
1333 // Output known-0 bits are known if clear or set in both the low clear bits
1334 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1335 // low 3 bits clear.
1336 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1337 CountTrailingZeros_64(~KnownZero2));
1338
1339 KnownZero = (1ULL << KnownZeroOut) - 1;
1340 KnownOne = 0;
1341 return;
1342 }
1343 case ISD::SUB: {
1344 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1345 if (!CLHS) return;
1346
1347 // We know that the top bits of C-X are clear if X contains less bits
1348 // than C (i.e. no wrap-around can happen). For example, 20-X is
1349 // positive if we can prove that X is >= 0 and < 16.
1350 MVT::ValueType VT = CLHS->getValueType(0);
1351 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1352 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1353 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1354 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1355 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1356
1357 // If all of the MaskV bits are known to be zero, then we know the output
1358 // top bits are zero, because we now know that the output is from [0-C].
1359 if ((KnownZero & MaskV) == MaskV) {
1360 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1361 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1362 KnownOne = 0; // No one bits known.
1363 } else {
1364 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1365 }
1366 }
1367 return;
1368 }
1369 default:
1370 // Allow the target to implement this method for its nodes.
1371 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1372 case ISD::INTRINSIC_WO_CHAIN:
1373 case ISD::INTRINSIC_W_CHAIN:
1374 case ISD::INTRINSIC_VOID:
1375 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1376 }
1377 return;
1378 }
1379}
1380
1381/// ComputeNumSignBits - Return the number of times the sign bit of the
1382/// register is replicated into the other bits. We know that at least 1 bit
1383/// is always equal to the sign bit (itself), but other cases can give us
1384/// information. For example, immediately after an "SRA X, 2", we know that
1385/// the top 3 bits are all equal to each other, so we return 3.
1386unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1387 MVT::ValueType VT = Op.getValueType();
1388 assert(MVT::isInteger(VT) && "Invalid VT!");
1389 unsigned VTBits = MVT::getSizeInBits(VT);
1390 unsigned Tmp, Tmp2;
1391
1392 if (Depth == 6)
1393 return 1; // Limit search depth.
1394
1395 switch (Op.getOpcode()) {
1396 default: break;
1397 case ISD::AssertSext:
1398 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1399 return VTBits-Tmp+1;
1400 case ISD::AssertZext:
1401 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1402 return VTBits-Tmp;
1403
1404 case ISD::Constant: {
1405 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1406 // If negative, invert the bits, then look at it.
1407 if (Val & MVT::getIntVTSignBit(VT))
1408 Val = ~Val;
1409
1410 // Shift the bits so they are the leading bits in the int64_t.
1411 Val <<= 64-VTBits;
1412
1413 // Return # leading zeros. We use 'min' here in case Val was zero before
1414 // shifting. We don't want to return '64' as for an i32 "0".
1415 return std::min(VTBits, CountLeadingZeros_64(Val));
1416 }
1417
1418 case ISD::SIGN_EXTEND:
1419 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1420 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1421
1422 case ISD::SIGN_EXTEND_INREG:
1423 // Max of the input and what this extends.
1424 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1425 Tmp = VTBits-Tmp+1;
1426
1427 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1428 return std::max(Tmp, Tmp2);
1429
1430 case ISD::SRA:
1431 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1432 // SRA X, C -> adds C sign bits.
1433 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1434 Tmp += C->getValue();
1435 if (Tmp > VTBits) Tmp = VTBits;
1436 }
1437 return Tmp;
1438 case ISD::SHL:
1439 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1440 // shl destroys sign bits.
1441 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1442 if (C->getValue() >= VTBits || // Bad shift.
1443 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1444 return Tmp - C->getValue();
1445 }
1446 break;
1447 case ISD::AND:
1448 case ISD::OR:
1449 case ISD::XOR: // NOT is handled here.
1450 // Logical binary ops preserve the number of sign bits.
1451 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1452 if (Tmp == 1) return 1; // Early out.
1453 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1454 return std::min(Tmp, Tmp2);
1455
1456 case ISD::SELECT:
1457 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1458 if (Tmp == 1) return 1; // Early out.
1459 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1460 return std::min(Tmp, Tmp2);
1461
1462 case ISD::SETCC:
1463 // If setcc returns 0/-1, all bits are sign bits.
1464 if (TLI.getSetCCResultContents() ==
1465 TargetLowering::ZeroOrNegativeOneSetCCResult)
1466 return VTBits;
1467 break;
1468 case ISD::ROTL:
1469 case ISD::ROTR:
1470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1471 unsigned RotAmt = C->getValue() & (VTBits-1);
1472
1473 // Handle rotate right by N like a rotate left by 32-N.
1474 if (Op.getOpcode() == ISD::ROTR)
1475 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1476
1477 // If we aren't rotating out all of the known-in sign bits, return the
1478 // number that are left. This handles rotl(sext(x), 1) for example.
1479 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1480 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1481 }
1482 break;
1483 case ISD::ADD:
1484 // Add can have at most one carry bit. Thus we know that the output
1485 // is, at worst, one more bit than the inputs.
1486 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1487 if (Tmp == 1) return 1; // Early out.
1488
1489 // Special case decrementing a value (ADD X, -1):
1490 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1491 if (CRHS->isAllOnesValue()) {
1492 uint64_t KnownZero, KnownOne;
1493 uint64_t Mask = MVT::getIntVTBitMask(VT);
1494 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1495
1496 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1497 // sign bits set.
1498 if ((KnownZero|1) == Mask)
1499 return VTBits;
1500
1501 // If we are subtracting one from a positive number, there is no carry
1502 // out of the result.
1503 if (KnownZero & MVT::getIntVTSignBit(VT))
1504 return Tmp;
1505 }
1506
1507 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1508 if (Tmp2 == 1) return 1;
1509 return std::min(Tmp, Tmp2)-1;
1510 break;
1511
1512 case ISD::SUB:
1513 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1514 if (Tmp2 == 1) return 1;
1515
1516 // Handle NEG.
1517 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1518 if (CLHS->getValue() == 0) {
1519 uint64_t KnownZero, KnownOne;
1520 uint64_t Mask = MVT::getIntVTBitMask(VT);
1521 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1522 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1523 // sign bits set.
1524 if ((KnownZero|1) == Mask)
1525 return VTBits;
1526
1527 // If the input is known to be positive (the sign bit is known clear),
1528 // the output of the NEG has the same number of sign bits as the input.
1529 if (KnownZero & MVT::getIntVTSignBit(VT))
1530 return Tmp2;
1531
1532 // Otherwise, we treat this like a SUB.
1533 }
1534
1535 // Sub can have at most one carry bit. Thus we know that the output
1536 // is, at worst, one more bit than the inputs.
1537 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1538 if (Tmp == 1) return 1; // Early out.
1539 return std::min(Tmp, Tmp2)-1;
1540 break;
1541 case ISD::TRUNCATE:
1542 // FIXME: it's tricky to do anything useful for this, but it is an important
1543 // case for targets like X86.
1544 break;
1545 }
1546
1547 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1548 if (Op.getOpcode() == ISD::LOAD) {
1549 LoadSDNode *LD = cast<LoadSDNode>(Op);
1550 unsigned ExtType = LD->getExtensionType();
1551 switch (ExtType) {
1552 default: break;
1553 case ISD::SEXTLOAD: // '17' bits known
1554 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1555 return VTBits-Tmp+1;
1556 case ISD::ZEXTLOAD: // '16' bits known
1557 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1558 return VTBits-Tmp;
1559 }
1560 }
1561
1562 // Allow the target to implement this method for its nodes.
1563 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1564 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1565 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1566 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1567 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1568 if (NumBits > 1) return NumBits;
1569 }
1570
1571 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1572 // use this information.
1573 uint64_t KnownZero, KnownOne;
1574 uint64_t Mask = MVT::getIntVTBitMask(VT);
1575 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1576
1577 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1578 if (KnownZero & SignBit) { // SignBit is 0
1579 Mask = KnownZero;
1580 } else if (KnownOne & SignBit) { // SignBit is 1;
1581 Mask = KnownOne;
1582 } else {
1583 // Nothing known.
1584 return 1;
1585 }
1586
1587 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1588 // the number of identical bits in the top of the input value.
1589 Mask ^= ~0ULL;
1590 Mask <<= 64-VTBits;
1591 // Return # leading zeros. We use 'min' here in case Val was zero before
1592 // shifting. We don't want to return '64' as for an i32 "0".
1593 return std::min(VTBits, CountLeadingZeros_64(Mask));
1594}
1595
Chris Lattner51dabfb2006-10-14 00:41:01 +00001596
Chris Lattnerc3aae252005-01-07 07:46:32 +00001597/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001598///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001599SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001600 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001601 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001602 void *IP = 0;
1603 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1604 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001605 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001606 CSEMap.InsertNode(N, IP);
1607
1608 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001609 return SDOperand(N, 0);
1610}
1611
Chris Lattnerc3aae252005-01-07 07:46:32 +00001612SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1613 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001614 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001615 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001616 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1617 uint64_t Val = C->getValue();
1618 switch (Opcode) {
1619 default: break;
1620 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001621 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001622 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1623 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001624 case ISD::UINT_TO_FP:
1625 case ISD::SINT_TO_FP: {
1626 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001627 // No compile time operations on this type.
1628 if (VT==MVT::ppcf128)
1629 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001630 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001631 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001632 MVT::getSizeInBits(Operand.getValueType()),
1633 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001634 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001635 return getConstantFP(apf, VT);
1636 }
Chris Lattner94683772005-12-23 05:30:37 +00001637 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001638 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001639 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001640 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001641 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001642 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001643 case ISD::BSWAP:
1644 switch(VT) {
1645 default: assert(0 && "Invalid bswap!"); break;
1646 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1647 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1648 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1649 }
1650 break;
1651 case ISD::CTPOP:
1652 switch(VT) {
1653 default: assert(0 && "Invalid ctpop!"); break;
1654 case MVT::i1: return getConstant(Val != 0, VT);
1655 case MVT::i8:
1656 Tmp1 = (unsigned)Val & 0xFF;
1657 return getConstant(CountPopulation_32(Tmp1), VT);
1658 case MVT::i16:
1659 Tmp1 = (unsigned)Val & 0xFFFF;
1660 return getConstant(CountPopulation_32(Tmp1), VT);
1661 case MVT::i32:
1662 return getConstant(CountPopulation_32((unsigned)Val), VT);
1663 case MVT::i64:
1664 return getConstant(CountPopulation_64(Val), VT);
1665 }
1666 case ISD::CTLZ:
1667 switch(VT) {
1668 default: assert(0 && "Invalid ctlz!"); break;
1669 case MVT::i1: return getConstant(Val == 0, VT);
1670 case MVT::i8:
1671 Tmp1 = (unsigned)Val & 0xFF;
1672 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1673 case MVT::i16:
1674 Tmp1 = (unsigned)Val & 0xFFFF;
1675 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1676 case MVT::i32:
1677 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1678 case MVT::i64:
1679 return getConstant(CountLeadingZeros_64(Val), VT);
1680 }
1681 case ISD::CTTZ:
1682 switch(VT) {
1683 default: assert(0 && "Invalid cttz!"); break;
1684 case MVT::i1: return getConstant(Val == 0, VT);
1685 case MVT::i8:
1686 Tmp1 = (unsigned)Val | 0x100;
1687 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1688 case MVT::i16:
1689 Tmp1 = (unsigned)Val | 0x10000;
1690 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1691 case MVT::i32:
1692 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1693 case MVT::i64:
1694 return getConstant(CountTrailingZeros_64(Val), VT);
1695 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001696 }
1697 }
1698
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001699 // Constant fold unary operations with a floating point constant operand.
1700 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1701 APFloat V = C->getValueAPF(); // make copy
Dale Johannesendb44bf82007-10-16 23:38:29 +00001702 if (VT!=MVT::ppcf128 && Operand.getValueType()!=MVT::ppcf128) {
1703 switch (Opcode) {
1704 case ISD::FNEG:
1705 V.changeSign();
1706 return getConstantFP(V, VT);
1707 case ISD::FABS:
1708 V.clearSign();
1709 return getConstantFP(V, VT);
1710 case ISD::FP_ROUND:
1711 case ISD::FP_EXTEND:
1712 // This can return overflow, underflow, or inexact; we don't care.
1713 // FIXME need to be more flexible about rounding mode.
1714 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1715 VT==MVT::f64 ? APFloat::IEEEdouble :
1716 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1717 VT==MVT::f128 ? APFloat::IEEEquad :
1718 APFloat::Bogus,
1719 APFloat::rmNearestTiesToEven);
1720 return getConstantFP(V, VT);
1721 case ISD::FP_TO_SINT:
1722 case ISD::FP_TO_UINT: {
1723 integerPart x;
1724 assert(integerPartWidth >= 64);
1725 // FIXME need to be more flexible about rounding mode.
1726 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1727 Opcode==ISD::FP_TO_SINT,
1728 APFloat::rmTowardZero);
1729 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1730 break;
1731 return getConstant(x, VT);
1732 }
1733 case ISD::BIT_CONVERT:
1734 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1735 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1736 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1737 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001738 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001739 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001740 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001741 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001742
1743 unsigned OpOpcode = Operand.Val->getOpcode();
1744 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001745 case ISD::TokenFactor:
1746 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001747 case ISD::FP_ROUND:
1748 case ISD::FP_EXTEND:
1749 assert(MVT::isFloatingPoint(VT) &&
1750 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1751 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001752 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001753 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1754 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001755 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001756 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1757 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1759 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1760 break;
1761 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001762 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1763 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001764 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001765 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1766 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001767 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001768 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001769 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001770 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001771 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1772 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001773 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001774 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1775 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001776 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1777 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1778 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1779 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001780 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001781 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1782 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001783 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001784 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1785 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001786 if (OpOpcode == ISD::TRUNCATE)
1787 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001788 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1789 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001790 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001791 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1792 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001793 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001794 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1795 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001796 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1797 else
1798 return Operand.Val->getOperand(0);
1799 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001800 break;
Chris Lattner35481892005-12-23 00:16:34 +00001801 case ISD::BIT_CONVERT:
1802 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001803 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001804 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001805 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001806 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1807 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001808 if (OpOpcode == ISD::UNDEF)
1809 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001810 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001811 case ISD::SCALAR_TO_VECTOR:
1812 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001813 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001814 "Illegal SCALAR_TO_VECTOR node!");
1815 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001816 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001817 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1818 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001819 Operand.Val->getOperand(0));
1820 if (OpOpcode == ISD::FNEG) // --X -> X
1821 return Operand.Val->getOperand(0);
1822 break;
1823 case ISD::FABS:
1824 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1825 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1826 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001827 }
1828
Chris Lattner43247a12005-08-25 19:12:10 +00001829 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001830 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001831 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001832 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001833 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001834 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001835 void *IP = 0;
1836 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1837 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001838 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001839 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001840 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001841 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001842 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001843 AllNodes.push_back(N);
1844 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001845}
1846
Chris Lattner36019aa2005-04-18 03:48:41 +00001847
1848
Chris Lattnerc3aae252005-01-07 07:46:32 +00001849SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1850 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001851#ifndef NDEBUG
1852 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001853 case ISD::TokenFactor:
1854 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1855 N2.getValueType() == MVT::Other && "Invalid token factor!");
1856 break;
Chris Lattner76365122005-01-16 02:23:22 +00001857 case ISD::AND:
1858 case ISD::OR:
1859 case ISD::XOR:
1860 case ISD::UDIV:
1861 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001862 case ISD::MULHU:
1863 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001864 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1865 // fall through
1866 case ISD::ADD:
1867 case ISD::SUB:
1868 case ISD::MUL:
1869 case ISD::SDIV:
1870 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001871 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1872 // fall through.
1873 case ISD::FADD:
1874 case ISD::FSUB:
1875 case ISD::FMUL:
1876 case ISD::FDIV:
1877 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001878 assert(N1.getValueType() == N2.getValueType() &&
1879 N1.getValueType() == VT && "Binary operator types must match!");
1880 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001881 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1882 assert(N1.getValueType() == VT &&
1883 MVT::isFloatingPoint(N1.getValueType()) &&
1884 MVT::isFloatingPoint(N2.getValueType()) &&
1885 "Invalid FCOPYSIGN!");
1886 break;
Chris Lattner76365122005-01-16 02:23:22 +00001887 case ISD::SHL:
1888 case ISD::SRA:
1889 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001890 case ISD::ROTL:
1891 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001892 assert(VT == N1.getValueType() &&
1893 "Shift operators return type must be the same as their first arg");
1894 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001895 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001896 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001897 case ISD::FP_ROUND_INREG: {
1898 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1899 assert(VT == N1.getValueType() && "Not an inreg round!");
1900 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1901 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001902 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1903 "Not rounding down!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001904 break;
1905 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001906 case ISD::AssertSext:
1907 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001908 case ISD::SIGN_EXTEND_INREG: {
1909 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1910 assert(VT == N1.getValueType() && "Not an inreg extend!");
1911 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1912 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001913 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1914 "Not extending!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001915 }
1916
Chris Lattner76365122005-01-16 02:23:22 +00001917 default: break;
1918 }
1919#endif
1920
Chris Lattnerc3aae252005-01-07 07:46:32 +00001921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1922 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1923 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001924 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1925 int64_t Val = N1C->getValue();
1926 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1927 Val <<= 64-FromBits;
1928 Val >>= 64-FromBits;
1929 return getConstant(Val, VT);
1930 }
1931
Chris Lattnerc3aae252005-01-07 07:46:32 +00001932 if (N2C) {
1933 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1934 switch (Opcode) {
1935 case ISD::ADD: return getConstant(C1 + C2, VT);
1936 case ISD::SUB: return getConstant(C1 - C2, VT);
1937 case ISD::MUL: return getConstant(C1 * C2, VT);
1938 case ISD::UDIV:
1939 if (C2) return getConstant(C1 / C2, VT);
1940 break;
1941 case ISD::UREM :
1942 if (C2) return getConstant(C1 % C2, VT);
1943 break;
1944 case ISD::SDIV :
1945 if (C2) return getConstant(N1C->getSignExtended() /
1946 N2C->getSignExtended(), VT);
1947 break;
1948 case ISD::SREM :
1949 if (C2) return getConstant(N1C->getSignExtended() %
1950 N2C->getSignExtended(), VT);
1951 break;
1952 case ISD::AND : return getConstant(C1 & C2, VT);
1953 case ISD::OR : return getConstant(C1 | C2, VT);
1954 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001955 case ISD::SHL : return getConstant(C1 << C2, VT);
1956 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001957 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001958 case ISD::ROTL :
1959 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1960 VT);
1961 case ISD::ROTR :
1962 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1963 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001964 default: break;
1965 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001966 } else { // Cannonicalize constant to RHS if commutative
1967 if (isCommutativeBinOp(Opcode)) {
1968 std::swap(N1C, N2C);
1969 std::swap(N1, N2);
1970 }
1971 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001972 }
1973
1974 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1975 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001976 if (N1CFP) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001977 if (N2CFP && VT!=MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001978 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1979 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001980 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001981 case ISD::FADD:
1982 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1983 if (s!=APFloat::opInvalidOp)
1984 return getConstantFP(V1, VT);
1985 break;
1986 case ISD::FSUB:
1987 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1988 if (s!=APFloat::opInvalidOp)
1989 return getConstantFP(V1, VT);
1990 break;
1991 case ISD::FMUL:
1992 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1993 if (s!=APFloat::opInvalidOp)
1994 return getConstantFP(V1, VT);
1995 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001996 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001997 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1998 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1999 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002000 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002001 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002002 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2003 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2004 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002005 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002006 case ISD::FCOPYSIGN:
2007 V1.copySign(V2);
2008 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002009 default: break;
2010 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002011 } else { // Cannonicalize constant to RHS if commutative
2012 if (isCommutativeBinOp(Opcode)) {
2013 std::swap(N1CFP, N2CFP);
2014 std::swap(N1, N2);
2015 }
2016 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002017 }
Chris Lattner62b57722006-04-20 05:39:12 +00002018
2019 // Canonicalize an UNDEF to the RHS, even over a constant.
2020 if (N1.getOpcode() == ISD::UNDEF) {
2021 if (isCommutativeBinOp(Opcode)) {
2022 std::swap(N1, N2);
2023 } else {
2024 switch (Opcode) {
2025 case ISD::FP_ROUND_INREG:
2026 case ISD::SIGN_EXTEND_INREG:
2027 case ISD::SUB:
2028 case ISD::FSUB:
2029 case ISD::FDIV:
2030 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002031 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002032 return N1; // fold op(undef, arg2) -> undef
2033 case ISD::UDIV:
2034 case ISD::SDIV:
2035 case ISD::UREM:
2036 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002037 case ISD::SRL:
2038 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002039 if (!MVT::isVector(VT))
2040 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2041 // For vectors, we can't easily build an all zero vector, just return
2042 // the LHS.
2043 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002044 }
2045 }
2046 }
2047
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002048 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002049 if (N2.getOpcode() == ISD::UNDEF) {
2050 switch (Opcode) {
2051 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002052 case ISD::ADDC:
2053 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002054 case ISD::SUB:
2055 case ISD::FADD:
2056 case ISD::FSUB:
2057 case ISD::FMUL:
2058 case ISD::FDIV:
2059 case ISD::FREM:
2060 case ISD::UDIV:
2061 case ISD::SDIV:
2062 case ISD::UREM:
2063 case ISD::SREM:
2064 case ISD::XOR:
2065 return N2; // fold op(arg1, undef) -> undef
2066 case ISD::MUL:
2067 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002068 case ISD::SRL:
2069 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002070 if (!MVT::isVector(VT))
2071 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2072 // For vectors, we can't easily build an all zero vector, just return
2073 // the LHS.
2074 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002075 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002076 if (!MVT::isVector(VT))
2077 return getConstant(MVT::getIntVTBitMask(VT), VT);
2078 // For vectors, we can't easily build an all one vector, just return
2079 // the LHS.
2080 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002081 case ISD::SRA:
2082 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002083 }
2084 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002085
Chris Lattner51dabfb2006-10-14 00:41:01 +00002086 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002087 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002088 case ISD::TokenFactor:
2089 // Fold trivial token factors.
2090 if (N1.getOpcode() == ISD::EntryToken) return N2;
2091 if (N2.getOpcode() == ISD::EntryToken) return N1;
2092 break;
2093
Chris Lattner51dabfb2006-10-14 00:41:01 +00002094 case ISD::AND:
2095 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2096 // worth handling here.
2097 if (N2C && N2C->getValue() == 0)
2098 return N2;
2099 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002100 case ISD::OR:
2101 case ISD::XOR:
2102 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2103 // worth handling here.
2104 if (N2C && N2C->getValue() == 0)
2105 return N1;
2106 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002107 case ISD::FP_ROUND_INREG:
2108 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2109 break;
2110 case ISD::SIGN_EXTEND_INREG: {
2111 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2112 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002113 break;
2114 }
Dan Gohman7f321562007-06-25 16:23:39 +00002115 case ISD::EXTRACT_VECTOR_ELT:
2116 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2117
Dan Gohman743d3a72007-07-10 18:20:44 +00002118 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002119 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002120 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2121 N1.getNumOperands() > 0) {
2122 unsigned Factor =
2123 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2124 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2125 N1.getOperand(N2C->getValue() / Factor),
2126 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002127 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002128
Dan Gohman7f321562007-06-25 16:23:39 +00002129 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2130 // expanding large vector constants.
2131 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2132 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002133
2134 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2135 // operations are lowered to scalars.
2136 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2137 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2138 if (IEC == N2C)
2139 return N1.getOperand(1);
2140 else
2141 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2142 }
Dan Gohman7f321562007-06-25 16:23:39 +00002143 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002144 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002145 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2146
Chris Lattner5c6621c2006-09-19 04:51:23 +00002147 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2148 // 64-bit integers into 32-bit parts. Instead of building the extract of
2149 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002150 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002151 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002152
2153 // EXTRACT_ELEMENT of a constant int is also very common.
2154 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2155 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2156 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002157 }
2158 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002159
Nate Begemaneea805e2005-04-13 21:23:31 +00002160 // FIXME: figure out how to safely handle things like
2161 // int foo(int x) { return 1 << (x & 255); }
2162 // int bar() { return foo(256); }
2163#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002164 case ISD::SHL:
2165 case ISD::SRL:
2166 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002167 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002168 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002169 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002170 else if (N2.getOpcode() == ISD::AND)
2171 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2172 // If the and is only masking out bits that cannot effect the shift,
2173 // eliminate the and.
2174 unsigned NumBits = MVT::getSizeInBits(VT);
2175 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2176 return getNode(Opcode, VT, N1, N2.getOperand(0));
2177 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002178 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002179#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002180 }
2181
Chris Lattner27e9b412005-05-11 18:57:39 +00002182 // Memoize this node if possible.
2183 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002184 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002185 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002186 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002187 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002188 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002189 void *IP = 0;
2190 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2191 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002192 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002193 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002194 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002195 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002196 }
2197
Chris Lattnerc3aae252005-01-07 07:46:32 +00002198 AllNodes.push_back(N);
2199 return SDOperand(N, 0);
2200}
2201
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2203 SDOperand N1, SDOperand N2, SDOperand N3) {
2204 // Perform various simplifications.
2205 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2206 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002207 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002208 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002209 // Use FoldSetCC to simplify SETCC's.
2210 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002211 if (Simp.Val) return Simp;
2212 break;
2213 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002214 case ISD::SELECT:
2215 if (N1C)
2216 if (N1C->getValue())
2217 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002218 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002219 return N3; // select false, X, Y -> Y
2220
2221 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002222 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002223 case ISD::BRCOND:
2224 if (N2C)
2225 if (N2C->getValue()) // Unconditional branch
2226 return getNode(ISD::BR, MVT::Other, N1, N3);
2227 else
2228 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002229 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002230 case ISD::VECTOR_SHUFFLE:
2231 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2232 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2233 N3.getOpcode() == ISD::BUILD_VECTOR &&
2234 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2235 "Illegal VECTOR_SHUFFLE node!");
2236 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002237 case ISD::BIT_CONVERT:
2238 // Fold bit_convert nodes from a type to themselves.
2239 if (N1.getValueType() == VT)
2240 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002241 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002242 }
2243
Chris Lattner43247a12005-08-25 19:12:10 +00002244 // Memoize node if it doesn't produce a flag.
2245 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002246 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002247 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002248 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002249 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002250 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002251 void *IP = 0;
2252 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2253 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002254 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002255 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002256 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002257 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002258 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002259 AllNodes.push_back(N);
2260 return SDOperand(N, 0);
2261}
2262
2263SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002264 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002265 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002266 SDOperand Ops[] = { N1, N2, N3, N4 };
2267 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002268}
2269
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002270SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2271 SDOperand N1, SDOperand N2, SDOperand N3,
2272 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002273 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2274 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002275}
2276
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002277SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2278 SDOperand Src, SDOperand Size,
2279 SDOperand Align,
2280 SDOperand AlwaysInline) {
2281 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2282 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2283}
2284
2285SDOperand SelectionDAG::getMemmove(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::MEMMOVE, MVT::Other, Ops, 6);
2291}
2292
2293SDOperand SelectionDAG::getMemset(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::MEMSET, MVT::Other, Ops, 6);
2299}
2300
Evan Cheng7038daf2005-12-10 00:37:58 +00002301SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2302 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002303 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002304 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002305 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2306 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002307 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002308 Ty = MVT::getTypeForValueType(VT);
2309 } else if (SV) {
2310 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2311 assert(PT && "Value for load must be a pointer");
2312 Ty = PT->getElementType();
2313 }
2314 assert(Ty && "Could not get type information for load");
2315 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2316 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002317 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002318 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002319 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002320 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002321 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002322 ID.AddInteger(ISD::UNINDEXED);
2323 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002324 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002325 ID.AddInteger(Alignment);
2326 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002327 void *IP = 0;
2328 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2329 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002330 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002331 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2332 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002333 CSEMap.InsertNode(N, IP);
2334 AllNodes.push_back(N);
2335 return SDOperand(N, 0);
2336}
2337
2338SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002339 SDOperand Chain, SDOperand Ptr,
2340 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002341 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002342 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002343 // If they are asking for an extending load from/to the same thing, return a
2344 // normal load.
2345 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002346 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002347
2348 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002349 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002350 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002351 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2352 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002353 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2354 "Cannot sign/zero extend a FP/Vector load!");
2355 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2356 "Cannot convert from FP to Int or Int -> FP!");
2357
Dan Gohman575e2f42007-06-04 15:49:41 +00002358 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2359 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002360 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002361 Ty = MVT::getTypeForValueType(VT);
2362 } else if (SV) {
2363 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2364 assert(PT && "Value for load must be a pointer");
2365 Ty = PT->getElementType();
2366 }
2367 assert(Ty && "Could not get type information for load");
2368 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2369 }
Evan Cheng466685d2006-10-09 20:57:25 +00002370 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002371 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002372 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002373 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002374 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002375 ID.AddInteger(ISD::UNINDEXED);
2376 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002377 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002378 ID.AddInteger(Alignment);
2379 ID.AddInteger(isVolatile);
2380 void *IP = 0;
2381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2382 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002383 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002384 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002385 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002386 AllNodes.push_back(N);
2387 return SDOperand(N, 0);
2388}
2389
Evan Cheng144d8f02006-11-09 17:55:04 +00002390SDOperand
2391SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2392 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002393 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002394 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2395 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002396 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002397 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002398 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002399 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002400 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002401 ID.AddInteger(AM);
2402 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002403 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002404 ID.AddInteger(LD->getAlignment());
2405 ID.AddInteger(LD->isVolatile());
2406 void *IP = 0;
2407 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2408 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002409 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002410 LD->getExtensionType(), LD->getLoadedVT(),
2411 LD->getSrcValue(), LD->getSrcValueOffset(),
2412 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002413 CSEMap.InsertNode(N, IP);
2414 AllNodes.push_back(N);
2415 return SDOperand(N, 0);
2416}
2417
Jeff Cohend41b30d2006-11-05 19:31:28 +00002418SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002419 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002420 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002421 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422
Dan Gohman575e2f42007-06-04 15:49:41 +00002423 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2424 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002425 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002426 Ty = MVT::getTypeForValueType(VT);
2427 } else if (SV) {
2428 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2429 assert(PT && "Value for store must be a pointer");
2430 Ty = PT->getElementType();
2431 }
2432 assert(Ty && "Could not get type information for store");
2433 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2434 }
Evan Chengad071e12006-10-05 22:57:11 +00002435 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002436 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002437 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002438 FoldingSetNodeID ID;
2439 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002440 ID.AddInteger(ISD::UNINDEXED);
2441 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002442 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002443 ID.AddInteger(Alignment);
2444 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002445 void *IP = 0;
2446 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2447 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002448 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002449 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002450 CSEMap.InsertNode(N, IP);
2451 AllNodes.push_back(N);
2452 return SDOperand(N, 0);
2453}
2454
Jeff Cohend41b30d2006-11-05 19:31:28 +00002455SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002456 SDOperand Ptr, const Value *SV,
2457 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002458 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002459 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002460
2461 if (VT == SVT)
2462 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002463
Duncan Sandsaf47b112007-10-16 09:56:48 +00002464 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2465 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002466 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2467 "Can't do FP-INT conversion!");
2468
Dan Gohman575e2f42007-06-04 15:49:41 +00002469 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2470 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002471 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002472 Ty = MVT::getTypeForValueType(VT);
2473 } else if (SV) {
2474 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2475 assert(PT && "Value for store must be a pointer");
2476 Ty = PT->getElementType();
2477 }
2478 assert(Ty && "Could not get type information for store");
2479 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2480 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002481 SDVTList VTs = getVTList(MVT::Other);
2482 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002483 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002484 FoldingSetNodeID ID;
2485 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002486 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002487 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002488 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002489 ID.AddInteger(Alignment);
2490 ID.AddInteger(isVolatile);
2491 void *IP = 0;
2492 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2493 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002494 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002495 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002496 CSEMap.InsertNode(N, IP);
2497 AllNodes.push_back(N);
2498 return SDOperand(N, 0);
2499}
2500
Evan Cheng144d8f02006-11-09 17:55:04 +00002501SDOperand
2502SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2503 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002504 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2505 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2506 "Store is already a indexed store!");
2507 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2508 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2509 FoldingSetNodeID ID;
2510 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2511 ID.AddInteger(AM);
2512 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002513 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002514 ID.AddInteger(ST->getAlignment());
2515 ID.AddInteger(ST->isVolatile());
2516 void *IP = 0;
2517 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2518 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002519 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002520 ST->isTruncatingStore(), ST->getStoredVT(),
2521 ST->getSrcValue(), ST->getSrcValueOffset(),
2522 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002523 CSEMap.InsertNode(N, IP);
2524 AllNodes.push_back(N);
2525 return SDOperand(N, 0);
2526}
2527
Nate Begemanacc398c2006-01-25 18:21:52 +00002528SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2529 SDOperand Chain, SDOperand Ptr,
2530 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002531 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002532 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002533}
2534
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002535SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002536 const SDOperand *Ops, unsigned NumOps) {
2537 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002538 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002539 case 1: return getNode(Opcode, VT, Ops[0]);
2540 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2541 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002542 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002543 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002544
Chris Lattneref847df2005-04-09 03:27:28 +00002545 switch (Opcode) {
2546 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002547 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002548 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002549 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2550 "LHS and RHS of condition must have same type!");
2551 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2552 "True and False arms of SelectCC must have same type!");
2553 assert(Ops[2].getValueType() == VT &&
2554 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002555 break;
2556 }
2557 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002558 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002559 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2560 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002561 break;
2562 }
Chris Lattneref847df2005-04-09 03:27:28 +00002563 }
2564
Chris Lattner385328c2005-05-14 07:42:29 +00002565 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002566 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002567 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002568 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002569 FoldingSetNodeID ID;
2570 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002571 void *IP = 0;
2572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2573 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002574 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002575 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002576 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002577 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002578 }
Chris Lattneref847df2005-04-09 03:27:28 +00002579 AllNodes.push_back(N);
2580 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002581}
2582
Chris Lattner89c34632005-05-14 06:20:26 +00002583SDOperand SelectionDAG::getNode(unsigned Opcode,
2584 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002585 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002586 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2587 Ops, NumOps);
2588}
2589
2590SDOperand SelectionDAG::getNode(unsigned Opcode,
2591 const MVT::ValueType *VTs, unsigned NumVTs,
2592 const SDOperand *Ops, unsigned NumOps) {
2593 if (NumVTs == 1)
2594 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002595 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2596}
2597
2598SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2599 const SDOperand *Ops, unsigned NumOps) {
2600 if (VTList.NumVTs == 1)
2601 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002602
Chris Lattner5f056bf2005-07-10 01:55:33 +00002603 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002604 // FIXME: figure out how to safely handle things like
2605 // int foo(int x) { return 1 << (x & 255); }
2606 // int bar() { return foo(256); }
2607#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002608 case ISD::SRA_PARTS:
2609 case ISD::SRL_PARTS:
2610 case ISD::SHL_PARTS:
2611 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002612 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002613 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2614 else if (N3.getOpcode() == ISD::AND)
2615 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2616 // If the and is only masking out bits that cannot effect the shift,
2617 // eliminate the and.
2618 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2619 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2620 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2621 }
2622 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002623#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002624 }
Chris Lattner89c34632005-05-14 06:20:26 +00002625
Chris Lattner43247a12005-08-25 19:12:10 +00002626 // Memoize the node unless it returns a flag.
2627 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002628 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002629 FoldingSetNodeID ID;
2630 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002631 void *IP = 0;
2632 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2633 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002634 if (NumOps == 1)
2635 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2636 else if (NumOps == 2)
2637 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2638 else if (NumOps == 3)
2639 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2640 else
2641 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002642 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002643 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002644 if (NumOps == 1)
2645 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2646 else if (NumOps == 2)
2647 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2648 else if (NumOps == 3)
2649 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2650 else
2651 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002652 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002653 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002654 return SDOperand(N, 0);
2655}
2656
Dan Gohman08ce9762007-10-08 15:49:58 +00002657SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2658 return getNode(Opcode, VTList, 0, 0);
2659}
2660
2661SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2662 SDOperand N1) {
2663 SDOperand Ops[] = { N1 };
2664 return getNode(Opcode, VTList, Ops, 1);
2665}
2666
2667SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2668 SDOperand N1, SDOperand N2) {
2669 SDOperand Ops[] = { N1, N2 };
2670 return getNode(Opcode, VTList, Ops, 2);
2671}
2672
2673SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2674 SDOperand N1, SDOperand N2, SDOperand N3) {
2675 SDOperand Ops[] = { N1, N2, N3 };
2676 return getNode(Opcode, VTList, Ops, 3);
2677}
2678
2679SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2680 SDOperand N1, SDOperand N2, SDOperand N3,
2681 SDOperand N4) {
2682 SDOperand Ops[] = { N1, N2, N3, N4 };
2683 return getNode(Opcode, VTList, Ops, 4);
2684}
2685
2686SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2687 SDOperand N1, SDOperand N2, SDOperand N3,
2688 SDOperand N4, SDOperand N5) {
2689 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2690 return getNode(Opcode, VTList, Ops, 5);
2691}
2692
Chris Lattner70046e92006-08-15 17:46:01 +00002693SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002694 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002695}
2696
Chris Lattner70046e92006-08-15 17:46:01 +00002697SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002698 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2699 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002700 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002701 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002702 }
2703 std::vector<MVT::ValueType> V;
2704 V.push_back(VT1);
2705 V.push_back(VT2);
2706 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002707 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002708}
Chris Lattner70046e92006-08-15 17:46:01 +00002709SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2710 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002711 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002712 E = VTList.end(); I != E; ++I) {
2713 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2714 (*I)[2] == VT3)
2715 return makeVTList(&(*I)[0], 3);
2716 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002717 std::vector<MVT::ValueType> V;
2718 V.push_back(VT1);
2719 V.push_back(VT2);
2720 V.push_back(VT3);
2721 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002722 return makeVTList(&(*VTList.begin())[0], 3);
2723}
2724
2725SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2726 switch (NumVTs) {
2727 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002728 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002729 case 2: return getVTList(VTs[0], VTs[1]);
2730 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2731 default: break;
2732 }
2733
2734 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2735 E = VTList.end(); I != E; ++I) {
2736 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2737
2738 bool NoMatch = false;
2739 for (unsigned i = 2; i != NumVTs; ++i)
2740 if (VTs[i] != (*I)[i]) {
2741 NoMatch = true;
2742 break;
2743 }
2744 if (!NoMatch)
2745 return makeVTList(&*I->begin(), NumVTs);
2746 }
2747
2748 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2749 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002750}
2751
2752
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002753/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2754/// specified operands. If the resultant node already exists in the DAG,
2755/// this does not modify the specified node, instead it returns the node that
2756/// already exists. If the resultant node does not exist in the DAG, the
2757/// input node is returned. As a degenerate case, if you specify the same
2758/// input operands as the node already has, the input node is returned.
2759SDOperand SelectionDAG::
2760UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2761 SDNode *N = InN.Val;
2762 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2763
2764 // Check to see if there is no change.
2765 if (Op == N->getOperand(0)) return InN;
2766
2767 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002768 void *InsertPos = 0;
2769 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2770 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002771
2772 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002773 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002774 RemoveNodeFromCSEMaps(N);
2775
2776 // Now we update the operands.
2777 N->OperandList[0].Val->removeUser(N);
2778 Op.Val->addUser(N);
2779 N->OperandList[0] = Op;
2780
2781 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002782 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002783 return InN;
2784}
2785
2786SDOperand SelectionDAG::
2787UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2788 SDNode *N = InN.Val;
2789 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2790
2791 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002792 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2793 return InN; // No operands changed, just return the input node.
2794
2795 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002796 void *InsertPos = 0;
2797 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2798 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002799
2800 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002801 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002802 RemoveNodeFromCSEMaps(N);
2803
2804 // Now we update the operands.
2805 if (N->OperandList[0] != Op1) {
2806 N->OperandList[0].Val->removeUser(N);
2807 Op1.Val->addUser(N);
2808 N->OperandList[0] = Op1;
2809 }
2810 if (N->OperandList[1] != Op2) {
2811 N->OperandList[1].Val->removeUser(N);
2812 Op2.Val->addUser(N);
2813 N->OperandList[1] = Op2;
2814 }
2815
2816 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002817 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002818 return InN;
2819}
2820
2821SDOperand SelectionDAG::
2822UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002823 SDOperand Ops[] = { Op1, Op2, Op3 };
2824 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002825}
2826
2827SDOperand SelectionDAG::
2828UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2829 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002830 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2831 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002832}
2833
2834SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002835UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2836 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002837 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2838 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002839}
2840
2841
2842SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002843UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002844 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002845 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002846 "Update with wrong number of operands");
2847
2848 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002849 bool AnyChange = false;
2850 for (unsigned i = 0; i != NumOps; ++i) {
2851 if (Ops[i] != N->getOperand(i)) {
2852 AnyChange = true;
2853 break;
2854 }
2855 }
2856
2857 // No operands changed, just return the input node.
2858 if (!AnyChange) return InN;
2859
2860 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002861 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002862 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002863 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002864
2865 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002866 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002867 RemoveNodeFromCSEMaps(N);
2868
2869 // Now we update the operands.
2870 for (unsigned i = 0; i != NumOps; ++i) {
2871 if (N->OperandList[i] != Ops[i]) {
2872 N->OperandList[i].Val->removeUser(N);
2873 Ops[i].Val->addUser(N);
2874 N->OperandList[i] = Ops[i];
2875 }
2876 }
2877
2878 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002879 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002880 return InN;
2881}
2882
2883
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002884/// MorphNodeTo - This frees the operands of the current node, resets the
2885/// opcode, types, and operands to the specified value. This should only be
2886/// used by the SelectionDAG class.
2887void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2888 const SDOperand *Ops, unsigned NumOps) {
2889 NodeType = Opc;
2890 ValueList = L.VTs;
2891 NumValues = L.NumVTs;
2892
2893 // Clear the operands list, updating used nodes to remove this from their
2894 // use list.
2895 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2896 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002897
2898 // If NumOps is larger than the # of operands we currently have, reallocate
2899 // the operand list.
2900 if (NumOps > NumOperands) {
2901 if (OperandsNeedDelete)
2902 delete [] OperandList;
2903 OperandList = new SDOperand[NumOps];
2904 OperandsNeedDelete = true;
2905 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002906
2907 // Assign the new operands.
2908 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002909
2910 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2911 OperandList[i] = Ops[i];
2912 SDNode *N = OperandList[i].Val;
2913 N->Uses.push_back(this);
2914 }
2915}
Chris Lattner149c58c2005-08-16 18:17:10 +00002916
2917/// SelectNodeTo - These are used for target selectors to *mutate* the
2918/// specified node to have the specified return type, Target opcode, and
2919/// operands. Note that target opcodes are stored as
2920/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002921///
2922/// Note that SelectNodeTo returns the resultant node. If there is already a
2923/// node of the specified opcode and operands, it returns that node instead of
2924/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002925SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2926 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002927 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002928 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002929 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002930 void *IP = 0;
2931 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002932 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002933
Chris Lattner7651fa42005-08-24 23:00:29 +00002934 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002935
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002936 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002937
Chris Lattner4a283e92006-08-11 18:38:11 +00002938 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002939 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002940}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002941
Evan Cheng95514ba2006-08-26 08:00:10 +00002942SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2943 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002944 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002945 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002946 SDOperand Ops[] = { Op1 };
2947
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, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002950 void *IP = 0;
2951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002952 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002953
Chris Lattner149c58c2005-08-16 18:17:10 +00002954 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002955 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002956 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002957 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002958}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002959
Evan Cheng95514ba2006-08-26 08:00:10 +00002960SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2961 MVT::ValueType VT, SDOperand Op1,
2962 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002963 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002964 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002965 SDOperand Ops[] = { Op1, Op2 };
2966
Jim Laskey583bd472006-10-27 23:46:08 +00002967 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002968 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002969 void *IP = 0;
2970 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002971 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002972
Chris Lattner149c58c2005-08-16 18:17:10 +00002973 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002974
Chris Lattner63e3f142007-02-04 07:28:00 +00002975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002976
Chris Lattnera5682852006-08-07 23:03:03 +00002977 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002978 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002979}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002980
Evan Cheng95514ba2006-08-26 08:00:10 +00002981SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2982 MVT::ValueType VT, SDOperand Op1,
2983 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002984 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002985 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002986 SDOperand Ops[] = { Op1, Op2, Op3 };
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, 3);
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, 3);
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 Lattnerc975e1d2005-08-21 22:30:30 +00003000
Evan Cheng95514ba2006-08-26 08:00:10 +00003001SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003002 MVT::ValueType VT, const SDOperand *Ops,
3003 unsigned NumOps) {
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);
Jim Laskey583bd472006-10-27 23:46:08 +00003006 FoldingSetNodeID ID;
3007 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003008 void *IP = 0;
3009 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003010 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003011
Chris Lattner6b09a292005-08-21 18:49:33 +00003012 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003014
Chris Lattnera5682852006-08-07 23:03:03 +00003015 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003016 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003017}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003018
Evan Cheng95514ba2006-08-26 08:00:10 +00003019SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3020 MVT::ValueType VT1, MVT::ValueType VT2,
3021 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003022 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003023 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003024 SDOperand Ops[] = { Op1, Op2 };
3025 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003026 void *IP = 0;
3027 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003028 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003029
Chris Lattner0fb094f2005-11-19 01:44:53 +00003030 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003031 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003032 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003033 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003034}
3035
Evan Cheng95514ba2006-08-26 08:00:10 +00003036SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3037 MVT::ValueType VT1, MVT::ValueType VT2,
3038 SDOperand Op1, SDOperand Op2,
3039 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003040 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003041 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003042 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003043 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003044 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003045 void *IP = 0;
3046 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003047 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003048
Chris Lattner0fb094f2005-11-19 01:44:53 +00003049 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003050
Chris Lattner63e3f142007-02-04 07:28:00 +00003051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
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
Chris Lattner0fb094f2005-11-19 01:44:53 +00003056
Evan Cheng6ae46c42006-02-09 07:15:23 +00003057/// getTargetNode - These are used for target selectors to create a new node
3058/// with specified return type(s), target opcode, and operands.
3059///
3060/// Note that getTargetNode returns the resultant node. If there is already a
3061/// node of the specified opcode and operands, it returns that node instead of
3062/// the current one.
3063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3064 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3065}
3066SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3067 SDOperand Op1) {
3068 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3069}
3070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3071 SDOperand Op1, SDOperand Op2) {
3072 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3073}
3074SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003075 SDOperand Op1, SDOperand Op2,
3076 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003077 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3078}
3079SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003080 const SDOperand *Ops, unsigned NumOps) {
3081 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003082}
3083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003084 MVT::ValueType VT2) {
3085 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3086 SDOperand Op;
3087 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3088}
3089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003090 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003091 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003092 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003093}
3094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003095 MVT::ValueType VT2, SDOperand Op1,
3096 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003097 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003098 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003099 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003100}
3101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003102 MVT::ValueType VT2, SDOperand Op1,
3103 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003104 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003105 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003106 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003107}
Evan Cheng694481e2006-08-27 08:08:54 +00003108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3109 MVT::ValueType VT2,
3110 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003111 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003112 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003113}
3114SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3115 MVT::ValueType VT2, MVT::ValueType VT3,
3116 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003117 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
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, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003120}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3122 MVT::ValueType VT2, MVT::ValueType VT3,
3123 SDOperand Op1, SDOperand Op2,
3124 SDOperand Op3) {
3125 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3126 SDOperand Ops[] = { Op1, Op2, Op3 };
3127 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3128}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003129SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003130 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003131 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003132 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3133 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003134}
Evan Cheng05e69c12007-09-12 23:39:49 +00003135SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3136 MVT::ValueType VT2, MVT::ValueType VT3,
3137 MVT::ValueType VT4,
3138 const SDOperand *Ops, unsigned NumOps) {
3139 std::vector<MVT::ValueType> VTList;
3140 VTList.push_back(VT1);
3141 VTList.push_back(VT2);
3142 VTList.push_back(VT3);
3143 VTList.push_back(VT4);
3144 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3145 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3146}
Evan Cheng39305cf2007-10-05 01:10:49 +00003147SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3148 std::vector<MVT::ValueType> &ResultTys,
3149 const SDOperand *Ops, unsigned NumOps) {
3150 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3151 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3152 Ops, NumOps).Val;
3153}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003154
Evan Cheng99157a02006-08-07 22:13:29 +00003155/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003156/// This can cause recursive merging of nodes in the DAG.
3157///
Chris Lattner8b52f212005-08-26 18:36:28 +00003158/// This version assumes From/To have a single result value.
3159///
Chris Lattner1e111c72005-09-07 05:37:01 +00003160void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3161 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003162 SDNode *From = FromN.Val, *To = ToN.Val;
3163 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3164 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003165 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003166
Chris Lattner8b8749f2005-08-17 19:00:20 +00003167 while (!From->use_empty()) {
3168 // Process users until they are all gone.
3169 SDNode *U = *From->use_begin();
3170
3171 // This node is about to morph, remove its old self from the CSE maps.
3172 RemoveNodeFromCSEMaps(U);
3173
Chris Lattner65113b22005-11-08 22:07:03 +00003174 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3175 I != E; ++I)
3176 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003177 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003178 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003179 To->addUser(U);
3180 }
3181
3182 // Now that we have modified U, add it back to the CSE maps. If it already
3183 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003184 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3185 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003186 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003187 if (Deleted) Deleted->push_back(U);
3188 DeleteNodeNotInCSEMaps(U);
3189 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003190 }
3191}
3192
3193/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3194/// This can cause recursive merging of nodes in the DAG.
3195///
3196/// This version assumes From/To have matching types and numbers of result
3197/// values.
3198///
Chris Lattner1e111c72005-09-07 05:37:01 +00003199void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3200 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003201 assert(From != To && "Cannot replace uses of with self");
3202 assert(From->getNumValues() == To->getNumValues() &&
3203 "Cannot use this version of ReplaceAllUsesWith!");
3204 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003205 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003206 return;
3207 }
3208
3209 while (!From->use_empty()) {
3210 // Process users until they are all gone.
3211 SDNode *U = *From->use_begin();
3212
3213 // This node is about to morph, remove its old self from the CSE maps.
3214 RemoveNodeFromCSEMaps(U);
3215
Chris Lattner65113b22005-11-08 22:07:03 +00003216 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3217 I != E; ++I)
3218 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003219 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003220 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003221 To->addUser(U);
3222 }
3223
3224 // Now that we have modified U, add it back to the CSE maps. If it already
3225 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003226 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3227 ReplaceAllUsesWith(U, Existing, Deleted);
3228 // U is now dead.
3229 if (Deleted) Deleted->push_back(U);
3230 DeleteNodeNotInCSEMaps(U);
3231 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003232 }
3233}
3234
Chris Lattner8b52f212005-08-26 18:36:28 +00003235/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3236/// This can cause recursive merging of nodes in the DAG.
3237///
3238/// This version can replace From with any result values. To must match the
3239/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003240void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003241 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003242 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003243 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003244 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003245 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003246 return;
3247 }
3248
3249 while (!From->use_empty()) {
3250 // Process users until they are all gone.
3251 SDNode *U = *From->use_begin();
3252
3253 // This node is about to morph, remove its old self from the CSE maps.
3254 RemoveNodeFromCSEMaps(U);
3255
Chris Lattner65113b22005-11-08 22:07:03 +00003256 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3257 I != E; ++I)
3258 if (I->Val == From) {
3259 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003260 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003261 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003262 ToOp.Val->addUser(U);
3263 }
3264
3265 // Now that we have modified U, add it back to the CSE maps. If it already
3266 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003267 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3268 ReplaceAllUsesWith(U, Existing, Deleted);
3269 // U is now dead.
3270 if (Deleted) Deleted->push_back(U);
3271 DeleteNodeNotInCSEMaps(U);
3272 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003273 }
3274}
3275
Chris Lattner012f2412006-02-17 21:58:01 +00003276/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3277/// uses of other values produced by From.Val alone. The Deleted vector is
3278/// handled the same was as for ReplaceAllUsesWith.
3279void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003280 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003281 assert(From != To && "Cannot replace a value with itself");
3282 // Handle the simple, trivial, case efficiently.
3283 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003284 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003285 return;
3286 }
3287
Chris Lattnercf5640b2007-02-04 00:14:31 +00003288 // Get all of the users of From.Val. We want these in a nice,
3289 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3290 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003291
Chris Lattner01d029b2007-10-15 06:10:22 +00003292 std::vector<SDNode*> LocalDeletionVector;
3293
3294 // Pick a deletion vector to use. If the user specified one, use theirs,
3295 // otherwise use a local one.
3296 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003297 while (!Users.empty()) {
3298 // We know that this user uses some value of From. If it is the right
3299 // value, update it.
3300 SDNode *User = Users.back();
3301 Users.pop_back();
3302
Chris Lattner01d029b2007-10-15 06:10:22 +00003303 // Scan for an operand that matches From.
3304 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3305 for (; Op != E; ++Op)
3306 if (*Op == From) break;
3307
3308 // If there are no matches, the user must use some other result of From.
3309 if (Op == E) continue;
3310
3311 // Okay, we know this user needs to be updated. Remove its old self
3312 // from the CSE maps.
3313 RemoveNodeFromCSEMaps(User);
3314
3315 // Update all operands that match "From".
3316 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003317 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003318 From.Val->removeUser(User);
3319 *Op = To;
3320 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003321 }
3322 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003323
3324 // Now that we have modified User, add it back to the CSE maps. If it
3325 // already exists there, recursively merge the results together.
3326 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3327 if (!Existing) continue; // Continue on to next user.
3328
3329 // If there was already an existing matching node, use ReplaceAllUsesWith
3330 // to replace the dead one with the existing one. However, this can cause
3331 // recursive merging of other unrelated nodes down the line. The merging
3332 // can cause deletion of nodes that used the old value. In this case,
3333 // we have to be certain to remove them from the Users set.
3334 unsigned NumDeleted = DeleteVector->size();
3335 ReplaceAllUsesWith(User, Existing, DeleteVector);
3336
3337 // User is now dead.
3338 DeleteVector->push_back(User);
3339 DeleteNodeNotInCSEMaps(User);
3340
3341 // We have to be careful here, because ReplaceAllUsesWith could have
3342 // deleted a user of From, which means there may be dangling pointers
3343 // in the "Users" setvector. Scan over the deleted node pointers and
3344 // remove them from the setvector.
3345 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3346 Users.remove((*DeleteVector)[i]);
3347
3348 // If the user doesn't need the set of deleted elements, don't retain them
3349 // to the next loop iteration.
3350 if (Deleted == 0)
3351 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003352 }
3353}
3354
Chris Lattner7b2880c2005-08-24 22:44:39 +00003355
Evan Chenge6f35d82006-08-01 08:20:41 +00003356/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3357/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003358unsigned SelectionDAG::AssignNodeIds() {
3359 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003360 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3361 SDNode *N = I;
3362 N->setNodeId(Id++);
3363 }
3364 return Id;
3365}
3366
Evan Chenge6f35d82006-08-01 08:20:41 +00003367/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003368/// based on their topological order. It returns the maximum id and a vector
3369/// of the SDNodes* in assigned order by reference.
3370unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003371 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003372 std::vector<unsigned> InDegree(DAGSize);
3373 std::vector<SDNode*> Sources;
3374
3375 // Use a two pass approach to avoid using a std::map which is slow.
3376 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003377 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3378 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003379 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003380 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003381 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003382 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003383 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003384 }
3385
Evan Cheng99157a02006-08-07 22:13:29 +00003386 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003387 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003388 SDNode *N = Sources.back();
3389 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003390 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003391 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3392 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003393 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003394 if (Degree == 0)
3395 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003396 }
3397 }
3398
Evan Chengc384d6c2006-08-02 22:00:34 +00003399 // Second pass, assign the actual topological order as node ids.
3400 Id = 0;
3401 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3402 TI != TE; ++TI)
3403 (*TI)->setNodeId(Id++);
3404
3405 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003406}
3407
3408
Evan Cheng091cba12006-07-27 06:39:06 +00003409
Jim Laskey58b968b2005-08-17 20:08:02 +00003410//===----------------------------------------------------------------------===//
3411// SDNode Class
3412//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003413
Chris Lattner917d2c92006-07-19 00:00:37 +00003414// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003415void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003416void UnarySDNode::ANCHOR() {}
3417void BinarySDNode::ANCHOR() {}
3418void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003419void HandleSDNode::ANCHOR() {}
3420void StringSDNode::ANCHOR() {}
3421void ConstantSDNode::ANCHOR() {}
3422void ConstantFPSDNode::ANCHOR() {}
3423void GlobalAddressSDNode::ANCHOR() {}
3424void FrameIndexSDNode::ANCHOR() {}
3425void JumpTableSDNode::ANCHOR() {}
3426void ConstantPoolSDNode::ANCHOR() {}
3427void BasicBlockSDNode::ANCHOR() {}
3428void SrcValueSDNode::ANCHOR() {}
3429void RegisterSDNode::ANCHOR() {}
3430void ExternalSymbolSDNode::ANCHOR() {}
3431void CondCodeSDNode::ANCHOR() {}
3432void VTSDNode::ANCHOR() {}
3433void LoadSDNode::ANCHOR() {}
3434void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003435
Chris Lattner48b85922007-02-04 02:41:42 +00003436HandleSDNode::~HandleSDNode() {
3437 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003438 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003439}
3440
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003441GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3442 MVT::ValueType VT, int o)
3443 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003444 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003445 // Thread Local
3446 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3447 // Non Thread Local
3448 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3449 getSDVTList(VT)), Offset(o) {
3450 TheGlobal = const_cast<GlobalValue*>(GA);
3451}
Chris Lattner48b85922007-02-04 02:41:42 +00003452
Jim Laskey583bd472006-10-27 23:46:08 +00003453/// Profile - Gather unique data for the node.
3454///
3455void SDNode::Profile(FoldingSetNodeID &ID) {
3456 AddNodeIDNode(ID, this);
3457}
3458
Chris Lattnera3255112005-11-08 23:30:28 +00003459/// getValueTypeList - Return a pointer to the specified value type.
3460///
3461MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003462 if (MVT::isExtendedVT(VT)) {
3463 static std::set<MVT::ValueType> EVTs;
3464 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3465 } else {
3466 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3467 VTs[VT] = VT;
3468 return &VTs[VT];
3469 }
Chris Lattnera3255112005-11-08 23:30:28 +00003470}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003471
Chris Lattner5c884562005-01-12 18:37:47 +00003472/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3473/// indicated value. This method ignores uses of other values defined by this
3474/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003475bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003476 assert(Value < getNumValues() && "Bad value!");
3477
3478 // If there is only one value, this is easy.
3479 if (getNumValues() == 1)
3480 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003481 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003482
Evan Cheng4ee62112006-02-05 06:29:23 +00003483 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003484
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003485 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003486
Chris Lattnerf83482d2006-08-16 20:59:32 +00003487 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003488 SDNode *User = *UI;
3489 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003490 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003491 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3492 if (User->getOperand(i) == TheValue) {
3493 if (NUses == 0)
3494 return false; // too many uses
3495 --NUses;
3496 }
3497 }
3498
3499 // Found exactly the right number of uses?
3500 return NUses == 0;
3501}
3502
3503
Evan Cheng33d55952007-08-02 05:29:38 +00003504/// hasAnyUseOfValue - Return true if there are any use of the indicated
3505/// value. This method ignores uses of other values defined by this operation.
3506bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3507 assert(Value < getNumValues() && "Bad value!");
3508
3509 if (use_size() == 0) return false;
3510
3511 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3512
3513 SmallPtrSet<SDNode*, 32> UsersHandled;
3514
3515 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3516 SDNode *User = *UI;
3517 if (User->getNumOperands() == 1 ||
3518 UsersHandled.insert(User)) // First time we've seen this?
3519 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3520 if (User->getOperand(i) == TheValue) {
3521 return true;
3522 }
3523 }
3524
3525 return false;
3526}
3527
3528
Evan Chenge6e97e62006-11-03 07:31:32 +00003529/// isOnlyUse - Return true if this node is the only use of N.
3530///
Evan Cheng4ee62112006-02-05 06:29:23 +00003531bool SDNode::isOnlyUse(SDNode *N) const {
3532 bool Seen = false;
3533 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3534 SDNode *User = *I;
3535 if (User == this)
3536 Seen = true;
3537 else
3538 return false;
3539 }
3540
3541 return Seen;
3542}
3543
Evan Chenge6e97e62006-11-03 07:31:32 +00003544/// isOperand - Return true if this node is an operand of N.
3545///
Evan Chengbfa284f2006-03-03 06:42:32 +00003546bool SDOperand::isOperand(SDNode *N) const {
3547 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3548 if (*this == N->getOperand(i))
3549 return true;
3550 return false;
3551}
3552
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003553bool SDNode::isOperand(SDNode *N) const {
3554 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3555 if (this == N->OperandList[i].Val)
3556 return true;
3557 return false;
3558}
Evan Cheng4ee62112006-02-05 06:29:23 +00003559
Evan Chengc5fc57d2006-11-03 03:05:24 +00003560static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003561 SmallPtrSet<SDNode *, 32> &Visited) {
3562 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003563 return;
3564
3565 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3566 SDNode *Op = N->getOperand(i).Val;
3567 if (Op == P) {
3568 found = true;
3569 return;
3570 }
3571 findPredecessor(Op, P, found, Visited);
3572 }
3573}
3574
Evan Chenge6e97e62006-11-03 07:31:32 +00003575/// isPredecessor - Return true if this node is a predecessor of N. This node
3576/// is either an operand of N or it can be reached by recursively traversing
3577/// up the operands.
3578/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003579bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003580 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003581 bool found = false;
3582 findPredecessor(N, this, found, Visited);
3583 return found;
3584}
3585
Evan Chengc5484282006-10-04 00:56:09 +00003586uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3587 assert(Num < NumOperands && "Invalid child # of SDNode!");
3588 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3589}
3590
Reid Spencer577cc322007-04-01 07:32:19 +00003591std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003592 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003593 default:
3594 if (getOpcode() < ISD::BUILTIN_OP_END)
3595 return "<<Unknown DAG Node>>";
3596 else {
Evan Cheng72261582005-12-20 06:22:03 +00003597 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003598 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003599 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3600 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003601
Evan Cheng72261582005-12-20 06:22:03 +00003602 TargetLowering &TLI = G->getTargetLoweringInfo();
3603 const char *Name =
3604 TLI.getTargetNodeName(getOpcode());
3605 if (Name) return Name;
3606 }
3607
3608 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003609 }
3610
Andrew Lenharth95762122005-03-31 21:24:06 +00003611 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003612 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003613 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003614 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003615 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003616 case ISD::AssertSext: return "AssertSext";
3617 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003618
3619 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003620 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003621 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003622 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003623
3624 case ISD::Constant: return "Constant";
3625 case ISD::ConstantFP: return "ConstantFP";
3626 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003627 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003628 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003629 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003630 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003631 case ISD::RETURNADDR: return "RETURNADDR";
3632 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003633 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003634 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3635 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003636 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003637 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003638 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003639 case ISD::INTRINSIC_WO_CHAIN: {
3640 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3641 return Intrinsic::getName((Intrinsic::ID)IID);
3642 }
3643 case ISD::INTRINSIC_VOID:
3644 case ISD::INTRINSIC_W_CHAIN: {
3645 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003646 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003647 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003648
Chris Lattnerb2827b02006-03-19 00:52:58 +00003649 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003650 case ISD::TargetConstant: return "TargetConstant";
3651 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003652 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003653 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003654 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003655 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003656 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003657 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003658
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003659 case ISD::CopyToReg: return "CopyToReg";
3660 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003661 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003662 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003663 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003664 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003665 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003666 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003667 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003668
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003669 // Unary operators
3670 case ISD::FABS: return "fabs";
3671 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003672 case ISD::FSQRT: return "fsqrt";
3673 case ISD::FSIN: return "fsin";
3674 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003675 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003676 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003677
3678 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003679 case ISD::ADD: return "add";
3680 case ISD::SUB: return "sub";
3681 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003682 case ISD::MULHU: return "mulhu";
3683 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003684 case ISD::SDIV: return "sdiv";
3685 case ISD::UDIV: return "udiv";
3686 case ISD::SREM: return "srem";
3687 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003688 case ISD::SMUL_LOHI: return "smul_lohi";
3689 case ISD::UMUL_LOHI: return "umul_lohi";
3690 case ISD::SDIVREM: return "sdivrem";
3691 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003692 case ISD::AND: return "and";
3693 case ISD::OR: return "or";
3694 case ISD::XOR: return "xor";
3695 case ISD::SHL: return "shl";
3696 case ISD::SRA: return "sra";
3697 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003698 case ISD::ROTL: return "rotl";
3699 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003700 case ISD::FADD: return "fadd";
3701 case ISD::FSUB: return "fsub";
3702 case ISD::FMUL: return "fmul";
3703 case ISD::FDIV: return "fdiv";
3704 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003705 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003706
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003707 case ISD::SETCC: return "setcc";
3708 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003709 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003710 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003711 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003712 case ISD::CONCAT_VECTORS: return "concat_vectors";
3713 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003714 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003715 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003716 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003717 case ISD::ADDC: return "addc";
3718 case ISD::ADDE: return "adde";
3719 case ISD::SUBC: return "subc";
3720 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003721 case ISD::SHL_PARTS: return "shl_parts";
3722 case ISD::SRA_PARTS: return "sra_parts";
3723 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003724
3725 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3726 case ISD::INSERT_SUBREG: return "insert_subreg";
3727
Chris Lattner7f644642005-04-28 21:44:03 +00003728 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003729 case ISD::SIGN_EXTEND: return "sign_extend";
3730 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003731 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003732 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003733 case ISD::TRUNCATE: return "truncate";
3734 case ISD::FP_ROUND: return "fp_round";
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003735 case ISD::FLT_ROUNDS: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003736 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003737 case ISD::FP_EXTEND: return "fp_extend";
3738
3739 case ISD::SINT_TO_FP: return "sint_to_fp";
3740 case ISD::UINT_TO_FP: return "uint_to_fp";
3741 case ISD::FP_TO_SINT: return "fp_to_sint";
3742 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003743 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003744
3745 // Control flow instructions
3746 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003747 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003748 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003749 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003750 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003751 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003752 case ISD::CALLSEQ_START: return "callseq_start";
3753 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003754
3755 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003756 case ISD::LOAD: return "load";
3757 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003758 case ISD::VAARG: return "vaarg";
3759 case ISD::VACOPY: return "vacopy";
3760 case ISD::VAEND: return "vaend";
3761 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003762 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003763 case ISD::EXTRACT_ELEMENT: return "extract_element";
3764 case ISD::BUILD_PAIR: return "build_pair";
3765 case ISD::STACKSAVE: return "stacksave";
3766 case ISD::STACKRESTORE: return "stackrestore";
3767
3768 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003769 case ISD::MEMSET: return "memset";
3770 case ISD::MEMCPY: return "memcpy";
3771 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003772
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003773 // Bit manipulation
3774 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003775 case ISD::CTPOP: return "ctpop";
3776 case ISD::CTTZ: return "cttz";
3777 case ISD::CTLZ: return "ctlz";
3778
Chris Lattner36ce6912005-11-29 06:21:05 +00003779 // Debug info
3780 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003781 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003782
Duncan Sands36397f52007-07-27 12:58:54 +00003783 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003784 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003785
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003786 case ISD::CONDCODE:
3787 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003788 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003789 case ISD::SETOEQ: return "setoeq";
3790 case ISD::SETOGT: return "setogt";
3791 case ISD::SETOGE: return "setoge";
3792 case ISD::SETOLT: return "setolt";
3793 case ISD::SETOLE: return "setole";
3794 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003795
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003796 case ISD::SETO: return "seto";
3797 case ISD::SETUO: return "setuo";
3798 case ISD::SETUEQ: return "setue";
3799 case ISD::SETUGT: return "setugt";
3800 case ISD::SETUGE: return "setuge";
3801 case ISD::SETULT: return "setult";
3802 case ISD::SETULE: return "setule";
3803 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003804
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003805 case ISD::SETEQ: return "seteq";
3806 case ISD::SETGT: return "setgt";
3807 case ISD::SETGE: return "setge";
3808 case ISD::SETLT: return "setlt";
3809 case ISD::SETLE: return "setle";
3810 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003811 }
3812 }
3813}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003814
Evan Cheng144d8f02006-11-09 17:55:04 +00003815const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003816 switch (AM) {
3817 default:
3818 return "";
3819 case ISD::PRE_INC:
3820 return "<pre-inc>";
3821 case ISD::PRE_DEC:
3822 return "<pre-dec>";
3823 case ISD::POST_INC:
3824 return "<post-inc>";
3825 case ISD::POST_DEC:
3826 return "<post-dec>";
3827 }
3828}
3829
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003830void SDNode::dump() const { dump(0); }
3831void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003832 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003833
3834 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003835 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003836 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003837 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003838 else
Bill Wendling832171c2006-12-07 20:04:42 +00003839 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003840 }
Bill Wendling832171c2006-12-07 20:04:42 +00003841 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003842
Bill Wendling832171c2006-12-07 20:04:42 +00003843 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003844 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003845 if (i) cerr << ", ";
3846 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003847 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003848 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003849 }
3850
Evan Chengce254432007-12-11 02:08:35 +00003851 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3852 SDNode *Mask = getOperand(2).Val;
3853 cerr << "<";
3854 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3855 if (i) cerr << ",";
3856 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3857 cerr << "u";
3858 else
3859 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3860 }
3861 cerr << ">";
3862 }
3863
Chris Lattnerc3aae252005-01-07 07:46:32 +00003864 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003865 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003866 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003867 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3868 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3869 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3870 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3871 else {
3872 cerr << "<APFloat(";
3873 CSDN->getValueAPF().convertToAPInt().dump();
3874 cerr << ")>";
3875 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003876 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003877 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003878 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003879 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003880 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003881 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003882 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003883 else
Bill Wendling832171c2006-12-07 20:04:42 +00003884 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003885 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003886 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003887 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003888 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003889 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003890 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003891 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003892 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003893 else
Bill Wendling832171c2006-12-07 20:04:42 +00003894 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003895 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003896 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003897 else
Bill Wendling832171c2006-12-07 20:04:42 +00003898 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003899 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003900 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003901 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3902 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003903 cerr << LBB->getName() << " ";
3904 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003905 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003906 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003907 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003908 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003909 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003910 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003911 } else if (const ExternalSymbolSDNode *ES =
3912 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003913 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003914 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3915 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003916 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003917 else
Bill Wendling832171c2006-12-07 20:04:42 +00003918 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003919 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003920 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003921 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00003922 const Value *SrcValue = LD->getSrcValue();
3923 int SrcOffset = LD->getSrcValueOffset();
3924 cerr << " <";
3925 if (SrcValue)
3926 cerr << SrcValue;
3927 else
3928 cerr << "null";
3929 cerr << ":" << SrcOffset << ">";
3930
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003931 bool doExt = true;
3932 switch (LD->getExtensionType()) {
3933 default: doExt = false; break;
3934 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003935 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003936 break;
3937 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003938 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003939 break;
3940 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003941 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003942 break;
3943 }
3944 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003945 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003946
Evan Cheng144d8f02006-11-09 17:55:04 +00003947 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003948 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003949 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00003950 if (LD->isVolatile())
3951 cerr << " <volatile>";
3952 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00003953 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00003954 const Value *SrcValue = ST->getSrcValue();
3955 int SrcOffset = ST->getSrcValueOffset();
3956 cerr << " <";
3957 if (SrcValue)
3958 cerr << SrcValue;
3959 else
3960 cerr << "null";
3961 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00003962
3963 if (ST->isTruncatingStore())
3964 cerr << " <trunc "
3965 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3966
3967 const char *AM = getIndexedModeName(ST->getAddressingMode());
3968 if (*AM)
3969 cerr << " " << AM;
3970 if (ST->isVolatile())
3971 cerr << " <volatile>";
3972 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003973 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003974}
3975
Chris Lattnerde202b32005-11-09 23:47:37 +00003976static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003977 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3978 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003979 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003980 else
Bill Wendling832171c2006-12-07 20:04:42 +00003981 cerr << "\n" << std::string(indent+2, ' ')
3982 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003983
Chris Lattnerea946cd2005-01-09 20:38:33 +00003984
Bill Wendling832171c2006-12-07 20:04:42 +00003985 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003986 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003987}
3988
Chris Lattnerc3aae252005-01-07 07:46:32 +00003989void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003990 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003991 std::vector<const SDNode*> Nodes;
3992 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3993 I != E; ++I)
3994 Nodes.push_back(I);
3995
Chris Lattner49d24712005-01-09 20:26:36 +00003996 std::sort(Nodes.begin(), Nodes.end());
3997
3998 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003999 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004000 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004001 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004002
Jim Laskey26f7fa72006-10-17 19:33:52 +00004003 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004004
Bill Wendling832171c2006-12-07 20:04:42 +00004005 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004006}
4007
Evan Chengd6594ae2006-09-12 21:00:35 +00004008const Type *ConstantPoolSDNode::getType() const {
4009 if (isMachineConstantPoolEntry())
4010 return Val.MachineCPVal->getType();
4011 return Val.ConstVal->getType();
4012}