blob: 6f0e98d8258953b397fc7b79bdf36eb0806a29e4 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Evan Chengbb81d972008-01-31 09:59:15 +0000175/// isDebugLabel - Return true if the specified node represents a debug
176/// label (i.e. ISD::LABEL or TargetInstrInfo::LANEL node and third operand
177/// is 0).
178bool ISD::isDebugLabel(const SDNode *N) {
179 SDOperand Zero;
180 if (N->getOpcode() == ISD::LABEL)
181 Zero = N->getOperand(2);
182 else if (N->isTargetOpcode() &&
183 N->getTargetOpcode() == TargetInstrInfo::LABEL)
184 // Chain moved to last operand.
185 Zero = N->getOperand(1);
186 else
187 return false;
188 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
189}
190
Chris Lattnerc3aae252005-01-07 07:46:32 +0000191/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
192/// when given the operation for (X op Y).
193ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
194 // To perform this operation, we just need to swap the L and G bits of the
195 // operation.
196 unsigned OldL = (Operation >> 2) & 1;
197 unsigned OldG = (Operation >> 1) & 1;
198 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
199 (OldL << 1) | // New G bit
200 (OldG << 2)); // New L bit.
201}
202
203/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
204/// 'op' is a valid SetCC operation.
205ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
206 unsigned Operation = Op;
207 if (isInteger)
208 Operation ^= 7; // Flip L, G, E bits, but not U.
209 else
210 Operation ^= 15; // Flip all of the condition bits.
211 if (Operation > ISD::SETTRUE2)
212 Operation &= ~8; // Don't let N and U bits get set.
213 return ISD::CondCode(Operation);
214}
215
216
217/// isSignedOp - For an integer comparison, return 1 if the comparison is a
218/// signed operation and 2 if the result is an unsigned comparison. Return zero
219/// if the operation does not depend on the sign of the input (setne and seteq).
220static int isSignedOp(ISD::CondCode Opcode) {
221 switch (Opcode) {
222 default: assert(0 && "Illegal integer setcc operation!");
223 case ISD::SETEQ:
224 case ISD::SETNE: return 0;
225 case ISD::SETLT:
226 case ISD::SETLE:
227 case ISD::SETGT:
228 case ISD::SETGE: return 1;
229 case ISD::SETULT:
230 case ISD::SETULE:
231 case ISD::SETUGT:
232 case ISD::SETUGE: return 2;
233 }
234}
235
236/// getSetCCOrOperation - Return the result of a logical OR between different
237/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
238/// returns SETCC_INVALID if it is not possible to represent the resultant
239/// comparison.
240ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
241 bool isInteger) {
242 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
243 // Cannot fold a signed integer setcc with an unsigned integer setcc.
244 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000245
Chris Lattnerc3aae252005-01-07 07:46:32 +0000246 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000247
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248 // If the N and U bits get set then the resultant comparison DOES suddenly
249 // care about orderedness, and is true when ordered.
250 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000251 Op &= ~16; // Clear the U bit if the N bit is set.
252
253 // Canonicalize illegal integer setcc's.
254 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
255 Op = ISD::SETNE;
256
Chris Lattnerc3aae252005-01-07 07:46:32 +0000257 return ISD::CondCode(Op);
258}
259
260/// getSetCCAndOperation - Return the result of a logical AND between different
261/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
262/// function returns zero if it is not possible to represent the resultant
263/// comparison.
264ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
265 bool isInteger) {
266 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
267 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269
270 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000271 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
272
273 // Canonicalize illegal integer setcc's.
274 if (isInteger) {
275 switch (Result) {
276 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000277 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
278 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
279 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
280 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000281 }
282 }
283
284 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285}
286
Chris Lattnerb48da392005-01-23 04:39:44 +0000287const TargetMachine &SelectionDAG::getTarget() const {
288 return TLI.getTargetMachine();
289}
290
Jim Laskey58b968b2005-08-17 20:08:02 +0000291//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000292// SDNode Profile Support
293//===----------------------------------------------------------------------===//
294
Jim Laskeydef69b92006-10-27 23:52:51 +0000295/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
296///
Jim Laskey583bd472006-10-27 23:46:08 +0000297static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
298 ID.AddInteger(OpC);
299}
300
301/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
302/// solely with their pointer.
303void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
304 ID.AddPointer(VTList.VTs);
305}
306
Jim Laskeydef69b92006-10-27 23:52:51 +0000307/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
308///
Jim Laskey583bd472006-10-27 23:46:08 +0000309static void AddNodeIDOperands(FoldingSetNodeID &ID,
310 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000311 for (; NumOps; --NumOps, ++Ops) {
312 ID.AddPointer(Ops->Val);
313 ID.AddInteger(Ops->ResNo);
314 }
Jim Laskey583bd472006-10-27 23:46:08 +0000315}
316
Jim Laskey583bd472006-10-27 23:46:08 +0000317static void AddNodeIDNode(FoldingSetNodeID &ID,
318 unsigned short OpC, SDVTList VTList,
319 const SDOperand *OpList, unsigned N) {
320 AddNodeIDOpcode(ID, OpC);
321 AddNodeIDValueTypes(ID, VTList);
322 AddNodeIDOperands(ID, OpList, N);
323}
324
Jim Laskeydef69b92006-10-27 23:52:51 +0000325/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
326/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000327static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
328 AddNodeIDOpcode(ID, N->getOpcode());
329 // Add the return value info.
330 AddNodeIDValueTypes(ID, N->getVTList());
331 // Add the operand info.
332 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
333
334 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000335 switch (N->getOpcode()) {
336 default: break; // Normal nodes don't need extra info.
337 case ISD::TargetConstant:
338 case ISD::Constant:
339 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
340 break;
341 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000342 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000343 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000344 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000345 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000346 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000347 case ISD::GlobalAddress:
348 case ISD::TargetGlobalTLSAddress:
349 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000350 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
351 ID.AddPointer(GA->getGlobal());
352 ID.AddInteger(GA->getOffset());
353 break;
354 }
355 case ISD::BasicBlock:
356 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
357 break;
358 case ISD::Register:
359 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
360 break;
Evan Cheng334dc1f2008-01-31 21:00:00 +0000361 case ISD::SRCVALUE: {
362 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
363 ID.AddPointer(SV->getValue());
364 ID.AddInteger(SV->getOffset());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 break;
366 }
367 case ISD::FrameIndex:
368 case ISD::TargetFrameIndex:
369 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
370 break;
371 case ISD::JumpTable:
372 case ISD::TargetJumpTable:
373 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
374 break;
375 case ISD::ConstantPool:
376 case ISD::TargetConstantPool: {
377 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
378 ID.AddInteger(CP->getAlignment());
379 ID.AddInteger(CP->getOffset());
380 if (CP->isMachineConstantPoolEntry())
381 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
382 else
383 ID.AddPointer(CP->getConstVal());
384 break;
385 }
386 case ISD::LOAD: {
387 LoadSDNode *LD = cast<LoadSDNode>(N);
388 ID.AddInteger(LD->getAddressingMode());
389 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000390 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000391 ID.AddInteger(LD->getAlignment());
392 ID.AddInteger(LD->isVolatile());
393 break;
394 }
395 case ISD::STORE: {
396 StoreSDNode *ST = cast<StoreSDNode>(N);
397 ID.AddInteger(ST->getAddressingMode());
398 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000399 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000400 ID.AddInteger(ST->getAlignment());
401 ID.AddInteger(ST->isVolatile());
402 break;
403 }
Jim Laskey583bd472006-10-27 23:46:08 +0000404 }
405}
406
407//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000408// SelectionDAG Class
409//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000410
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000411/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000412/// SelectionDAG.
413void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414 // Create a dummy node (which is not added to allnodes), that adds a reference
415 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000416 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000417
Chris Lattner190a4182006-08-04 17:45:20 +0000418 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000419
Chris Lattner190a4182006-08-04 17:45:20 +0000420 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000421 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000422 if (I->use_empty())
423 DeadNodes.push_back(I);
424
425 // Process the worklist, deleting the nodes and adding their uses to the
426 // worklist.
427 while (!DeadNodes.empty()) {
428 SDNode *N = DeadNodes.back();
429 DeadNodes.pop_back();
430
431 // Take the node out of the appropriate CSE map.
432 RemoveNodeFromCSEMaps(N);
433
434 // Next, brutally remove the operand list. This is safe to do, as there are
435 // no cycles in the graph.
436 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
437 SDNode *Operand = I->Val;
438 Operand->removeUser(N);
439
440 // Now that we removed this operand, see if there are no uses of it left.
441 if (Operand->use_empty())
442 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000443 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000444 if (N->OperandsNeedDelete)
445 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000446 N->OperandList = 0;
447 N->NumOperands = 0;
448
449 // Finally, remove N itself.
450 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000451 }
452
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000453 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000454 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000455}
456
Evan Cheng130a6472006-10-12 20:34:05 +0000457void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
458 SmallVector<SDNode*, 16> DeadNodes;
459 DeadNodes.push_back(N);
460
461 // Process the worklist, deleting the nodes and adding their uses to the
462 // worklist.
463 while (!DeadNodes.empty()) {
464 SDNode *N = DeadNodes.back();
465 DeadNodes.pop_back();
466
467 // Take the node out of the appropriate CSE map.
468 RemoveNodeFromCSEMaps(N);
469
470 // Next, brutally remove the operand list. This is safe to do, as there are
471 // no cycles in the graph.
472 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
473 SDNode *Operand = I->Val;
474 Operand->removeUser(N);
475
476 // Now that we removed this operand, see if there are no uses of it left.
477 if (Operand->use_empty())
478 DeadNodes.push_back(Operand);
479 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000480 if (N->OperandsNeedDelete)
481 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000482 N->OperandList = 0;
483 N->NumOperands = 0;
484
485 // Finally, remove N itself.
486 Deleted.push_back(N);
487 AllNodes.erase(N);
488 }
489}
490
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000491void SelectionDAG::DeleteNode(SDNode *N) {
492 assert(N->use_empty() && "Cannot delete a node that is not dead!");
493
494 // First take this out of the appropriate CSE map.
495 RemoveNodeFromCSEMaps(N);
496
Chris Lattner1e111c72005-09-07 05:37:01 +0000497 // Finally, remove uses due to operands of this node, remove from the
498 // AllNodes list, and delete the node.
499 DeleteNodeNotInCSEMaps(N);
500}
501
502void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
503
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000504 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000505 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000506
507 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000508 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
509 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000510 if (N->OperandsNeedDelete)
511 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000512 N->OperandList = 0;
513 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000514
515 delete N;
516}
517
Chris Lattner149c58c2005-08-16 18:17:10 +0000518/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
519/// correspond to it. This is useful when we're about to delete or repurpose
520/// the node. We don't want future request for structurally identical nodes
521/// to return N anymore.
522void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000523 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000524 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000525 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000526 case ISD::STRING:
527 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
528 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000529 case ISD::CONDCODE:
530 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
531 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000532 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000533 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
534 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000535 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000536 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000537 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000538 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000539 Erased =
540 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000541 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000542 case ISD::VALUETYPE: {
543 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
544 if (MVT::isExtendedVT(VT)) {
545 Erased = ExtendedValueTypeNodes.erase(VT);
546 } else {
547 Erased = ValueTypeNodes[VT] != 0;
548 ValueTypeNodes[VT] = 0;
549 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000550 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000551 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000552 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000553 // Remove it from the CSE Map.
554 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000555 break;
556 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000557#ifndef NDEBUG
558 // Verify that the node was actually in one of the CSE maps, unless it has a
559 // flag result (which cannot be CSE'd) or is one of the special cases that are
560 // not subject to CSE.
561 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000562 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000563 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000564 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000565 assert(0 && "Node is not in map!");
566 }
567#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000568}
569
Chris Lattner8b8749f2005-08-17 19:00:20 +0000570/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
571/// has been taken out and modified in some way. If the specified node already
572/// exists in the CSE maps, do not modify the maps, but return the existing node
573/// instead. If it doesn't exist, add it and return null.
574///
575SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
576 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000577 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000578 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000579
Chris Lattner9f8cc692005-12-19 22:21:21 +0000580 // Check that remaining values produced are not flags.
581 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
582 if (N->getValueType(i) == MVT::Flag)
583 return 0; // Never CSE anything that produces a flag.
584
Chris Lattnera5682852006-08-07 23:03:03 +0000585 SDNode *New = CSEMap.GetOrInsertNode(N);
586 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000587 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000588}
589
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000590/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
591/// were replaced with those specified. If this node is never memoized,
592/// return null, otherwise return a pointer to the slot it would take. If a
593/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000594SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
595 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000596 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000597 return 0; // Never add these nodes.
598
599 // Check that remaining values produced are not flags.
600 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
601 if (N->getValueType(i) == MVT::Flag)
602 return 0; // Never CSE anything that produces a flag.
603
Chris Lattner63e3f142007-02-04 07:28:00 +0000604 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000605 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000606 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000607 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000608}
609
610/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
611/// were replaced with those specified. If this node is never memoized,
612/// return null, otherwise return a pointer to the slot it would take. If a
613/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000614SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
615 SDOperand Op1, SDOperand Op2,
616 void *&InsertPos) {
617 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
618 return 0; // Never add these nodes.
619
620 // Check that remaining values produced are not flags.
621 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
622 if (N->getValueType(i) == MVT::Flag)
623 return 0; // Never CSE anything that produces a flag.
624
Chris Lattner63e3f142007-02-04 07:28:00 +0000625 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000626 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000627 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000628 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
629}
630
631
632/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
633/// were replaced with those specified. If this node is never memoized,
634/// return null, otherwise return a pointer to the slot it would take. If a
635/// node already exists with these operands, the slot will be non-null.
636SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000637 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000638 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000639 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000640 return 0; // Never add these nodes.
641
642 // Check that remaining values produced are not flags.
643 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
644 if (N->getValueType(i) == MVT::Flag)
645 return 0; // Never CSE anything that produces a flag.
646
Jim Laskey583bd472006-10-27 23:46:08 +0000647 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000648 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000649
Evan Cheng9629aba2006-10-11 01:47:58 +0000650 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
651 ID.AddInteger(LD->getAddressingMode());
652 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000653 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000654 ID.AddInteger(LD->getAlignment());
655 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000656 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
657 ID.AddInteger(ST->getAddressingMode());
658 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000659 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000660 ID.AddInteger(ST->getAlignment());
661 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000662 }
Jim Laskey583bd472006-10-27 23:46:08 +0000663
Chris Lattnera5682852006-08-07 23:03:03 +0000664 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000665}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000666
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000667
Chris Lattner78ec3112003-08-11 14:57:33 +0000668SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000669 while (!AllNodes.empty()) {
670 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000671 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000672 if (N->OperandsNeedDelete)
673 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000674 N->OperandList = 0;
675 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000676 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000677 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000678}
679
Chris Lattner0f2287b2005-04-13 02:38:18 +0000680SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000681 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000682 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000683 return getNode(ISD::AND, Op.getValueType(), Op,
684 getConstant(Imm, Op.getValueType()));
685}
686
Chris Lattner36ce6912005-11-29 06:21:05 +0000687SDOperand SelectionDAG::getString(const std::string &Val) {
688 StringSDNode *&N = StringNodes[Val];
689 if (!N) {
690 N = new StringSDNode(Val);
691 AllNodes.push_back(N);
692 }
693 return SDOperand(N, 0);
694}
695
Chris Lattner61b09412006-08-11 21:01:22 +0000696SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000697 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000698
699 MVT::ValueType EltVT =
700 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000701
Chris Lattner61b09412006-08-11 21:01:22 +0000702 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000703 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000704
705 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000706 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000707 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000708 ID.AddInteger(Val);
709 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000710 SDNode *N = NULL;
711 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
712 if (!MVT::isVector(VT))
713 return SDOperand(N, 0);
714 if (!N) {
715 N = new ConstantSDNode(isT, Val, EltVT);
716 CSEMap.InsertNode(N, IP);
717 AllNodes.push_back(N);
718 }
719
720 SDOperand Result(N, 0);
721 if (MVT::isVector(VT)) {
722 SmallVector<SDOperand, 8> Ops;
723 Ops.assign(MVT::getVectorNumElements(VT), Result);
724 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
725 }
726 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000727}
728
Chris Lattner0bd48932008-01-17 07:00:52 +0000729SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
730 return getConstant(Val, TLI.getPointerTy(), isTarget);
731}
732
733
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000734SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000735 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000736 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000737
Dan Gohman7f321562007-06-25 16:23:39 +0000738 MVT::ValueType EltVT =
739 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740
Chris Lattnerd8658612005-02-17 20:17:32 +0000741 // Do the map lookup using the actual bit pattern for the floating point
742 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
743 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000744 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000745 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000746 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000747 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000748 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000749 SDNode *N = NULL;
750 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
751 if (!MVT::isVector(VT))
752 return SDOperand(N, 0);
753 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000754 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000755 CSEMap.InsertNode(N, IP);
756 AllNodes.push_back(N);
757 }
758
Dan Gohman7f321562007-06-25 16:23:39 +0000759 SDOperand Result(N, 0);
760 if (MVT::isVector(VT)) {
761 SmallVector<SDOperand, 8> Ops;
762 Ops.assign(MVT::getVectorNumElements(VT), Result);
763 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
764 }
765 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000766}
767
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000768SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
769 bool isTarget) {
770 MVT::ValueType EltVT =
771 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
772 if (EltVT==MVT::f32)
773 return getConstantFP(APFloat((float)Val), VT, isTarget);
774 else
775 return getConstantFP(APFloat(Val), VT, isTarget);
776}
777
Chris Lattnerc3aae252005-01-07 07:46:32 +0000778SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000779 MVT::ValueType VT, int Offset,
780 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000781 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
782 unsigned Opc;
783 if (GVar && GVar->isThreadLocal())
784 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
785 else
786 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000787 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000788 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000789 ID.AddPointer(GV);
790 ID.AddInteger(Offset);
791 void *IP = 0;
792 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
793 return SDOperand(E, 0);
794 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
795 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000796 AllNodes.push_back(N);
797 return SDOperand(N, 0);
798}
799
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000800SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
801 bool isTarget) {
802 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000803 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000804 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000805 ID.AddInteger(FI);
806 void *IP = 0;
807 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
808 return SDOperand(E, 0);
809 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
810 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000811 AllNodes.push_back(N);
812 return SDOperand(N, 0);
813}
814
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000815SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
816 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000817 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000818 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000819 ID.AddInteger(JTI);
820 void *IP = 0;
821 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
822 return SDOperand(E, 0);
823 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
824 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000825 AllNodes.push_back(N);
826 return SDOperand(N, 0);
827}
828
Evan Chengb8973bd2006-01-31 22:23:14 +0000829SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000830 unsigned Alignment, int Offset,
831 bool isTarget) {
832 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000833 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000834 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000835 ID.AddInteger(Alignment);
836 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000837 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000838 void *IP = 0;
839 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
840 return SDOperand(E, 0);
841 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
842 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000843 AllNodes.push_back(N);
844 return SDOperand(N, 0);
845}
846
Chris Lattnerc3aae252005-01-07 07:46:32 +0000847
Evan Chengd6594ae2006-09-12 21:00:35 +0000848SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
849 MVT::ValueType VT,
850 unsigned Alignment, int Offset,
851 bool isTarget) {
852 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000853 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000854 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000855 ID.AddInteger(Alignment);
856 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000857 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000858 void *IP = 0;
859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
860 return SDOperand(E, 0);
861 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
862 CSEMap.InsertNode(N, IP);
863 AllNodes.push_back(N);
864 return SDOperand(N, 0);
865}
866
867
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000869 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000870 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000871 ID.AddPointer(MBB);
872 void *IP = 0;
873 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
874 return SDOperand(E, 0);
875 SDNode *N = new BasicBlockSDNode(MBB);
876 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
879}
880
Chris Lattner15e4b012005-07-10 00:07:11 +0000881SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000882 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000883 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000884
Duncan Sandsf411b832007-10-17 13:49:58 +0000885 SDNode *&N = MVT::isExtendedVT(VT) ?
886 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
887
888 if (N) return SDOperand(N, 0);
889 N = new VTSDNode(VT);
890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000892}
893
Chris Lattnerc3aae252005-01-07 07:46:32 +0000894SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
895 SDNode *&N = ExternalSymbols[Sym];
896 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000897 N = new ExternalSymbolSDNode(false, Sym, VT);
898 AllNodes.push_back(N);
899 return SDOperand(N, 0);
900}
901
Chris Lattner809ec112006-01-28 10:09:25 +0000902SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
903 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000904 SDNode *&N = TargetExternalSymbols[Sym];
905 if (N) return SDOperand(N, 0);
906 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907 AllNodes.push_back(N);
908 return SDOperand(N, 0);
909}
910
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000911SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
912 if ((unsigned)Cond >= CondCodeNodes.size())
913 CondCodeNodes.resize(Cond+1);
914
Chris Lattner079a27a2005-08-09 20:40:02 +0000915 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000916 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000917 AllNodes.push_back(CondCodeNodes[Cond]);
918 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000919 return SDOperand(CondCodeNodes[Cond], 0);
920}
921
Chris Lattner0fdd7682005-08-30 22:38:38 +0000922SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000923 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000924 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000925 ID.AddInteger(RegNo);
926 void *IP = 0;
927 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
928 return SDOperand(E, 0);
929 SDNode *N = new RegisterSDNode(RegNo, VT);
930 CSEMap.InsertNode(N, IP);
931 AllNodes.push_back(N);
932 return SDOperand(N, 0);
933}
934
Evan Cheng334dc1f2008-01-31 21:00:00 +0000935SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Chris Lattner61b09412006-08-11 21:01:22 +0000936 assert((!V || isa<PointerType>(V->getType())) &&
937 "SrcValue is not a pointer?");
938
Jim Laskey583bd472006-10-27 23:46:08 +0000939 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000940 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000941 ID.AddPointer(V);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000942 ID.AddInteger(Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000943 void *IP = 0;
944 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
945 return SDOperand(E, 0);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000946 SDNode *N = new SrcValueSDNode(V, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000947 CSEMap.InsertNode(N, IP);
948 AllNodes.push_back(N);
949 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000950}
951
Chris Lattner37ce9df2007-10-15 17:47:20 +0000952/// CreateStackTemporary - Create a stack temporary, suitable for holding the
953/// specified value type.
954SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
955 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
956 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
957 const Type *Ty = MVT::getTypeForValueType(VT);
958 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
959 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
960 return getFrameIndex(FrameIdx, TLI.getPointerTy());
961}
962
963
Chris Lattner51dabfb2006-10-14 00:41:01 +0000964SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
965 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966 // These setcc operations always fold.
967 switch (Cond) {
968 default: break;
969 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000970 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000972 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000973
974 case ISD::SETOEQ:
975 case ISD::SETOGT:
976 case ISD::SETOGE:
977 case ISD::SETOLT:
978 case ISD::SETOLE:
979 case ISD::SETONE:
980 case ISD::SETO:
981 case ISD::SETUO:
982 case ISD::SETUEQ:
983 case ISD::SETUNE:
984 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
985 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000987
Chris Lattner67255a12005-04-07 18:14:58 +0000988 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
989 uint64_t C2 = N2C->getValue();
990 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
991 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000992
Chris Lattnerc3aae252005-01-07 07:46:32 +0000993 // Sign extend the operands if required
994 if (ISD::isSignedIntSetCC(Cond)) {
995 C1 = N1C->getSignExtended();
996 C2 = N2C->getSignExtended();
997 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000998
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 switch (Cond) {
1000 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001001 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1002 case ISD::SETNE: return getConstant(C1 != C2, VT);
1003 case ISD::SETULT: return getConstant(C1 < C2, VT);
1004 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1005 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1006 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1007 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1008 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1009 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1010 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 }
Chris Lattner67255a12005-04-07 18:14:58 +00001013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1015 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001016 // No compile time operations on this type yet.
1017 if (N1C->getValueType(0) == MVT::ppcf128)
1018 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001019
1020 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001022 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001023 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1024 return getNode(ISD::UNDEF, VT);
1025 // fall through
1026 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1027 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1028 return getNode(ISD::UNDEF, VT);
1029 // fall through
1030 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001031 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001032 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1033 return getNode(ISD::UNDEF, VT);
1034 // fall through
1035 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1036 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1037 return getNode(ISD::UNDEF, VT);
1038 // fall through
1039 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1040 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1041 return getNode(ISD::UNDEF, VT);
1042 // fall through
1043 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001044 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001045 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1046 return getNode(ISD::UNDEF, VT);
1047 // fall through
1048 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001049 R==APFloat::cmpEqual, VT);
1050 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1051 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1052 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1053 R==APFloat::cmpEqual, VT);
1054 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1055 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1056 R==APFloat::cmpLessThan, VT);
1057 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1058 R==APFloat::cmpUnordered, VT);
1059 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1060 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 }
1062 } else {
1063 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001064 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001066
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001067 // Could not fold it.
1068 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069}
1070
Dan Gohmanea859be2007-06-22 14:59:07 +00001071/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1072/// this predicate to simplify operations downstream. Mask is known to be zero
1073/// for bits that V cannot have.
1074bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1075 unsigned Depth) const {
1076 // The masks are not wide enough to represent this type! Should use APInt.
1077 if (Op.getValueType() == MVT::i128)
1078 return false;
1079
1080 uint64_t KnownZero, KnownOne;
1081 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1082 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1083 return (KnownZero & Mask) == Mask;
1084}
1085
1086/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1087/// known to be either zero or one and return them in the KnownZero/KnownOne
1088/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1089/// processing.
1090void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1091 uint64_t &KnownZero, uint64_t &KnownOne,
1092 unsigned Depth) const {
1093 KnownZero = KnownOne = 0; // Don't know anything.
1094 if (Depth == 6 || Mask == 0)
1095 return; // Limit search depth.
1096
1097 // The masks are not wide enough to represent this type! Should use APInt.
1098 if (Op.getValueType() == MVT::i128)
1099 return;
1100
1101 uint64_t KnownZero2, KnownOne2;
1102
1103 switch (Op.getOpcode()) {
1104 case ISD::Constant:
1105 // We know all of the bits for a constant!
1106 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1107 KnownZero = ~KnownOne & Mask;
1108 return;
1109 case ISD::AND:
1110 // If either the LHS or the RHS are Zero, the result is zero.
1111 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1112 Mask &= ~KnownZero;
1113 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1114 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1115 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1116
1117 // Output known-1 bits are only known if set in both the LHS & RHS.
1118 KnownOne &= KnownOne2;
1119 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1120 KnownZero |= KnownZero2;
1121 return;
1122 case ISD::OR:
1123 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1124 Mask &= ~KnownOne;
1125 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1126 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1127 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1128
1129 // Output known-0 bits are only known if clear in both the LHS & RHS.
1130 KnownZero &= KnownZero2;
1131 // Output known-1 are known to be set if set in either the LHS | RHS.
1132 KnownOne |= KnownOne2;
1133 return;
1134 case ISD::XOR: {
1135 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1136 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1137 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1138 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1139
1140 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1141 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1142 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1143 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1144 KnownZero = KnownZeroOut;
1145 return;
1146 }
1147 case ISD::SELECT:
1148 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1149 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1150 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1151 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1152
1153 // Only known if known in both the LHS and RHS.
1154 KnownOne &= KnownOne2;
1155 KnownZero &= KnownZero2;
1156 return;
1157 case ISD::SELECT_CC:
1158 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1159 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1160 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1161 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1162
1163 // Only known if known in both the LHS and RHS.
1164 KnownOne &= KnownOne2;
1165 KnownZero &= KnownZero2;
1166 return;
1167 case ISD::SETCC:
1168 // If we know the result of a setcc has the top bits zero, use this info.
1169 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1170 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1171 return;
1172 case ISD::SHL:
1173 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1174 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1175 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1176 KnownZero, KnownOne, Depth+1);
1177 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1178 KnownZero <<= SA->getValue();
1179 KnownOne <<= SA->getValue();
1180 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1181 }
1182 return;
1183 case ISD::SRL:
1184 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1185 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1186 MVT::ValueType VT = Op.getValueType();
1187 unsigned ShAmt = SA->getValue();
1188
1189 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1190 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1191 KnownZero, KnownOne, Depth+1);
1192 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1193 KnownZero &= TypeMask;
1194 KnownOne &= TypeMask;
1195 KnownZero >>= ShAmt;
1196 KnownOne >>= ShAmt;
1197
1198 uint64_t HighBits = (1ULL << ShAmt)-1;
1199 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1200 KnownZero |= HighBits; // High bits known zero.
1201 }
1202 return;
1203 case ISD::SRA:
1204 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1205 MVT::ValueType VT = Op.getValueType();
1206 unsigned ShAmt = SA->getValue();
1207
1208 // Compute the new bits that are at the top now.
1209 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1210
1211 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1212 // If any of the demanded bits are produced by the sign extension, we also
1213 // demand the input sign bit.
1214 uint64_t HighBits = (1ULL << ShAmt)-1;
1215 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1216 if (HighBits & Mask)
1217 InDemandedMask |= MVT::getIntVTSignBit(VT);
1218
1219 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1220 Depth+1);
1221 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1222 KnownZero &= TypeMask;
1223 KnownOne &= TypeMask;
1224 KnownZero >>= ShAmt;
1225 KnownOne >>= ShAmt;
1226
1227 // Handle the sign bits.
1228 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1229 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1230
1231 if (KnownZero & SignBit) {
1232 KnownZero |= HighBits; // New bits are known zero.
1233 } else if (KnownOne & SignBit) {
1234 KnownOne |= HighBits; // New bits are known one.
1235 }
1236 }
1237 return;
1238 case ISD::SIGN_EXTEND_INREG: {
1239 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1240
1241 // Sign extension. Compute the demanded bits in the result that are not
1242 // present in the input.
1243 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1244
1245 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1246 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1247
1248 // If the sign extended bits are demanded, we know that the sign
1249 // bit is demanded.
1250 if (NewBits)
1251 InputDemandedBits |= InSignBit;
1252
1253 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1254 KnownZero, KnownOne, Depth+1);
1255 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1256
1257 // If the sign bit of the input is known set or clear, then we know the
1258 // top bits of the result.
1259 if (KnownZero & InSignBit) { // Input sign bit known clear
1260 KnownZero |= NewBits;
1261 KnownOne &= ~NewBits;
1262 } else if (KnownOne & InSignBit) { // Input sign bit known set
1263 KnownOne |= NewBits;
1264 KnownZero &= ~NewBits;
1265 } else { // Input sign bit unknown
1266 KnownZero &= ~NewBits;
1267 KnownOne &= ~NewBits;
1268 }
1269 return;
1270 }
1271 case ISD::CTTZ:
1272 case ISD::CTLZ:
1273 case ISD::CTPOP: {
1274 MVT::ValueType VT = Op.getValueType();
1275 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1276 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1277 KnownOne = 0;
1278 return;
1279 }
1280 case ISD::LOAD: {
1281 if (ISD::isZEXTLoad(Op.Val)) {
1282 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001283 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001284 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1285 }
1286 return;
1287 }
1288 case ISD::ZERO_EXTEND: {
1289 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1290 uint64_t NewBits = (~InMask) & Mask;
1291 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1292 KnownOne, Depth+1);
1293 KnownZero |= NewBits & Mask;
1294 KnownOne &= ~NewBits;
1295 return;
1296 }
1297 case ISD::SIGN_EXTEND: {
1298 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1299 unsigned InBits = MVT::getSizeInBits(InVT);
1300 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1301 uint64_t InSignBit = 1ULL << (InBits-1);
1302 uint64_t NewBits = (~InMask) & Mask;
1303 uint64_t InDemandedBits = Mask & InMask;
1304
1305 // If any of the sign extended bits are demanded, we know that the sign
1306 // bit is demanded.
1307 if (NewBits & Mask)
1308 InDemandedBits |= InSignBit;
1309
1310 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1311 KnownOne, Depth+1);
1312 // If the sign bit is known zero or one, the top bits match.
1313 if (KnownZero & InSignBit) {
1314 KnownZero |= NewBits;
1315 KnownOne &= ~NewBits;
1316 } else if (KnownOne & InSignBit) {
1317 KnownOne |= NewBits;
1318 KnownZero &= ~NewBits;
1319 } else { // Otherwise, top bits aren't known.
1320 KnownOne &= ~NewBits;
1321 KnownZero &= ~NewBits;
1322 }
1323 return;
1324 }
1325 case ISD::ANY_EXTEND: {
1326 MVT::ValueType VT = Op.getOperand(0).getValueType();
1327 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1328 KnownZero, KnownOne, Depth+1);
1329 return;
1330 }
1331 case ISD::TRUNCATE: {
1332 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1333 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1334 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1335 KnownZero &= OutMask;
1336 KnownOne &= OutMask;
1337 break;
1338 }
1339 case ISD::AssertZext: {
1340 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1341 uint64_t InMask = MVT::getIntVTBitMask(VT);
1342 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1343 KnownOne, Depth+1);
1344 KnownZero |= (~InMask) & Mask;
1345 return;
1346 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001347 case ISD::FGETSIGN:
1348 // All bits are zero except the low bit.
1349 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1350 return;
1351
Dan Gohmanea859be2007-06-22 14:59:07 +00001352 case ISD::ADD: {
1353 // If either the LHS or the RHS are Zero, the result is zero.
1354 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1355 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1356 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1357 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1358
1359 // Output known-0 bits are known if clear or set in both the low clear bits
1360 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1361 // low 3 bits clear.
1362 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1363 CountTrailingZeros_64(~KnownZero2));
1364
1365 KnownZero = (1ULL << KnownZeroOut) - 1;
1366 KnownOne = 0;
1367 return;
1368 }
1369 case ISD::SUB: {
1370 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1371 if (!CLHS) return;
1372
1373 // We know that the top bits of C-X are clear if X contains less bits
1374 // than C (i.e. no wrap-around can happen). For example, 20-X is
1375 // positive if we can prove that X is >= 0 and < 16.
1376 MVT::ValueType VT = CLHS->getValueType(0);
1377 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1378 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1379 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1380 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1381 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1382
1383 // If all of the MaskV bits are known to be zero, then we know the output
1384 // top bits are zero, because we now know that the output is from [0-C].
1385 if ((KnownZero & MaskV) == MaskV) {
1386 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1387 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1388 KnownOne = 0; // No one bits known.
1389 } else {
1390 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1391 }
1392 }
1393 return;
1394 }
1395 default:
1396 // Allow the target to implement this method for its nodes.
1397 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1398 case ISD::INTRINSIC_WO_CHAIN:
1399 case ISD::INTRINSIC_W_CHAIN:
1400 case ISD::INTRINSIC_VOID:
1401 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1402 }
1403 return;
1404 }
1405}
1406
1407/// ComputeNumSignBits - Return the number of times the sign bit of the
1408/// register is replicated into the other bits. We know that at least 1 bit
1409/// is always equal to the sign bit (itself), but other cases can give us
1410/// information. For example, immediately after an "SRA X, 2", we know that
1411/// the top 3 bits are all equal to each other, so we return 3.
1412unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1413 MVT::ValueType VT = Op.getValueType();
1414 assert(MVT::isInteger(VT) && "Invalid VT!");
1415 unsigned VTBits = MVT::getSizeInBits(VT);
1416 unsigned Tmp, Tmp2;
1417
1418 if (Depth == 6)
1419 return 1; // Limit search depth.
1420
1421 switch (Op.getOpcode()) {
1422 default: break;
1423 case ISD::AssertSext:
1424 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1425 return VTBits-Tmp+1;
1426 case ISD::AssertZext:
1427 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1428 return VTBits-Tmp;
1429
1430 case ISD::Constant: {
1431 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1432 // If negative, invert the bits, then look at it.
1433 if (Val & MVT::getIntVTSignBit(VT))
1434 Val = ~Val;
1435
1436 // Shift the bits so they are the leading bits in the int64_t.
1437 Val <<= 64-VTBits;
1438
1439 // Return # leading zeros. We use 'min' here in case Val was zero before
1440 // shifting. We don't want to return '64' as for an i32 "0".
1441 return std::min(VTBits, CountLeadingZeros_64(Val));
1442 }
1443
1444 case ISD::SIGN_EXTEND:
1445 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1446 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1447
1448 case ISD::SIGN_EXTEND_INREG:
1449 // Max of the input and what this extends.
1450 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1451 Tmp = VTBits-Tmp+1;
1452
1453 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1454 return std::max(Tmp, Tmp2);
1455
1456 case ISD::SRA:
1457 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1458 // SRA X, C -> adds C sign bits.
1459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1460 Tmp += C->getValue();
1461 if (Tmp > VTBits) Tmp = VTBits;
1462 }
1463 return Tmp;
1464 case ISD::SHL:
1465 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1466 // shl destroys sign bits.
1467 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1468 if (C->getValue() >= VTBits || // Bad shift.
1469 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1470 return Tmp - C->getValue();
1471 }
1472 break;
1473 case ISD::AND:
1474 case ISD::OR:
1475 case ISD::XOR: // NOT is handled here.
1476 // Logical binary ops preserve the number of sign bits.
1477 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1478 if (Tmp == 1) return 1; // Early out.
1479 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1480 return std::min(Tmp, Tmp2);
1481
1482 case ISD::SELECT:
1483 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1484 if (Tmp == 1) return 1; // Early out.
1485 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1486 return std::min(Tmp, Tmp2);
1487
1488 case ISD::SETCC:
1489 // If setcc returns 0/-1, all bits are sign bits.
1490 if (TLI.getSetCCResultContents() ==
1491 TargetLowering::ZeroOrNegativeOneSetCCResult)
1492 return VTBits;
1493 break;
1494 case ISD::ROTL:
1495 case ISD::ROTR:
1496 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1497 unsigned RotAmt = C->getValue() & (VTBits-1);
1498
1499 // Handle rotate right by N like a rotate left by 32-N.
1500 if (Op.getOpcode() == ISD::ROTR)
1501 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1502
1503 // If we aren't rotating out all of the known-in sign bits, return the
1504 // number that are left. This handles rotl(sext(x), 1) for example.
1505 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1506 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1507 }
1508 break;
1509 case ISD::ADD:
1510 // Add can have at most one carry bit. Thus we know that the output
1511 // is, at worst, one more bit than the inputs.
1512 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1513 if (Tmp == 1) return 1; // Early out.
1514
1515 // Special case decrementing a value (ADD X, -1):
1516 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1517 if (CRHS->isAllOnesValue()) {
1518 uint64_t KnownZero, KnownOne;
1519 uint64_t Mask = MVT::getIntVTBitMask(VT);
1520 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1521
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 we are subtracting one from a positive number, there is no carry
1528 // out of the result.
1529 if (KnownZero & MVT::getIntVTSignBit(VT))
1530 return Tmp;
1531 }
1532
1533 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1534 if (Tmp2 == 1) return 1;
1535 return std::min(Tmp, Tmp2)-1;
1536 break;
1537
1538 case ISD::SUB:
1539 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1540 if (Tmp2 == 1) return 1;
1541
1542 // Handle NEG.
1543 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1544 if (CLHS->getValue() == 0) {
1545 uint64_t KnownZero, KnownOne;
1546 uint64_t Mask = MVT::getIntVTBitMask(VT);
1547 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1548 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1549 // sign bits set.
1550 if ((KnownZero|1) == Mask)
1551 return VTBits;
1552
1553 // If the input is known to be positive (the sign bit is known clear),
1554 // the output of the NEG has the same number of sign bits as the input.
1555 if (KnownZero & MVT::getIntVTSignBit(VT))
1556 return Tmp2;
1557
1558 // Otherwise, we treat this like a SUB.
1559 }
1560
1561 // Sub can have at most one carry bit. Thus we know that the output
1562 // is, at worst, one more bit than the inputs.
1563 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1564 if (Tmp == 1) return 1; // Early out.
1565 return std::min(Tmp, Tmp2)-1;
1566 break;
1567 case ISD::TRUNCATE:
1568 // FIXME: it's tricky to do anything useful for this, but it is an important
1569 // case for targets like X86.
1570 break;
1571 }
1572
1573 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1574 if (Op.getOpcode() == ISD::LOAD) {
1575 LoadSDNode *LD = cast<LoadSDNode>(Op);
1576 unsigned ExtType = LD->getExtensionType();
1577 switch (ExtType) {
1578 default: break;
1579 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001580 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001581 return VTBits-Tmp+1;
1582 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001583 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001584 return VTBits-Tmp;
1585 }
1586 }
1587
1588 // Allow the target to implement this method for its nodes.
1589 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1590 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1591 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1592 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1593 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1594 if (NumBits > 1) return NumBits;
1595 }
1596
1597 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1598 // use this information.
1599 uint64_t KnownZero, KnownOne;
1600 uint64_t Mask = MVT::getIntVTBitMask(VT);
1601 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1602
1603 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1604 if (KnownZero & SignBit) { // SignBit is 0
1605 Mask = KnownZero;
1606 } else if (KnownOne & SignBit) { // SignBit is 1;
1607 Mask = KnownOne;
1608 } else {
1609 // Nothing known.
1610 return 1;
1611 }
1612
1613 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1614 // the number of identical bits in the top of the input value.
1615 Mask ^= ~0ULL;
1616 Mask <<= 64-VTBits;
1617 // Return # leading zeros. We use 'min' here in case Val was zero before
1618 // shifting. We don't want to return '64' as for an i32 "0".
1619 return std::min(VTBits, CountLeadingZeros_64(Mask));
1620}
1621
Chris Lattner51dabfb2006-10-14 00:41:01 +00001622
Chris Lattnerc3aae252005-01-07 07:46:32 +00001623/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001624///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001625SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001626 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001627 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001628 void *IP = 0;
1629 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1630 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001631 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001632 CSEMap.InsertNode(N, IP);
1633
1634 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001635 return SDOperand(N, 0);
1636}
1637
Chris Lattnerc3aae252005-01-07 07:46:32 +00001638SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1639 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001640 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001641 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001642 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1643 uint64_t Val = C->getValue();
1644 switch (Opcode) {
1645 default: break;
1646 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001647 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001648 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1649 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001650 case ISD::UINT_TO_FP:
1651 case ISD::SINT_TO_FP: {
1652 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001653 // No compile time operations on this type.
1654 if (VT==MVT::ppcf128)
1655 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001656 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001657 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001658 MVT::getSizeInBits(Operand.getValueType()),
1659 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001660 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001661 return getConstantFP(apf, VT);
1662 }
Chris Lattner94683772005-12-23 05:30:37 +00001663 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001664 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001665 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001666 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001667 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001668 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001669 case ISD::BSWAP:
1670 switch(VT) {
1671 default: assert(0 && "Invalid bswap!"); break;
1672 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1673 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1674 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1675 }
1676 break;
1677 case ISD::CTPOP:
1678 switch(VT) {
1679 default: assert(0 && "Invalid ctpop!"); break;
1680 case MVT::i1: return getConstant(Val != 0, VT);
1681 case MVT::i8:
1682 Tmp1 = (unsigned)Val & 0xFF;
1683 return getConstant(CountPopulation_32(Tmp1), VT);
1684 case MVT::i16:
1685 Tmp1 = (unsigned)Val & 0xFFFF;
1686 return getConstant(CountPopulation_32(Tmp1), VT);
1687 case MVT::i32:
1688 return getConstant(CountPopulation_32((unsigned)Val), VT);
1689 case MVT::i64:
1690 return getConstant(CountPopulation_64(Val), VT);
1691 }
1692 case ISD::CTLZ:
1693 switch(VT) {
1694 default: assert(0 && "Invalid ctlz!"); break;
1695 case MVT::i1: return getConstant(Val == 0, VT);
1696 case MVT::i8:
1697 Tmp1 = (unsigned)Val & 0xFF;
1698 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1699 case MVT::i16:
1700 Tmp1 = (unsigned)Val & 0xFFFF;
1701 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1702 case MVT::i32:
1703 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1704 case MVT::i64:
1705 return getConstant(CountLeadingZeros_64(Val), VT);
1706 }
1707 case ISD::CTTZ:
1708 switch(VT) {
1709 default: assert(0 && "Invalid cttz!"); break;
1710 case MVT::i1: return getConstant(Val == 0, VT);
1711 case MVT::i8:
1712 Tmp1 = (unsigned)Val | 0x100;
1713 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1714 case MVT::i16:
1715 Tmp1 = (unsigned)Val | 0x10000;
1716 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1717 case MVT::i32:
1718 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1719 case MVT::i64:
1720 return getConstant(CountTrailingZeros_64(Val), VT);
1721 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001722 }
1723 }
1724
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001725 // Constant fold unary operations with a floating point constant operand.
1726 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1727 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001728 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001729 switch (Opcode) {
1730 case ISD::FNEG:
1731 V.changeSign();
1732 return getConstantFP(V, VT);
1733 case ISD::FABS:
1734 V.clearSign();
1735 return getConstantFP(V, VT);
1736 case ISD::FP_ROUND:
1737 case ISD::FP_EXTEND:
1738 // This can return overflow, underflow, or inexact; we don't care.
1739 // FIXME need to be more flexible about rounding mode.
1740 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1741 VT==MVT::f64 ? APFloat::IEEEdouble :
1742 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1743 VT==MVT::f128 ? APFloat::IEEEquad :
1744 APFloat::Bogus,
1745 APFloat::rmNearestTiesToEven);
1746 return getConstantFP(V, VT);
1747 case ISD::FP_TO_SINT:
1748 case ISD::FP_TO_UINT: {
1749 integerPart x;
1750 assert(integerPartWidth >= 64);
1751 // FIXME need to be more flexible about rounding mode.
1752 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1753 Opcode==ISD::FP_TO_SINT,
1754 APFloat::rmTowardZero);
1755 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1756 break;
1757 return getConstant(x, VT);
1758 }
1759 case ISD::BIT_CONVERT:
1760 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1761 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1762 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1763 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001764 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001765 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001766 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001767 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001768
1769 unsigned OpOpcode = Operand.Val->getOpcode();
1770 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001771 case ISD::TokenFactor:
1772 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001773 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001774 case ISD::FP_EXTEND:
1775 assert(MVT::isFloatingPoint(VT) &&
1776 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001777 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001778 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00001779 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001780 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1781 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001782 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001783 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1784 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001785 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1786 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1787 break;
1788 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001789 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1790 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001791 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001792 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1793 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001794 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001795 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001796 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001797 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001798 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1799 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001800 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001801 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1802 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001803 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1804 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1805 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1806 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001807 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001808 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1809 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001810 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001811 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1812 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001813 if (OpOpcode == ISD::TRUNCATE)
1814 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001815 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1816 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001817 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001818 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1819 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001820 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001821 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1822 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001823 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1824 else
1825 return Operand.Val->getOperand(0);
1826 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001827 break;
Chris Lattner35481892005-12-23 00:16:34 +00001828 case ISD::BIT_CONVERT:
1829 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001830 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001831 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001832 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001833 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1834 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001835 if (OpOpcode == ISD::UNDEF)
1836 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001837 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001838 case ISD::SCALAR_TO_VECTOR:
1839 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001840 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001841 "Illegal SCALAR_TO_VECTOR node!");
1842 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001843 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001844 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1845 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001846 Operand.Val->getOperand(0));
1847 if (OpOpcode == ISD::FNEG) // --X -> X
1848 return Operand.Val->getOperand(0);
1849 break;
1850 case ISD::FABS:
1851 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1852 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1853 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001854 }
1855
Chris Lattner43247a12005-08-25 19:12:10 +00001856 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001857 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001858 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001859 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001860 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001861 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001862 void *IP = 0;
1863 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1864 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001865 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001866 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001867 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001868 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001869 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001870 AllNodes.push_back(N);
1871 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001872}
1873
Chris Lattner36019aa2005-04-18 03:48:41 +00001874
1875
Chris Lattnerc3aae252005-01-07 07:46:32 +00001876SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1877 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001878 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1879 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001880 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001881 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001882 case ISD::TokenFactor:
1883 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1884 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001885 // Fold trivial token factors.
1886 if (N1.getOpcode() == ISD::EntryToken) return N2;
1887 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001888 break;
Chris Lattner76365122005-01-16 02:23:22 +00001889 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001890 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1891 N1.getValueType() == VT && "Binary operator types must match!");
1892 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1893 // worth handling here.
1894 if (N2C && N2C->getValue() == 0)
1895 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001896 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1897 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001898 break;
Chris Lattner76365122005-01-16 02:23:22 +00001899 case ISD::OR:
1900 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001901 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1902 N1.getValueType() == VT && "Binary operator types must match!");
1903 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1904 // worth handling here.
1905 if (N2C && N2C->getValue() == 0)
1906 return N1;
1907 break;
Chris Lattner76365122005-01-16 02:23:22 +00001908 case ISD::UDIV:
1909 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001910 case ISD::MULHU:
1911 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001912 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1913 // fall through
1914 case ISD::ADD:
1915 case ISD::SUB:
1916 case ISD::MUL:
1917 case ISD::SDIV:
1918 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001919 case ISD::FADD:
1920 case ISD::FSUB:
1921 case ISD::FMUL:
1922 case ISD::FDIV:
1923 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001924 assert(N1.getValueType() == N2.getValueType() &&
1925 N1.getValueType() == VT && "Binary operator types must match!");
1926 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001927 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1928 assert(N1.getValueType() == VT &&
1929 MVT::isFloatingPoint(N1.getValueType()) &&
1930 MVT::isFloatingPoint(N2.getValueType()) &&
1931 "Invalid FCOPYSIGN!");
1932 break;
Chris Lattner76365122005-01-16 02:23:22 +00001933 case ISD::SHL:
1934 case ISD::SRA:
1935 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001936 case ISD::ROTL:
1937 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001938 assert(VT == N1.getValueType() &&
1939 "Shift operators return type must be the same as their first arg");
1940 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001941 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001942 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001943 case ISD::FP_ROUND_INREG: {
1944 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1945 assert(VT == N1.getValueType() && "Not an inreg round!");
1946 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1947 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001948 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1949 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001950 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00001951 break;
1952 }
Chris Lattner0bd48932008-01-17 07:00:52 +00001953 case ISD::FP_ROUND:
1954 assert(MVT::isFloatingPoint(VT) &&
1955 MVT::isFloatingPoint(N1.getValueType()) &&
1956 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1957 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001958 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00001959 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00001960 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001961 case ISD::AssertZext: {
1962 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1963 assert(VT == N1.getValueType() && "Not an inreg extend!");
1964 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1965 "Cannot *_EXTEND_INREG FP types");
1966 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1967 "Not extending!");
1968 break;
1969 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001970 case ISD::SIGN_EXTEND_INREG: {
1971 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1972 assert(VT == N1.getValueType() && "Not an inreg extend!");
1973 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1974 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001975 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1976 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001977 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001978
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001979 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001980 int64_t Val = N1C->getValue();
1981 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1982 Val <<= 64-FromBits;
1983 Val >>= 64-FromBits;
1984 return getConstant(Val, VT);
1985 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001986 break;
1987 }
1988 case ISD::EXTRACT_VECTOR_ELT:
1989 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
1990
1991 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
1992 // expanding copies of large vectors from registers.
1993 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
1994 N1.getNumOperands() > 0) {
1995 unsigned Factor =
1996 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
1997 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
1998 N1.getOperand(N2C->getValue() / Factor),
1999 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2000 }
2001
2002 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2003 // expanding large vector constants.
2004 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2005 return N1.getOperand(N2C->getValue());
2006
2007 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2008 // operations are lowered to scalars.
2009 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2010 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2011 if (IEC == N2C)
2012 return N1.getOperand(1);
2013 else
2014 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2015 }
2016 break;
2017 case ISD::EXTRACT_ELEMENT:
2018 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002019
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002020 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2021 // 64-bit integers into 32-bit parts. Instead of building the extract of
2022 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2023 if (N1.getOpcode() == ISD::BUILD_PAIR)
2024 return N1.getOperand(N2C->getValue());
2025
2026 // EXTRACT_ELEMENT of a constant int is also very common.
2027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2028 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2029 return getConstant(C->getValue() >> Shift, VT);
2030 }
2031 break;
2032 }
2033
2034 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002035 if (N2C) {
2036 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2037 switch (Opcode) {
2038 case ISD::ADD: return getConstant(C1 + C2, VT);
2039 case ISD::SUB: return getConstant(C1 - C2, VT);
2040 case ISD::MUL: return getConstant(C1 * C2, VT);
2041 case ISD::UDIV:
2042 if (C2) return getConstant(C1 / C2, VT);
2043 break;
2044 case ISD::UREM :
2045 if (C2) return getConstant(C1 % C2, VT);
2046 break;
2047 case ISD::SDIV :
2048 if (C2) return getConstant(N1C->getSignExtended() /
2049 N2C->getSignExtended(), VT);
2050 break;
2051 case ISD::SREM :
2052 if (C2) return getConstant(N1C->getSignExtended() %
2053 N2C->getSignExtended(), VT);
2054 break;
2055 case ISD::AND : return getConstant(C1 & C2, VT);
2056 case ISD::OR : return getConstant(C1 | C2, VT);
2057 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002058 case ISD::SHL : return getConstant(C1 << C2, VT);
2059 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00002060 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00002061 case ISD::ROTL :
2062 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2063 VT);
2064 case ISD::ROTR :
2065 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2066 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002067 default: break;
2068 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002069 } else { // Cannonicalize constant to RHS if commutative
2070 if (isCommutativeBinOp(Opcode)) {
2071 std::swap(N1C, N2C);
2072 std::swap(N1, N2);
2073 }
2074 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002075 }
2076
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002077 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002078 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2079 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002080 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002081 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2082 // Cannonicalize constant to RHS if commutative
2083 std::swap(N1CFP, N2CFP);
2084 std::swap(N1, N2);
2085 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002086 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2087 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002088 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002089 case ISD::FADD:
2090 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002091 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002092 return getConstantFP(V1, VT);
2093 break;
2094 case ISD::FSUB:
2095 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2096 if (s!=APFloat::opInvalidOp)
2097 return getConstantFP(V1, VT);
2098 break;
2099 case ISD::FMUL:
2100 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2101 if (s!=APFloat::opInvalidOp)
2102 return getConstantFP(V1, VT);
2103 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002104 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002105 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2106 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2107 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002108 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002109 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002110 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2111 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2112 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002113 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002114 case ISD::FCOPYSIGN:
2115 V1.copySign(V2);
2116 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002117 default: break;
2118 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002120 }
Chris Lattner62b57722006-04-20 05:39:12 +00002121
2122 // Canonicalize an UNDEF to the RHS, even over a constant.
2123 if (N1.getOpcode() == ISD::UNDEF) {
2124 if (isCommutativeBinOp(Opcode)) {
2125 std::swap(N1, N2);
2126 } else {
2127 switch (Opcode) {
2128 case ISD::FP_ROUND_INREG:
2129 case ISD::SIGN_EXTEND_INREG:
2130 case ISD::SUB:
2131 case ISD::FSUB:
2132 case ISD::FDIV:
2133 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002134 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002135 return N1; // fold op(undef, arg2) -> undef
2136 case ISD::UDIV:
2137 case ISD::SDIV:
2138 case ISD::UREM:
2139 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002140 case ISD::SRL:
2141 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002142 if (!MVT::isVector(VT))
2143 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2144 // For vectors, we can't easily build an all zero vector, just return
2145 // the LHS.
2146 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002147 }
2148 }
2149 }
2150
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002151 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002152 if (N2.getOpcode() == ISD::UNDEF) {
2153 switch (Opcode) {
2154 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002155 case ISD::ADDC:
2156 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002157 case ISD::SUB:
2158 case ISD::FADD:
2159 case ISD::FSUB:
2160 case ISD::FMUL:
2161 case ISD::FDIV:
2162 case ISD::FREM:
2163 case ISD::UDIV:
2164 case ISD::SDIV:
2165 case ISD::UREM:
2166 case ISD::SREM:
2167 case ISD::XOR:
2168 return N2; // fold op(arg1, undef) -> undef
2169 case ISD::MUL:
2170 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002171 case ISD::SRL:
2172 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002173 if (!MVT::isVector(VT))
2174 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2175 // For vectors, we can't easily build an all zero vector, just return
2176 // the LHS.
2177 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002178 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002179 if (!MVT::isVector(VT))
2180 return getConstant(MVT::getIntVTBitMask(VT), VT);
2181 // For vectors, we can't easily build an all one vector, just return
2182 // the LHS.
2183 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002184 case ISD::SRA:
2185 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002186 }
2187 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002188
Chris Lattner27e9b412005-05-11 18:57:39 +00002189 // Memoize this node if possible.
2190 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002191 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002192 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002193 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002194 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002195 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002196 void *IP = 0;
2197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2198 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002199 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002200 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002201 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002202 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002203 }
2204
Chris Lattnerc3aae252005-01-07 07:46:32 +00002205 AllNodes.push_back(N);
2206 return SDOperand(N, 0);
2207}
2208
Chris Lattnerc3aae252005-01-07 07:46:32 +00002209SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2210 SDOperand N1, SDOperand N2, SDOperand N3) {
2211 // Perform various simplifications.
2212 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2213 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002214 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002215 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002216 // Use FoldSetCC to simplify SETCC's.
2217 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002218 if (Simp.Val) return Simp;
2219 break;
2220 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002221 case ISD::SELECT:
2222 if (N1C)
2223 if (N1C->getValue())
2224 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002225 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002226 return N3; // select false, X, Y -> Y
2227
2228 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002229 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002230 case ISD::BRCOND:
2231 if (N2C)
2232 if (N2C->getValue()) // Unconditional branch
2233 return getNode(ISD::BR, MVT::Other, N1, N3);
2234 else
2235 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002236 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002237 case ISD::VECTOR_SHUFFLE:
2238 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2239 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2240 N3.getOpcode() == ISD::BUILD_VECTOR &&
2241 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2242 "Illegal VECTOR_SHUFFLE node!");
2243 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002244 case ISD::BIT_CONVERT:
2245 // Fold bit_convert nodes from a type to themselves.
2246 if (N1.getValueType() == VT)
2247 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002248 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002249 }
2250
Chris Lattner43247a12005-08-25 19:12:10 +00002251 // Memoize node if it doesn't produce a flag.
2252 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002253 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002254 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002255 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002256 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002257 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002258 void *IP = 0;
2259 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2260 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002261 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002262 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002263 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002264 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002265 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002266 AllNodes.push_back(N);
2267 return SDOperand(N, 0);
2268}
2269
2270SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002271 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002272 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002273 SDOperand Ops[] = { N1, N2, N3, N4 };
2274 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002275}
2276
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002277SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2278 SDOperand N1, SDOperand N2, SDOperand N3,
2279 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002280 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2281 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002282}
2283
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002284SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2285 SDOperand Src, SDOperand Size,
2286 SDOperand Align,
2287 SDOperand AlwaysInline) {
2288 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2289 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2290}
2291
2292SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2293 SDOperand Src, SDOperand Size,
2294 SDOperand Align,
2295 SDOperand AlwaysInline) {
2296 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2297 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2298}
2299
2300SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2301 SDOperand Src, SDOperand Size,
2302 SDOperand Align,
2303 SDOperand AlwaysInline) {
2304 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2305 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2306}
2307
Evan Cheng7038daf2005-12-10 00:37:58 +00002308SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2309 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002310 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002311 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002312 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2313 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002314 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002315 Ty = MVT::getTypeForValueType(VT);
2316 } else if (SV) {
2317 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2318 assert(PT && "Value for load must be a pointer");
2319 Ty = PT->getElementType();
2320 }
2321 assert(Ty && "Could not get type information for load");
2322 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2323 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002324 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002325 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002326 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002327 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002328 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002329 ID.AddInteger(ISD::UNINDEXED);
2330 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002331 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002332 ID.AddInteger(Alignment);
2333 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002334 void *IP = 0;
2335 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2336 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002337 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002338 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2339 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002340 CSEMap.InsertNode(N, IP);
2341 AllNodes.push_back(N);
2342 return SDOperand(N, 0);
2343}
2344
2345SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002346 SDOperand Chain, SDOperand Ptr,
2347 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002348 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002349 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002350 // If they are asking for an extending load from/to the same thing, return a
2351 // normal load.
2352 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002353 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002354
2355 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002356 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002357 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002358 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2359 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002360 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2361 "Cannot sign/zero extend a FP/Vector load!");
2362 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2363 "Cannot convert from FP to Int or Int -> FP!");
2364
Dan Gohman575e2f42007-06-04 15:49:41 +00002365 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2366 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002367 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002368 Ty = MVT::getTypeForValueType(VT);
2369 } else if (SV) {
2370 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2371 assert(PT && "Value for load must be a pointer");
2372 Ty = PT->getElementType();
2373 }
2374 assert(Ty && "Could not get type information for load");
2375 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2376 }
Evan Cheng466685d2006-10-09 20:57:25 +00002377 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002378 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002379 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002380 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002381 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002382 ID.AddInteger(ISD::UNINDEXED);
2383 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002384 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002385 ID.AddInteger(Alignment);
2386 ID.AddInteger(isVolatile);
2387 void *IP = 0;
2388 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2389 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002390 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002391 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002392 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002393 AllNodes.push_back(N);
2394 return SDOperand(N, 0);
2395}
2396
Evan Cheng144d8f02006-11-09 17:55:04 +00002397SDOperand
2398SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2399 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002400 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002401 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2402 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002403 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002404 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002405 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002406 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002407 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002408 ID.AddInteger(AM);
2409 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002410 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002411 ID.AddInteger(LD->getAlignment());
2412 ID.AddInteger(LD->isVolatile());
2413 void *IP = 0;
2414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2415 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002416 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002417 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002418 LD->getSrcValue(), LD->getSrcValueOffset(),
2419 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002420 CSEMap.InsertNode(N, IP);
2421 AllNodes.push_back(N);
2422 return SDOperand(N, 0);
2423}
2424
Jeff Cohend41b30d2006-11-05 19:31:28 +00002425SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002426 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002427 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002428 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002429
Dan Gohman575e2f42007-06-04 15:49:41 +00002430 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2431 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002432 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002433 Ty = MVT::getTypeForValueType(VT);
2434 } else if (SV) {
2435 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2436 assert(PT && "Value for store must be a pointer");
2437 Ty = PT->getElementType();
2438 }
2439 assert(Ty && "Could not get type information for store");
2440 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2441 }
Evan Chengad071e12006-10-05 22:57:11 +00002442 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002443 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002444 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002445 FoldingSetNodeID ID;
2446 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002447 ID.AddInteger(ISD::UNINDEXED);
2448 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002449 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002450 ID.AddInteger(Alignment);
2451 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002452 void *IP = 0;
2453 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2454 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002455 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002456 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002457 CSEMap.InsertNode(N, IP);
2458 AllNodes.push_back(N);
2459 return SDOperand(N, 0);
2460}
2461
Jeff Cohend41b30d2006-11-05 19:31:28 +00002462SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002463 SDOperand Ptr, const Value *SV,
2464 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002465 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002466 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002467
2468 if (VT == SVT)
2469 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002470
Duncan Sandsaf47b112007-10-16 09:56:48 +00002471 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2472 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002473 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2474 "Can't do FP-INT conversion!");
2475
Dan Gohman575e2f42007-06-04 15:49:41 +00002476 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2477 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002478 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002479 Ty = MVT::getTypeForValueType(VT);
2480 } else if (SV) {
2481 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2482 assert(PT && "Value for store must be a pointer");
2483 Ty = PT->getElementType();
2484 }
2485 assert(Ty && "Could not get type information for store");
2486 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2487 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002488 SDVTList VTs = getVTList(MVT::Other);
2489 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002490 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002491 FoldingSetNodeID ID;
2492 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002493 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002494 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002495 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002496 ID.AddInteger(Alignment);
2497 ID.AddInteger(isVolatile);
2498 void *IP = 0;
2499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2500 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002501 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002502 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002503 CSEMap.InsertNode(N, IP);
2504 AllNodes.push_back(N);
2505 return SDOperand(N, 0);
2506}
2507
Evan Cheng144d8f02006-11-09 17:55:04 +00002508SDOperand
2509SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2510 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002511 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2512 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2513 "Store is already a indexed store!");
2514 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2515 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2516 FoldingSetNodeID ID;
2517 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2518 ID.AddInteger(AM);
2519 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002520 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002521 ID.AddInteger(ST->getAlignment());
2522 ID.AddInteger(ST->isVolatile());
2523 void *IP = 0;
2524 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2525 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002526 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002527 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002528 ST->getSrcValue(), ST->getSrcValueOffset(),
2529 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002530 CSEMap.InsertNode(N, IP);
2531 AllNodes.push_back(N);
2532 return SDOperand(N, 0);
2533}
2534
Nate Begemanacc398c2006-01-25 18:21:52 +00002535SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2536 SDOperand Chain, SDOperand Ptr,
2537 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002538 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002539 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002540}
2541
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002542SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002543 const SDOperand *Ops, unsigned NumOps) {
2544 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002545 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002546 case 1: return getNode(Opcode, VT, Ops[0]);
2547 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2548 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002549 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002550 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002551
Chris Lattneref847df2005-04-09 03:27:28 +00002552 switch (Opcode) {
2553 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002554 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002555 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002556 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2557 "LHS and RHS of condition must have same type!");
2558 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2559 "True and False arms of SelectCC must have same type!");
2560 assert(Ops[2].getValueType() == VT &&
2561 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002562 break;
2563 }
2564 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002565 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002566 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2567 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002568 break;
2569 }
Chris Lattneref847df2005-04-09 03:27:28 +00002570 }
2571
Chris Lattner385328c2005-05-14 07:42:29 +00002572 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002573 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002574 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002575 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002576 FoldingSetNodeID ID;
2577 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002578 void *IP = 0;
2579 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2580 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002581 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002582 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002583 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002584 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002585 }
Chris Lattneref847df2005-04-09 03:27:28 +00002586 AllNodes.push_back(N);
2587 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002588}
2589
Chris Lattner89c34632005-05-14 06:20:26 +00002590SDOperand SelectionDAG::getNode(unsigned Opcode,
2591 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002592 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002593 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2594 Ops, NumOps);
2595}
2596
2597SDOperand SelectionDAG::getNode(unsigned Opcode,
2598 const MVT::ValueType *VTs, unsigned NumVTs,
2599 const SDOperand *Ops, unsigned NumOps) {
2600 if (NumVTs == 1)
2601 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002602 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2603}
2604
2605SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2606 const SDOperand *Ops, unsigned NumOps) {
2607 if (VTList.NumVTs == 1)
2608 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002609
Chris Lattner5f056bf2005-07-10 01:55:33 +00002610 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002611 // FIXME: figure out how to safely handle things like
2612 // int foo(int x) { return 1 << (x & 255); }
2613 // int bar() { return foo(256); }
2614#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002615 case ISD::SRA_PARTS:
2616 case ISD::SRL_PARTS:
2617 case ISD::SHL_PARTS:
2618 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002619 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002620 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2621 else if (N3.getOpcode() == ISD::AND)
2622 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2623 // If the and is only masking out bits that cannot effect the shift,
2624 // eliminate the and.
2625 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2626 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2627 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2628 }
2629 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002630#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002631 }
Chris Lattner89c34632005-05-14 06:20:26 +00002632
Chris Lattner43247a12005-08-25 19:12:10 +00002633 // Memoize the node unless it returns a flag.
2634 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002635 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002636 FoldingSetNodeID ID;
2637 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002638 void *IP = 0;
2639 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2640 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002641 if (NumOps == 1)
2642 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2643 else if (NumOps == 2)
2644 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2645 else if (NumOps == 3)
2646 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2647 else
2648 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002649 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002650 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002651 if (NumOps == 1)
2652 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2653 else if (NumOps == 2)
2654 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2655 else if (NumOps == 3)
2656 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2657 else
2658 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002659 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002660 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002661 return SDOperand(N, 0);
2662}
2663
Dan Gohman08ce9762007-10-08 15:49:58 +00002664SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2665 return getNode(Opcode, VTList, 0, 0);
2666}
2667
2668SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2669 SDOperand N1) {
2670 SDOperand Ops[] = { N1 };
2671 return getNode(Opcode, VTList, Ops, 1);
2672}
2673
2674SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2675 SDOperand N1, SDOperand N2) {
2676 SDOperand Ops[] = { N1, N2 };
2677 return getNode(Opcode, VTList, Ops, 2);
2678}
2679
2680SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2681 SDOperand N1, SDOperand N2, SDOperand N3) {
2682 SDOperand Ops[] = { N1, N2, N3 };
2683 return getNode(Opcode, VTList, Ops, 3);
2684}
2685
2686SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2687 SDOperand N1, SDOperand N2, SDOperand N3,
2688 SDOperand N4) {
2689 SDOperand Ops[] = { N1, N2, N3, N4 };
2690 return getNode(Opcode, VTList, Ops, 4);
2691}
2692
2693SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2694 SDOperand N1, SDOperand N2, SDOperand N3,
2695 SDOperand N4, SDOperand N5) {
2696 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2697 return getNode(Opcode, VTList, Ops, 5);
2698}
2699
Chris Lattner70046e92006-08-15 17:46:01 +00002700SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002701 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002702}
2703
Chris Lattner70046e92006-08-15 17:46:01 +00002704SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002705 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2706 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002707 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002708 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002709 }
2710 std::vector<MVT::ValueType> V;
2711 V.push_back(VT1);
2712 V.push_back(VT2);
2713 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002714 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002715}
Chris Lattner70046e92006-08-15 17:46:01 +00002716SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2717 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002718 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002719 E = VTList.end(); I != E; ++I) {
2720 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2721 (*I)[2] == VT3)
2722 return makeVTList(&(*I)[0], 3);
2723 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002724 std::vector<MVT::ValueType> V;
2725 V.push_back(VT1);
2726 V.push_back(VT2);
2727 V.push_back(VT3);
2728 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002729 return makeVTList(&(*VTList.begin())[0], 3);
2730}
2731
2732SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2733 switch (NumVTs) {
2734 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002735 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002736 case 2: return getVTList(VTs[0], VTs[1]);
2737 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2738 default: break;
2739 }
2740
2741 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2742 E = VTList.end(); I != E; ++I) {
2743 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2744
2745 bool NoMatch = false;
2746 for (unsigned i = 2; i != NumVTs; ++i)
2747 if (VTs[i] != (*I)[i]) {
2748 NoMatch = true;
2749 break;
2750 }
2751 if (!NoMatch)
2752 return makeVTList(&*I->begin(), NumVTs);
2753 }
2754
2755 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2756 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002757}
2758
2759
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002760/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2761/// specified operands. If the resultant node already exists in the DAG,
2762/// this does not modify the specified node, instead it returns the node that
2763/// already exists. If the resultant node does not exist in the DAG, the
2764/// input node is returned. As a degenerate case, if you specify the same
2765/// input operands as the node already has, the input node is returned.
2766SDOperand SelectionDAG::
2767UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2768 SDNode *N = InN.Val;
2769 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2770
2771 // Check to see if there is no change.
2772 if (Op == N->getOperand(0)) return InN;
2773
2774 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002775 void *InsertPos = 0;
2776 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2777 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002778
2779 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002780 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002781 RemoveNodeFromCSEMaps(N);
2782
2783 // Now we update the operands.
2784 N->OperandList[0].Val->removeUser(N);
2785 Op.Val->addUser(N);
2786 N->OperandList[0] = Op;
2787
2788 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002789 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002790 return InN;
2791}
2792
2793SDOperand SelectionDAG::
2794UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2795 SDNode *N = InN.Val;
2796 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2797
2798 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002799 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2800 return InN; // No operands changed, just return the input node.
2801
2802 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002803 void *InsertPos = 0;
2804 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2805 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002806
2807 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002808 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002809 RemoveNodeFromCSEMaps(N);
2810
2811 // Now we update the operands.
2812 if (N->OperandList[0] != Op1) {
2813 N->OperandList[0].Val->removeUser(N);
2814 Op1.Val->addUser(N);
2815 N->OperandList[0] = Op1;
2816 }
2817 if (N->OperandList[1] != Op2) {
2818 N->OperandList[1].Val->removeUser(N);
2819 Op2.Val->addUser(N);
2820 N->OperandList[1] = Op2;
2821 }
2822
2823 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002824 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002825 return InN;
2826}
2827
2828SDOperand SelectionDAG::
2829UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002830 SDOperand Ops[] = { Op1, Op2, Op3 };
2831 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002832}
2833
2834SDOperand SelectionDAG::
2835UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2836 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002837 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2838 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002839}
2840
2841SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002842UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2843 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002844 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2845 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002846}
2847
2848
2849SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002850UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002851 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002852 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002853 "Update with wrong number of operands");
2854
2855 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002856 bool AnyChange = false;
2857 for (unsigned i = 0; i != NumOps; ++i) {
2858 if (Ops[i] != N->getOperand(i)) {
2859 AnyChange = true;
2860 break;
2861 }
2862 }
2863
2864 // No operands changed, just return the input node.
2865 if (!AnyChange) return InN;
2866
2867 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002868 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002869 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002870 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002871
2872 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002873 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002874 RemoveNodeFromCSEMaps(N);
2875
2876 // Now we update the operands.
2877 for (unsigned i = 0; i != NumOps; ++i) {
2878 if (N->OperandList[i] != Ops[i]) {
2879 N->OperandList[i].Val->removeUser(N);
2880 Ops[i].Val->addUser(N);
2881 N->OperandList[i] = Ops[i];
2882 }
2883 }
2884
2885 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002886 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002887 return InN;
2888}
2889
2890
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002891/// MorphNodeTo - This frees the operands of the current node, resets the
2892/// opcode, types, and operands to the specified value. This should only be
2893/// used by the SelectionDAG class.
2894void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2895 const SDOperand *Ops, unsigned NumOps) {
2896 NodeType = Opc;
2897 ValueList = L.VTs;
2898 NumValues = L.NumVTs;
2899
2900 // Clear the operands list, updating used nodes to remove this from their
2901 // use list.
2902 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2903 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002904
2905 // If NumOps is larger than the # of operands we currently have, reallocate
2906 // the operand list.
2907 if (NumOps > NumOperands) {
2908 if (OperandsNeedDelete)
2909 delete [] OperandList;
2910 OperandList = new SDOperand[NumOps];
2911 OperandsNeedDelete = true;
2912 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002913
2914 // Assign the new operands.
2915 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002916
2917 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2918 OperandList[i] = Ops[i];
2919 SDNode *N = OperandList[i].Val;
2920 N->Uses.push_back(this);
2921 }
2922}
Chris Lattner149c58c2005-08-16 18:17:10 +00002923
2924/// SelectNodeTo - These are used for target selectors to *mutate* the
2925/// specified node to have the specified return type, Target opcode, and
2926/// operands. Note that target opcodes are stored as
2927/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002928///
2929/// Note that SelectNodeTo returns the resultant node. If there is already a
2930/// node of the specified opcode and operands, it returns that node instead of
2931/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002932SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2933 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002934 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002935 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002936 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002937 void *IP = 0;
2938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002939 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002940
Chris Lattner7651fa42005-08-24 23:00:29 +00002941 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002942
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002944
Chris Lattner4a283e92006-08-11 18:38:11 +00002945 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002946 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002947}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002948
Evan Cheng95514ba2006-08-26 08:00:10 +00002949SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2950 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002951 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002952 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002953 SDOperand Ops[] = { Op1 };
2954
Jim Laskey583bd472006-10-27 23:46:08 +00002955 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002956 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002957 void *IP = 0;
2958 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002959 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002960
Chris Lattner149c58c2005-08-16 18:17:10 +00002961 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002962 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002963 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002964 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002965}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002966
Evan Cheng95514ba2006-08-26 08:00:10 +00002967SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2968 MVT::ValueType VT, SDOperand Op1,
2969 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002970 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002971 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002972 SDOperand Ops[] = { Op1, Op2 };
2973
Jim Laskey583bd472006-10-27 23:46:08 +00002974 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002975 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002976 void *IP = 0;
2977 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002978 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002979
Chris Lattner149c58c2005-08-16 18:17:10 +00002980 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002981
Chris Lattner63e3f142007-02-04 07:28:00 +00002982 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002983
Chris Lattnera5682852006-08-07 23:03:03 +00002984 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002985 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002986}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002987
Evan Cheng95514ba2006-08-26 08:00:10 +00002988SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2989 MVT::ValueType VT, SDOperand Op1,
2990 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002991 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002992 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002993 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002994 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002995 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002996 void *IP = 0;
2997 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002998 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002999
Chris Lattner149c58c2005-08-16 18:17:10 +00003000 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003001
Chris Lattner63e3f142007-02-04 07:28:00 +00003002 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003003
Chris Lattnera5682852006-08-07 23:03:03 +00003004 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003005 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003006}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003007
Evan Cheng95514ba2006-08-26 08:00:10 +00003008SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003009 MVT::ValueType VT, const SDOperand *Ops,
3010 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003011 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003012 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003013 FoldingSetNodeID ID;
3014 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003015 void *IP = 0;
3016 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003017 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003018
Chris Lattner6b09a292005-08-21 18:49:33 +00003019 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003020 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003021
Chris Lattnera5682852006-08-07 23:03:03 +00003022 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003023 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003024}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003025
Evan Cheng95514ba2006-08-26 08:00:10 +00003026SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3027 MVT::ValueType VT1, MVT::ValueType VT2,
3028 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003029 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003030 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003031 SDOperand Ops[] = { Op1, Op2 };
3032 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003033 void *IP = 0;
3034 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003035 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003036
Chris Lattner0fb094f2005-11-19 01:44:53 +00003037 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003038 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003039 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003040 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003041}
3042
Evan Cheng95514ba2006-08-26 08:00:10 +00003043SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3044 MVT::ValueType VT1, MVT::ValueType VT2,
3045 SDOperand Op1, SDOperand Op2,
3046 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003047 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003048 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003049 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003050 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003051 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003052 void *IP = 0;
3053 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003054 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003055
Chris Lattner0fb094f2005-11-19 01:44:53 +00003056 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003057
Chris Lattner63e3f142007-02-04 07:28:00 +00003058 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003059 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003060 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003061}
3062
Chris Lattner0fb094f2005-11-19 01:44:53 +00003063
Evan Cheng6ae46c42006-02-09 07:15:23 +00003064/// getTargetNode - These are used for target selectors to create a new node
3065/// with specified return type(s), target opcode, and operands.
3066///
3067/// Note that getTargetNode returns the resultant node. If there is already a
3068/// node of the specified opcode and operands, it returns that node instead of
3069/// the current one.
3070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3071 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3072}
3073SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3074 SDOperand Op1) {
3075 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3076}
3077SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3078 SDOperand Op1, SDOperand Op2) {
3079 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3080}
3081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003082 SDOperand Op1, SDOperand Op2,
3083 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003084 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3085}
3086SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003087 const SDOperand *Ops, unsigned NumOps) {
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003089}
3090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003091 MVT::ValueType VT2) {
3092 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3093 SDOperand Op;
3094 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3095}
3096SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003097 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003098 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003099 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).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) {
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 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003106 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003107}
3108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003109 MVT::ValueType VT2, SDOperand Op1,
3110 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003111 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003112 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003113 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003114}
Evan Cheng694481e2006-08-27 08:08:54 +00003115SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3116 MVT::ValueType VT2,
3117 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003118 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003120}
3121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3122 MVT::ValueType VT2, MVT::ValueType VT3,
3123 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003124 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003125 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003126 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003127}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003128SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3129 MVT::ValueType VT2, MVT::ValueType VT3,
3130 SDOperand Op1, SDOperand Op2,
3131 SDOperand Op3) {
3132 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3133 SDOperand Ops[] = { Op1, Op2, Op3 };
3134 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3135}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003137 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003138 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003139 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3140 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003141}
Evan Cheng05e69c12007-09-12 23:39:49 +00003142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3143 MVT::ValueType VT2, MVT::ValueType VT3,
3144 MVT::ValueType VT4,
3145 const SDOperand *Ops, unsigned NumOps) {
3146 std::vector<MVT::ValueType> VTList;
3147 VTList.push_back(VT1);
3148 VTList.push_back(VT2);
3149 VTList.push_back(VT3);
3150 VTList.push_back(VT4);
3151 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3152 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3153}
Evan Cheng39305cf2007-10-05 01:10:49 +00003154SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3155 std::vector<MVT::ValueType> &ResultTys,
3156 const SDOperand *Ops, unsigned NumOps) {
3157 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3158 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3159 Ops, NumOps).Val;
3160}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003161
Evan Cheng99157a02006-08-07 22:13:29 +00003162/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003163/// This can cause recursive merging of nodes in the DAG.
3164///
Chris Lattner8b52f212005-08-26 18:36:28 +00003165/// This version assumes From/To have a single result value.
3166///
Chris Lattner1e111c72005-09-07 05:37:01 +00003167void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3168 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003169 SDNode *From = FromN.Val, *To = ToN.Val;
3170 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3171 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003172 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003173
Chris Lattner8b8749f2005-08-17 19:00:20 +00003174 while (!From->use_empty()) {
3175 // Process users until they are all gone.
3176 SDNode *U = *From->use_begin();
3177
3178 // This node is about to morph, remove its old self from the CSE maps.
3179 RemoveNodeFromCSEMaps(U);
3180
Chris Lattner65113b22005-11-08 22:07:03 +00003181 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3182 I != E; ++I)
3183 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003184 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003185 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003186 To->addUser(U);
3187 }
3188
3189 // Now that we have modified U, add it back to the CSE maps. If it already
3190 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003191 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3192 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003193 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003194 if (Deleted) Deleted->push_back(U);
3195 DeleteNodeNotInCSEMaps(U);
3196 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003197 }
3198}
3199
3200/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3201/// This can cause recursive merging of nodes in the DAG.
3202///
3203/// This version assumes From/To have matching types and numbers of result
3204/// values.
3205///
Chris Lattner1e111c72005-09-07 05:37:01 +00003206void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3207 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003208 assert(From != To && "Cannot replace uses of with self");
3209 assert(From->getNumValues() == To->getNumValues() &&
3210 "Cannot use this version of ReplaceAllUsesWith!");
3211 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003212 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003213 return;
3214 }
3215
3216 while (!From->use_empty()) {
3217 // Process users until they are all gone.
3218 SDNode *U = *From->use_begin();
3219
3220 // This node is about to morph, remove its old self from the CSE maps.
3221 RemoveNodeFromCSEMaps(U);
3222
Chris Lattner65113b22005-11-08 22:07:03 +00003223 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3224 I != E; ++I)
3225 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003226 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003227 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003228 To->addUser(U);
3229 }
3230
3231 // Now that we have modified U, add it back to the CSE maps. If it already
3232 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003233 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3234 ReplaceAllUsesWith(U, Existing, Deleted);
3235 // U is now dead.
3236 if (Deleted) Deleted->push_back(U);
3237 DeleteNodeNotInCSEMaps(U);
3238 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003239 }
3240}
3241
Chris Lattner8b52f212005-08-26 18:36:28 +00003242/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3243/// This can cause recursive merging of nodes in the DAG.
3244///
3245/// This version can replace From with any result values. To must match the
3246/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003247void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003248 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003249 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003250 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003251 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003252 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003253 return;
3254 }
3255
3256 while (!From->use_empty()) {
3257 // Process users until they are all gone.
3258 SDNode *U = *From->use_begin();
3259
3260 // This node is about to morph, remove its old self from the CSE maps.
3261 RemoveNodeFromCSEMaps(U);
3262
Chris Lattner65113b22005-11-08 22:07:03 +00003263 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3264 I != E; ++I)
3265 if (I->Val == From) {
3266 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003267 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003268 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003269 ToOp.Val->addUser(U);
3270 }
3271
3272 // Now that we have modified U, add it back to the CSE maps. If it already
3273 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003274 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3275 ReplaceAllUsesWith(U, Existing, Deleted);
3276 // U is now dead.
3277 if (Deleted) Deleted->push_back(U);
3278 DeleteNodeNotInCSEMaps(U);
3279 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003280 }
3281}
3282
Chris Lattner012f2412006-02-17 21:58:01 +00003283/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3284/// uses of other values produced by From.Val alone. The Deleted vector is
3285/// handled the same was as for ReplaceAllUsesWith.
3286void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003287 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003288 assert(From != To && "Cannot replace a value with itself");
3289 // Handle the simple, trivial, case efficiently.
3290 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003291 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003292 return;
3293 }
3294
Chris Lattnercf5640b2007-02-04 00:14:31 +00003295 // Get all of the users of From.Val. We want these in a nice,
3296 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3297 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003298
Chris Lattner01d029b2007-10-15 06:10:22 +00003299 std::vector<SDNode*> LocalDeletionVector;
3300
3301 // Pick a deletion vector to use. If the user specified one, use theirs,
3302 // otherwise use a local one.
3303 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003304 while (!Users.empty()) {
3305 // We know that this user uses some value of From. If it is the right
3306 // value, update it.
3307 SDNode *User = Users.back();
3308 Users.pop_back();
3309
Chris Lattner01d029b2007-10-15 06:10:22 +00003310 // Scan for an operand that matches From.
3311 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3312 for (; Op != E; ++Op)
3313 if (*Op == From) break;
3314
3315 // If there are no matches, the user must use some other result of From.
3316 if (Op == E) continue;
3317
3318 // Okay, we know this user needs to be updated. Remove its old self
3319 // from the CSE maps.
3320 RemoveNodeFromCSEMaps(User);
3321
3322 // Update all operands that match "From".
3323 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003324 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003325 From.Val->removeUser(User);
3326 *Op = To;
3327 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003328 }
3329 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003330
3331 // Now that we have modified User, add it back to the CSE maps. If it
3332 // already exists there, recursively merge the results together.
3333 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3334 if (!Existing) continue; // Continue on to next user.
3335
3336 // If there was already an existing matching node, use ReplaceAllUsesWith
3337 // to replace the dead one with the existing one. However, this can cause
3338 // recursive merging of other unrelated nodes down the line. The merging
3339 // can cause deletion of nodes that used the old value. In this case,
3340 // we have to be certain to remove them from the Users set.
3341 unsigned NumDeleted = DeleteVector->size();
3342 ReplaceAllUsesWith(User, Existing, DeleteVector);
3343
3344 // User is now dead.
3345 DeleteVector->push_back(User);
3346 DeleteNodeNotInCSEMaps(User);
3347
3348 // We have to be careful here, because ReplaceAllUsesWith could have
3349 // deleted a user of From, which means there may be dangling pointers
3350 // in the "Users" setvector. Scan over the deleted node pointers and
3351 // remove them from the setvector.
3352 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3353 Users.remove((*DeleteVector)[i]);
3354
3355 // If the user doesn't need the set of deleted elements, don't retain them
3356 // to the next loop iteration.
3357 if (Deleted == 0)
3358 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003359 }
3360}
3361
Chris Lattner7b2880c2005-08-24 22:44:39 +00003362
Evan Chenge6f35d82006-08-01 08:20:41 +00003363/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3364/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003365unsigned SelectionDAG::AssignNodeIds() {
3366 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003367 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3368 SDNode *N = I;
3369 N->setNodeId(Id++);
3370 }
3371 return Id;
3372}
3373
Evan Chenge6f35d82006-08-01 08:20:41 +00003374/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003375/// based on their topological order. It returns the maximum id and a vector
3376/// of the SDNodes* in assigned order by reference.
3377unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003378 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003379 std::vector<unsigned> InDegree(DAGSize);
3380 std::vector<SDNode*> Sources;
3381
3382 // Use a two pass approach to avoid using a std::map which is slow.
3383 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003384 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3385 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003386 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003387 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003388 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003389 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003390 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003391 }
3392
Evan Cheng99157a02006-08-07 22:13:29 +00003393 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003394 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003395 SDNode *N = Sources.back();
3396 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003397 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003398 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3399 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003400 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003401 if (Degree == 0)
3402 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003403 }
3404 }
3405
Evan Chengc384d6c2006-08-02 22:00:34 +00003406 // Second pass, assign the actual topological order as node ids.
3407 Id = 0;
3408 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3409 TI != TE; ++TI)
3410 (*TI)->setNodeId(Id++);
3411
3412 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003413}
3414
3415
Evan Cheng091cba12006-07-27 06:39:06 +00003416
Jim Laskey58b968b2005-08-17 20:08:02 +00003417//===----------------------------------------------------------------------===//
3418// SDNode Class
3419//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003420
Chris Lattner917d2c92006-07-19 00:00:37 +00003421// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003422void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003423void UnarySDNode::ANCHOR() {}
3424void BinarySDNode::ANCHOR() {}
3425void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003426void HandleSDNode::ANCHOR() {}
3427void StringSDNode::ANCHOR() {}
3428void ConstantSDNode::ANCHOR() {}
3429void ConstantFPSDNode::ANCHOR() {}
3430void GlobalAddressSDNode::ANCHOR() {}
3431void FrameIndexSDNode::ANCHOR() {}
3432void JumpTableSDNode::ANCHOR() {}
3433void ConstantPoolSDNode::ANCHOR() {}
3434void BasicBlockSDNode::ANCHOR() {}
3435void SrcValueSDNode::ANCHOR() {}
3436void RegisterSDNode::ANCHOR() {}
3437void ExternalSymbolSDNode::ANCHOR() {}
3438void CondCodeSDNode::ANCHOR() {}
3439void VTSDNode::ANCHOR() {}
3440void LoadSDNode::ANCHOR() {}
3441void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003442
Chris Lattner48b85922007-02-04 02:41:42 +00003443HandleSDNode::~HandleSDNode() {
3444 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003445 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003446}
3447
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003448GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3449 MVT::ValueType VT, int o)
3450 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003451 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003452 // Thread Local
3453 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3454 // Non Thread Local
3455 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3456 getSDVTList(VT)), Offset(o) {
3457 TheGlobal = const_cast<GlobalValue*>(GA);
3458}
Chris Lattner48b85922007-02-04 02:41:42 +00003459
Jim Laskey583bd472006-10-27 23:46:08 +00003460/// Profile - Gather unique data for the node.
3461///
3462void SDNode::Profile(FoldingSetNodeID &ID) {
3463 AddNodeIDNode(ID, this);
3464}
3465
Chris Lattnera3255112005-11-08 23:30:28 +00003466/// getValueTypeList - Return a pointer to the specified value type.
3467///
3468MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003469 if (MVT::isExtendedVT(VT)) {
3470 static std::set<MVT::ValueType> EVTs;
3471 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3472 } else {
3473 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3474 VTs[VT] = VT;
3475 return &VTs[VT];
3476 }
Chris Lattnera3255112005-11-08 23:30:28 +00003477}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003478
Chris Lattner5c884562005-01-12 18:37:47 +00003479/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3480/// indicated value. This method ignores uses of other values defined by this
3481/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003482bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003483 assert(Value < getNumValues() && "Bad value!");
3484
3485 // If there is only one value, this is easy.
3486 if (getNumValues() == 1)
3487 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003488 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003489
Evan Cheng4ee62112006-02-05 06:29:23 +00003490 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003491
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003492 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003493
Chris Lattnerf83482d2006-08-16 20:59:32 +00003494 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003495 SDNode *User = *UI;
3496 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003497 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003498 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3499 if (User->getOperand(i) == TheValue) {
3500 if (NUses == 0)
3501 return false; // too many uses
3502 --NUses;
3503 }
3504 }
3505
3506 // Found exactly the right number of uses?
3507 return NUses == 0;
3508}
3509
3510
Evan Cheng33d55952007-08-02 05:29:38 +00003511/// hasAnyUseOfValue - Return true if there are any use of the indicated
3512/// value. This method ignores uses of other values defined by this operation.
3513bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3514 assert(Value < getNumValues() && "Bad value!");
3515
Dan Gohman30359592008-01-29 13:02:09 +00003516 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003517
3518 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3519
3520 SmallPtrSet<SDNode*, 32> UsersHandled;
3521
3522 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3523 SDNode *User = *UI;
3524 if (User->getNumOperands() == 1 ||
3525 UsersHandled.insert(User)) // First time we've seen this?
3526 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3527 if (User->getOperand(i) == TheValue) {
3528 return true;
3529 }
3530 }
3531
3532 return false;
3533}
3534
3535
Evan Chenge6e97e62006-11-03 07:31:32 +00003536/// isOnlyUse - Return true if this node is the only use of N.
3537///
Evan Cheng4ee62112006-02-05 06:29:23 +00003538bool SDNode::isOnlyUse(SDNode *N) const {
3539 bool Seen = false;
3540 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3541 SDNode *User = *I;
3542 if (User == this)
3543 Seen = true;
3544 else
3545 return false;
3546 }
3547
3548 return Seen;
3549}
3550
Evan Chenge6e97e62006-11-03 07:31:32 +00003551/// isOperand - Return true if this node is an operand of N.
3552///
Evan Chengbfa284f2006-03-03 06:42:32 +00003553bool SDOperand::isOperand(SDNode *N) const {
3554 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3555 if (*this == N->getOperand(i))
3556 return true;
3557 return false;
3558}
3559
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003560bool SDNode::isOperand(SDNode *N) const {
3561 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3562 if (this == N->OperandList[i].Val)
3563 return true;
3564 return false;
3565}
Evan Cheng4ee62112006-02-05 06:29:23 +00003566
Chris Lattner572dee72008-01-16 05:49:24 +00003567/// reachesChainWithoutSideEffects - Return true if this operand (which must
3568/// be a chain) reaches the specified operand without crossing any
3569/// side-effecting instructions. In practice, this looks through token
3570/// factors and non-volatile loads. In order to remain efficient, this only
3571/// looks a couple of nodes in, it does not do an exhaustive search.
3572bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3573 unsigned Depth) const {
3574 if (*this == Dest) return true;
3575
3576 // Don't search too deeply, we just want to be able to see through
3577 // TokenFactor's etc.
3578 if (Depth == 0) return false;
3579
3580 // If this is a token factor, all inputs to the TF happen in parallel. If any
3581 // of the operands of the TF reach dest, then we can do the xform.
3582 if (getOpcode() == ISD::TokenFactor) {
3583 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3584 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3585 return true;
3586 return false;
3587 }
3588
3589 // Loads don't have side effects, look through them.
3590 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3591 if (!Ld->isVolatile())
3592 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3593 }
3594 return false;
3595}
3596
3597
Evan Chengc5fc57d2006-11-03 03:05:24 +00003598static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003599 SmallPtrSet<SDNode *, 32> &Visited) {
3600 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003601 return;
3602
3603 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3604 SDNode *Op = N->getOperand(i).Val;
3605 if (Op == P) {
3606 found = true;
3607 return;
3608 }
3609 findPredecessor(Op, P, found, Visited);
3610 }
3611}
3612
Evan Chenge6e97e62006-11-03 07:31:32 +00003613/// isPredecessor - Return true if this node is a predecessor of N. This node
3614/// is either an operand of N or it can be reached by recursively traversing
3615/// up the operands.
3616/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003617bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003618 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003619 bool found = false;
3620 findPredecessor(N, this, found, Visited);
3621 return found;
3622}
3623
Evan Chengc5484282006-10-04 00:56:09 +00003624uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3625 assert(Num < NumOperands && "Invalid child # of SDNode!");
3626 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3627}
3628
Reid Spencer577cc322007-04-01 07:32:19 +00003629std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003630 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003631 default:
3632 if (getOpcode() < ISD::BUILTIN_OP_END)
3633 return "<<Unknown DAG Node>>";
3634 else {
Evan Cheng72261582005-12-20 06:22:03 +00003635 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003636 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003637 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003638 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003639
Evan Cheng72261582005-12-20 06:22:03 +00003640 TargetLowering &TLI = G->getTargetLoweringInfo();
3641 const char *Name =
3642 TLI.getTargetNodeName(getOpcode());
3643 if (Name) return Name;
3644 }
3645
3646 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003647 }
3648
Andrew Lenharth95762122005-03-31 21:24:06 +00003649 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003650 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003651 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003652 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003653 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003654 case ISD::AssertSext: return "AssertSext";
3655 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003656
3657 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003658 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003659 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003660 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003661
3662 case ISD::Constant: return "Constant";
3663 case ISD::ConstantFP: return "ConstantFP";
3664 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003665 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003666 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003667 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003668 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003669 case ISD::RETURNADDR: return "RETURNADDR";
3670 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003671 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003672 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3673 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003674 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003675 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003676 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003677 case ISD::INTRINSIC_WO_CHAIN: {
3678 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3679 return Intrinsic::getName((Intrinsic::ID)IID);
3680 }
3681 case ISD::INTRINSIC_VOID:
3682 case ISD::INTRINSIC_W_CHAIN: {
3683 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003684 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003685 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003686
Chris Lattnerb2827b02006-03-19 00:52:58 +00003687 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003688 case ISD::TargetConstant: return "TargetConstant";
3689 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003690 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003691 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003692 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003693 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003694 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003695 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003696
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003697 case ISD::CopyToReg: return "CopyToReg";
3698 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003699 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003700 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003701 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003702 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003703 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003704 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003705 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003706
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003707 // Unary operators
3708 case ISD::FABS: return "fabs";
3709 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003710 case ISD::FSQRT: return "fsqrt";
3711 case ISD::FSIN: return "fsin";
3712 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003713 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003714 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003715
3716 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003717 case ISD::ADD: return "add";
3718 case ISD::SUB: return "sub";
3719 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003720 case ISD::MULHU: return "mulhu";
3721 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003722 case ISD::SDIV: return "sdiv";
3723 case ISD::UDIV: return "udiv";
3724 case ISD::SREM: return "srem";
3725 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003726 case ISD::SMUL_LOHI: return "smul_lohi";
3727 case ISD::UMUL_LOHI: return "umul_lohi";
3728 case ISD::SDIVREM: return "sdivrem";
3729 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003730 case ISD::AND: return "and";
3731 case ISD::OR: return "or";
3732 case ISD::XOR: return "xor";
3733 case ISD::SHL: return "shl";
3734 case ISD::SRA: return "sra";
3735 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003736 case ISD::ROTL: return "rotl";
3737 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003738 case ISD::FADD: return "fadd";
3739 case ISD::FSUB: return "fsub";
3740 case ISD::FMUL: return "fmul";
3741 case ISD::FDIV: return "fdiv";
3742 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003743 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003744 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003745
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003746 case ISD::SETCC: return "setcc";
3747 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003748 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003749 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003750 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003751 case ISD::CONCAT_VECTORS: return "concat_vectors";
3752 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003753 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003754 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003755 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003756 case ISD::ADDC: return "addc";
3757 case ISD::ADDE: return "adde";
3758 case ISD::SUBC: return "subc";
3759 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003760 case ISD::SHL_PARTS: return "shl_parts";
3761 case ISD::SRA_PARTS: return "sra_parts";
3762 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003763
3764 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3765 case ISD::INSERT_SUBREG: return "insert_subreg";
3766
Chris Lattner7f644642005-04-28 21:44:03 +00003767 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003768 case ISD::SIGN_EXTEND: return "sign_extend";
3769 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003770 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003771 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003772 case ISD::TRUNCATE: return "truncate";
3773 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003774 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003775 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003776 case ISD::FP_EXTEND: return "fp_extend";
3777
3778 case ISD::SINT_TO_FP: return "sint_to_fp";
3779 case ISD::UINT_TO_FP: return "uint_to_fp";
3780 case ISD::FP_TO_SINT: return "fp_to_sint";
3781 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003782 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003783
3784 // Control flow instructions
3785 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003786 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003787 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003788 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003789 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003790 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003791 case ISD::CALLSEQ_START: return "callseq_start";
3792 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003793
3794 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003795 case ISD::LOAD: return "load";
3796 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003797 case ISD::VAARG: return "vaarg";
3798 case ISD::VACOPY: return "vacopy";
3799 case ISD::VAEND: return "vaend";
3800 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003801 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003802 case ISD::EXTRACT_ELEMENT: return "extract_element";
3803 case ISD::BUILD_PAIR: return "build_pair";
3804 case ISD::STACKSAVE: return "stacksave";
3805 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003806 case ISD::TRAP: return "trap";
3807
Chris Lattner5a67afc2006-01-13 02:39:42 +00003808 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003809 case ISD::MEMSET: return "memset";
3810 case ISD::MEMCPY: return "memcpy";
3811 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003812
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003813 // Bit manipulation
3814 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003815 case ISD::CTPOP: return "ctpop";
3816 case ISD::CTTZ: return "cttz";
3817 case ISD::CTLZ: return "ctlz";
3818
Chris Lattner36ce6912005-11-29 06:21:05 +00003819 // Debug info
3820 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003821 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003822
Duncan Sands36397f52007-07-27 12:58:54 +00003823 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003824 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003825
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003826 case ISD::CONDCODE:
3827 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003828 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003829 case ISD::SETOEQ: return "setoeq";
3830 case ISD::SETOGT: return "setogt";
3831 case ISD::SETOGE: return "setoge";
3832 case ISD::SETOLT: return "setolt";
3833 case ISD::SETOLE: return "setole";
3834 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003835
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003836 case ISD::SETO: return "seto";
3837 case ISD::SETUO: return "setuo";
3838 case ISD::SETUEQ: return "setue";
3839 case ISD::SETUGT: return "setugt";
3840 case ISD::SETUGE: return "setuge";
3841 case ISD::SETULT: return "setult";
3842 case ISD::SETULE: return "setule";
3843 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003844
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003845 case ISD::SETEQ: return "seteq";
3846 case ISD::SETGT: return "setgt";
3847 case ISD::SETGE: return "setge";
3848 case ISD::SETLT: return "setlt";
3849 case ISD::SETLE: return "setle";
3850 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003851 }
3852 }
3853}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003854
Evan Cheng144d8f02006-11-09 17:55:04 +00003855const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003856 switch (AM) {
3857 default:
3858 return "";
3859 case ISD::PRE_INC:
3860 return "<pre-inc>";
3861 case ISD::PRE_DEC:
3862 return "<pre-dec>";
3863 case ISD::POST_INC:
3864 return "<post-inc>";
3865 case ISD::POST_DEC:
3866 return "<post-dec>";
3867 }
3868}
3869
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003870void SDNode::dump() const { dump(0); }
3871void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003872 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003873
3874 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003875 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003876 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003877 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003878 else
Bill Wendling832171c2006-12-07 20:04:42 +00003879 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003880 }
Bill Wendling832171c2006-12-07 20:04:42 +00003881 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003882
Bill Wendling832171c2006-12-07 20:04:42 +00003883 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003884 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003885 if (i) cerr << ", ";
3886 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003887 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003888 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003889 }
3890
Evan Chengce254432007-12-11 02:08:35 +00003891 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3892 SDNode *Mask = getOperand(2).Val;
3893 cerr << "<";
3894 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3895 if (i) cerr << ",";
3896 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3897 cerr << "u";
3898 else
3899 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3900 }
3901 cerr << ">";
3902 }
3903
Chris Lattnerc3aae252005-01-07 07:46:32 +00003904 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003905 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003906 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003907 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3908 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3909 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3910 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3911 else {
3912 cerr << "<APFloat(";
3913 CSDN->getValueAPF().convertToAPInt().dump();
3914 cerr << ")>";
3915 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003916 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003917 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003918 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003919 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003920 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003921 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003922 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003923 else
Bill Wendling832171c2006-12-07 20:04:42 +00003924 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003925 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003926 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003927 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003928 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003929 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003930 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003931 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003932 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003933 else
Bill Wendling832171c2006-12-07 20:04:42 +00003934 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003935 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003936 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003937 else
Bill Wendling832171c2006-12-07 20:04:42 +00003938 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003939 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003940 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003941 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3942 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003943 cerr << LBB->getName() << " ";
3944 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003945 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003946 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003947 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003948 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003949 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003950 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003951 } else if (const ExternalSymbolSDNode *ES =
3952 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003953 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003954 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3955 if (M->getValue())
Evan Cheng334dc1f2008-01-31 21:00:00 +00003956 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003957 else
Evan Cheng334dc1f2008-01-31 21:00:00 +00003958 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003959 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003960 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003961 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00003962 const Value *SrcValue = LD->getSrcValue();
3963 int SrcOffset = LD->getSrcValueOffset();
3964 cerr << " <";
3965 if (SrcValue)
3966 cerr << SrcValue;
3967 else
3968 cerr << "null";
3969 cerr << ":" << SrcOffset << ">";
3970
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003971 bool doExt = true;
3972 switch (LD->getExtensionType()) {
3973 default: doExt = false; break;
3974 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003975 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003976 break;
3977 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003978 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003979 break;
3980 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003981 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003982 break;
3983 }
3984 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003985 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003986
Evan Cheng144d8f02006-11-09 17:55:04 +00003987 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003988 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003989 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00003990 if (LD->isVolatile())
3991 cerr << " <volatile>";
3992 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00003993 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00003994 const Value *SrcValue = ST->getSrcValue();
3995 int SrcOffset = ST->getSrcValueOffset();
3996 cerr << " <";
3997 if (SrcValue)
3998 cerr << SrcValue;
3999 else
4000 cerr << "null";
4001 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004002
4003 if (ST->isTruncatingStore())
4004 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004005 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004006
4007 const char *AM = getIndexedModeName(ST->getAddressingMode());
4008 if (*AM)
4009 cerr << " " << AM;
4010 if (ST->isVolatile())
4011 cerr << " <volatile>";
4012 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004014}
4015
Chris Lattnerde202b32005-11-09 23:47:37 +00004016static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004017 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4018 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004019 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004020 else
Bill Wendling832171c2006-12-07 20:04:42 +00004021 cerr << "\n" << std::string(indent+2, ' ')
4022 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004023
Chris Lattnerea946cd2005-01-09 20:38:33 +00004024
Bill Wendling832171c2006-12-07 20:04:42 +00004025 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004026 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004027}
4028
Chris Lattnerc3aae252005-01-07 07:46:32 +00004029void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004030 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004031 std::vector<const SDNode*> Nodes;
4032 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4033 I != E; ++I)
4034 Nodes.push_back(I);
4035
Chris Lattner49d24712005-01-09 20:26:36 +00004036 std::sort(Nodes.begin(), Nodes.end());
4037
4038 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004039 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004040 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004041 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004042
Jim Laskey26f7fa72006-10-17 19:33:52 +00004043 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004044
Bill Wendling832171c2006-12-07 20:04:42 +00004045 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004046}
4047
Evan Chengd6594ae2006-09-12 21:00:35 +00004048const Type *ConstantPoolSDNode::getType() const {
4049 if (isMachineConstantPoolEntry())
4050 return Val.MachineCPVal->getType();
4051 return Val.ConstVal->getType();
4052}