blob: 28d7006ac925eaad89adc5fddf8fd7ba5a2cc668 [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"
Dan Gohmanc6c391d2008-01-31 00:25:39 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000025#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000026#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000030#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000031#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000032#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000033#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000034#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner0b3e5252006-08-15 19:11:05 +000039/// makeVTList - Return an instance of the SDVTList struct initialized with the
40/// specified members.
41static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
42 SDVTList Res = {VTs, NumVTs};
43 return Res;
44}
45
Jim Laskey58b968b2005-08-17 20:08:02 +000046//===----------------------------------------------------------------------===//
47// ConstantFPSDNode Class
48//===----------------------------------------------------------------------===//
49
50/// isExactlyValue - We don't rely on operator== working on double values, as
51/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
52/// As such, this method can be used to do an exact bit-for-bit comparison of
53/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000054bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000055 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000056}
57
Dale Johannesenf04afdb2007-08-30 00:23:21 +000058bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
59 const APFloat& Val) {
60 // convert modifies in place, so make a copy.
61 APFloat Val2 = APFloat(Val);
62 switch (VT) {
63 default:
64 return false; // These can't be represented as floating point!
65
66 // FIXME rounding mode needs to be more flexible
67 case MVT::f32:
68 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
69 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
70 APFloat::opOK;
71 case MVT::f64:
72 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
73 &Val2.getSemantics() == &APFloat::IEEEdouble ||
74 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
75 APFloat::opOK;
76 // TODO: Figure out how to test if we can use a shorter type instead!
77 case MVT::f80:
78 case MVT::f128:
79 case MVT::ppcf128:
80 return true;
81 }
82}
83
Jim Laskey58b968b2005-08-17 20:08:02 +000084//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000085// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000086//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000087
Evan Chenga8df1662006-03-27 06:58:47 +000088/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000089/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000090bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000091 // Look through a bit convert.
92 if (N->getOpcode() == ISD::BIT_CONVERT)
93 N = N->getOperand(0).Val;
94
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000096
97 unsigned i = 0, e = N->getNumOperands();
98
99 // Skip over all of the undef values.
100 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
101 ++i;
102
103 // Do not accept an all-undef vector.
104 if (i == e) return false;
105
106 // Do not accept build_vectors that aren't all constants or which have non-~0
107 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000108 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000109 if (isa<ConstantSDNode>(NotZero)) {
110 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
111 return false;
112 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000113 MVT::ValueType VT = NotZero.getValueType();
114 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000115 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
116 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000117 return false;
118 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000119 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
120 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000121 (uint32_t)-1)
122 return false;
123 }
Evan Chenga8df1662006-03-27 06:58:47 +0000124 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000125 return false;
126
127 // Okay, we have at least one ~0 value, check to see if the rest match or are
128 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000129 for (++i; i != e; ++i)
130 if (N->getOperand(i) != NotZero &&
131 N->getOperand(i).getOpcode() != ISD::UNDEF)
132 return false;
133 return true;
134}
135
136
Evan Cheng4a147842006-03-26 09:50:58 +0000137/// isBuildVectorAllZeros - Return true if the specified node is a
138/// BUILD_VECTOR where all of the elements are 0 or undef.
139bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000140 // Look through a bit convert.
141 if (N->getOpcode() == ISD::BIT_CONVERT)
142 N = N->getOperand(0).Val;
143
Evan Cheng4a147842006-03-26 09:50:58 +0000144 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000145
146 unsigned i = 0, e = N->getNumOperands();
147
148 // Skip over all of the undef values.
149 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
150 ++i;
151
152 // Do not accept an all-undef vector.
153 if (i == e) return false;
154
155 // Do not accept build_vectors that aren't all constants or which have non-~0
156 // elements.
157 SDOperand Zero = N->getOperand(i);
158 if (isa<ConstantSDNode>(Zero)) {
159 if (!cast<ConstantSDNode>(Zero)->isNullValue())
160 return false;
161 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000162 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000163 return false;
164 } else
165 return false;
166
167 // Okay, we have at least one ~0 value, check to see if the rest match or are
168 // undefs.
169 for (++i; i != e; ++i)
170 if (N->getOperand(i) != Zero &&
171 N->getOperand(i).getOpcode() != ISD::UNDEF)
172 return false;
173 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000174}
175
Evan Chengbb81d972008-01-31 09:59:15 +0000176/// isDebugLabel - Return true if the specified node represents a debug
177/// label (i.e. ISD::LABEL or TargetInstrInfo::LANEL node and third operand
178/// is 0).
179bool ISD::isDebugLabel(const SDNode *N) {
180 SDOperand Zero;
181 if (N->getOpcode() == ISD::LABEL)
182 Zero = N->getOperand(2);
183 else if (N->isTargetOpcode() &&
184 N->getTargetOpcode() == TargetInstrInfo::LABEL)
185 // Chain moved to last operand.
186 Zero = N->getOperand(1);
187 else
188 return false;
189 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
190}
191
Chris Lattnerc3aae252005-01-07 07:46:32 +0000192/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
193/// when given the operation for (X op Y).
194ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
195 // To perform this operation, we just need to swap the L and G bits of the
196 // operation.
197 unsigned OldL = (Operation >> 2) & 1;
198 unsigned OldG = (Operation >> 1) & 1;
199 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
200 (OldL << 1) | // New G bit
201 (OldG << 2)); // New L bit.
202}
203
204/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
205/// 'op' is a valid SetCC operation.
206ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
207 unsigned Operation = Op;
208 if (isInteger)
209 Operation ^= 7; // Flip L, G, E bits, but not U.
210 else
211 Operation ^= 15; // Flip all of the condition bits.
212 if (Operation > ISD::SETTRUE2)
213 Operation &= ~8; // Don't let N and U bits get set.
214 return ISD::CondCode(Operation);
215}
216
217
218/// isSignedOp - For an integer comparison, return 1 if the comparison is a
219/// signed operation and 2 if the result is an unsigned comparison. Return zero
220/// if the operation does not depend on the sign of the input (setne and seteq).
221static int isSignedOp(ISD::CondCode Opcode) {
222 switch (Opcode) {
223 default: assert(0 && "Illegal integer setcc operation!");
224 case ISD::SETEQ:
225 case ISD::SETNE: return 0;
226 case ISD::SETLT:
227 case ISD::SETLE:
228 case ISD::SETGT:
229 case ISD::SETGE: return 1;
230 case ISD::SETULT:
231 case ISD::SETULE:
232 case ISD::SETUGT:
233 case ISD::SETUGE: return 2;
234 }
235}
236
237/// getSetCCOrOperation - Return the result of a logical OR between different
238/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
239/// returns SETCC_INVALID if it is not possible to represent the resultant
240/// comparison.
241ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
242 bool isInteger) {
243 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
244 // Cannot fold a signed integer setcc with an unsigned integer setcc.
245 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000246
Chris Lattnerc3aae252005-01-07 07:46:32 +0000247 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Chris Lattnerc3aae252005-01-07 07:46:32 +0000249 // If the N and U bits get set then the resultant comparison DOES suddenly
250 // care about orderedness, and is true when ordered.
251 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000252 Op &= ~16; // Clear the U bit if the N bit is set.
253
254 // Canonicalize illegal integer setcc's.
255 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
256 Op = ISD::SETNE;
257
Chris Lattnerc3aae252005-01-07 07:46:32 +0000258 return ISD::CondCode(Op);
259}
260
261/// getSetCCAndOperation - Return the result of a logical AND between different
262/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
263/// function returns zero if it is not possible to represent the resultant
264/// comparison.
265ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
266 bool isInteger) {
267 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
268 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270
271 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000272 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger) {
276 switch (Result) {
277 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000278 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
279 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
280 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
281 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000282 }
283 }
284
285 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000286}
287
Chris Lattnerb48da392005-01-23 04:39:44 +0000288const TargetMachine &SelectionDAG::getTarget() const {
289 return TLI.getTargetMachine();
290}
291
Jim Laskey58b968b2005-08-17 20:08:02 +0000292//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000293// SDNode Profile Support
294//===----------------------------------------------------------------------===//
295
Jim Laskeydef69b92006-10-27 23:52:51 +0000296/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
297///
Jim Laskey583bd472006-10-27 23:46:08 +0000298static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
299 ID.AddInteger(OpC);
300}
301
302/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
303/// solely with their pointer.
304void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
305 ID.AddPointer(VTList.VTs);
306}
307
Jim Laskeydef69b92006-10-27 23:52:51 +0000308/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
309///
Jim Laskey583bd472006-10-27 23:46:08 +0000310static void AddNodeIDOperands(FoldingSetNodeID &ID,
311 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000312 for (; NumOps; --NumOps, ++Ops) {
313 ID.AddPointer(Ops->Val);
314 ID.AddInteger(Ops->ResNo);
315 }
Jim Laskey583bd472006-10-27 23:46:08 +0000316}
317
Jim Laskey583bd472006-10-27 23:46:08 +0000318static void AddNodeIDNode(FoldingSetNodeID &ID,
319 unsigned short OpC, SDVTList VTList,
320 const SDOperand *OpList, unsigned N) {
321 AddNodeIDOpcode(ID, OpC);
322 AddNodeIDValueTypes(ID, VTList);
323 AddNodeIDOperands(ID, OpList, N);
324}
325
Jim Laskeydef69b92006-10-27 23:52:51 +0000326/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
327/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000328static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
329 AddNodeIDOpcode(ID, N->getOpcode());
330 // Add the return value info.
331 AddNodeIDValueTypes(ID, N->getVTList());
332 // Add the operand info.
333 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
334
335 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000336 switch (N->getOpcode()) {
337 default: break; // Normal nodes don't need extra info.
338 case ISD::TargetConstant:
339 case ISD::Constant:
340 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
341 break;
342 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000343 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000344 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000345 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000346 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000347 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000348 case ISD::GlobalAddress:
349 case ISD::TargetGlobalTLSAddress:
350 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000351 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
352 ID.AddPointer(GA->getGlobal());
353 ID.AddInteger(GA->getOffset());
354 break;
355 }
356 case ISD::BasicBlock:
357 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
358 break;
359 case ISD::Register:
360 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
361 break;
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000362 case ISD::SRCVALUE:
363 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
364 break;
365 case ISD::MEMOPERAND: {
366 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
367 ID.AddPointer(MO.getValue());
368 ID.AddInteger(MO.getFlags());
369 ID.AddInteger(MO.getOffset());
370 ID.AddInteger(MO.getSize());
371 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000372 break;
373 }
374 case ISD::FrameIndex:
375 case ISD::TargetFrameIndex:
376 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
377 break;
378 case ISD::JumpTable:
379 case ISD::TargetJumpTable:
380 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
381 break;
382 case ISD::ConstantPool:
383 case ISD::TargetConstantPool: {
384 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
385 ID.AddInteger(CP->getAlignment());
386 ID.AddInteger(CP->getOffset());
387 if (CP->isMachineConstantPoolEntry())
388 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
389 else
390 ID.AddPointer(CP->getConstVal());
391 break;
392 }
393 case ISD::LOAD: {
394 LoadSDNode *LD = cast<LoadSDNode>(N);
395 ID.AddInteger(LD->getAddressingMode());
396 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000397 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000398 ID.AddInteger(LD->getAlignment());
399 ID.AddInteger(LD->isVolatile());
400 break;
401 }
402 case ISD::STORE: {
403 StoreSDNode *ST = cast<StoreSDNode>(N);
404 ID.AddInteger(ST->getAddressingMode());
405 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000406 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000407 ID.AddInteger(ST->getAlignment());
408 ID.AddInteger(ST->isVolatile());
409 break;
410 }
Jim Laskey583bd472006-10-27 23:46:08 +0000411 }
412}
413
414//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000415// SelectionDAG Class
416//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000417
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000418/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000419/// SelectionDAG.
420void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000421 // Create a dummy node (which is not added to allnodes), that adds a reference
422 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000423 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000424
Chris Lattner190a4182006-08-04 17:45:20 +0000425 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000426
Chris Lattner190a4182006-08-04 17:45:20 +0000427 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000428 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000429 if (I->use_empty())
430 DeadNodes.push_back(I);
431
432 // Process the worklist, deleting the nodes and adding their uses to the
433 // worklist.
434 while (!DeadNodes.empty()) {
435 SDNode *N = DeadNodes.back();
436 DeadNodes.pop_back();
437
438 // Take the node out of the appropriate CSE map.
439 RemoveNodeFromCSEMaps(N);
440
441 // Next, brutally remove the operand list. This is safe to do, as there are
442 // no cycles in the graph.
443 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
444 SDNode *Operand = I->Val;
445 Operand->removeUser(N);
446
447 // Now that we removed this operand, see if there are no uses of it left.
448 if (Operand->use_empty())
449 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000450 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000451 if (N->OperandsNeedDelete)
452 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000453 N->OperandList = 0;
454 N->NumOperands = 0;
455
456 // Finally, remove N itself.
457 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000458 }
459
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000460 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000461 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000462}
463
Evan Cheng130a6472006-10-12 20:34:05 +0000464void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
465 SmallVector<SDNode*, 16> DeadNodes;
466 DeadNodes.push_back(N);
467
468 // Process the worklist, deleting the nodes and adding their uses to the
469 // worklist.
470 while (!DeadNodes.empty()) {
471 SDNode *N = DeadNodes.back();
472 DeadNodes.pop_back();
473
474 // Take the node out of the appropriate CSE map.
475 RemoveNodeFromCSEMaps(N);
476
477 // Next, brutally remove the operand list. This is safe to do, as there are
478 // no cycles in the graph.
479 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
480 SDNode *Operand = I->Val;
481 Operand->removeUser(N);
482
483 // Now that we removed this operand, see if there are no uses of it left.
484 if (Operand->use_empty())
485 DeadNodes.push_back(Operand);
486 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000487 if (N->OperandsNeedDelete)
488 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000489 N->OperandList = 0;
490 N->NumOperands = 0;
491
492 // Finally, remove N itself.
493 Deleted.push_back(N);
494 AllNodes.erase(N);
495 }
496}
497
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000498void SelectionDAG::DeleteNode(SDNode *N) {
499 assert(N->use_empty() && "Cannot delete a node that is not dead!");
500
501 // First take this out of the appropriate CSE map.
502 RemoveNodeFromCSEMaps(N);
503
Chris Lattner1e111c72005-09-07 05:37:01 +0000504 // Finally, remove uses due to operands of this node, remove from the
505 // AllNodes list, and delete the node.
506 DeleteNodeNotInCSEMaps(N);
507}
508
509void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
510
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000511 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000512 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000513
514 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000515 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
516 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000517 if (N->OperandsNeedDelete)
518 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000519 N->OperandList = 0;
520 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000521
522 delete N;
523}
524
Chris Lattner149c58c2005-08-16 18:17:10 +0000525/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
526/// correspond to it. This is useful when we're about to delete or repurpose
527/// the node. We don't want future request for structurally identical nodes
528/// to return N anymore.
529void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000530 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000531 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000532 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000533 case ISD::STRING:
534 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
535 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000536 case ISD::CONDCODE:
537 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
538 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000539 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000540 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
541 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000542 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000543 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000544 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000545 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000546 Erased =
547 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000548 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000549 case ISD::VALUETYPE: {
550 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
551 if (MVT::isExtendedVT(VT)) {
552 Erased = ExtendedValueTypeNodes.erase(VT);
553 } else {
554 Erased = ValueTypeNodes[VT] != 0;
555 ValueTypeNodes[VT] = 0;
556 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000557 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000558 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000559 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000560 // Remove it from the CSE Map.
561 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000562 break;
563 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000564#ifndef NDEBUG
565 // Verify that the node was actually in one of the CSE maps, unless it has a
566 // flag result (which cannot be CSE'd) or is one of the special cases that are
567 // not subject to CSE.
568 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000569 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000570 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000571 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572 assert(0 && "Node is not in map!");
573 }
574#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575}
576
Chris Lattner8b8749f2005-08-17 19:00:20 +0000577/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
578/// has been taken out and modified in some way. If the specified node already
579/// exists in the CSE maps, do not modify the maps, but return the existing node
580/// instead. If it doesn't exist, add it and return null.
581///
582SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
583 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000584 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000585 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000586
Chris Lattner9f8cc692005-12-19 22:21:21 +0000587 // Check that remaining values produced are not flags.
588 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
589 if (N->getValueType(i) == MVT::Flag)
590 return 0; // Never CSE anything that produces a flag.
591
Chris Lattnera5682852006-08-07 23:03:03 +0000592 SDNode *New = CSEMap.GetOrInsertNode(N);
593 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000594 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000595}
596
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000597/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
598/// were replaced with those specified. If this node is never memoized,
599/// return null, otherwise return a pointer to the slot it would take. If a
600/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000601SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
602 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000603 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000604 return 0; // Never add these nodes.
605
606 // Check that remaining values produced are not flags.
607 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
608 if (N->getValueType(i) == MVT::Flag)
609 return 0; // Never CSE anything that produces a flag.
610
Chris Lattner63e3f142007-02-04 07:28:00 +0000611 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000612 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000613 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000614 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000615}
616
617/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
618/// were replaced with those specified. If this node is never memoized,
619/// return null, otherwise return a pointer to the slot it would take. If a
620/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000621SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
622 SDOperand Op1, SDOperand Op2,
623 void *&InsertPos) {
624 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
625 return 0; // Never add these nodes.
626
627 // Check that remaining values produced are not flags.
628 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
629 if (N->getValueType(i) == MVT::Flag)
630 return 0; // Never CSE anything that produces a flag.
631
Chris Lattner63e3f142007-02-04 07:28:00 +0000632 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000633 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000634 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000635 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
636}
637
638
639/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
640/// were replaced with those specified. If this node is never memoized,
641/// return null, otherwise return a pointer to the slot it would take. If a
642/// node already exists with these operands, the slot will be non-null.
643SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000644 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000645 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000646 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000647 return 0; // Never add these nodes.
648
649 // Check that remaining values produced are not flags.
650 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
651 if (N->getValueType(i) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
653
Jim Laskey583bd472006-10-27 23:46:08 +0000654 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000655 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000656
Evan Cheng9629aba2006-10-11 01:47:58 +0000657 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
658 ID.AddInteger(LD->getAddressingMode());
659 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000660 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000661 ID.AddInteger(LD->getAlignment());
662 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000663 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
664 ID.AddInteger(ST->getAddressingMode());
665 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000666 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000667 ID.AddInteger(ST->getAlignment());
668 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000669 }
Jim Laskey583bd472006-10-27 23:46:08 +0000670
Chris Lattnera5682852006-08-07 23:03:03 +0000671 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000672}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000673
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000674
Chris Lattner78ec3112003-08-11 14:57:33 +0000675SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000676 while (!AllNodes.empty()) {
677 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000678 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000679 if (N->OperandsNeedDelete)
680 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000681 N->OperandList = 0;
682 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000683 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000684 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000685}
686
Chris Lattner0f2287b2005-04-13 02:38:18 +0000687SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000688 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000689 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000690 return getNode(ISD::AND, Op.getValueType(), Op,
691 getConstant(Imm, Op.getValueType()));
692}
693
Chris Lattner36ce6912005-11-29 06:21:05 +0000694SDOperand SelectionDAG::getString(const std::string &Val) {
695 StringSDNode *&N = StringNodes[Val];
696 if (!N) {
697 N = new StringSDNode(Val);
698 AllNodes.push_back(N);
699 }
700 return SDOperand(N, 0);
701}
702
Chris Lattner61b09412006-08-11 21:01:22 +0000703SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000704 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000705
706 MVT::ValueType EltVT =
707 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000708
Chris Lattner61b09412006-08-11 21:01:22 +0000709 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000710 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000711
712 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000713 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000714 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000715 ID.AddInteger(Val);
716 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000717 SDNode *N = NULL;
718 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
719 if (!MVT::isVector(VT))
720 return SDOperand(N, 0);
721 if (!N) {
722 N = new ConstantSDNode(isT, Val, EltVT);
723 CSEMap.InsertNode(N, IP);
724 AllNodes.push_back(N);
725 }
726
727 SDOperand Result(N, 0);
728 if (MVT::isVector(VT)) {
729 SmallVector<SDOperand, 8> Ops;
730 Ops.assign(MVT::getVectorNumElements(VT), Result);
731 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
732 }
733 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000734}
735
Chris Lattner0bd48932008-01-17 07:00:52 +0000736SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
737 return getConstant(Val, TLI.getPointerTy(), isTarget);
738}
739
740
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000741SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000742 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000744
Dan Gohman7f321562007-06-25 16:23:39 +0000745 MVT::ValueType EltVT =
746 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000747
Chris Lattnerd8658612005-02-17 20:17:32 +0000748 // Do the map lookup using the actual bit pattern for the floating point
749 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
750 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000751 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000752 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000753 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000754 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000755 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000756 SDNode *N = NULL;
757 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
758 if (!MVT::isVector(VT))
759 return SDOperand(N, 0);
760 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000761 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000762 CSEMap.InsertNode(N, IP);
763 AllNodes.push_back(N);
764 }
765
Dan Gohman7f321562007-06-25 16:23:39 +0000766 SDOperand Result(N, 0);
767 if (MVT::isVector(VT)) {
768 SmallVector<SDOperand, 8> Ops;
769 Ops.assign(MVT::getVectorNumElements(VT), Result);
770 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
771 }
772 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000773}
774
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000775SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
776 bool isTarget) {
777 MVT::ValueType EltVT =
778 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
779 if (EltVT==MVT::f32)
780 return getConstantFP(APFloat((float)Val), VT, isTarget);
781 else
782 return getConstantFP(APFloat(Val), VT, isTarget);
783}
784
Chris Lattnerc3aae252005-01-07 07:46:32 +0000785SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000786 MVT::ValueType VT, int Offset,
787 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000788 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
789 unsigned Opc;
790 if (GVar && GVar->isThreadLocal())
791 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
792 else
793 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000794 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000795 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000796 ID.AddPointer(GV);
797 ID.AddInteger(Offset);
798 void *IP = 0;
799 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
800 return SDOperand(E, 0);
801 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
802 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000803 AllNodes.push_back(N);
804 return SDOperand(N, 0);
805}
806
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000807SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
808 bool isTarget) {
809 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000810 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000811 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000812 ID.AddInteger(FI);
813 void *IP = 0;
814 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
815 return SDOperand(E, 0);
816 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
817 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000818 AllNodes.push_back(N);
819 return SDOperand(N, 0);
820}
821
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000822SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
823 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000824 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000825 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000826 ID.AddInteger(JTI);
827 void *IP = 0;
828 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
829 return SDOperand(E, 0);
830 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
831 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000832 AllNodes.push_back(N);
833 return SDOperand(N, 0);
834}
835
Evan Chengb8973bd2006-01-31 22:23:14 +0000836SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000837 unsigned Alignment, int Offset,
838 bool isTarget) {
839 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000840 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000841 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000842 ID.AddInteger(Alignment);
843 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000844 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000845 void *IP = 0;
846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
847 return SDOperand(E, 0);
848 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
849 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000850 AllNodes.push_back(N);
851 return SDOperand(N, 0);
852}
853
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854
Evan Chengd6594ae2006-09-12 21:00:35 +0000855SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
856 MVT::ValueType VT,
857 unsigned Alignment, int Offset,
858 bool isTarget) {
859 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000861 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000862 ID.AddInteger(Alignment);
863 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000864 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000865 void *IP = 0;
866 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
867 return SDOperand(E, 0);
868 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
869 CSEMap.InsertNode(N, IP);
870 AllNodes.push_back(N);
871 return SDOperand(N, 0);
872}
873
874
Chris Lattnerc3aae252005-01-07 07:46:32 +0000875SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000876 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000877 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000878 ID.AddPointer(MBB);
879 void *IP = 0;
880 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
881 return SDOperand(E, 0);
882 SDNode *N = new BasicBlockSDNode(MBB);
883 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 AllNodes.push_back(N);
885 return SDOperand(N, 0);
886}
887
Chris Lattner15e4b012005-07-10 00:07:11 +0000888SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000889 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000890 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000891
Duncan Sandsf411b832007-10-17 13:49:58 +0000892 SDNode *&N = MVT::isExtendedVT(VT) ?
893 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
894
895 if (N) return SDOperand(N, 0);
896 N = new VTSDNode(VT);
897 AllNodes.push_back(N);
898 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000899}
900
Chris Lattnerc3aae252005-01-07 07:46:32 +0000901SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
902 SDNode *&N = ExternalSymbols[Sym];
903 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000904 N = new ExternalSymbolSDNode(false, Sym, VT);
905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
907}
908
Chris Lattner809ec112006-01-28 10:09:25 +0000909SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
910 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000911 SDNode *&N = TargetExternalSymbols[Sym];
912 if (N) return SDOperand(N, 0);
913 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914 AllNodes.push_back(N);
915 return SDOperand(N, 0);
916}
917
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000918SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
919 if ((unsigned)Cond >= CondCodeNodes.size())
920 CondCodeNodes.resize(Cond+1);
921
Chris Lattner079a27a2005-08-09 20:40:02 +0000922 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000923 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000924 AllNodes.push_back(CondCodeNodes[Cond]);
925 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000926 return SDOperand(CondCodeNodes[Cond], 0);
927}
928
Chris Lattner0fdd7682005-08-30 22:38:38 +0000929SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000930 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000931 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000932 ID.AddInteger(RegNo);
933 void *IP = 0;
934 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
935 return SDOperand(E, 0);
936 SDNode *N = new RegisterSDNode(RegNo, VT);
937 CSEMap.InsertNode(N, IP);
938 AllNodes.push_back(N);
939 return SDOperand(N, 0);
940}
941
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000942SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000943 assert((!V || isa<PointerType>(V->getType())) &&
944 "SrcValue is not a pointer?");
945
Jim Laskey583bd472006-10-27 23:46:08 +0000946 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000947 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000948 ID.AddPointer(V);
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000949
Chris Lattner61b09412006-08-11 21:01:22 +0000950 void *IP = 0;
951 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
952 return SDOperand(E, 0);
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000953
954 SDNode *N = new SrcValueSDNode(V);
955 CSEMap.InsertNode(N, IP);
956 AllNodes.push_back(N);
957 return SDOperand(N, 0);
958}
959
960SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
961 const Value *v = MO.getValue();
962 assert((!v || isa<PointerType>(v->getType())) &&
963 "SrcValue is not a pointer?");
964
965 FoldingSetNodeID ID;
966 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
967 ID.AddPointer(v);
968 ID.AddInteger(MO.getFlags());
969 ID.AddInteger(MO.getOffset());
970 ID.AddInteger(MO.getSize());
971 ID.AddInteger(MO.getAlignment());
972
973 void *IP = 0;
974 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
975 return SDOperand(E, 0);
976
977 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +0000978 CSEMap.InsertNode(N, IP);
979 AllNodes.push_back(N);
980 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000981}
982
Chris Lattner37ce9df2007-10-15 17:47:20 +0000983/// CreateStackTemporary - Create a stack temporary, suitable for holding the
984/// specified value type.
985SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
986 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
987 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
988 const Type *Ty = MVT::getTypeForValueType(VT);
989 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
990 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
991 return getFrameIndex(FrameIdx, TLI.getPointerTy());
992}
993
994
Chris Lattner51dabfb2006-10-14 00:41:01 +0000995SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
996 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 // These setcc operations always fold.
998 switch (Cond) {
999 default: break;
1000 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001001 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001002 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001003 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001004
1005 case ISD::SETOEQ:
1006 case ISD::SETOGT:
1007 case ISD::SETOGE:
1008 case ISD::SETOLT:
1009 case ISD::SETOLE:
1010 case ISD::SETONE:
1011 case ISD::SETO:
1012 case ISD::SETUO:
1013 case ISD::SETUEQ:
1014 case ISD::SETUNE:
1015 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1016 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001018
Chris Lattner67255a12005-04-07 18:14:58 +00001019 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1020 uint64_t C2 = N2C->getValue();
1021 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1022 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001023
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 // Sign extend the operands if required
1025 if (ISD::isSignedIntSetCC(Cond)) {
1026 C1 = N1C->getSignExtended();
1027 C2 = N2C->getSignExtended();
1028 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001029
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 switch (Cond) {
1031 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001032 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1033 case ISD::SETNE: return getConstant(C1 != C2, VT);
1034 case ISD::SETULT: return getConstant(C1 < C2, VT);
1035 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1036 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1037 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1038 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1039 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1040 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1041 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043 }
Chris Lattner67255a12005-04-07 18:14:58 +00001044 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001045 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1046 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001047 // No compile time operations on this type yet.
1048 if (N1C->getValueType(0) == MVT::ppcf128)
1049 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001050
1051 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001053 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001054 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1055 return getNode(ISD::UNDEF, VT);
1056 // fall through
1057 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1058 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1059 return getNode(ISD::UNDEF, VT);
1060 // fall through
1061 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001062 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001063 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1064 return getNode(ISD::UNDEF, VT);
1065 // fall through
1066 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1067 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1068 return getNode(ISD::UNDEF, VT);
1069 // fall through
1070 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1071 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1072 return getNode(ISD::UNDEF, VT);
1073 // fall through
1074 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001075 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001076 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1077 return getNode(ISD::UNDEF, VT);
1078 // fall through
1079 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001080 R==APFloat::cmpEqual, VT);
1081 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1082 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1083 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1084 R==APFloat::cmpEqual, VT);
1085 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1086 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1087 R==APFloat::cmpLessThan, VT);
1088 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1089 R==APFloat::cmpUnordered, VT);
1090 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1091 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 }
1093 } else {
1094 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001095 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001097
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001098 // Could not fold it.
1099 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100}
1101
Dan Gohmanea859be2007-06-22 14:59:07 +00001102/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1103/// this predicate to simplify operations downstream. Mask is known to be zero
1104/// for bits that V cannot have.
1105bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1106 unsigned Depth) const {
1107 // The masks are not wide enough to represent this type! Should use APInt.
1108 if (Op.getValueType() == MVT::i128)
1109 return false;
1110
1111 uint64_t KnownZero, KnownOne;
1112 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1113 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1114 return (KnownZero & Mask) == Mask;
1115}
1116
1117/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1118/// known to be either zero or one and return them in the KnownZero/KnownOne
1119/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1120/// processing.
1121void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1122 uint64_t &KnownZero, uint64_t &KnownOne,
1123 unsigned Depth) const {
1124 KnownZero = KnownOne = 0; // Don't know anything.
1125 if (Depth == 6 || Mask == 0)
1126 return; // Limit search depth.
1127
1128 // The masks are not wide enough to represent this type! Should use APInt.
1129 if (Op.getValueType() == MVT::i128)
1130 return;
1131
1132 uint64_t KnownZero2, KnownOne2;
1133
1134 switch (Op.getOpcode()) {
1135 case ISD::Constant:
1136 // We know all of the bits for a constant!
1137 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1138 KnownZero = ~KnownOne & Mask;
1139 return;
1140 case ISD::AND:
1141 // If either the LHS or the RHS are Zero, the result is zero.
1142 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1143 Mask &= ~KnownZero;
1144 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1145 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1146 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1147
1148 // Output known-1 bits are only known if set in both the LHS & RHS.
1149 KnownOne &= KnownOne2;
1150 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1151 KnownZero |= KnownZero2;
1152 return;
1153 case ISD::OR:
1154 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1155 Mask &= ~KnownOne;
1156 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1157 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1158 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1159
1160 // Output known-0 bits are only known if clear in both the LHS & RHS.
1161 KnownZero &= KnownZero2;
1162 // Output known-1 are known to be set if set in either the LHS | RHS.
1163 KnownOne |= KnownOne2;
1164 return;
1165 case ISD::XOR: {
1166 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1167 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1168 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1169 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1170
1171 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1172 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1173 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1174 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1175 KnownZero = KnownZeroOut;
1176 return;
1177 }
1178 case ISD::SELECT:
1179 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1180 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1181 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1182 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1183
1184 // Only known if known in both the LHS and RHS.
1185 KnownOne &= KnownOne2;
1186 KnownZero &= KnownZero2;
1187 return;
1188 case ISD::SELECT_CC:
1189 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1190 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1191 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1192 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1193
1194 // Only known if known in both the LHS and RHS.
1195 KnownOne &= KnownOne2;
1196 KnownZero &= KnownZero2;
1197 return;
1198 case ISD::SETCC:
1199 // If we know the result of a setcc has the top bits zero, use this info.
1200 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1201 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1202 return;
1203 case ISD::SHL:
1204 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1205 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1206 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1207 KnownZero, KnownOne, Depth+1);
1208 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1209 KnownZero <<= SA->getValue();
1210 KnownOne <<= SA->getValue();
1211 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1212 }
1213 return;
1214 case ISD::SRL:
1215 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1216 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1217 MVT::ValueType VT = Op.getValueType();
1218 unsigned ShAmt = SA->getValue();
1219
1220 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1221 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1222 KnownZero, KnownOne, Depth+1);
1223 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1224 KnownZero &= TypeMask;
1225 KnownOne &= TypeMask;
1226 KnownZero >>= ShAmt;
1227 KnownOne >>= ShAmt;
1228
1229 uint64_t HighBits = (1ULL << ShAmt)-1;
1230 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1231 KnownZero |= HighBits; // High bits known zero.
1232 }
1233 return;
1234 case ISD::SRA:
1235 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1236 MVT::ValueType VT = Op.getValueType();
1237 unsigned ShAmt = SA->getValue();
1238
1239 // Compute the new bits that are at the top now.
1240 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1241
1242 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1243 // If any of the demanded bits are produced by the sign extension, we also
1244 // demand the input sign bit.
1245 uint64_t HighBits = (1ULL << ShAmt)-1;
1246 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1247 if (HighBits & Mask)
1248 InDemandedMask |= MVT::getIntVTSignBit(VT);
1249
1250 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1251 Depth+1);
1252 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1253 KnownZero &= TypeMask;
1254 KnownOne &= TypeMask;
1255 KnownZero >>= ShAmt;
1256 KnownOne >>= ShAmt;
1257
1258 // Handle the sign bits.
1259 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1260 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1261
1262 if (KnownZero & SignBit) {
1263 KnownZero |= HighBits; // New bits are known zero.
1264 } else if (KnownOne & SignBit) {
1265 KnownOne |= HighBits; // New bits are known one.
1266 }
1267 }
1268 return;
1269 case ISD::SIGN_EXTEND_INREG: {
1270 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1271
1272 // Sign extension. Compute the demanded bits in the result that are not
1273 // present in the input.
1274 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1275
1276 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1277 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1278
1279 // If the sign extended bits are demanded, we know that the sign
1280 // bit is demanded.
1281 if (NewBits)
1282 InputDemandedBits |= InSignBit;
1283
1284 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1285 KnownZero, KnownOne, Depth+1);
1286 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1287
1288 // If the sign bit of the input is known set or clear, then we know the
1289 // top bits of the result.
1290 if (KnownZero & InSignBit) { // Input sign bit known clear
1291 KnownZero |= NewBits;
1292 KnownOne &= ~NewBits;
1293 } else if (KnownOne & InSignBit) { // Input sign bit known set
1294 KnownOne |= NewBits;
1295 KnownZero &= ~NewBits;
1296 } else { // Input sign bit unknown
1297 KnownZero &= ~NewBits;
1298 KnownOne &= ~NewBits;
1299 }
1300 return;
1301 }
1302 case ISD::CTTZ:
1303 case ISD::CTLZ:
1304 case ISD::CTPOP: {
1305 MVT::ValueType VT = Op.getValueType();
1306 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1307 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1308 KnownOne = 0;
1309 return;
1310 }
1311 case ISD::LOAD: {
1312 if (ISD::isZEXTLoad(Op.Val)) {
1313 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001314 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001315 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1316 }
1317 return;
1318 }
1319 case ISD::ZERO_EXTEND: {
1320 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1321 uint64_t NewBits = (~InMask) & Mask;
1322 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1323 KnownOne, Depth+1);
1324 KnownZero |= NewBits & Mask;
1325 KnownOne &= ~NewBits;
1326 return;
1327 }
1328 case ISD::SIGN_EXTEND: {
1329 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1330 unsigned InBits = MVT::getSizeInBits(InVT);
1331 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1332 uint64_t InSignBit = 1ULL << (InBits-1);
1333 uint64_t NewBits = (~InMask) & Mask;
1334 uint64_t InDemandedBits = Mask & InMask;
1335
1336 // If any of the sign extended bits are demanded, we know that the sign
1337 // bit is demanded.
1338 if (NewBits & Mask)
1339 InDemandedBits |= InSignBit;
1340
1341 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1342 KnownOne, Depth+1);
1343 // If the sign bit is known zero or one, the top bits match.
1344 if (KnownZero & InSignBit) {
1345 KnownZero |= NewBits;
1346 KnownOne &= ~NewBits;
1347 } else if (KnownOne & InSignBit) {
1348 KnownOne |= NewBits;
1349 KnownZero &= ~NewBits;
1350 } else { // Otherwise, top bits aren't known.
1351 KnownOne &= ~NewBits;
1352 KnownZero &= ~NewBits;
1353 }
1354 return;
1355 }
1356 case ISD::ANY_EXTEND: {
1357 MVT::ValueType VT = Op.getOperand(0).getValueType();
1358 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1359 KnownZero, KnownOne, Depth+1);
1360 return;
1361 }
1362 case ISD::TRUNCATE: {
1363 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1364 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1365 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1366 KnownZero &= OutMask;
1367 KnownOne &= OutMask;
1368 break;
1369 }
1370 case ISD::AssertZext: {
1371 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1372 uint64_t InMask = MVT::getIntVTBitMask(VT);
1373 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1374 KnownOne, Depth+1);
1375 KnownZero |= (~InMask) & Mask;
1376 return;
1377 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001378 case ISD::FGETSIGN:
1379 // All bits are zero except the low bit.
1380 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1381 return;
1382
Dan Gohmanea859be2007-06-22 14:59:07 +00001383 case ISD::ADD: {
1384 // If either the LHS or the RHS are Zero, the result is zero.
1385 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1386 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1387 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1388 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1389
1390 // Output known-0 bits are known if clear or set in both the low clear bits
1391 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1392 // low 3 bits clear.
1393 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1394 CountTrailingZeros_64(~KnownZero2));
1395
1396 KnownZero = (1ULL << KnownZeroOut) - 1;
1397 KnownOne = 0;
1398 return;
1399 }
1400 case ISD::SUB: {
1401 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1402 if (!CLHS) return;
1403
1404 // We know that the top bits of C-X are clear if X contains less bits
1405 // than C (i.e. no wrap-around can happen). For example, 20-X is
1406 // positive if we can prove that X is >= 0 and < 16.
1407 MVT::ValueType VT = CLHS->getValueType(0);
1408 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1409 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1410 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1411 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1412 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1413
1414 // If all of the MaskV bits are known to be zero, then we know the output
1415 // top bits are zero, because we now know that the output is from [0-C].
1416 if ((KnownZero & MaskV) == MaskV) {
1417 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1418 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1419 KnownOne = 0; // No one bits known.
1420 } else {
1421 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1422 }
1423 }
1424 return;
1425 }
1426 default:
1427 // Allow the target to implement this method for its nodes.
1428 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1429 case ISD::INTRINSIC_WO_CHAIN:
1430 case ISD::INTRINSIC_W_CHAIN:
1431 case ISD::INTRINSIC_VOID:
1432 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1433 }
1434 return;
1435 }
1436}
1437
1438/// ComputeNumSignBits - Return the number of times the sign bit of the
1439/// register is replicated into the other bits. We know that at least 1 bit
1440/// is always equal to the sign bit (itself), but other cases can give us
1441/// information. For example, immediately after an "SRA X, 2", we know that
1442/// the top 3 bits are all equal to each other, so we return 3.
1443unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1444 MVT::ValueType VT = Op.getValueType();
1445 assert(MVT::isInteger(VT) && "Invalid VT!");
1446 unsigned VTBits = MVT::getSizeInBits(VT);
1447 unsigned Tmp, Tmp2;
1448
1449 if (Depth == 6)
1450 return 1; // Limit search depth.
1451
1452 switch (Op.getOpcode()) {
1453 default: break;
1454 case ISD::AssertSext:
1455 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1456 return VTBits-Tmp+1;
1457 case ISD::AssertZext:
1458 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1459 return VTBits-Tmp;
1460
1461 case ISD::Constant: {
1462 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1463 // If negative, invert the bits, then look at it.
1464 if (Val & MVT::getIntVTSignBit(VT))
1465 Val = ~Val;
1466
1467 // Shift the bits so they are the leading bits in the int64_t.
1468 Val <<= 64-VTBits;
1469
1470 // Return # leading zeros. We use 'min' here in case Val was zero before
1471 // shifting. We don't want to return '64' as for an i32 "0".
1472 return std::min(VTBits, CountLeadingZeros_64(Val));
1473 }
1474
1475 case ISD::SIGN_EXTEND:
1476 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1477 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1478
1479 case ISD::SIGN_EXTEND_INREG:
1480 // Max of the input and what this extends.
1481 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1482 Tmp = VTBits-Tmp+1;
1483
1484 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1485 return std::max(Tmp, Tmp2);
1486
1487 case ISD::SRA:
1488 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1489 // SRA X, C -> adds C sign bits.
1490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1491 Tmp += C->getValue();
1492 if (Tmp > VTBits) Tmp = VTBits;
1493 }
1494 return Tmp;
1495 case ISD::SHL:
1496 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1497 // shl destroys sign bits.
1498 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1499 if (C->getValue() >= VTBits || // Bad shift.
1500 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1501 return Tmp - C->getValue();
1502 }
1503 break;
1504 case ISD::AND:
1505 case ISD::OR:
1506 case ISD::XOR: // NOT is handled here.
1507 // Logical binary ops preserve the number of sign bits.
1508 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1509 if (Tmp == 1) return 1; // Early out.
1510 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1511 return std::min(Tmp, Tmp2);
1512
1513 case ISD::SELECT:
1514 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1515 if (Tmp == 1) return 1; // Early out.
1516 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1517 return std::min(Tmp, Tmp2);
1518
1519 case ISD::SETCC:
1520 // If setcc returns 0/-1, all bits are sign bits.
1521 if (TLI.getSetCCResultContents() ==
1522 TargetLowering::ZeroOrNegativeOneSetCCResult)
1523 return VTBits;
1524 break;
1525 case ISD::ROTL:
1526 case ISD::ROTR:
1527 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1528 unsigned RotAmt = C->getValue() & (VTBits-1);
1529
1530 // Handle rotate right by N like a rotate left by 32-N.
1531 if (Op.getOpcode() == ISD::ROTR)
1532 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1533
1534 // If we aren't rotating out all of the known-in sign bits, return the
1535 // number that are left. This handles rotl(sext(x), 1) for example.
1536 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1537 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1538 }
1539 break;
1540 case ISD::ADD:
1541 // Add can have at most one carry bit. Thus we know that the output
1542 // is, at worst, one more bit than the inputs.
1543 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1544 if (Tmp == 1) return 1; // Early out.
1545
1546 // Special case decrementing a value (ADD X, -1):
1547 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1548 if (CRHS->isAllOnesValue()) {
1549 uint64_t KnownZero, KnownOne;
1550 uint64_t Mask = MVT::getIntVTBitMask(VT);
1551 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1552
1553 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1554 // sign bits set.
1555 if ((KnownZero|1) == Mask)
1556 return VTBits;
1557
1558 // If we are subtracting one from a positive number, there is no carry
1559 // out of the result.
1560 if (KnownZero & MVT::getIntVTSignBit(VT))
1561 return Tmp;
1562 }
1563
1564 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1565 if (Tmp2 == 1) return 1;
1566 return std::min(Tmp, Tmp2)-1;
1567 break;
1568
1569 case ISD::SUB:
1570 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1571 if (Tmp2 == 1) return 1;
1572
1573 // Handle NEG.
1574 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1575 if (CLHS->getValue() == 0) {
1576 uint64_t KnownZero, KnownOne;
1577 uint64_t Mask = MVT::getIntVTBitMask(VT);
1578 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1579 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1580 // sign bits set.
1581 if ((KnownZero|1) == Mask)
1582 return VTBits;
1583
1584 // If the input is known to be positive (the sign bit is known clear),
1585 // the output of the NEG has the same number of sign bits as the input.
1586 if (KnownZero & MVT::getIntVTSignBit(VT))
1587 return Tmp2;
1588
1589 // Otherwise, we treat this like a SUB.
1590 }
1591
1592 // Sub can have at most one carry bit. Thus we know that the output
1593 // is, at worst, one more bit than the inputs.
1594 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1595 if (Tmp == 1) return 1; // Early out.
1596 return std::min(Tmp, Tmp2)-1;
1597 break;
1598 case ISD::TRUNCATE:
1599 // FIXME: it's tricky to do anything useful for this, but it is an important
1600 // case for targets like X86.
1601 break;
1602 }
1603
1604 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1605 if (Op.getOpcode() == ISD::LOAD) {
1606 LoadSDNode *LD = cast<LoadSDNode>(Op);
1607 unsigned ExtType = LD->getExtensionType();
1608 switch (ExtType) {
1609 default: break;
1610 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001611 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001612 return VTBits-Tmp+1;
1613 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001614 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001615 return VTBits-Tmp;
1616 }
1617 }
1618
1619 // Allow the target to implement this method for its nodes.
1620 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1621 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1622 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1623 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1624 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1625 if (NumBits > 1) return NumBits;
1626 }
1627
1628 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1629 // use this information.
1630 uint64_t KnownZero, KnownOne;
1631 uint64_t Mask = MVT::getIntVTBitMask(VT);
1632 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1633
1634 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1635 if (KnownZero & SignBit) { // SignBit is 0
1636 Mask = KnownZero;
1637 } else if (KnownOne & SignBit) { // SignBit is 1;
1638 Mask = KnownOne;
1639 } else {
1640 // Nothing known.
1641 return 1;
1642 }
1643
1644 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1645 // the number of identical bits in the top of the input value.
1646 Mask ^= ~0ULL;
1647 Mask <<= 64-VTBits;
1648 // Return # leading zeros. We use 'min' here in case Val was zero before
1649 // shifting. We don't want to return '64' as for an i32 "0".
1650 return std::min(VTBits, CountLeadingZeros_64(Mask));
1651}
1652
Chris Lattner51dabfb2006-10-14 00:41:01 +00001653
Chris Lattnerc3aae252005-01-07 07:46:32 +00001654/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001655///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001656SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001657 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001658 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001659 void *IP = 0;
1660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1661 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001662 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001663 CSEMap.InsertNode(N, IP);
1664
1665 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001666 return SDOperand(N, 0);
1667}
1668
Chris Lattnerc3aae252005-01-07 07:46:32 +00001669SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1670 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001671 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001672 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001673 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1674 uint64_t Val = C->getValue();
1675 switch (Opcode) {
1676 default: break;
1677 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001678 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001679 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1680 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001681 case ISD::UINT_TO_FP:
1682 case ISD::SINT_TO_FP: {
1683 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001684 // No compile time operations on this type.
1685 if (VT==MVT::ppcf128)
1686 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001687 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001688 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001689 MVT::getSizeInBits(Operand.getValueType()),
1690 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001691 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001692 return getConstantFP(apf, VT);
1693 }
Chris Lattner94683772005-12-23 05:30:37 +00001694 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001695 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001696 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001697 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001698 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001699 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001700 case ISD::BSWAP:
1701 switch(VT) {
1702 default: assert(0 && "Invalid bswap!"); break;
1703 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1704 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1705 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1706 }
1707 break;
1708 case ISD::CTPOP:
1709 switch(VT) {
1710 default: assert(0 && "Invalid ctpop!"); break;
1711 case MVT::i1: return getConstant(Val != 0, VT);
1712 case MVT::i8:
1713 Tmp1 = (unsigned)Val & 0xFF;
1714 return getConstant(CountPopulation_32(Tmp1), VT);
1715 case MVT::i16:
1716 Tmp1 = (unsigned)Val & 0xFFFF;
1717 return getConstant(CountPopulation_32(Tmp1), VT);
1718 case MVT::i32:
1719 return getConstant(CountPopulation_32((unsigned)Val), VT);
1720 case MVT::i64:
1721 return getConstant(CountPopulation_64(Val), VT);
1722 }
1723 case ISD::CTLZ:
1724 switch(VT) {
1725 default: assert(0 && "Invalid ctlz!"); break;
1726 case MVT::i1: return getConstant(Val == 0, VT);
1727 case MVT::i8:
1728 Tmp1 = (unsigned)Val & 0xFF;
1729 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1730 case MVT::i16:
1731 Tmp1 = (unsigned)Val & 0xFFFF;
1732 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1733 case MVT::i32:
1734 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1735 case MVT::i64:
1736 return getConstant(CountLeadingZeros_64(Val), VT);
1737 }
1738 case ISD::CTTZ:
1739 switch(VT) {
1740 default: assert(0 && "Invalid cttz!"); break;
1741 case MVT::i1: return getConstant(Val == 0, VT);
1742 case MVT::i8:
1743 Tmp1 = (unsigned)Val | 0x100;
1744 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1745 case MVT::i16:
1746 Tmp1 = (unsigned)Val | 0x10000;
1747 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1748 case MVT::i32:
1749 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1750 case MVT::i64:
1751 return getConstant(CountTrailingZeros_64(Val), VT);
1752 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001753 }
1754 }
1755
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001756 // Constant fold unary operations with a floating point constant operand.
1757 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1758 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001759 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001760 switch (Opcode) {
1761 case ISD::FNEG:
1762 V.changeSign();
1763 return getConstantFP(V, VT);
1764 case ISD::FABS:
1765 V.clearSign();
1766 return getConstantFP(V, VT);
1767 case ISD::FP_ROUND:
1768 case ISD::FP_EXTEND:
1769 // This can return overflow, underflow, or inexact; we don't care.
1770 // FIXME need to be more flexible about rounding mode.
1771 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1772 VT==MVT::f64 ? APFloat::IEEEdouble :
1773 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1774 VT==MVT::f128 ? APFloat::IEEEquad :
1775 APFloat::Bogus,
1776 APFloat::rmNearestTiesToEven);
1777 return getConstantFP(V, VT);
1778 case ISD::FP_TO_SINT:
1779 case ISD::FP_TO_UINT: {
1780 integerPart x;
1781 assert(integerPartWidth >= 64);
1782 // FIXME need to be more flexible about rounding mode.
1783 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1784 Opcode==ISD::FP_TO_SINT,
1785 APFloat::rmTowardZero);
1786 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1787 break;
1788 return getConstant(x, VT);
1789 }
1790 case ISD::BIT_CONVERT:
1791 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1792 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1793 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1794 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001795 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001796 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001797 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001798 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799
1800 unsigned OpOpcode = Operand.Val->getOpcode();
1801 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001802 case ISD::TokenFactor:
1803 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001804 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001805 case ISD::FP_EXTEND:
1806 assert(MVT::isFloatingPoint(VT) &&
1807 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001808 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001809 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00001810 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001811 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1812 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001813 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001814 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1815 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001816 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1817 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1818 break;
1819 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001820 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1821 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001822 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001823 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1824 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001825 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001826 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001827 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001828 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001829 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1830 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001831 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001832 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1833 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001834 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1835 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1836 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1837 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001838 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001839 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1840 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001841 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001842 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1843 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001844 if (OpOpcode == ISD::TRUNCATE)
1845 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001846 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1847 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001848 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001849 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1850 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001851 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001852 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1853 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001854 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1855 else
1856 return Operand.Val->getOperand(0);
1857 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001858 break;
Chris Lattner35481892005-12-23 00:16:34 +00001859 case ISD::BIT_CONVERT:
1860 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001861 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001862 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001863 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001864 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1865 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001866 if (OpOpcode == ISD::UNDEF)
1867 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001868 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001869 case ISD::SCALAR_TO_VECTOR:
1870 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001871 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001872 "Illegal SCALAR_TO_VECTOR node!");
1873 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001874 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001875 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1876 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001877 Operand.Val->getOperand(0));
1878 if (OpOpcode == ISD::FNEG) // --X -> X
1879 return Operand.Val->getOperand(0);
1880 break;
1881 case ISD::FABS:
1882 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1883 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1884 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001885 }
1886
Chris Lattner43247a12005-08-25 19:12:10 +00001887 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001888 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001889 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001890 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001891 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001892 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001893 void *IP = 0;
1894 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1895 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001896 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001897 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001898 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001899 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001900 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001901 AllNodes.push_back(N);
1902 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001903}
1904
Chris Lattner36019aa2005-04-18 03:48:41 +00001905
1906
Chris Lattnerc3aae252005-01-07 07:46:32 +00001907SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1908 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001909 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1910 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001911 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001912 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001913 case ISD::TokenFactor:
1914 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1915 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001916 // Fold trivial token factors.
1917 if (N1.getOpcode() == ISD::EntryToken) return N2;
1918 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001919 break;
Chris Lattner76365122005-01-16 02:23:22 +00001920 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001921 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1922 N1.getValueType() == VT && "Binary operator types must match!");
1923 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1924 // worth handling here.
1925 if (N2C && N2C->getValue() == 0)
1926 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001927 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1928 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001929 break;
Chris Lattner76365122005-01-16 02:23:22 +00001930 case ISD::OR:
1931 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001932 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1933 N1.getValueType() == VT && "Binary operator types must match!");
1934 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1935 // worth handling here.
1936 if (N2C && N2C->getValue() == 0)
1937 return N1;
1938 break;
Chris Lattner76365122005-01-16 02:23:22 +00001939 case ISD::UDIV:
1940 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001941 case ISD::MULHU:
1942 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001943 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1944 // fall through
1945 case ISD::ADD:
1946 case ISD::SUB:
1947 case ISD::MUL:
1948 case ISD::SDIV:
1949 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001950 case ISD::FADD:
1951 case ISD::FSUB:
1952 case ISD::FMUL:
1953 case ISD::FDIV:
1954 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001955 assert(N1.getValueType() == N2.getValueType() &&
1956 N1.getValueType() == VT && "Binary operator types must match!");
1957 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001958 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1959 assert(N1.getValueType() == VT &&
1960 MVT::isFloatingPoint(N1.getValueType()) &&
1961 MVT::isFloatingPoint(N2.getValueType()) &&
1962 "Invalid FCOPYSIGN!");
1963 break;
Chris Lattner76365122005-01-16 02:23:22 +00001964 case ISD::SHL:
1965 case ISD::SRA:
1966 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001967 case ISD::ROTL:
1968 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001969 assert(VT == N1.getValueType() &&
1970 "Shift operators return type must be the same as their first arg");
1971 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001972 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001973 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001974 case ISD::FP_ROUND_INREG: {
1975 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1976 assert(VT == N1.getValueType() && "Not an inreg round!");
1977 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1978 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001979 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1980 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001981 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00001982 break;
1983 }
Chris Lattner0bd48932008-01-17 07:00:52 +00001984 case ISD::FP_ROUND:
1985 assert(MVT::isFloatingPoint(VT) &&
1986 MVT::isFloatingPoint(N1.getValueType()) &&
1987 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1988 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001989 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00001990 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00001991 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001992 case ISD::AssertZext: {
1993 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1994 assert(VT == N1.getValueType() && "Not an inreg extend!");
1995 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1996 "Cannot *_EXTEND_INREG FP types");
1997 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1998 "Not extending!");
1999 break;
2000 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002001 case ISD::SIGN_EXTEND_INREG: {
2002 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2003 assert(VT == N1.getValueType() && "Not an inreg extend!");
2004 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2005 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002006 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2007 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002008 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002009
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002010 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002011 int64_t Val = N1C->getValue();
2012 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
2013 Val <<= 64-FromBits;
2014 Val >>= 64-FromBits;
2015 return getConstant(Val, VT);
2016 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002017 break;
2018 }
2019 case ISD::EXTRACT_VECTOR_ELT:
2020 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2021
2022 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2023 // expanding copies of large vectors from registers.
2024 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2025 N1.getNumOperands() > 0) {
2026 unsigned Factor =
2027 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2028 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2029 N1.getOperand(N2C->getValue() / Factor),
2030 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2031 }
2032
2033 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2034 // expanding large vector constants.
2035 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2036 return N1.getOperand(N2C->getValue());
2037
2038 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2039 // operations are lowered to scalars.
2040 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2041 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2042 if (IEC == N2C)
2043 return N1.getOperand(1);
2044 else
2045 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2046 }
2047 break;
2048 case ISD::EXTRACT_ELEMENT:
2049 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002050
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002051 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2052 // 64-bit integers into 32-bit parts. Instead of building the extract of
2053 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2054 if (N1.getOpcode() == ISD::BUILD_PAIR)
2055 return N1.getOperand(N2C->getValue());
2056
2057 // EXTRACT_ELEMENT of a constant int is also very common.
2058 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2059 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2060 return getConstant(C->getValue() >> Shift, VT);
2061 }
2062 break;
2063 }
2064
2065 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002066 if (N2C) {
2067 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2068 switch (Opcode) {
2069 case ISD::ADD: return getConstant(C1 + C2, VT);
2070 case ISD::SUB: return getConstant(C1 - C2, VT);
2071 case ISD::MUL: return getConstant(C1 * C2, VT);
2072 case ISD::UDIV:
2073 if (C2) return getConstant(C1 / C2, VT);
2074 break;
2075 case ISD::UREM :
2076 if (C2) return getConstant(C1 % C2, VT);
2077 break;
2078 case ISD::SDIV :
2079 if (C2) return getConstant(N1C->getSignExtended() /
2080 N2C->getSignExtended(), VT);
2081 break;
2082 case ISD::SREM :
2083 if (C2) return getConstant(N1C->getSignExtended() %
2084 N2C->getSignExtended(), VT);
2085 break;
2086 case ISD::AND : return getConstant(C1 & C2, VT);
2087 case ISD::OR : return getConstant(C1 | C2, VT);
2088 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002089 case ISD::SHL : return getConstant(C1 << C2, VT);
2090 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00002091 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00002092 case ISD::ROTL :
2093 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2094 VT);
2095 case ISD::ROTR :
2096 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2097 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002098 default: break;
2099 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002100 } else { // Cannonicalize constant to RHS if commutative
2101 if (isCommutativeBinOp(Opcode)) {
2102 std::swap(N1C, N2C);
2103 std::swap(N1, N2);
2104 }
2105 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002106 }
2107
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002108 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2110 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002111 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002112 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2113 // Cannonicalize constant to RHS if commutative
2114 std::swap(N1CFP, N2CFP);
2115 std::swap(N1, N2);
2116 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002117 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2118 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002120 case ISD::FADD:
2121 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002122 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002123 return getConstantFP(V1, VT);
2124 break;
2125 case ISD::FSUB:
2126 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2127 if (s!=APFloat::opInvalidOp)
2128 return getConstantFP(V1, VT);
2129 break;
2130 case ISD::FMUL:
2131 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2132 if (s!=APFloat::opInvalidOp)
2133 return getConstantFP(V1, VT);
2134 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002135 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002136 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2137 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2138 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002139 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002140 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002141 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2142 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2143 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002144 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002145 case ISD::FCOPYSIGN:
2146 V1.copySign(V2);
2147 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002148 default: break;
2149 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002150 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002151 }
Chris Lattner62b57722006-04-20 05:39:12 +00002152
2153 // Canonicalize an UNDEF to the RHS, even over a constant.
2154 if (N1.getOpcode() == ISD::UNDEF) {
2155 if (isCommutativeBinOp(Opcode)) {
2156 std::swap(N1, N2);
2157 } else {
2158 switch (Opcode) {
2159 case ISD::FP_ROUND_INREG:
2160 case ISD::SIGN_EXTEND_INREG:
2161 case ISD::SUB:
2162 case ISD::FSUB:
2163 case ISD::FDIV:
2164 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002165 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002166 return N1; // fold op(undef, arg2) -> undef
2167 case ISD::UDIV:
2168 case ISD::SDIV:
2169 case ISD::UREM:
2170 case ISD::SREM:
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(undef, arg2) -> 0
2175 // For vectors, we can't easily build an all zero vector, just return
2176 // the LHS.
2177 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002178 }
2179 }
2180 }
2181
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002182 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002183 if (N2.getOpcode() == ISD::UNDEF) {
2184 switch (Opcode) {
2185 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002186 case ISD::ADDC:
2187 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002188 case ISD::SUB:
2189 case ISD::FADD:
2190 case ISD::FSUB:
2191 case ISD::FMUL:
2192 case ISD::FDIV:
2193 case ISD::FREM:
2194 case ISD::UDIV:
2195 case ISD::SDIV:
2196 case ISD::UREM:
2197 case ISD::SREM:
2198 case ISD::XOR:
2199 return N2; // fold op(arg1, undef) -> undef
2200 case ISD::MUL:
2201 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002202 case ISD::SRL:
2203 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002204 if (!MVT::isVector(VT))
2205 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2206 // For vectors, we can't easily build an all zero vector, just return
2207 // the LHS.
2208 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002209 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002210 if (!MVT::isVector(VT))
2211 return getConstant(MVT::getIntVTBitMask(VT), VT);
2212 // For vectors, we can't easily build an all one vector, just return
2213 // the LHS.
2214 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002215 case ISD::SRA:
2216 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002217 }
2218 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002219
Chris Lattner27e9b412005-05-11 18:57:39 +00002220 // Memoize this node if possible.
2221 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002222 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002223 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002224 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002225 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002226 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002227 void *IP = 0;
2228 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2229 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002230 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002231 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002232 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002233 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002234 }
2235
Chris Lattnerc3aae252005-01-07 07:46:32 +00002236 AllNodes.push_back(N);
2237 return SDOperand(N, 0);
2238}
2239
Chris Lattnerc3aae252005-01-07 07:46:32 +00002240SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2241 SDOperand N1, SDOperand N2, SDOperand N3) {
2242 // Perform various simplifications.
2243 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2244 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002245 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002246 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002247 // Use FoldSetCC to simplify SETCC's.
2248 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002249 if (Simp.Val) return Simp;
2250 break;
2251 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002252 case ISD::SELECT:
2253 if (N1C)
2254 if (N1C->getValue())
2255 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002256 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002257 return N3; // select false, X, Y -> Y
2258
2259 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002260 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002261 case ISD::BRCOND:
2262 if (N2C)
2263 if (N2C->getValue()) // Unconditional branch
2264 return getNode(ISD::BR, MVT::Other, N1, N3);
2265 else
2266 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002267 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002268 case ISD::VECTOR_SHUFFLE:
2269 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2270 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2271 N3.getOpcode() == ISD::BUILD_VECTOR &&
2272 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2273 "Illegal VECTOR_SHUFFLE node!");
2274 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002275 case ISD::BIT_CONVERT:
2276 // Fold bit_convert nodes from a type to themselves.
2277 if (N1.getValueType() == VT)
2278 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002279 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002280 }
2281
Chris Lattner43247a12005-08-25 19:12:10 +00002282 // Memoize node if it doesn't produce a flag.
2283 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002284 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002285 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002286 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002287 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002288 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002289 void *IP = 0;
2290 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2291 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002292 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002293 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002294 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002295 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002296 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002297 AllNodes.push_back(N);
2298 return SDOperand(N, 0);
2299}
2300
2301SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002302 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002303 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002304 SDOperand Ops[] = { N1, N2, N3, N4 };
2305 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002306}
2307
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002308SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2309 SDOperand N1, SDOperand N2, SDOperand N3,
2310 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002311 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2312 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002313}
2314
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002315SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2316 SDOperand Src, SDOperand Size,
2317 SDOperand Align,
2318 SDOperand AlwaysInline) {
2319 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2320 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2321}
2322
2323SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2324 SDOperand Src, SDOperand Size,
2325 SDOperand Align,
2326 SDOperand AlwaysInline) {
2327 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2328 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2329}
2330
2331SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2332 SDOperand Src, SDOperand Size,
2333 SDOperand Align,
2334 SDOperand AlwaysInline) {
2335 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2336 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2337}
2338
Evan Cheng7038daf2005-12-10 00:37:58 +00002339SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2340 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002341 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002342 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002343 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2344 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002345 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002346 Ty = MVT::getTypeForValueType(VT);
2347 } else if (SV) {
2348 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2349 assert(PT && "Value for load must be a pointer");
2350 Ty = PT->getElementType();
2351 }
2352 assert(Ty && "Could not get type information for load");
2353 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2354 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002355 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002356 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002357 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002358 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002359 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002360 ID.AddInteger(ISD::UNINDEXED);
2361 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002362 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002363 ID.AddInteger(Alignment);
2364 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002365 void *IP = 0;
2366 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2367 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002368 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002369 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2370 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002371 CSEMap.InsertNode(N, IP);
2372 AllNodes.push_back(N);
2373 return SDOperand(N, 0);
2374}
2375
2376SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002377 SDOperand Chain, SDOperand Ptr,
2378 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002379 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002380 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002381 // If they are asking for an extending load from/to the same thing, return a
2382 // normal load.
2383 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002384 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002385
2386 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002387 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002388 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002389 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2390 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002391 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2392 "Cannot sign/zero extend a FP/Vector load!");
2393 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2394 "Cannot convert from FP to Int or Int -> FP!");
2395
Dan Gohman575e2f42007-06-04 15:49:41 +00002396 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2397 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002398 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002399 Ty = MVT::getTypeForValueType(VT);
2400 } else if (SV) {
2401 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2402 assert(PT && "Value for load must be a pointer");
2403 Ty = PT->getElementType();
2404 }
2405 assert(Ty && "Could not get type information for load");
2406 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2407 }
Evan Cheng466685d2006-10-09 20:57:25 +00002408 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002409 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002410 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002411 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002412 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002413 ID.AddInteger(ISD::UNINDEXED);
2414 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002415 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002416 ID.AddInteger(Alignment);
2417 ID.AddInteger(isVolatile);
2418 void *IP = 0;
2419 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2420 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002421 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002423 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002424 AllNodes.push_back(N);
2425 return SDOperand(N, 0);
2426}
2427
Evan Cheng144d8f02006-11-09 17:55:04 +00002428SDOperand
2429SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2430 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002431 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002432 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2433 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002434 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002435 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002436 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002437 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002438 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002439 ID.AddInteger(AM);
2440 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002441 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002442 ID.AddInteger(LD->getAlignment());
2443 ID.AddInteger(LD->isVolatile());
2444 void *IP = 0;
2445 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2446 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002447 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002448 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002449 LD->getSrcValue(), LD->getSrcValueOffset(),
2450 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002451 CSEMap.InsertNode(N, IP);
2452 AllNodes.push_back(N);
2453 return SDOperand(N, 0);
2454}
2455
Jeff Cohend41b30d2006-11-05 19:31:28 +00002456SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002457 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002458 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002459 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002460
Dan Gohman575e2f42007-06-04 15:49:41 +00002461 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2462 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002463 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002464 Ty = MVT::getTypeForValueType(VT);
2465 } else if (SV) {
2466 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2467 assert(PT && "Value for store must be a pointer");
2468 Ty = PT->getElementType();
2469 }
2470 assert(Ty && "Could not get type information for store");
2471 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2472 }
Evan Chengad071e12006-10-05 22:57:11 +00002473 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002474 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002475 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002476 FoldingSetNodeID ID;
2477 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002478 ID.AddInteger(ISD::UNINDEXED);
2479 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002480 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002481 ID.AddInteger(Alignment);
2482 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002483 void *IP = 0;
2484 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2485 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002486 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002487 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002488 CSEMap.InsertNode(N, IP);
2489 AllNodes.push_back(N);
2490 return SDOperand(N, 0);
2491}
2492
Jeff Cohend41b30d2006-11-05 19:31:28 +00002493SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002494 SDOperand Ptr, const Value *SV,
2495 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002496 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002497 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002498
2499 if (VT == SVT)
2500 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002501
Duncan Sandsaf47b112007-10-16 09:56:48 +00002502 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2503 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002504 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2505 "Can't do FP-INT conversion!");
2506
Dan Gohman575e2f42007-06-04 15:49:41 +00002507 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2508 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002509 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002510 Ty = MVT::getTypeForValueType(VT);
2511 } else if (SV) {
2512 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2513 assert(PT && "Value for store must be a pointer");
2514 Ty = PT->getElementType();
2515 }
2516 assert(Ty && "Could not get type information for store");
2517 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2518 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002519 SDVTList VTs = getVTList(MVT::Other);
2520 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002521 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002522 FoldingSetNodeID ID;
2523 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002524 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002525 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002526 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002527 ID.AddInteger(Alignment);
2528 ID.AddInteger(isVolatile);
2529 void *IP = 0;
2530 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2531 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002532 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002533 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002534 CSEMap.InsertNode(N, IP);
2535 AllNodes.push_back(N);
2536 return SDOperand(N, 0);
2537}
2538
Evan Cheng144d8f02006-11-09 17:55:04 +00002539SDOperand
2540SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2541 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002542 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2543 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2544 "Store is already a indexed store!");
2545 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2546 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2547 FoldingSetNodeID ID;
2548 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2549 ID.AddInteger(AM);
2550 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002551 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002552 ID.AddInteger(ST->getAlignment());
2553 ID.AddInteger(ST->isVolatile());
2554 void *IP = 0;
2555 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2556 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002557 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002558 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002559 ST->getSrcValue(), ST->getSrcValueOffset(),
2560 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002561 CSEMap.InsertNode(N, IP);
2562 AllNodes.push_back(N);
2563 return SDOperand(N, 0);
2564}
2565
Nate Begemanacc398c2006-01-25 18:21:52 +00002566SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2567 SDOperand Chain, SDOperand Ptr,
2568 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002569 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002570 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002571}
2572
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002573SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002574 const SDOperand *Ops, unsigned NumOps) {
2575 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002576 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002577 case 1: return getNode(Opcode, VT, Ops[0]);
2578 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2579 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002580 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002581 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002582
Chris Lattneref847df2005-04-09 03:27:28 +00002583 switch (Opcode) {
2584 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002585 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002586 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002587 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2588 "LHS and RHS of condition must have same type!");
2589 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2590 "True and False arms of SelectCC must have same type!");
2591 assert(Ops[2].getValueType() == VT &&
2592 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002593 break;
2594 }
2595 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002596 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002597 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2598 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002599 break;
2600 }
Chris Lattneref847df2005-04-09 03:27:28 +00002601 }
2602
Chris Lattner385328c2005-05-14 07:42:29 +00002603 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002604 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002605 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002606 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002607 FoldingSetNodeID ID;
2608 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002609 void *IP = 0;
2610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2611 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002612 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002613 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002614 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002615 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002616 }
Chris Lattneref847df2005-04-09 03:27:28 +00002617 AllNodes.push_back(N);
2618 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002619}
2620
Chris Lattner89c34632005-05-14 06:20:26 +00002621SDOperand SelectionDAG::getNode(unsigned Opcode,
2622 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002623 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002624 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2625 Ops, NumOps);
2626}
2627
2628SDOperand SelectionDAG::getNode(unsigned Opcode,
2629 const MVT::ValueType *VTs, unsigned NumVTs,
2630 const SDOperand *Ops, unsigned NumOps) {
2631 if (NumVTs == 1)
2632 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002633 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2634}
2635
2636SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2637 const SDOperand *Ops, unsigned NumOps) {
2638 if (VTList.NumVTs == 1)
2639 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002640
Chris Lattner5f056bf2005-07-10 01:55:33 +00002641 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002642 // FIXME: figure out how to safely handle things like
2643 // int foo(int x) { return 1 << (x & 255); }
2644 // int bar() { return foo(256); }
2645#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002646 case ISD::SRA_PARTS:
2647 case ISD::SRL_PARTS:
2648 case ISD::SHL_PARTS:
2649 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002650 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002651 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2652 else if (N3.getOpcode() == ISD::AND)
2653 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2654 // If the and is only masking out bits that cannot effect the shift,
2655 // eliminate the and.
2656 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2657 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2658 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2659 }
2660 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002661#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002662 }
Chris Lattner89c34632005-05-14 06:20:26 +00002663
Chris Lattner43247a12005-08-25 19:12:10 +00002664 // Memoize the node unless it returns a flag.
2665 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002666 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002667 FoldingSetNodeID ID;
2668 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002669 void *IP = 0;
2670 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2671 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002672 if (NumOps == 1)
2673 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2674 else if (NumOps == 2)
2675 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2676 else if (NumOps == 3)
2677 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2678 else
2679 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002680 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002681 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002682 if (NumOps == 1)
2683 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2684 else if (NumOps == 2)
2685 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2686 else if (NumOps == 3)
2687 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2688 else
2689 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002690 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002691 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002692 return SDOperand(N, 0);
2693}
2694
Dan Gohman08ce9762007-10-08 15:49:58 +00002695SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2696 return getNode(Opcode, VTList, 0, 0);
2697}
2698
2699SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2700 SDOperand N1) {
2701 SDOperand Ops[] = { N1 };
2702 return getNode(Opcode, VTList, Ops, 1);
2703}
2704
2705SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2706 SDOperand N1, SDOperand N2) {
2707 SDOperand Ops[] = { N1, N2 };
2708 return getNode(Opcode, VTList, Ops, 2);
2709}
2710
2711SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2712 SDOperand N1, SDOperand N2, SDOperand N3) {
2713 SDOperand Ops[] = { N1, N2, N3 };
2714 return getNode(Opcode, VTList, Ops, 3);
2715}
2716
2717SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2718 SDOperand N1, SDOperand N2, SDOperand N3,
2719 SDOperand N4) {
2720 SDOperand Ops[] = { N1, N2, N3, N4 };
2721 return getNode(Opcode, VTList, Ops, 4);
2722}
2723
2724SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2725 SDOperand N1, SDOperand N2, SDOperand N3,
2726 SDOperand N4, SDOperand N5) {
2727 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2728 return getNode(Opcode, VTList, Ops, 5);
2729}
2730
Chris Lattner70046e92006-08-15 17:46:01 +00002731SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002732 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002733}
2734
Chris Lattner70046e92006-08-15 17:46:01 +00002735SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002736 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2737 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002738 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002739 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002740 }
2741 std::vector<MVT::ValueType> V;
2742 V.push_back(VT1);
2743 V.push_back(VT2);
2744 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002745 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002746}
Chris Lattner70046e92006-08-15 17:46:01 +00002747SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2748 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002749 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002750 E = VTList.end(); I != E; ++I) {
2751 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2752 (*I)[2] == VT3)
2753 return makeVTList(&(*I)[0], 3);
2754 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002755 std::vector<MVT::ValueType> V;
2756 V.push_back(VT1);
2757 V.push_back(VT2);
2758 V.push_back(VT3);
2759 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002760 return makeVTList(&(*VTList.begin())[0], 3);
2761}
2762
2763SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2764 switch (NumVTs) {
2765 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002766 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002767 case 2: return getVTList(VTs[0], VTs[1]);
2768 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2769 default: break;
2770 }
2771
2772 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2773 E = VTList.end(); I != E; ++I) {
2774 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2775
2776 bool NoMatch = false;
2777 for (unsigned i = 2; i != NumVTs; ++i)
2778 if (VTs[i] != (*I)[i]) {
2779 NoMatch = true;
2780 break;
2781 }
2782 if (!NoMatch)
2783 return makeVTList(&*I->begin(), NumVTs);
2784 }
2785
2786 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2787 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002788}
2789
2790
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002791/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2792/// specified operands. If the resultant node already exists in the DAG,
2793/// this does not modify the specified node, instead it returns the node that
2794/// already exists. If the resultant node does not exist in the DAG, the
2795/// input node is returned. As a degenerate case, if you specify the same
2796/// input operands as the node already has, the input node is returned.
2797SDOperand SelectionDAG::
2798UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2799 SDNode *N = InN.Val;
2800 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2801
2802 // Check to see if there is no change.
2803 if (Op == N->getOperand(0)) return InN;
2804
2805 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002806 void *InsertPos = 0;
2807 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2808 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002809
2810 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002811 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002812 RemoveNodeFromCSEMaps(N);
2813
2814 // Now we update the operands.
2815 N->OperandList[0].Val->removeUser(N);
2816 Op.Val->addUser(N);
2817 N->OperandList[0] = Op;
2818
2819 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002820 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002821 return InN;
2822}
2823
2824SDOperand SelectionDAG::
2825UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2826 SDNode *N = InN.Val;
2827 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2828
2829 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002830 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2831 return InN; // No operands changed, just return the input node.
2832
2833 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002834 void *InsertPos = 0;
2835 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2836 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002837
2838 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002839 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002840 RemoveNodeFromCSEMaps(N);
2841
2842 // Now we update the operands.
2843 if (N->OperandList[0] != Op1) {
2844 N->OperandList[0].Val->removeUser(N);
2845 Op1.Val->addUser(N);
2846 N->OperandList[0] = Op1;
2847 }
2848 if (N->OperandList[1] != Op2) {
2849 N->OperandList[1].Val->removeUser(N);
2850 Op2.Val->addUser(N);
2851 N->OperandList[1] = Op2;
2852 }
2853
2854 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002855 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002856 return InN;
2857}
2858
2859SDOperand SelectionDAG::
2860UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002861 SDOperand Ops[] = { Op1, Op2, Op3 };
2862 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002863}
2864
2865SDOperand SelectionDAG::
2866UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2867 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002868 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2869 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002870}
2871
2872SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002873UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2874 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002875 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2876 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002877}
2878
2879
2880SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002881UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002882 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002883 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002884 "Update with wrong number of operands");
2885
2886 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002887 bool AnyChange = false;
2888 for (unsigned i = 0; i != NumOps; ++i) {
2889 if (Ops[i] != N->getOperand(i)) {
2890 AnyChange = true;
2891 break;
2892 }
2893 }
2894
2895 // No operands changed, just return the input node.
2896 if (!AnyChange) return InN;
2897
2898 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002899 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002900 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002901 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002902
2903 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002904 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002905 RemoveNodeFromCSEMaps(N);
2906
2907 // Now we update the operands.
2908 for (unsigned i = 0; i != NumOps; ++i) {
2909 if (N->OperandList[i] != Ops[i]) {
2910 N->OperandList[i].Val->removeUser(N);
2911 Ops[i].Val->addUser(N);
2912 N->OperandList[i] = Ops[i];
2913 }
2914 }
2915
2916 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002917 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002918 return InN;
2919}
2920
2921
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002922/// MorphNodeTo - This frees the operands of the current node, resets the
2923/// opcode, types, and operands to the specified value. This should only be
2924/// used by the SelectionDAG class.
2925void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2926 const SDOperand *Ops, unsigned NumOps) {
2927 NodeType = Opc;
2928 ValueList = L.VTs;
2929 NumValues = L.NumVTs;
2930
2931 // Clear the operands list, updating used nodes to remove this from their
2932 // use list.
2933 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2934 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002935
2936 // If NumOps is larger than the # of operands we currently have, reallocate
2937 // the operand list.
2938 if (NumOps > NumOperands) {
2939 if (OperandsNeedDelete)
2940 delete [] OperandList;
2941 OperandList = new SDOperand[NumOps];
2942 OperandsNeedDelete = true;
2943 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002944
2945 // Assign the new operands.
2946 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002947
2948 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2949 OperandList[i] = Ops[i];
2950 SDNode *N = OperandList[i].Val;
2951 N->Uses.push_back(this);
2952 }
2953}
Chris Lattner149c58c2005-08-16 18:17:10 +00002954
2955/// SelectNodeTo - These are used for target selectors to *mutate* the
2956/// specified node to have the specified return type, Target opcode, and
2957/// operands. Note that target opcodes are stored as
2958/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002959///
2960/// Note that SelectNodeTo returns the resultant node. If there is already a
2961/// node of the specified opcode and operands, it returns that node instead of
2962/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002963SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2964 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002965 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002966 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002967 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002968 void *IP = 0;
2969 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002970 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002971
Chris Lattner7651fa42005-08-24 23:00:29 +00002972 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002973
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002974 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002975
Chris Lattner4a283e92006-08-11 18:38:11 +00002976 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002977 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002978}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002979
Evan Cheng95514ba2006-08-26 08:00:10 +00002980SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2981 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002982 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002983 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002984 SDOperand Ops[] = { Op1 };
2985
Jim Laskey583bd472006-10-27 23:46:08 +00002986 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002987 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002988 void *IP = 0;
2989 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002990 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002991
Chris Lattner149c58c2005-08-16 18:17:10 +00002992 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002993 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002994 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002995 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002996}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002997
Evan Cheng95514ba2006-08-26 08:00:10 +00002998SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2999 MVT::ValueType VT, SDOperand Op1,
3000 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003001 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003002 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003003 SDOperand Ops[] = { Op1, Op2 };
3004
Jim Laskey583bd472006-10-27 23:46:08 +00003005 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003006 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003007 void *IP = 0;
3008 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003009 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003010
Chris Lattner149c58c2005-08-16 18:17:10 +00003011 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003012
Chris Lattner63e3f142007-02-04 07:28:00 +00003013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003014
Chris Lattnera5682852006-08-07 23:03:03 +00003015 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003016 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003017}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003018
Evan Cheng95514ba2006-08-26 08:00:10 +00003019SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3020 MVT::ValueType VT, SDOperand Op1,
3021 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003022 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003023 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003024 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003025 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003026 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003027 void *IP = 0;
3028 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003029 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003030
Chris Lattner149c58c2005-08-16 18:17:10 +00003031 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003032
Chris Lattner63e3f142007-02-04 07:28:00 +00003033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003034
Chris Lattnera5682852006-08-07 23:03:03 +00003035 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003036 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003037}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003038
Evan Cheng95514ba2006-08-26 08:00:10 +00003039SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003040 MVT::ValueType VT, const SDOperand *Ops,
3041 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003042 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003043 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003044 FoldingSetNodeID ID;
3045 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003046 void *IP = 0;
3047 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003048 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003049
Chris Lattner6b09a292005-08-21 18:49:33 +00003050 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003052
Chris Lattnera5682852006-08-07 23:03:03 +00003053 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003054 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003055}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003056
Evan Cheng95514ba2006-08-26 08:00:10 +00003057SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3058 MVT::ValueType VT1, MVT::ValueType VT2,
3059 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003060 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003061 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003062 SDOperand Ops[] = { Op1, Op2 };
3063 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003064 void *IP = 0;
3065 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003066 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003067
Chris Lattner0fb094f2005-11-19 01:44:53 +00003068 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003070 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003071 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003072}
3073
Evan Cheng95514ba2006-08-26 08:00:10 +00003074SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3075 MVT::ValueType VT1, MVT::ValueType VT2,
3076 SDOperand Op1, SDOperand Op2,
3077 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003078 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003079 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003080 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003081 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003082 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003083 void *IP = 0;
3084 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003085 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003086
Chris Lattner0fb094f2005-11-19 01:44:53 +00003087 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003088
Chris Lattner63e3f142007-02-04 07:28:00 +00003089 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003090 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003091 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003092}
3093
Chris Lattner0fb094f2005-11-19 01:44:53 +00003094
Evan Cheng6ae46c42006-02-09 07:15:23 +00003095/// getTargetNode - These are used for target selectors to create a new node
3096/// with specified return type(s), target opcode, and operands.
3097///
3098/// Note that getTargetNode returns the resultant node. If there is already a
3099/// node of the specified opcode and operands, it returns that node instead of
3100/// the current one.
3101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3102 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3103}
3104SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3105 SDOperand Op1) {
3106 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3107}
3108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3109 SDOperand Op1, SDOperand Op2) {
3110 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3111}
3112SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003113 SDOperand Op1, SDOperand Op2,
3114 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003115 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3116}
3117SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003118 const SDOperand *Ops, unsigned NumOps) {
3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003120}
3121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003122 MVT::ValueType VT2) {
3123 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3124 SDOperand Op;
3125 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3126}
3127SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003128 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003129 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003130 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003131}
3132SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003133 MVT::ValueType VT2, SDOperand Op1,
3134 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003135 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003136 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003137 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003138}
3139SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003140 MVT::ValueType VT2, SDOperand Op1,
3141 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003142 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003143 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003144 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003145}
Evan Cheng694481e2006-08-27 08:08:54 +00003146SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3147 MVT::ValueType VT2,
3148 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003149 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003150 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003151}
3152SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3153 MVT::ValueType VT2, MVT::ValueType VT3,
3154 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003155 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003156 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003157 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003158}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003159SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3160 MVT::ValueType VT2, MVT::ValueType VT3,
3161 SDOperand Op1, SDOperand Op2,
3162 SDOperand Op3) {
3163 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3164 SDOperand Ops[] = { Op1, Op2, Op3 };
3165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3166}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003168 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003169 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003170 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3171 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003172}
Evan Cheng05e69c12007-09-12 23:39:49 +00003173SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3174 MVT::ValueType VT2, MVT::ValueType VT3,
3175 MVT::ValueType VT4,
3176 const SDOperand *Ops, unsigned NumOps) {
3177 std::vector<MVT::ValueType> VTList;
3178 VTList.push_back(VT1);
3179 VTList.push_back(VT2);
3180 VTList.push_back(VT3);
3181 VTList.push_back(VT4);
3182 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3183 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3184}
Evan Cheng39305cf2007-10-05 01:10:49 +00003185SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3186 std::vector<MVT::ValueType> &ResultTys,
3187 const SDOperand *Ops, unsigned NumOps) {
3188 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3189 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3190 Ops, NumOps).Val;
3191}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003192
Evan Cheng99157a02006-08-07 22:13:29 +00003193/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003194/// This can cause recursive merging of nodes in the DAG.
3195///
Chris Lattner8b52f212005-08-26 18:36:28 +00003196/// This version assumes From/To have a single result value.
3197///
Chris Lattner1e111c72005-09-07 05:37:01 +00003198void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3199 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003200 SDNode *From = FromN.Val, *To = ToN.Val;
3201 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3202 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003203 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003204
Chris Lattner8b8749f2005-08-17 19:00:20 +00003205 while (!From->use_empty()) {
3206 // Process users until they are all gone.
3207 SDNode *U = *From->use_begin();
3208
3209 // This node is about to morph, remove its old self from the CSE maps.
3210 RemoveNodeFromCSEMaps(U);
3211
Chris Lattner65113b22005-11-08 22:07:03 +00003212 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3213 I != E; ++I)
3214 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003215 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003216 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003217 To->addUser(U);
3218 }
3219
3220 // Now that we have modified U, add it back to the CSE maps. If it already
3221 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003222 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3223 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003224 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003225 if (Deleted) Deleted->push_back(U);
3226 DeleteNodeNotInCSEMaps(U);
3227 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003228 }
3229}
3230
3231/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3232/// This can cause recursive merging of nodes in the DAG.
3233///
3234/// This version assumes From/To have matching types and numbers of result
3235/// values.
3236///
Chris Lattner1e111c72005-09-07 05:37:01 +00003237void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3238 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003239 assert(From != To && "Cannot replace uses of with self");
3240 assert(From->getNumValues() == To->getNumValues() &&
3241 "Cannot use this version of ReplaceAllUsesWith!");
3242 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003243 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003244 return;
3245 }
3246
3247 while (!From->use_empty()) {
3248 // Process users until they are all gone.
3249 SDNode *U = *From->use_begin();
3250
3251 // This node is about to morph, remove its old self from the CSE maps.
3252 RemoveNodeFromCSEMaps(U);
3253
Chris Lattner65113b22005-11-08 22:07:03 +00003254 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3255 I != E; ++I)
3256 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003257 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003258 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003259 To->addUser(U);
3260 }
3261
3262 // Now that we have modified U, add it back to the CSE maps. If it already
3263 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003264 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3265 ReplaceAllUsesWith(U, Existing, Deleted);
3266 // U is now dead.
3267 if (Deleted) Deleted->push_back(U);
3268 DeleteNodeNotInCSEMaps(U);
3269 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003270 }
3271}
3272
Chris Lattner8b52f212005-08-26 18:36:28 +00003273/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3274/// This can cause recursive merging of nodes in the DAG.
3275///
3276/// This version can replace From with any result values. To must match the
3277/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003278void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003279 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003280 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003281 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003282 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003283 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003284 return;
3285 }
3286
3287 while (!From->use_empty()) {
3288 // Process users until they are all gone.
3289 SDNode *U = *From->use_begin();
3290
3291 // This node is about to morph, remove its old self from the CSE maps.
3292 RemoveNodeFromCSEMaps(U);
3293
Chris Lattner65113b22005-11-08 22:07:03 +00003294 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3295 I != E; ++I)
3296 if (I->Val == From) {
3297 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003298 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003299 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003300 ToOp.Val->addUser(U);
3301 }
3302
3303 // Now that we have modified U, add it back to the CSE maps. If it already
3304 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003305 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3306 ReplaceAllUsesWith(U, Existing, Deleted);
3307 // U is now dead.
3308 if (Deleted) Deleted->push_back(U);
3309 DeleteNodeNotInCSEMaps(U);
3310 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003311 }
3312}
3313
Chris Lattner012f2412006-02-17 21:58:01 +00003314/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3315/// uses of other values produced by From.Val alone. The Deleted vector is
3316/// handled the same was as for ReplaceAllUsesWith.
3317void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003318 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003319 assert(From != To && "Cannot replace a value with itself");
3320 // Handle the simple, trivial, case efficiently.
3321 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003322 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003323 return;
3324 }
3325
Chris Lattnercf5640b2007-02-04 00:14:31 +00003326 // Get all of the users of From.Val. We want these in a nice,
3327 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3328 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003329
Chris Lattner01d029b2007-10-15 06:10:22 +00003330 std::vector<SDNode*> LocalDeletionVector;
3331
3332 // Pick a deletion vector to use. If the user specified one, use theirs,
3333 // otherwise use a local one.
3334 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003335 while (!Users.empty()) {
3336 // We know that this user uses some value of From. If it is the right
3337 // value, update it.
3338 SDNode *User = Users.back();
3339 Users.pop_back();
3340
Chris Lattner01d029b2007-10-15 06:10:22 +00003341 // Scan for an operand that matches From.
3342 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3343 for (; Op != E; ++Op)
3344 if (*Op == From) break;
3345
3346 // If there are no matches, the user must use some other result of From.
3347 if (Op == E) continue;
3348
3349 // Okay, we know this user needs to be updated. Remove its old self
3350 // from the CSE maps.
3351 RemoveNodeFromCSEMaps(User);
3352
3353 // Update all operands that match "From".
3354 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003355 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003356 From.Val->removeUser(User);
3357 *Op = To;
3358 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003359 }
3360 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003361
3362 // Now that we have modified User, add it back to the CSE maps. If it
3363 // already exists there, recursively merge the results together.
3364 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3365 if (!Existing) continue; // Continue on to next user.
3366
3367 // If there was already an existing matching node, use ReplaceAllUsesWith
3368 // to replace the dead one with the existing one. However, this can cause
3369 // recursive merging of other unrelated nodes down the line. The merging
3370 // can cause deletion of nodes that used the old value. In this case,
3371 // we have to be certain to remove them from the Users set.
3372 unsigned NumDeleted = DeleteVector->size();
3373 ReplaceAllUsesWith(User, Existing, DeleteVector);
3374
3375 // User is now dead.
3376 DeleteVector->push_back(User);
3377 DeleteNodeNotInCSEMaps(User);
3378
3379 // We have to be careful here, because ReplaceAllUsesWith could have
3380 // deleted a user of From, which means there may be dangling pointers
3381 // in the "Users" setvector. Scan over the deleted node pointers and
3382 // remove them from the setvector.
3383 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3384 Users.remove((*DeleteVector)[i]);
3385
3386 // If the user doesn't need the set of deleted elements, don't retain them
3387 // to the next loop iteration.
3388 if (Deleted == 0)
3389 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003390 }
3391}
3392
Chris Lattner7b2880c2005-08-24 22:44:39 +00003393
Evan Chenge6f35d82006-08-01 08:20:41 +00003394/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3395/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003396unsigned SelectionDAG::AssignNodeIds() {
3397 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003398 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3399 SDNode *N = I;
3400 N->setNodeId(Id++);
3401 }
3402 return Id;
3403}
3404
Evan Chenge6f35d82006-08-01 08:20:41 +00003405/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003406/// based on their topological order. It returns the maximum id and a vector
3407/// of the SDNodes* in assigned order by reference.
3408unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003409 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003410 std::vector<unsigned> InDegree(DAGSize);
3411 std::vector<SDNode*> Sources;
3412
3413 // Use a two pass approach to avoid using a std::map which is slow.
3414 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003415 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3416 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003417 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003418 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003419 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003420 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003421 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003422 }
3423
Evan Cheng99157a02006-08-07 22:13:29 +00003424 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003425 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003426 SDNode *N = Sources.back();
3427 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003428 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003429 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3430 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003431 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003432 if (Degree == 0)
3433 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003434 }
3435 }
3436
Evan Chengc384d6c2006-08-02 22:00:34 +00003437 // Second pass, assign the actual topological order as node ids.
3438 Id = 0;
3439 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3440 TI != TE; ++TI)
3441 (*TI)->setNodeId(Id++);
3442
3443 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003444}
3445
3446
Evan Cheng091cba12006-07-27 06:39:06 +00003447
Jim Laskey58b968b2005-08-17 20:08:02 +00003448//===----------------------------------------------------------------------===//
3449// SDNode Class
3450//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003451
Chris Lattner917d2c92006-07-19 00:00:37 +00003452// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003453void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003454void UnarySDNode::ANCHOR() {}
3455void BinarySDNode::ANCHOR() {}
3456void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003457void HandleSDNode::ANCHOR() {}
3458void StringSDNode::ANCHOR() {}
3459void ConstantSDNode::ANCHOR() {}
3460void ConstantFPSDNode::ANCHOR() {}
3461void GlobalAddressSDNode::ANCHOR() {}
3462void FrameIndexSDNode::ANCHOR() {}
3463void JumpTableSDNode::ANCHOR() {}
3464void ConstantPoolSDNode::ANCHOR() {}
3465void BasicBlockSDNode::ANCHOR() {}
3466void SrcValueSDNode::ANCHOR() {}
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003467void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003468void RegisterSDNode::ANCHOR() {}
3469void ExternalSymbolSDNode::ANCHOR() {}
3470void CondCodeSDNode::ANCHOR() {}
3471void VTSDNode::ANCHOR() {}
3472void LoadSDNode::ANCHOR() {}
3473void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003474
Chris Lattner48b85922007-02-04 02:41:42 +00003475HandleSDNode::~HandleSDNode() {
3476 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003477 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003478}
3479
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003480GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3481 MVT::ValueType VT, int o)
3482 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003483 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003484 // Thread Local
3485 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3486 // Non Thread Local
3487 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3488 getSDVTList(VT)), Offset(o) {
3489 TheGlobal = const_cast<GlobalValue*>(GA);
3490}
Chris Lattner48b85922007-02-04 02:41:42 +00003491
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003492/// getMemOperand - Return a MemOperand object describing the memory
3493/// reference performed by this load or store.
3494MemOperand LSBaseSDNode::getMemOperand() const {
3495 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3496 int Flags =
3497 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3498 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3499
3500 // Check if the load references a frame index, and does not have
3501 // an SV attached.
3502 const FrameIndexSDNode *FI =
3503 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3504 if (!getSrcValue() && FI)
3505 return MemOperand(&PseudoSourceValue::FPRel, Flags,
3506 FI->getIndex(), Size, Alignment);
3507 else
3508 return MemOperand(getSrcValue(), Flags,
3509 getSrcValueOffset(), Size, Alignment);
3510}
3511
Jim Laskey583bd472006-10-27 23:46:08 +00003512/// Profile - Gather unique data for the node.
3513///
3514void SDNode::Profile(FoldingSetNodeID &ID) {
3515 AddNodeIDNode(ID, this);
3516}
3517
Chris Lattnera3255112005-11-08 23:30:28 +00003518/// getValueTypeList - Return a pointer to the specified value type.
3519///
3520MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003521 if (MVT::isExtendedVT(VT)) {
3522 static std::set<MVT::ValueType> EVTs;
3523 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3524 } else {
3525 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3526 VTs[VT] = VT;
3527 return &VTs[VT];
3528 }
Chris Lattnera3255112005-11-08 23:30:28 +00003529}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003530
Chris Lattner5c884562005-01-12 18:37:47 +00003531/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3532/// indicated value. This method ignores uses of other values defined by this
3533/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003534bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003535 assert(Value < getNumValues() && "Bad value!");
3536
3537 // If there is only one value, this is easy.
3538 if (getNumValues() == 1)
3539 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003540 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003541
Evan Cheng4ee62112006-02-05 06:29:23 +00003542 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003543
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003544 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003545
Chris Lattnerf83482d2006-08-16 20:59:32 +00003546 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003547 SDNode *User = *UI;
3548 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003549 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003550 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3551 if (User->getOperand(i) == TheValue) {
3552 if (NUses == 0)
3553 return false; // too many uses
3554 --NUses;
3555 }
3556 }
3557
3558 // Found exactly the right number of uses?
3559 return NUses == 0;
3560}
3561
3562
Evan Cheng33d55952007-08-02 05:29:38 +00003563/// hasAnyUseOfValue - Return true if there are any use of the indicated
3564/// value. This method ignores uses of other values defined by this operation.
3565bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3566 assert(Value < getNumValues() && "Bad value!");
3567
Dan Gohman30359592008-01-29 13:02:09 +00003568 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003569
3570 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3571
3572 SmallPtrSet<SDNode*, 32> UsersHandled;
3573
3574 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3575 SDNode *User = *UI;
3576 if (User->getNumOperands() == 1 ||
3577 UsersHandled.insert(User)) // First time we've seen this?
3578 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3579 if (User->getOperand(i) == TheValue) {
3580 return true;
3581 }
3582 }
3583
3584 return false;
3585}
3586
3587
Evan Chenge6e97e62006-11-03 07:31:32 +00003588/// isOnlyUse - Return true if this node is the only use of N.
3589///
Evan Cheng4ee62112006-02-05 06:29:23 +00003590bool SDNode::isOnlyUse(SDNode *N) const {
3591 bool Seen = false;
3592 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3593 SDNode *User = *I;
3594 if (User == this)
3595 Seen = true;
3596 else
3597 return false;
3598 }
3599
3600 return Seen;
3601}
3602
Evan Chenge6e97e62006-11-03 07:31:32 +00003603/// isOperand - Return true if this node is an operand of N.
3604///
Evan Chengbfa284f2006-03-03 06:42:32 +00003605bool SDOperand::isOperand(SDNode *N) const {
3606 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3607 if (*this == N->getOperand(i))
3608 return true;
3609 return false;
3610}
3611
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003612bool SDNode::isOperand(SDNode *N) const {
3613 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3614 if (this == N->OperandList[i].Val)
3615 return true;
3616 return false;
3617}
Evan Cheng4ee62112006-02-05 06:29:23 +00003618
Chris Lattner572dee72008-01-16 05:49:24 +00003619/// reachesChainWithoutSideEffects - Return true if this operand (which must
3620/// be a chain) reaches the specified operand without crossing any
3621/// side-effecting instructions. In practice, this looks through token
3622/// factors and non-volatile loads. In order to remain efficient, this only
3623/// looks a couple of nodes in, it does not do an exhaustive search.
3624bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3625 unsigned Depth) const {
3626 if (*this == Dest) return true;
3627
3628 // Don't search too deeply, we just want to be able to see through
3629 // TokenFactor's etc.
3630 if (Depth == 0) return false;
3631
3632 // If this is a token factor, all inputs to the TF happen in parallel. If any
3633 // of the operands of the TF reach dest, then we can do the xform.
3634 if (getOpcode() == ISD::TokenFactor) {
3635 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3636 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3637 return true;
3638 return false;
3639 }
3640
3641 // Loads don't have side effects, look through them.
3642 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3643 if (!Ld->isVolatile())
3644 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3645 }
3646 return false;
3647}
3648
3649
Evan Chengc5fc57d2006-11-03 03:05:24 +00003650static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003651 SmallPtrSet<SDNode *, 32> &Visited) {
3652 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003653 return;
3654
3655 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3656 SDNode *Op = N->getOperand(i).Val;
3657 if (Op == P) {
3658 found = true;
3659 return;
3660 }
3661 findPredecessor(Op, P, found, Visited);
3662 }
3663}
3664
Evan Chenge6e97e62006-11-03 07:31:32 +00003665/// isPredecessor - Return true if this node is a predecessor of N. This node
3666/// is either an operand of N or it can be reached by recursively traversing
3667/// up the operands.
3668/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003669bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003670 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003671 bool found = false;
3672 findPredecessor(N, this, found, Visited);
3673 return found;
3674}
3675
Evan Chengc5484282006-10-04 00:56:09 +00003676uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3677 assert(Num < NumOperands && "Invalid child # of SDNode!");
3678 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3679}
3680
Reid Spencer577cc322007-04-01 07:32:19 +00003681std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003682 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003683 default:
3684 if (getOpcode() < ISD::BUILTIN_OP_END)
3685 return "<<Unknown DAG Node>>";
3686 else {
Evan Cheng72261582005-12-20 06:22:03 +00003687 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003688 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003689 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003690 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003691
Evan Cheng72261582005-12-20 06:22:03 +00003692 TargetLowering &TLI = G->getTargetLoweringInfo();
3693 const char *Name =
3694 TLI.getTargetNodeName(getOpcode());
3695 if (Name) return Name;
3696 }
3697
3698 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003699 }
3700
Andrew Lenharth95762122005-03-31 21:24:06 +00003701 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003702 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003703 case ISD::SRCVALUE: return "SrcValue";
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003704 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003705 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003706 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003707 case ISD::AssertSext: return "AssertSext";
3708 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003709
3710 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003711 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003712 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003713 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003714
3715 case ISD::Constant: return "Constant";
3716 case ISD::ConstantFP: return "ConstantFP";
3717 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003718 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003719 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003720 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003721 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003722 case ISD::RETURNADDR: return "RETURNADDR";
3723 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003724 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003725 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3726 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003727 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003728 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003729 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003730 case ISD::INTRINSIC_WO_CHAIN: {
3731 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3732 return Intrinsic::getName((Intrinsic::ID)IID);
3733 }
3734 case ISD::INTRINSIC_VOID:
3735 case ISD::INTRINSIC_W_CHAIN: {
3736 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003737 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003738 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003739
Chris Lattnerb2827b02006-03-19 00:52:58 +00003740 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003741 case ISD::TargetConstant: return "TargetConstant";
3742 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003743 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003744 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003745 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003746 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003747 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003748 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003749
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003750 case ISD::CopyToReg: return "CopyToReg";
3751 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003752 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003753 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003754 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003755 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003756 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003757 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003758 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003759
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003760 // Unary operators
3761 case ISD::FABS: return "fabs";
3762 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003763 case ISD::FSQRT: return "fsqrt";
3764 case ISD::FSIN: return "fsin";
3765 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003766 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003767 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003768
3769 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003770 case ISD::ADD: return "add";
3771 case ISD::SUB: return "sub";
3772 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003773 case ISD::MULHU: return "mulhu";
3774 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003775 case ISD::SDIV: return "sdiv";
3776 case ISD::UDIV: return "udiv";
3777 case ISD::SREM: return "srem";
3778 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003779 case ISD::SMUL_LOHI: return "smul_lohi";
3780 case ISD::UMUL_LOHI: return "umul_lohi";
3781 case ISD::SDIVREM: return "sdivrem";
3782 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003783 case ISD::AND: return "and";
3784 case ISD::OR: return "or";
3785 case ISD::XOR: return "xor";
3786 case ISD::SHL: return "shl";
3787 case ISD::SRA: return "sra";
3788 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003789 case ISD::ROTL: return "rotl";
3790 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003791 case ISD::FADD: return "fadd";
3792 case ISD::FSUB: return "fsub";
3793 case ISD::FMUL: return "fmul";
3794 case ISD::FDIV: return "fdiv";
3795 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003796 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003797 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003798
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003799 case ISD::SETCC: return "setcc";
3800 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003801 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003802 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003803 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003804 case ISD::CONCAT_VECTORS: return "concat_vectors";
3805 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003806 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003807 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003808 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003809 case ISD::ADDC: return "addc";
3810 case ISD::ADDE: return "adde";
3811 case ISD::SUBC: return "subc";
3812 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003813 case ISD::SHL_PARTS: return "shl_parts";
3814 case ISD::SRA_PARTS: return "sra_parts";
3815 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003816
3817 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3818 case ISD::INSERT_SUBREG: return "insert_subreg";
3819
Chris Lattner7f644642005-04-28 21:44:03 +00003820 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003821 case ISD::SIGN_EXTEND: return "sign_extend";
3822 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003823 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003824 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003825 case ISD::TRUNCATE: return "truncate";
3826 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003827 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003828 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003829 case ISD::FP_EXTEND: return "fp_extend";
3830
3831 case ISD::SINT_TO_FP: return "sint_to_fp";
3832 case ISD::UINT_TO_FP: return "uint_to_fp";
3833 case ISD::FP_TO_SINT: return "fp_to_sint";
3834 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003835 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003836
3837 // Control flow instructions
3838 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003839 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003840 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003841 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003842 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003843 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003844 case ISD::CALLSEQ_START: return "callseq_start";
3845 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003846
3847 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003848 case ISD::LOAD: return "load";
3849 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003850 case ISD::VAARG: return "vaarg";
3851 case ISD::VACOPY: return "vacopy";
3852 case ISD::VAEND: return "vaend";
3853 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003854 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003855 case ISD::EXTRACT_ELEMENT: return "extract_element";
3856 case ISD::BUILD_PAIR: return "build_pair";
3857 case ISD::STACKSAVE: return "stacksave";
3858 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003859 case ISD::TRAP: return "trap";
3860
Chris Lattner5a67afc2006-01-13 02:39:42 +00003861 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003862 case ISD::MEMSET: return "memset";
3863 case ISD::MEMCPY: return "memcpy";
3864 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003865
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003866 // Bit manipulation
3867 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003868 case ISD::CTPOP: return "ctpop";
3869 case ISD::CTTZ: return "cttz";
3870 case ISD::CTLZ: return "ctlz";
3871
Chris Lattner36ce6912005-11-29 06:21:05 +00003872 // Debug info
3873 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003874 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003875
Duncan Sands36397f52007-07-27 12:58:54 +00003876 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003877 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003878
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003879 case ISD::CONDCODE:
3880 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003881 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003882 case ISD::SETOEQ: return "setoeq";
3883 case ISD::SETOGT: return "setogt";
3884 case ISD::SETOGE: return "setoge";
3885 case ISD::SETOLT: return "setolt";
3886 case ISD::SETOLE: return "setole";
3887 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003888
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003889 case ISD::SETO: return "seto";
3890 case ISD::SETUO: return "setuo";
3891 case ISD::SETUEQ: return "setue";
3892 case ISD::SETUGT: return "setugt";
3893 case ISD::SETUGE: return "setuge";
3894 case ISD::SETULT: return "setult";
3895 case ISD::SETULE: return "setule";
3896 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003897
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003898 case ISD::SETEQ: return "seteq";
3899 case ISD::SETGT: return "setgt";
3900 case ISD::SETGE: return "setge";
3901 case ISD::SETLT: return "setlt";
3902 case ISD::SETLE: return "setle";
3903 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003904 }
3905 }
3906}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003907
Evan Cheng144d8f02006-11-09 17:55:04 +00003908const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003909 switch (AM) {
3910 default:
3911 return "";
3912 case ISD::PRE_INC:
3913 return "<pre-inc>";
3914 case ISD::PRE_DEC:
3915 return "<pre-dec>";
3916 case ISD::POST_INC:
3917 return "<post-inc>";
3918 case ISD::POST_DEC:
3919 return "<post-dec>";
3920 }
3921}
3922
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003923void SDNode::dump() const { dump(0); }
3924void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003925 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003926
3927 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003928 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003929 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003930 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003931 else
Bill Wendling832171c2006-12-07 20:04:42 +00003932 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003933 }
Bill Wendling832171c2006-12-07 20:04:42 +00003934 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003935
Bill Wendling832171c2006-12-07 20:04:42 +00003936 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003937 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003938 if (i) cerr << ", ";
3939 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003940 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003941 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003942 }
3943
Evan Chengce254432007-12-11 02:08:35 +00003944 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3945 SDNode *Mask = getOperand(2).Val;
3946 cerr << "<";
3947 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3948 if (i) cerr << ",";
3949 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3950 cerr << "u";
3951 else
3952 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3953 }
3954 cerr << ">";
3955 }
3956
Chris Lattnerc3aae252005-01-07 07:46:32 +00003957 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003958 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003959 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003960 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3961 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3962 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3963 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3964 else {
3965 cerr << "<APFloat(";
3966 CSDN->getValueAPF().convertToAPInt().dump();
3967 cerr << ")>";
3968 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003969 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003970 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003971 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003972 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003973 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003974 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003975 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003976 else
Bill Wendling832171c2006-12-07 20:04:42 +00003977 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003978 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003979 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003980 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003981 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003982 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003983 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003984 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003985 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003986 else
Bill Wendling832171c2006-12-07 20:04:42 +00003987 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003988 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003989 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003990 else
Bill Wendling832171c2006-12-07 20:04:42 +00003991 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003992 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003993 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003994 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3995 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003996 cerr << LBB->getName() << " ";
3997 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003998 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003999 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00004000 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004001 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004002 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004003 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004004 } else if (const ExternalSymbolSDNode *ES =
4005 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004006 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004007 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4008 if (M->getValue())
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004009 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004010 else
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004011 cerr << "<null>";
4012 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4013 if (M->MO.getValue())
4014 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4015 else
4016 cerr << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00004017 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004018 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004019 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004020 const Value *SrcValue = LD->getSrcValue();
4021 int SrcOffset = LD->getSrcValueOffset();
4022 cerr << " <";
4023 if (SrcValue)
4024 cerr << SrcValue;
4025 else
4026 cerr << "null";
4027 cerr << ":" << SrcOffset << ">";
4028
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004029 bool doExt = true;
4030 switch (LD->getExtensionType()) {
4031 default: doExt = false; break;
4032 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004033 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004034 break;
4035 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004036 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004037 break;
4038 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004039 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004040 break;
4041 }
4042 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004043 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004044
Evan Cheng144d8f02006-11-09 17:55:04 +00004045 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004046 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004047 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004048 if (LD->isVolatile())
4049 cerr << " <volatile>";
4050 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004051 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004052 const Value *SrcValue = ST->getSrcValue();
4053 int SrcOffset = ST->getSrcValueOffset();
4054 cerr << " <";
4055 if (SrcValue)
4056 cerr << SrcValue;
4057 else
4058 cerr << "null";
4059 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004060
4061 if (ST->isTruncatingStore())
4062 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004063 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004064
4065 const char *AM = getIndexedModeName(ST->getAddressingMode());
4066 if (*AM)
4067 cerr << " " << AM;
4068 if (ST->isVolatile())
4069 cerr << " <volatile>";
4070 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004071 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004072}
4073
Chris Lattnerde202b32005-11-09 23:47:37 +00004074static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004075 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4076 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004077 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004078 else
Bill Wendling832171c2006-12-07 20:04:42 +00004079 cerr << "\n" << std::string(indent+2, ' ')
4080 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004081
Chris Lattnerea946cd2005-01-09 20:38:33 +00004082
Bill Wendling832171c2006-12-07 20:04:42 +00004083 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004084 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004085}
4086
Chris Lattnerc3aae252005-01-07 07:46:32 +00004087void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004088 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004089 std::vector<const SDNode*> Nodes;
4090 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4091 I != E; ++I)
4092 Nodes.push_back(I);
4093
Chris Lattner49d24712005-01-09 20:26:36 +00004094 std::sort(Nodes.begin(), Nodes.end());
4095
4096 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004097 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004098 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004099 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004100
Jim Laskey26f7fa72006-10-17 19:33:52 +00004101 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004102
Bill Wendling832171c2006-12-07 20:04:42 +00004103 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004104}
4105
Evan Chengd6594ae2006-09-12 21:00:35 +00004106const Type *ConstantPoolSDNode::getType() const {
4107 if (isMachineConstantPoolEntry())
4108 return Val.MachineCPVal->getType();
4109 return Val.ConstVal->getType();
4110}