blob: 3cd731073c0440f85d6ab77901f80473a23d09ed [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"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
21#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000026#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000032#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000033#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000034#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000035#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000036#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000037#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000038#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner0b3e5252006-08-15 19:11:05 +000041/// makeVTList - Return an instance of the SDVTList struct initialized with the
42/// specified members.
43static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
44 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Chris Lattnerec4a5672008-03-05 06:48:13 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
49 switch (VT) {
50 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
56 }
57}
58
Chris Lattnerf8dc0612008-02-03 06:49:24 +000059SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000069bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000070 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000071}
72
Dale Johannesenf04afdb2007-08-30 00:23:21 +000073bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
74 const APFloat& Val) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000075 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
76
77 // Anything can be extended to ppc long double.
78 if (VT == MVT::ppcf128)
79 return true;
80
81 // PPC long double cannot be shrunk to anything though.
82 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
83 return false;
84
Dale Johannesenf04afdb2007-08-30 00:23:21 +000085 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000087 return Val2.convert(*MVTToAPFloatSemantics(VT),
88 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
99 if (N->getOpcode() == ISD::BIT_CONVERT)
100 N = N->getOperand(0).Val;
101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000103
104 unsigned i = 0, e = N->getNumOperands();
105
106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
109
110 // Do not accept an all-undef vector.
111 if (i == e) return false;
112
113 // Do not accept build_vectors that aren't all constants or which have non-~0
114 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000115 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000116 if (isa<ConstantSDNode>(NotZero)) {
117 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
118 return false;
119 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000120 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
121 convertToAPInt().isAllOnesValue())
122 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000173}
174
Evan Chengefec7512008-02-18 23:04:32 +0000175/// isScalarToVector - Return true if the specified node is a
176/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
177/// element is not an undef.
178bool ISD::isScalarToVector(const SDNode *N) {
179 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
180 return true;
181
182 if (N->getOpcode() != ISD::BUILD_VECTOR)
183 return false;
184 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
185 return false;
186 unsigned NumElems = N->getNumOperands();
187 for (unsigned i = 1; i < NumElems; ++i) {
188 SDOperand V = N->getOperand(i);
189 if (V.getOpcode() != ISD::UNDEF)
190 return false;
191 }
192 return true;
193}
194
195
Evan Chengbb81d972008-01-31 09:59:15 +0000196/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000197/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000198/// is 0).
199bool ISD::isDebugLabel(const SDNode *N) {
200 SDOperand Zero;
201 if (N->getOpcode() == ISD::LABEL)
202 Zero = N->getOperand(2);
203 else if (N->isTargetOpcode() &&
204 N->getTargetOpcode() == TargetInstrInfo::LABEL)
205 // Chain moved to last operand.
206 Zero = N->getOperand(1);
207 else
208 return false;
209 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
210}
211
Chris Lattnerc3aae252005-01-07 07:46:32 +0000212/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
213/// when given the operation for (X op Y).
214ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
215 // To perform this operation, we just need to swap the L and G bits of the
216 // operation.
217 unsigned OldL = (Operation >> 2) & 1;
218 unsigned OldG = (Operation >> 1) & 1;
219 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
220 (OldL << 1) | // New G bit
221 (OldG << 2)); // New L bit.
222}
223
224/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
225/// 'op' is a valid SetCC operation.
226ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
227 unsigned Operation = Op;
228 if (isInteger)
229 Operation ^= 7; // Flip L, G, E bits, but not U.
230 else
231 Operation ^= 15; // Flip all of the condition bits.
232 if (Operation > ISD::SETTRUE2)
233 Operation &= ~8; // Don't let N and U bits get set.
234 return ISD::CondCode(Operation);
235}
236
237
238/// isSignedOp - For an integer comparison, return 1 if the comparison is a
239/// signed operation and 2 if the result is an unsigned comparison. Return zero
240/// if the operation does not depend on the sign of the input (setne and seteq).
241static int isSignedOp(ISD::CondCode Opcode) {
242 switch (Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
244 case ISD::SETEQ:
245 case ISD::SETNE: return 0;
246 case ISD::SETLT:
247 case ISD::SETLE:
248 case ISD::SETGT:
249 case ISD::SETGE: return 1;
250 case ISD::SETULT:
251 case ISD::SETULE:
252 case ISD::SETUGT:
253 case ISD::SETUGE: return 2;
254 }
255}
256
257/// getSetCCOrOperation - Return the result of a logical OR between different
258/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259/// returns SETCC_INVALID if it is not possible to represent the resultant
260/// comparison.
261ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
262 bool isInteger) {
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000272 Op &= ~16; // Clear the U bit if the N bit is set.
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
276 Op = ISD::SETNE;
277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 return ISD::CondCode(Op);
279}
280
281/// getSetCCAndOperation - Return the result of a logical AND between different
282/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283/// function returns zero if it is not possible to represent the resultant
284/// comparison.
285ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
286 bool isInteger) {
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000289 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000290
291 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
293
294 // Canonicalize illegal integer setcc's.
295 if (isInteger) {
296 switch (Result) {
297 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
299 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
300 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
301 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000302 }
303 }
304
305 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000306}
307
Chris Lattnerb48da392005-01-23 04:39:44 +0000308const TargetMachine &SelectionDAG::getTarget() const {
309 return TLI.getTargetMachine();
310}
311
Jim Laskey58b968b2005-08-17 20:08:02 +0000312//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000313// SDNode Profile Support
314//===----------------------------------------------------------------------===//
315
Jim Laskeydef69b92006-10-27 23:52:51 +0000316/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
317///
Jim Laskey583bd472006-10-27 23:46:08 +0000318static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
319 ID.AddInteger(OpC);
320}
321
322/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
323/// solely with their pointer.
324void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
325 ID.AddPointer(VTList.VTs);
326}
327
Jim Laskeydef69b92006-10-27 23:52:51 +0000328/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
329///
Jim Laskey583bd472006-10-27 23:46:08 +0000330static void AddNodeIDOperands(FoldingSetNodeID &ID,
331 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000332 for (; NumOps; --NumOps, ++Ops) {
333 ID.AddPointer(Ops->Val);
334 ID.AddInteger(Ops->ResNo);
335 }
Jim Laskey583bd472006-10-27 23:46:08 +0000336}
337
Jim Laskey583bd472006-10-27 23:46:08 +0000338static void AddNodeIDNode(FoldingSetNodeID &ID,
339 unsigned short OpC, SDVTList VTList,
340 const SDOperand *OpList, unsigned N) {
341 AddNodeIDOpcode(ID, OpC);
342 AddNodeIDValueTypes(ID, VTList);
343 AddNodeIDOperands(ID, OpList, N);
344}
345
Jim Laskeydef69b92006-10-27 23:52:51 +0000346/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
347/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000348static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
354
355 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
358 case ISD::TargetConstant:
359 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000360 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 break;
362 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000363 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000364 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000366 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000368 case ISD::GlobalAddress:
369 case ISD::TargetGlobalTLSAddress:
370 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
372 ID.AddPointer(GA->getGlobal());
373 ID.AddInteger(GA->getOffset());
374 break;
375 }
376 case ISD::BasicBlock:
377 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
378 break;
379 case ISD::Register:
380 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
381 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000382 case ISD::SRCVALUE:
383 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
384 break;
385 case ISD::MEMOPERAND: {
386 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
387 ID.AddPointer(MO.getValue());
388 ID.AddInteger(MO.getFlags());
389 ID.AddInteger(MO.getOffset());
390 ID.AddInteger(MO.getSize());
391 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000392 break;
393 }
394 case ISD::FrameIndex:
395 case ISD::TargetFrameIndex:
396 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
397 break;
398 case ISD::JumpTable:
399 case ISD::TargetJumpTable:
400 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
401 break;
402 case ISD::ConstantPool:
403 case ISD::TargetConstantPool: {
404 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
405 ID.AddInteger(CP->getAlignment());
406 ID.AddInteger(CP->getOffset());
407 if (CP->isMachineConstantPoolEntry())
408 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
409 else
410 ID.AddPointer(CP->getConstVal());
411 break;
412 }
413 case ISD::LOAD: {
414 LoadSDNode *LD = cast<LoadSDNode>(N);
415 ID.AddInteger(LD->getAddressingMode());
416 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000417 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 ID.AddInteger(LD->getAlignment());
419 ID.AddInteger(LD->isVolatile());
420 break;
421 }
422 case ISD::STORE: {
423 StoreSDNode *ST = cast<StoreSDNode>(N);
424 ID.AddInteger(ST->getAddressingMode());
425 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000426 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000427 ID.AddInteger(ST->getAlignment());
428 ID.AddInteger(ST->isVolatile());
429 break;
430 }
Jim Laskey583bd472006-10-27 23:46:08 +0000431 }
432}
433
434//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000435// SelectionDAG Class
436//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000437
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000438/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000439/// SelectionDAG.
440void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441 // Create a dummy node (which is not added to allnodes), that adds a reference
442 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000443 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444
Chris Lattner190a4182006-08-04 17:45:20 +0000445 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000446
Chris Lattner190a4182006-08-04 17:45:20 +0000447 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000448 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000449 if (I->use_empty())
450 DeadNodes.push_back(I);
451
452 // Process the worklist, deleting the nodes and adding their uses to the
453 // worklist.
454 while (!DeadNodes.empty()) {
455 SDNode *N = DeadNodes.back();
456 DeadNodes.pop_back();
457
458 // Take the node out of the appropriate CSE map.
459 RemoveNodeFromCSEMaps(N);
460
461 // Next, brutally remove the operand list. This is safe to do, as there are
462 // no cycles in the graph.
463 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
464 SDNode *Operand = I->Val;
465 Operand->removeUser(N);
466
467 // Now that we removed this operand, see if there are no uses of it left.
468 if (Operand->use_empty())
469 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000470 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000471 if (N->OperandsNeedDelete)
472 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000473 N->OperandList = 0;
474 N->NumOperands = 0;
475
476 // Finally, remove N itself.
477 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000478 }
479
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000480 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000481 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000482}
483
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000484void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000485 SmallVector<SDNode*, 16> DeadNodes;
486 DeadNodes.push_back(N);
487
488 // Process the worklist, deleting the nodes and adding their uses to the
489 // worklist.
490 while (!DeadNodes.empty()) {
491 SDNode *N = DeadNodes.back();
492 DeadNodes.pop_back();
493
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000494 if (UpdateListener)
495 UpdateListener->NodeDeleted(N);
496
Evan Cheng130a6472006-10-12 20:34:05 +0000497 // Take the node out of the appropriate CSE map.
498 RemoveNodeFromCSEMaps(N);
499
500 // Next, brutally remove the operand list. This is safe to do, as there are
501 // no cycles in the graph.
502 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
503 SDNode *Operand = I->Val;
504 Operand->removeUser(N);
505
506 // Now that we removed this operand, see if there are no uses of it left.
507 if (Operand->use_empty())
508 DeadNodes.push_back(Operand);
509 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000510 if (N->OperandsNeedDelete)
511 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000512 N->OperandList = 0;
513 N->NumOperands = 0;
514
515 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000516 AllNodes.erase(N);
517 }
518}
519
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000520void SelectionDAG::DeleteNode(SDNode *N) {
521 assert(N->use_empty() && "Cannot delete a node that is not dead!");
522
523 // First take this out of the appropriate CSE map.
524 RemoveNodeFromCSEMaps(N);
525
Chris Lattner1e111c72005-09-07 05:37:01 +0000526 // Finally, remove uses due to operands of this node, remove from the
527 // AllNodes list, and delete the node.
528 DeleteNodeNotInCSEMaps(N);
529}
530
531void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
532
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000533 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000534 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000535
536 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000537 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
538 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000539 if (N->OperandsNeedDelete)
540 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000541 N->OperandList = 0;
542 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000543
544 delete N;
545}
546
Chris Lattner149c58c2005-08-16 18:17:10 +0000547/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
548/// correspond to it. This is useful when we're about to delete or repurpose
549/// the node. We don't want future request for structurally identical nodes
550/// to return N anymore.
551void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000552 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000553 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000554 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000555 case ISD::STRING:
556 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
557 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000558 case ISD::CONDCODE:
559 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
560 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000561 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000562 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
563 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000565 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000566 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000567 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000568 Erased =
569 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000570 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000571 case ISD::VALUETYPE: {
572 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
573 if (MVT::isExtendedVT(VT)) {
574 Erased = ExtendedValueTypeNodes.erase(VT);
575 } else {
576 Erased = ValueTypeNodes[VT] != 0;
577 ValueTypeNodes[VT] = 0;
578 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000579 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000580 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000582 // Remove it from the CSE Map.
583 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000584 break;
585 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000586#ifndef NDEBUG
587 // Verify that the node was actually in one of the CSE maps, unless it has a
588 // flag result (which cannot be CSE'd) or is one of the special cases that are
589 // not subject to CSE.
590 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000591 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000592 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000593 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000594 assert(0 && "Node is not in map!");
595 }
596#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000597}
598
Chris Lattner8b8749f2005-08-17 19:00:20 +0000599/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
600/// has been taken out and modified in some way. If the specified node already
601/// exists in the CSE maps, do not modify the maps, but return the existing node
602/// instead. If it doesn't exist, add it and return null.
603///
604SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
605 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000606 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000607 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000608
Chris Lattner9f8cc692005-12-19 22:21:21 +0000609 // Check that remaining values produced are not flags.
610 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
611 if (N->getValueType(i) == MVT::Flag)
612 return 0; // Never CSE anything that produces a flag.
613
Chris Lattnera5682852006-08-07 23:03:03 +0000614 SDNode *New = CSEMap.GetOrInsertNode(N);
615 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000616 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000617}
618
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000619/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
620/// were replaced with those specified. If this node is never memoized,
621/// return null, otherwise return a pointer to the slot it would take. If a
622/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000623SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
624 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000626 return 0; // Never add these nodes.
627
628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
632
Chris Lattner63e3f142007-02-04 07:28:00 +0000633 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000634 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000635 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000636 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000637}
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.
Chris Lattnera5682852006-08-07 23:03:03 +0000643SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
644 SDOperand Op1, SDOperand Op2,
645 void *&InsertPos) {
646 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
647 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
Chris Lattner63e3f142007-02-04 07:28:00 +0000654 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000655 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000656 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000657 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
658}
659
660
661/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
662/// were replaced with those specified. If this node is never memoized,
663/// return null, otherwise return a pointer to the slot it would take. If a
664/// node already exists with these operands, the slot will be non-null.
665SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000666 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000667 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000668 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000669 return 0; // Never add these nodes.
670
671 // Check that remaining values produced are not flags.
672 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
673 if (N->getValueType(i) == MVT::Flag)
674 return 0; // Never CSE anything that produces a flag.
675
Jim Laskey583bd472006-10-27 23:46:08 +0000676 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000677 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000678
Evan Cheng9629aba2006-10-11 01:47:58 +0000679 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
680 ID.AddInteger(LD->getAddressingMode());
681 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000682 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000683 ID.AddInteger(LD->getAlignment());
684 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000685 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
686 ID.AddInteger(ST->getAddressingMode());
687 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000688 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000689 ID.AddInteger(ST->getAlignment());
690 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000691 }
Jim Laskey583bd472006-10-27 23:46:08 +0000692
Chris Lattnera5682852006-08-07 23:03:03 +0000693 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000694}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000695
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000696
Chris Lattner78ec3112003-08-11 14:57:33 +0000697SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000698 while (!AllNodes.empty()) {
699 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000700 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000701 if (N->OperandsNeedDelete)
702 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000703 N->OperandList = 0;
704 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000705 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000706 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000707}
708
Chris Lattner0f2287b2005-04-13 02:38:18 +0000709SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000710 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000711 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
712 MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000713 return getNode(ISD::AND, Op.getValueType(), Op,
714 getConstant(Imm, Op.getValueType()));
715}
716
Chris Lattner36ce6912005-11-29 06:21:05 +0000717SDOperand SelectionDAG::getString(const std::string &Val) {
718 StringSDNode *&N = StringNodes[Val];
719 if (!N) {
720 N = new StringSDNode(Val);
721 AllNodes.push_back(N);
722 }
723 return SDOperand(N, 0);
724}
725
Chris Lattner61b09412006-08-11 21:01:22 +0000726SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000727 MVT::ValueType EltVT =
728 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
729
730 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
731}
732
733SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000734 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000735
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000738
Dan Gohman6394b092008-02-08 22:59:30 +0000739 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
740 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000741
742 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000743 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000744 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000745 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000746 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000747 SDNode *N = NULL;
748 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
749 if (!MVT::isVector(VT))
750 return SDOperand(N, 0);
751 if (!N) {
752 N = new ConstantSDNode(isT, Val, EltVT);
753 CSEMap.InsertNode(N, IP);
754 AllNodes.push_back(N);
755 }
756
757 SDOperand Result(N, 0);
758 if (MVT::isVector(VT)) {
759 SmallVector<SDOperand, 8> Ops;
760 Ops.assign(MVT::getVectorNumElements(VT), Result);
761 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
762 }
763 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000764}
765
Chris Lattner0bd48932008-01-17 07:00:52 +0000766SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
767 return getConstant(Val, TLI.getPointerTy(), isTarget);
768}
769
770
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000771SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000772 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000773 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000774
Dan Gohman7f321562007-06-25 16:23:39 +0000775 MVT::ValueType EltVT =
776 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000777
Chris Lattnerd8658612005-02-17 20:17:32 +0000778 // Do the map lookup using the actual bit pattern for the floating point
779 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
780 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000781 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000782 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000783 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000784 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000785 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000786 SDNode *N = NULL;
787 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
788 if (!MVT::isVector(VT))
789 return SDOperand(N, 0);
790 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000791 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000792 CSEMap.InsertNode(N, IP);
793 AllNodes.push_back(N);
794 }
795
Dan Gohman7f321562007-06-25 16:23:39 +0000796 SDOperand Result(N, 0);
797 if (MVT::isVector(VT)) {
798 SmallVector<SDOperand, 8> Ops;
799 Ops.assign(MVT::getVectorNumElements(VT), Result);
800 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
801 }
802 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000803}
804
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000805SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
806 bool isTarget) {
807 MVT::ValueType EltVT =
808 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
809 if (EltVT==MVT::f32)
810 return getConstantFP(APFloat((float)Val), VT, isTarget);
811 else
812 return getConstantFP(APFloat(Val), VT, isTarget);
813}
814
Chris Lattnerc3aae252005-01-07 07:46:32 +0000815SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000816 MVT::ValueType VT, int Offset,
817 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000818 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000819
820 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
821 if (!GVar) {
822 // If GV is an alias - use aliasee for determing thread-localness
823 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
824 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
825 }
826
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000827 if (GVar && GVar->isThreadLocal())
828 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
829 else
830 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000831
Jim Laskey583bd472006-10-27 23:46:08 +0000832 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000833 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000834 ID.AddPointer(GV);
835 ID.AddInteger(Offset);
836 void *IP = 0;
837 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
838 return SDOperand(E, 0);
839 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
840 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 AllNodes.push_back(N);
842 return SDOperand(N, 0);
843}
844
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000845SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
846 bool isTarget) {
847 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000848 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000849 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000850 ID.AddInteger(FI);
851 void *IP = 0;
852 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
853 return SDOperand(E, 0);
854 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
855 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000856 AllNodes.push_back(N);
857 return SDOperand(N, 0);
858}
859
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000860SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
861 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000862 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000863 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000864 ID.AddInteger(JTI);
865 void *IP = 0;
866 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
867 return SDOperand(E, 0);
868 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
869 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000870 AllNodes.push_back(N);
871 return SDOperand(N, 0);
872}
873
Evan Chengb8973bd2006-01-31 22:23:14 +0000874SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000875 unsigned Alignment, int Offset,
876 bool isTarget) {
877 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000879 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000880 ID.AddInteger(Alignment);
881 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000882 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000883 void *IP = 0;
884 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
885 return SDOperand(E, 0);
886 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
887 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000888 AllNodes.push_back(N);
889 return SDOperand(N, 0);
890}
891
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892
Evan Chengd6594ae2006-09-12 21:00:35 +0000893SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
894 MVT::ValueType VT,
895 unsigned Alignment, int Offset,
896 bool isTarget) {
897 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000898 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000899 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000900 ID.AddInteger(Alignment);
901 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000902 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000903 void *IP = 0;
904 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
905 return SDOperand(E, 0);
906 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
907 CSEMap.InsertNode(N, IP);
908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
910}
911
912
Chris Lattnerc3aae252005-01-07 07:46:32 +0000913SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000914 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000915 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000916 ID.AddPointer(MBB);
917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new BasicBlockSDNode(MBB);
921 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
924}
925
Chris Lattner15e4b012005-07-10 00:07:11 +0000926SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000927 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000928 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000929
Duncan Sandsf411b832007-10-17 13:49:58 +0000930 SDNode *&N = MVT::isExtendedVT(VT) ?
931 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
932
933 if (N) return SDOperand(N, 0);
934 N = new VTSDNode(VT);
935 AllNodes.push_back(N);
936 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000937}
938
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
940 SDNode *&N = ExternalSymbols[Sym];
941 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000942 N = new ExternalSymbolSDNode(false, Sym, VT);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
945}
946
Chris Lattner809ec112006-01-28 10:09:25 +0000947SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
948 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000949 SDNode *&N = TargetExternalSymbols[Sym];
950 if (N) return SDOperand(N, 0);
951 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000952 AllNodes.push_back(N);
953 return SDOperand(N, 0);
954}
955
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000956SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
957 if ((unsigned)Cond >= CondCodeNodes.size())
958 CondCodeNodes.resize(Cond+1);
959
Chris Lattner079a27a2005-08-09 20:40:02 +0000960 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000961 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000962 AllNodes.push_back(CondCodeNodes[Cond]);
963 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000964 return SDOperand(CondCodeNodes[Cond], 0);
965}
966
Chris Lattner0fdd7682005-08-30 22:38:38 +0000967SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000968 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000969 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000970 ID.AddInteger(RegNo);
971 void *IP = 0;
972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
973 return SDOperand(E, 0);
974 SDNode *N = new RegisterSDNode(RegNo, VT);
975 CSEMap.InsertNode(N, IP);
976 AllNodes.push_back(N);
977 return SDOperand(N, 0);
978}
979
Dan Gohman69de1932008-02-06 22:27:42 +0000980SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000981 assert((!V || isa<PointerType>(V->getType())) &&
982 "SrcValue is not a pointer?");
983
Jim Laskey583bd472006-10-27 23:46:08 +0000984 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000985 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000986 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +0000987
Chris Lattner61b09412006-08-11 21:01:22 +0000988 void *IP = 0;
989 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
990 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +0000991
992 SDNode *N = new SrcValueSDNode(V);
993 CSEMap.InsertNode(N, IP);
994 AllNodes.push_back(N);
995 return SDOperand(N, 0);
996}
997
998SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
999 const Value *v = MO.getValue();
1000 assert((!v || isa<PointerType>(v->getType())) &&
1001 "SrcValue is not a pointer?");
1002
1003 FoldingSetNodeID ID;
1004 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1005 ID.AddPointer(v);
1006 ID.AddInteger(MO.getFlags());
1007 ID.AddInteger(MO.getOffset());
1008 ID.AddInteger(MO.getSize());
1009 ID.AddInteger(MO.getAlignment());
1010
1011 void *IP = 0;
1012 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1013 return SDOperand(E, 0);
1014
1015 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001016 CSEMap.InsertNode(N, IP);
1017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001019}
1020
Chris Lattner37ce9df2007-10-15 17:47:20 +00001021/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1022/// specified value type.
1023SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1024 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1025 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1026 const Type *Ty = MVT::getTypeForValueType(VT);
1027 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1028 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1029 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1030}
1031
1032
Chris Lattner51dabfb2006-10-14 00:41:01 +00001033SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1034 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 // These setcc operations always fold.
1036 switch (Cond) {
1037 default: break;
1038 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001039 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001041 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001042
1043 case ISD::SETOEQ:
1044 case ISD::SETOGT:
1045 case ISD::SETOGE:
1046 case ISD::SETOLT:
1047 case ISD::SETOLE:
1048 case ISD::SETONE:
1049 case ISD::SETO:
1050 case ISD::SETUO:
1051 case ISD::SETUEQ:
1052 case ISD::SETUNE:
1053 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1054 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001056
Chris Lattner67255a12005-04-07 18:14:58 +00001057 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001058 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001059 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001060 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001061
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 switch (Cond) {
1063 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001064 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1065 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001066 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1067 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1068 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1069 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1070 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1071 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1072 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1073 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075 }
Chris Lattner67255a12005-04-07 18:14:58 +00001076 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001077 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001079 // No compile time operations on this type yet.
1080 if (N1C->getValueType(0) == MVT::ppcf128)
1081 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001082
1083 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001085 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001086 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1087 return getNode(ISD::UNDEF, VT);
1088 // fall through
1089 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1090 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1091 return getNode(ISD::UNDEF, VT);
1092 // fall through
1093 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001094 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001095 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1096 return getNode(ISD::UNDEF, VT);
1097 // fall through
1098 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1099 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1100 return getNode(ISD::UNDEF, VT);
1101 // fall through
1102 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1103 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1104 return getNode(ISD::UNDEF, VT);
1105 // fall through
1106 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001107 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001108 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1109 return getNode(ISD::UNDEF, VT);
1110 // fall through
1111 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001112 R==APFloat::cmpEqual, VT);
1113 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1114 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1115 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1116 R==APFloat::cmpEqual, VT);
1117 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1118 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1119 R==APFloat::cmpLessThan, VT);
1120 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1121 R==APFloat::cmpUnordered, VT);
1122 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1123 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 }
1125 } else {
1126 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001127 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001129 }
1130
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001131 // Could not fold it.
1132 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133}
1134
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001135/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1136/// use this predicate to simplify operations downstream.
1137bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1138 unsigned BitWidth = Op.getValueSizeInBits();
1139 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1140}
1141
Dan Gohmanea859be2007-06-22 14:59:07 +00001142/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1143/// this predicate to simplify operations downstream. Mask is known to be zero
1144/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001145bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001146 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001147 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001148 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1149 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1150 return (KnownZero & Mask) == Mask;
1151}
1152
1153/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1154/// known to be either zero or one and return them in the KnownZero/KnownOne
1155/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1156/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001157void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001158 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001159 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001160 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001161 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1162 "Mask size mismatches value type size!");
1163
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001164 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001165 if (Depth == 6 || Mask == 0)
1166 return; // Limit search depth.
1167
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001168 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001169
1170 switch (Op.getOpcode()) {
1171 case ISD::Constant:
1172 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001173 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001174 KnownZero = ~KnownOne & Mask;
1175 return;
1176 case ISD::AND:
1177 // If either the LHS or the RHS are Zero, the result is zero.
1178 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001179 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1180 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001181 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 // Output known-1 bits are only known if set in both the LHS & RHS.
1185 KnownOne &= KnownOne2;
1186 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1187 KnownZero |= KnownZero2;
1188 return;
1189 case ISD::OR:
1190 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001191 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1192 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001193 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1194 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1195
1196 // Output known-0 bits are only known if clear in both the LHS & RHS.
1197 KnownZero &= KnownZero2;
1198 // Output known-1 are known to be set if set in either the LHS | RHS.
1199 KnownOne |= KnownOne2;
1200 return;
1201 case ISD::XOR: {
1202 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1203 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1204 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1205 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1206
1207 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001208 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001209 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1210 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1211 KnownZero = KnownZeroOut;
1212 return;
1213 }
1214 case ISD::SELECT:
1215 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1216 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1217 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1218 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1219
1220 // Only known if known in both the LHS and RHS.
1221 KnownOne &= KnownOne2;
1222 KnownZero &= KnownZero2;
1223 return;
1224 case ISD::SELECT_CC:
1225 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1226 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1227 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1228 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1229
1230 // Only known if known in both the LHS and RHS.
1231 KnownOne &= KnownOne2;
1232 KnownZero &= KnownZero2;
1233 return;
1234 case ISD::SETCC:
1235 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001236 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1237 BitWidth > 1)
1238 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001239 return;
1240 case ISD::SHL:
1241 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1242 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001243 unsigned ShAmt = SA->getValue();
1244
1245 // If the shift count is an invalid immediate, don't do anything.
1246 if (ShAmt >= BitWidth)
1247 return;
1248
1249 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001250 KnownZero, KnownOne, Depth+1);
1251 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001252 KnownZero <<= ShAmt;
1253 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001254 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001255 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001256 }
1257 return;
1258 case ISD::SRL:
1259 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1260 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001261 unsigned ShAmt = SA->getValue();
1262
Dan Gohmand4cf9922008-02-26 18:50:50 +00001263 // If the shift count is an invalid immediate, don't do anything.
1264 if (ShAmt >= BitWidth)
1265 return;
1266
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001267 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001268 KnownZero, KnownOne, Depth+1);
1269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001270 KnownZero = KnownZero.lshr(ShAmt);
1271 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001272
Dan Gohman72d2fd52008-02-13 22:43:25 +00001273 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001274 KnownZero |= HighBits; // High bits known zero.
1275 }
1276 return;
1277 case ISD::SRA:
1278 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001279 unsigned ShAmt = SA->getValue();
1280
Dan Gohmand4cf9922008-02-26 18:50:50 +00001281 // If the shift count is an invalid immediate, don't do anything.
1282 if (ShAmt >= BitWidth)
1283 return;
1284
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001285 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001286 // If any of the demanded bits are produced by the sign extension, we also
1287 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001288 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1289 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001290 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001291
1292 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1293 Depth+1);
1294 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001295 KnownZero = KnownZero.lshr(ShAmt);
1296 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001297
1298 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001299 APInt SignBit = APInt::getSignBit(BitWidth);
1300 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001301
Dan Gohmanca93a432008-02-20 16:30:17 +00001302 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001303 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001304 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001305 KnownOne |= HighBits; // New bits are known one.
1306 }
1307 }
1308 return;
1309 case ISD::SIGN_EXTEND_INREG: {
1310 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001311 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001312
1313 // Sign extension. Compute the demanded bits in the result that are not
1314 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001315 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001316
Dan Gohman977a76f2008-02-13 22:28:48 +00001317 APInt InSignBit = APInt::getSignBit(EBits);
1318 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001319
1320 // If the sign extended bits are demanded, we know that the sign
1321 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001322 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001323 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001324 InputDemandedBits |= InSignBit;
1325
1326 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1327 KnownZero, KnownOne, Depth+1);
1328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1329
1330 // If the sign bit of the input is known set or clear, then we know the
1331 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001332 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 KnownZero |= NewBits;
1334 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001335 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001336 KnownOne |= NewBits;
1337 KnownZero &= ~NewBits;
1338 } else { // Input sign bit unknown
1339 KnownZero &= ~NewBits;
1340 KnownOne &= ~NewBits;
1341 }
1342 return;
1343 }
1344 case ISD::CTTZ:
1345 case ISD::CTLZ:
1346 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001347 unsigned LowBits = Log2_32(BitWidth)+1;
1348 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1349 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001350 return;
1351 }
1352 case ISD::LOAD: {
1353 if (ISD::isZEXTLoad(Op.Val)) {
1354 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001355 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001356 unsigned MemBits = MVT::getSizeInBits(VT);
1357 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001358 }
1359 return;
1360 }
1361 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001362 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1363 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001364 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1365 APInt InMask = Mask;
1366 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001367 KnownZero.trunc(InBits);
1368 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001369 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001370 KnownZero.zext(BitWidth);
1371 KnownOne.zext(BitWidth);
1372 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001373 return;
1374 }
1375 case ISD::SIGN_EXTEND: {
1376 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001377 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001378 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001379 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1380 APInt InMask = Mask;
1381 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001382
1383 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001384 // bit is demanded. Temporarily set this bit in the mask for our callee.
1385 if (NewBits.getBoolValue())
1386 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001387
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001388 KnownZero.trunc(InBits);
1389 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001390 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1391
1392 // Note if the sign bit is known to be zero or one.
1393 bool SignBitKnownZero = KnownZero.isNegative();
1394 bool SignBitKnownOne = KnownOne.isNegative();
1395 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1396 "Sign bit can't be known to be both zero and one!");
1397
1398 // If the sign bit wasn't actually demanded by our caller, we don't
1399 // want it set in the KnownZero and KnownOne result values. Reset the
1400 // mask and reapply it to the result values.
1401 InMask = Mask;
1402 InMask.trunc(InBits);
1403 KnownZero &= InMask;
1404 KnownOne &= InMask;
1405
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001406 KnownZero.zext(BitWidth);
1407 KnownOne.zext(BitWidth);
1408
Dan Gohman977a76f2008-02-13 22:28:48 +00001409 // If the sign bit is known zero or one, the top bits match.
1410 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001411 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001412 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001413 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001414 return;
1415 }
1416 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001417 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1418 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001419 APInt InMask = Mask;
1420 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001421 KnownZero.trunc(InBits);
1422 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001423 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001424 KnownZero.zext(BitWidth);
1425 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001426 return;
1427 }
1428 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001429 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1430 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001431 APInt InMask = Mask;
1432 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001433 KnownZero.zext(InBits);
1434 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001435 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001437 KnownZero.trunc(BitWidth);
1438 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 break;
1440 }
1441 case ISD::AssertZext: {
1442 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001443 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001444 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1445 KnownOne, Depth+1);
1446 KnownZero |= (~InMask) & Mask;
1447 return;
1448 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001449 case ISD::FGETSIGN:
1450 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001451 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001452 return;
1453
Dan Gohmanea859be2007-06-22 14:59:07 +00001454 case ISD::ADD: {
1455 // If either the LHS or the RHS are Zero, the result is zero.
1456 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1457 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1458 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1459 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1460
1461 // Output known-0 bits are known if clear or set in both the low clear bits
1462 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1463 // low 3 bits clear.
Dan Gohman977a76f2008-02-13 22:28:48 +00001464 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1465 KnownZero2.countTrailingOnes());
Dan Gohmanea859be2007-06-22 14:59:07 +00001466
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001467 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1468 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001469 return;
1470 }
1471 case ISD::SUB: {
1472 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1473 if (!CLHS) return;
1474
1475 // We know that the top bits of C-X are clear if X contains less bits
1476 // than C (i.e. no wrap-around can happen). For example, 20-X is
1477 // positive if we can prove that X is >= 0 and < 16.
Dan Gohman977a76f2008-02-13 22:28:48 +00001478 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001479 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1480 // NLZ can't be BitWidth with no sign bit
Chris Lattner423be622008-02-14 18:48:56 +00001481 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001482 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1483
1484 // If all of the MaskV bits are known to be zero, then we know the output
1485 // top bits are zero, because we now know that the output is from [0-C].
1486 if ((KnownZero & MaskV) == MaskV) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001487 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1488 // Top bits known zero.
1489 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1490 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001491 } else {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001492 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001493 }
1494 }
1495 return;
1496 }
1497 default:
1498 // Allow the target to implement this method for its nodes.
1499 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1500 case ISD::INTRINSIC_WO_CHAIN:
1501 case ISD::INTRINSIC_W_CHAIN:
1502 case ISD::INTRINSIC_VOID:
1503 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1504 }
1505 return;
1506 }
1507}
1508
1509/// ComputeNumSignBits - Return the number of times the sign bit of the
1510/// register is replicated into the other bits. We know that at least 1 bit
1511/// is always equal to the sign bit (itself), but other cases can give us
1512/// information. For example, immediately after an "SRA X, 2", we know that
1513/// the top 3 bits are all equal to each other, so we return 3.
1514unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1515 MVT::ValueType VT = Op.getValueType();
1516 assert(MVT::isInteger(VT) && "Invalid VT!");
1517 unsigned VTBits = MVT::getSizeInBits(VT);
1518 unsigned Tmp, Tmp2;
1519
1520 if (Depth == 6)
1521 return 1; // Limit search depth.
1522
1523 switch (Op.getOpcode()) {
1524 default: break;
1525 case ISD::AssertSext:
1526 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1527 return VTBits-Tmp+1;
1528 case ISD::AssertZext:
1529 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1530 return VTBits-Tmp;
1531
1532 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001533 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1534 // If negative, return # leading ones.
1535 if (Val.isNegative())
1536 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001537
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001538 // Return # leading zeros.
1539 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 }
1541
1542 case ISD::SIGN_EXTEND:
1543 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1544 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1545
1546 case ISD::SIGN_EXTEND_INREG:
1547 // Max of the input and what this extends.
1548 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1549 Tmp = VTBits-Tmp+1;
1550
1551 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1552 return std::max(Tmp, Tmp2);
1553
1554 case ISD::SRA:
1555 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1556 // SRA X, C -> adds C sign bits.
1557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1558 Tmp += C->getValue();
1559 if (Tmp > VTBits) Tmp = VTBits;
1560 }
1561 return Tmp;
1562 case ISD::SHL:
1563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1564 // shl destroys sign bits.
1565 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1566 if (C->getValue() >= VTBits || // Bad shift.
1567 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1568 return Tmp - C->getValue();
1569 }
1570 break;
1571 case ISD::AND:
1572 case ISD::OR:
1573 case ISD::XOR: // NOT is handled here.
1574 // Logical binary ops preserve the number of sign bits.
1575 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1576 if (Tmp == 1) return 1; // Early out.
1577 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1578 return std::min(Tmp, Tmp2);
1579
1580 case ISD::SELECT:
1581 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1582 if (Tmp == 1) return 1; // Early out.
1583 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1584 return std::min(Tmp, Tmp2);
1585
1586 case ISD::SETCC:
1587 // If setcc returns 0/-1, all bits are sign bits.
1588 if (TLI.getSetCCResultContents() ==
1589 TargetLowering::ZeroOrNegativeOneSetCCResult)
1590 return VTBits;
1591 break;
1592 case ISD::ROTL:
1593 case ISD::ROTR:
1594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1595 unsigned RotAmt = C->getValue() & (VTBits-1);
1596
1597 // Handle rotate right by N like a rotate left by 32-N.
1598 if (Op.getOpcode() == ISD::ROTR)
1599 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1600
1601 // If we aren't rotating out all of the known-in sign bits, return the
1602 // number that are left. This handles rotl(sext(x), 1) for example.
1603 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1604 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1605 }
1606 break;
1607 case ISD::ADD:
1608 // Add can have at most one carry bit. Thus we know that the output
1609 // is, at worst, one more bit than the inputs.
1610 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1611 if (Tmp == 1) return 1; // Early out.
1612
1613 // Special case decrementing a value (ADD X, -1):
1614 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1615 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001616 APInt KnownZero, KnownOne;
1617 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001618 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1619
1620 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1621 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001622 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001623 return VTBits;
1624
1625 // If we are subtracting one from a positive number, there is no carry
1626 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001627 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001628 return Tmp;
1629 }
1630
1631 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1632 if (Tmp2 == 1) return 1;
1633 return std::min(Tmp, Tmp2)-1;
1634 break;
1635
1636 case ISD::SUB:
1637 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1638 if (Tmp2 == 1) return 1;
1639
1640 // Handle NEG.
1641 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001642 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001643 APInt KnownZero, KnownOne;
1644 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001645 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1646 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1647 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001648 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001649 return VTBits;
1650
1651 // If the input is known to be positive (the sign bit is known clear),
1652 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001653 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001654 return Tmp2;
1655
1656 // Otherwise, we treat this like a SUB.
1657 }
1658
1659 // Sub can have at most one carry bit. Thus we know that the output
1660 // is, at worst, one more bit than the inputs.
1661 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1662 if (Tmp == 1) return 1; // Early out.
1663 return std::min(Tmp, Tmp2)-1;
1664 break;
1665 case ISD::TRUNCATE:
1666 // FIXME: it's tricky to do anything useful for this, but it is an important
1667 // case for targets like X86.
1668 break;
1669 }
1670
1671 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1672 if (Op.getOpcode() == ISD::LOAD) {
1673 LoadSDNode *LD = cast<LoadSDNode>(Op);
1674 unsigned ExtType = LD->getExtensionType();
1675 switch (ExtType) {
1676 default: break;
1677 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001678 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001679 return VTBits-Tmp+1;
1680 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001681 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 return VTBits-Tmp;
1683 }
1684 }
1685
1686 // Allow the target to implement this method for its nodes.
1687 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1688 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1689 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1690 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1691 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1692 if (NumBits > 1) return NumBits;
1693 }
1694
1695 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1696 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001697 APInt KnownZero, KnownOne;
1698 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1700
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001701 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001702 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001703 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 Mask = KnownOne;
1705 } else {
1706 // Nothing known.
1707 return 1;
1708 }
1709
1710 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1711 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001712 Mask = ~Mask;
1713 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 // Return # leading zeros. We use 'min' here in case Val was zero before
1715 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001716 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanea859be2007-06-22 14:59:07 +00001717}
1718
Chris Lattner51dabfb2006-10-14 00:41:01 +00001719
Evan Chenga844bde2008-02-02 04:07:54 +00001720bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1721 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1722 if (!GA) return false;
1723 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1724 if (!GV) return false;
1725 MachineModuleInfo *MMI = getMachineModuleInfo();
1726 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1727}
1728
1729
Chris Lattnerc3aae252005-01-07 07:46:32 +00001730/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001731///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001732SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001733 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001734 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001735 void *IP = 0;
1736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1737 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001738 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001739 CSEMap.InsertNode(N, IP);
1740
1741 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001742 return SDOperand(N, 0);
1743}
1744
Chris Lattnerc3aae252005-01-07 07:46:32 +00001745SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1746 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001747 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001749 const APInt &Val = C->getAPIntValue();
1750 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751 switch (Opcode) {
1752 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001753 case ISD::SIGN_EXTEND:
1754 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001755 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001756 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001757 case ISD::TRUNCATE:
1758 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001759 case ISD::UINT_TO_FP:
1760 case ISD::SINT_TO_FP: {
1761 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001762 // No compile time operations on this type.
1763 if (VT==MVT::ppcf128)
1764 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001765 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1766 (void)apf.convertFromAPInt(Val,
1767 Opcode==ISD::SINT_TO_FP,
1768 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001769 return getConstantFP(apf, VT);
1770 }
Chris Lattner94683772005-12-23 05:30:37 +00001771 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001772 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001773 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001774 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001775 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001776 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001777 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001778 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001779 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001780 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001781 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001782 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001783 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001784 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001785 }
1786 }
1787
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001788 // Constant fold unary operations with a floating point constant operand.
1789 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1790 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001791 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001792 switch (Opcode) {
1793 case ISD::FNEG:
1794 V.changeSign();
1795 return getConstantFP(V, VT);
1796 case ISD::FABS:
1797 V.clearSign();
1798 return getConstantFP(V, VT);
1799 case ISD::FP_ROUND:
1800 case ISD::FP_EXTEND:
1801 // This can return overflow, underflow, or inexact; we don't care.
1802 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001803 (void)V.convert(*MVTToAPFloatSemantics(VT),
1804 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001805 return getConstantFP(V, VT);
1806 case ISD::FP_TO_SINT:
1807 case ISD::FP_TO_UINT: {
1808 integerPart x;
1809 assert(integerPartWidth >= 64);
1810 // FIXME need to be more flexible about rounding mode.
1811 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1812 Opcode==ISD::FP_TO_SINT,
1813 APFloat::rmTowardZero);
1814 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1815 break;
1816 return getConstant(x, VT);
1817 }
1818 case ISD::BIT_CONVERT:
1819 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1820 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1821 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1822 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001823 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001824 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001825 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001826 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001827
1828 unsigned OpOpcode = Operand.Val->getOpcode();
1829 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001830 case ISD::TokenFactor:
1831 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001832 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001833 case ISD::FP_EXTEND:
1834 assert(MVT::isFloatingPoint(VT) &&
1835 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001836 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001837 if (Operand.getOpcode() == ISD::UNDEF)
1838 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001839 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001840 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001841 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1842 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001843 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001844 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1845 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001846 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1847 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1848 break;
1849 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001850 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1851 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001852 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001853 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1854 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001855 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001856 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001857 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001858 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001859 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1860 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001861 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001862 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1863 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001864 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1865 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1867 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001868 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001869 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1870 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001871 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001872 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1873 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001874 if (OpOpcode == ISD::TRUNCATE)
1875 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001876 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1877 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001878 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001879 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1880 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001881 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001882 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1883 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001884 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1885 else
1886 return Operand.Val->getOperand(0);
1887 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001888 break;
Chris Lattner35481892005-12-23 00:16:34 +00001889 case ISD::BIT_CONVERT:
1890 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001891 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001892 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001893 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001894 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1895 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001896 if (OpOpcode == ISD::UNDEF)
1897 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001898 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001899 case ISD::SCALAR_TO_VECTOR:
1900 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001901 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001902 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001903 if (OpOpcode == ISD::UNDEF)
1904 return getNode(ISD::UNDEF, VT);
1905 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1906 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1907 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1908 Operand.getConstantOperandVal(1) == 0 &&
1909 Operand.getOperand(0).getValueType() == VT)
1910 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001911 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001912 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001913 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1914 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001915 Operand.Val->getOperand(0));
1916 if (OpOpcode == ISD::FNEG) // --X -> X
1917 return Operand.Val->getOperand(0);
1918 break;
1919 case ISD::FABS:
1920 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1921 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1922 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001923 }
1924
Chris Lattner43247a12005-08-25 19:12:10 +00001925 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001926 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001927 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001928 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001929 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001930 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001931 void *IP = 0;
1932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1933 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001934 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001935 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001936 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001937 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001938 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001939 AllNodes.push_back(N);
1940 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001941}
1942
Chris Lattner36019aa2005-04-18 03:48:41 +00001943
1944
Chris Lattnerc3aae252005-01-07 07:46:32 +00001945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1946 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1948 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001949 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001950 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001951 case ISD::TokenFactor:
1952 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1953 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001954 // Fold trivial token factors.
1955 if (N1.getOpcode() == ISD::EntryToken) return N2;
1956 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001957 break;
Chris Lattner76365122005-01-16 02:23:22 +00001958 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001959 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1960 N1.getValueType() == VT && "Binary operator types must match!");
1961 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1962 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001963 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001964 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001965 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1966 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001967 break;
Chris Lattner76365122005-01-16 02:23:22 +00001968 case ISD::OR:
1969 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001970 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1971 N1.getValueType() == VT && "Binary operator types must match!");
1972 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1973 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001974 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001975 return N1;
1976 break;
Chris Lattner76365122005-01-16 02:23:22 +00001977 case ISD::UDIV:
1978 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001979 case ISD::MULHU:
1980 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001981 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1982 // fall through
1983 case ISD::ADD:
1984 case ISD::SUB:
1985 case ISD::MUL:
1986 case ISD::SDIV:
1987 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001988 case ISD::FADD:
1989 case ISD::FSUB:
1990 case ISD::FMUL:
1991 case ISD::FDIV:
1992 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001993 assert(N1.getValueType() == N2.getValueType() &&
1994 N1.getValueType() == VT && "Binary operator types must match!");
1995 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001996 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1997 assert(N1.getValueType() == VT &&
1998 MVT::isFloatingPoint(N1.getValueType()) &&
1999 MVT::isFloatingPoint(N2.getValueType()) &&
2000 "Invalid FCOPYSIGN!");
2001 break;
Chris Lattner76365122005-01-16 02:23:22 +00002002 case ISD::SHL:
2003 case ISD::SRA:
2004 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002005 case ISD::ROTL:
2006 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002007 assert(VT == N1.getValueType() &&
2008 "Shift operators return type must be the same as their first arg");
2009 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002010 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002011 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002012 case ISD::FP_ROUND_INREG: {
2013 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2014 assert(VT == N1.getValueType() && "Not an inreg round!");
2015 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2016 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002017 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2018 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002019 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002020 break;
2021 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002022 case ISD::FP_ROUND:
2023 assert(MVT::isFloatingPoint(VT) &&
2024 MVT::isFloatingPoint(N1.getValueType()) &&
2025 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2026 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002027 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002028 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002029 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002030 case ISD::AssertZext: {
2031 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2032 assert(VT == N1.getValueType() && "Not an inreg extend!");
2033 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2034 "Cannot *_EXTEND_INREG FP types");
2035 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2036 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002037 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002038 break;
2039 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002040 case ISD::SIGN_EXTEND_INREG: {
2041 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2042 assert(VT == N1.getValueType() && "Not an inreg extend!");
2043 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2044 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002045 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2046 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002047 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002048
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002049 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002050 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002051 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002052 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002053 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002054 return getConstant(Val, VT);
2055 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002056 break;
2057 }
2058 case ISD::EXTRACT_VECTOR_ELT:
2059 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2060
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002061 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2062 if (N1.getOpcode() == ISD::UNDEF)
2063 return getNode(ISD::UNDEF, VT);
2064
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002065 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2066 // expanding copies of large vectors from registers.
2067 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2068 N1.getNumOperands() > 0) {
2069 unsigned Factor =
2070 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2071 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2072 N1.getOperand(N2C->getValue() / Factor),
2073 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2074 }
2075
2076 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2077 // expanding large vector constants.
2078 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2079 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002080
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002081 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2082 // operations are lowered to scalars.
2083 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2084 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2085 if (IEC == N2C)
2086 return N1.getOperand(1);
2087 else
2088 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2089 }
2090 break;
2091 case ISD::EXTRACT_ELEMENT:
2092 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002093 assert(!MVT::isVector(N1.getValueType()) &&
2094 MVT::isInteger(N1.getValueType()) &&
2095 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2096 "EXTRACT_ELEMENT only applies to integers!");
2097
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002098 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2099 // 64-bit integers into 32-bit parts. Instead of building the extract of
2100 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2101 if (N1.getOpcode() == ISD::BUILD_PAIR)
2102 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002103
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002104 // EXTRACT_ELEMENT of a constant int is also very common.
2105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2106 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2107 return getConstant(C->getValue() >> Shift, VT);
2108 }
2109 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002110 case ISD::EXTRACT_SUBVECTOR:
2111 if (N1.getValueType() == VT) // Trivial extraction.
2112 return N1;
2113 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002114 }
2115
2116 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002117 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002118 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 switch (Opcode) {
2120 case ISD::ADD: return getConstant(C1 + C2, VT);
2121 case ISD::SUB: return getConstant(C1 - C2, VT);
2122 case ISD::MUL: return getConstant(C1 * C2, VT);
2123 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002124 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002125 break;
2126 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002127 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002128 break;
2129 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002130 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002131 break;
2132 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002133 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002134 break;
2135 case ISD::AND : return getConstant(C1 & C2, VT);
2136 case ISD::OR : return getConstant(C1 | C2, VT);
2137 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002138 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002139 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2140 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2141 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2142 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143 default: break;
2144 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002145 } else { // Cannonicalize constant to RHS if commutative
2146 if (isCommutativeBinOp(Opcode)) {
2147 std::swap(N1C, N2C);
2148 std::swap(N1, N2);
2149 }
2150 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002151 }
2152
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002153 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002154 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2155 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002156 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002157 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2158 // Cannonicalize constant to RHS if commutative
2159 std::swap(N1CFP, N2CFP);
2160 std::swap(N1, N2);
2161 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002162 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2163 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002164 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002165 case ISD::FADD:
2166 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002167 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002168 return getConstantFP(V1, VT);
2169 break;
2170 case ISD::FSUB:
2171 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2172 if (s!=APFloat::opInvalidOp)
2173 return getConstantFP(V1, VT);
2174 break;
2175 case ISD::FMUL:
2176 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2177 if (s!=APFloat::opInvalidOp)
2178 return getConstantFP(V1, VT);
2179 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002180 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002181 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2182 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2183 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002184 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002185 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002186 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2187 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2188 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002189 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002190 case ISD::FCOPYSIGN:
2191 V1.copySign(V2);
2192 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002193 default: break;
2194 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002196 }
Chris Lattner62b57722006-04-20 05:39:12 +00002197
2198 // Canonicalize an UNDEF to the RHS, even over a constant.
2199 if (N1.getOpcode() == ISD::UNDEF) {
2200 if (isCommutativeBinOp(Opcode)) {
2201 std::swap(N1, N2);
2202 } else {
2203 switch (Opcode) {
2204 case ISD::FP_ROUND_INREG:
2205 case ISD::SIGN_EXTEND_INREG:
2206 case ISD::SUB:
2207 case ISD::FSUB:
2208 case ISD::FDIV:
2209 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002210 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002211 return N1; // fold op(undef, arg2) -> undef
2212 case ISD::UDIV:
2213 case ISD::SDIV:
2214 case ISD::UREM:
2215 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002216 case ISD::SRL:
2217 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002218 if (!MVT::isVector(VT))
2219 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2220 // For vectors, we can't easily build an all zero vector, just return
2221 // the LHS.
2222 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002223 }
2224 }
2225 }
2226
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002227 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002228 if (N2.getOpcode() == ISD::UNDEF) {
2229 switch (Opcode) {
2230 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002231 case ISD::ADDC:
2232 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002233 case ISD::SUB:
2234 case ISD::FADD:
2235 case ISD::FSUB:
2236 case ISD::FMUL:
2237 case ISD::FDIV:
2238 case ISD::FREM:
2239 case ISD::UDIV:
2240 case ISD::SDIV:
2241 case ISD::UREM:
2242 case ISD::SREM:
2243 case ISD::XOR:
2244 return N2; // fold op(arg1, undef) -> undef
2245 case ISD::MUL:
2246 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002247 case ISD::SRL:
2248 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002249 if (!MVT::isVector(VT))
2250 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2251 // For vectors, we can't easily build an all zero vector, just return
2252 // the LHS.
2253 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002254 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002255 if (!MVT::isVector(VT))
2256 return getConstant(MVT::getIntVTBitMask(VT), VT);
2257 // For vectors, we can't easily build an all one vector, just return
2258 // the LHS.
2259 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002260 case ISD::SRA:
2261 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002262 }
2263 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002264
Chris Lattner27e9b412005-05-11 18:57:39 +00002265 // Memoize this node if possible.
2266 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002267 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002268 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002269 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002270 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002271 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002272 void *IP = 0;
2273 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2274 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002275 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002276 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002277 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002278 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002279 }
2280
Chris Lattnerc3aae252005-01-07 07:46:32 +00002281 AllNodes.push_back(N);
2282 return SDOperand(N, 0);
2283}
2284
Chris Lattnerc3aae252005-01-07 07:46:32 +00002285SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2286 SDOperand N1, SDOperand N2, SDOperand N3) {
2287 // Perform various simplifications.
2288 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2289 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002290 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002291 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002292 // Use FoldSetCC to simplify SETCC's.
2293 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002294 if (Simp.Val) return Simp;
2295 break;
2296 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002297 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002298 if (N1C) {
2299 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002300 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002301 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002302 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002303 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002304
2305 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002306 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002307 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002308 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002309 if (N2C->getValue()) // Unconditional branch
2310 return getNode(ISD::BR, MVT::Other, N1, N3);
2311 else
2312 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002313 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002314 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002315 case ISD::VECTOR_SHUFFLE:
2316 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2317 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2318 N3.getOpcode() == ISD::BUILD_VECTOR &&
2319 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2320 "Illegal VECTOR_SHUFFLE node!");
2321 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002322 case ISD::BIT_CONVERT:
2323 // Fold bit_convert nodes from a type to themselves.
2324 if (N1.getValueType() == VT)
2325 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002326 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002327 }
2328
Chris Lattner43247a12005-08-25 19:12:10 +00002329 // Memoize node if it doesn't produce a flag.
2330 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002331 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002332 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002333 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002334 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002335 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002336 void *IP = 0;
2337 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2338 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002339 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002340 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002341 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002342 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002343 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 AllNodes.push_back(N);
2345 return SDOperand(N, 0);
2346}
2347
2348SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002349 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002350 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002351 SDOperand Ops[] = { N1, N2, N3, N4 };
2352 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002353}
2354
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002355SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2356 SDOperand N1, SDOperand N2, SDOperand N3,
2357 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002358 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2359 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002360}
2361
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002362SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2363 SDOperand Src, SDOperand Size,
2364 SDOperand Align,
2365 SDOperand AlwaysInline) {
2366 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2367 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2368}
2369
2370SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2371 SDOperand Src, SDOperand Size,
2372 SDOperand Align,
2373 SDOperand AlwaysInline) {
2374 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2375 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2376}
2377
2378SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2379 SDOperand Src, SDOperand Size,
2380 SDOperand Align,
2381 SDOperand AlwaysInline) {
2382 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2383 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2384}
2385
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002386SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002387 SDOperand Ptr, SDOperand Cmp,
2388 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002389 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002390 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2391 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002392 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002393 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002394 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2395 ID.AddInteger((unsigned int)VT);
2396 void* IP = 0;
2397 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2398 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002399 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002400 CSEMap.InsertNode(N, IP);
2401 AllNodes.push_back(N);
2402 return SDOperand(N, 0);
2403}
2404
2405SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002406 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002407 MVT::ValueType VT) {
2408 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2409 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002410 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002411 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002412 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002413 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2414 ID.AddInteger((unsigned int)VT);
2415 void* IP = 0;
2416 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2417 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002418 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002419 CSEMap.InsertNode(N, IP);
2420 AllNodes.push_back(N);
2421 return SDOperand(N, 0);
2422}
2423
Evan Cheng7038daf2005-12-10 00:37:58 +00002424SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2425 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002426 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002427 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002428 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2429 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002430 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002431 Ty = MVT::getTypeForValueType(VT);
2432 } else if (SV) {
2433 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2434 assert(PT && "Value for load must be a pointer");
2435 Ty = PT->getElementType();
2436 }
2437 assert(Ty && "Could not get type information for load");
2438 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2439 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002440 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002441 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002442 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002443 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002444 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002445 ID.AddInteger(ISD::UNINDEXED);
2446 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002447 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002448 ID.AddInteger(Alignment);
2449 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002450 void *IP = 0;
2451 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2452 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002453 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002454 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2455 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002456 CSEMap.InsertNode(N, IP);
2457 AllNodes.push_back(N);
2458 return SDOperand(N, 0);
2459}
2460
2461SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002462 SDOperand Chain, SDOperand Ptr,
2463 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002464 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002465 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002466 // If they are asking for an extending load from/to the same thing, return a
2467 // normal load.
2468 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002469 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002470
2471 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002472 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002473 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002474 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2475 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002476 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2477 "Cannot sign/zero extend a FP/Vector load!");
2478 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2479 "Cannot convert from FP to Int or Int -> FP!");
2480
Dan Gohman575e2f42007-06-04 15:49:41 +00002481 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2482 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002483 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002484 Ty = MVT::getTypeForValueType(VT);
2485 } else if (SV) {
2486 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2487 assert(PT && "Value for load must be a pointer");
2488 Ty = PT->getElementType();
2489 }
2490 assert(Ty && "Could not get type information for load");
2491 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2492 }
Evan Cheng466685d2006-10-09 20:57:25 +00002493 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002494 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002495 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002496 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002497 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002498 ID.AddInteger(ISD::UNINDEXED);
2499 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002500 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002501 ID.AddInteger(Alignment);
2502 ID.AddInteger(isVolatile);
2503 void *IP = 0;
2504 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2505 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002506 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002507 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002508 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002509 AllNodes.push_back(N);
2510 return SDOperand(N, 0);
2511}
2512
Evan Cheng144d8f02006-11-09 17:55:04 +00002513SDOperand
2514SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2515 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002516 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002517 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2518 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002519 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002520 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002521 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002522 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002523 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002524 ID.AddInteger(AM);
2525 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002526 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002527 ID.AddInteger(LD->getAlignment());
2528 ID.AddInteger(LD->isVolatile());
2529 void *IP = 0;
2530 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2531 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002532 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002533 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002534 LD->getSrcValue(), LD->getSrcValueOffset(),
2535 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002536 CSEMap.InsertNode(N, IP);
2537 AllNodes.push_back(N);
2538 return SDOperand(N, 0);
2539}
2540
Jeff Cohend41b30d2006-11-05 19:31:28 +00002541SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002542 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002543 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002544 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002545
Dan Gohman575e2f42007-06-04 15:49:41 +00002546 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2547 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002548 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002549 Ty = MVT::getTypeForValueType(VT);
2550 } else if (SV) {
2551 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2552 assert(PT && "Value for store must be a pointer");
2553 Ty = PT->getElementType();
2554 }
2555 assert(Ty && "Could not get type information for store");
2556 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2557 }
Evan Chengad071e12006-10-05 22:57:11 +00002558 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002559 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002560 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002561 FoldingSetNodeID ID;
2562 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563 ID.AddInteger(ISD::UNINDEXED);
2564 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002565 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002566 ID.AddInteger(Alignment);
2567 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002568 void *IP = 0;
2569 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2570 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002571 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002572 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002573 CSEMap.InsertNode(N, IP);
2574 AllNodes.push_back(N);
2575 return SDOperand(N, 0);
2576}
2577
Jeff Cohend41b30d2006-11-05 19:31:28 +00002578SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002579 SDOperand Ptr, const Value *SV,
2580 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002581 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002582 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002583
2584 if (VT == SVT)
2585 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586
Duncan Sandsaf47b112007-10-16 09:56:48 +00002587 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2588 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002589 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2590 "Can't do FP-INT conversion!");
2591
Dan Gohman575e2f42007-06-04 15:49:41 +00002592 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2593 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002594 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002595 Ty = MVT::getTypeForValueType(VT);
2596 } else if (SV) {
2597 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2598 assert(PT && "Value for store must be a pointer");
2599 Ty = PT->getElementType();
2600 }
2601 assert(Ty && "Could not get type information for store");
2602 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2603 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002604 SDVTList VTs = getVTList(MVT::Other);
2605 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002606 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002607 FoldingSetNodeID ID;
2608 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002609 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002610 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002611 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002612 ID.AddInteger(Alignment);
2613 ID.AddInteger(isVolatile);
2614 void *IP = 0;
2615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2616 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002617 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002618 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002619 CSEMap.InsertNode(N, IP);
2620 AllNodes.push_back(N);
2621 return SDOperand(N, 0);
2622}
2623
Evan Cheng144d8f02006-11-09 17:55:04 +00002624SDOperand
2625SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2626 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002627 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2628 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2629 "Store is already a indexed store!");
2630 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2631 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2632 FoldingSetNodeID ID;
2633 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2634 ID.AddInteger(AM);
2635 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002636 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002637 ID.AddInteger(ST->getAlignment());
2638 ID.AddInteger(ST->isVolatile());
2639 void *IP = 0;
2640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2641 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002642 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002643 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002644 ST->getSrcValue(), ST->getSrcValueOffset(),
2645 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002646 CSEMap.InsertNode(N, IP);
2647 AllNodes.push_back(N);
2648 return SDOperand(N, 0);
2649}
2650
Nate Begemanacc398c2006-01-25 18:21:52 +00002651SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2652 SDOperand Chain, SDOperand Ptr,
2653 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002654 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002655 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002656}
2657
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002658SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002659 const SDOperand *Ops, unsigned NumOps) {
2660 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002661 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002662 case 1: return getNode(Opcode, VT, Ops[0]);
2663 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2664 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002665 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002666 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002667
Chris Lattneref847df2005-04-09 03:27:28 +00002668 switch (Opcode) {
2669 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002670 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002671 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002672 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2673 "LHS and RHS of condition must have same type!");
2674 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2675 "True and False arms of SelectCC must have same type!");
2676 assert(Ops[2].getValueType() == VT &&
2677 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002678 break;
2679 }
2680 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002681 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002682 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2683 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002684 break;
2685 }
Chris Lattneref847df2005-04-09 03:27:28 +00002686 }
2687
Chris Lattner385328c2005-05-14 07:42:29 +00002688 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002689 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002690 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002691 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002692 FoldingSetNodeID ID;
2693 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002694 void *IP = 0;
2695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2696 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002697 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002698 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002699 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002700 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002701 }
Chris Lattneref847df2005-04-09 03:27:28 +00002702 AllNodes.push_back(N);
2703 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002704}
2705
Chris Lattner89c34632005-05-14 06:20:26 +00002706SDOperand SelectionDAG::getNode(unsigned Opcode,
2707 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002708 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002709 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2710 Ops, NumOps);
2711}
2712
2713SDOperand SelectionDAG::getNode(unsigned Opcode,
2714 const MVT::ValueType *VTs, unsigned NumVTs,
2715 const SDOperand *Ops, unsigned NumOps) {
2716 if (NumVTs == 1)
2717 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002718 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2719}
2720
2721SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2722 const SDOperand *Ops, unsigned NumOps) {
2723 if (VTList.NumVTs == 1)
2724 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002725
Chris Lattner5f056bf2005-07-10 01:55:33 +00002726 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002727 // FIXME: figure out how to safely handle things like
2728 // int foo(int x) { return 1 << (x & 255); }
2729 // int bar() { return foo(256); }
2730#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002731 case ISD::SRA_PARTS:
2732 case ISD::SRL_PARTS:
2733 case ISD::SHL_PARTS:
2734 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002735 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002736 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2737 else if (N3.getOpcode() == ISD::AND)
2738 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2739 // If the and is only masking out bits that cannot effect the shift,
2740 // eliminate the and.
2741 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2742 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2743 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2744 }
2745 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002746#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002747 }
Chris Lattner89c34632005-05-14 06:20:26 +00002748
Chris Lattner43247a12005-08-25 19:12:10 +00002749 // Memoize the node unless it returns a flag.
2750 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002751 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002752 FoldingSetNodeID ID;
2753 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002754 void *IP = 0;
2755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2756 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002757 if (NumOps == 1)
2758 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2759 else if (NumOps == 2)
2760 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2761 else if (NumOps == 3)
2762 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2763 else
2764 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002765 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002766 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002767 if (NumOps == 1)
2768 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2769 else if (NumOps == 2)
2770 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2771 else if (NumOps == 3)
2772 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2773 else
2774 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002775 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002776 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002777 return SDOperand(N, 0);
2778}
2779
Dan Gohman08ce9762007-10-08 15:49:58 +00002780SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2781 return getNode(Opcode, VTList, 0, 0);
2782}
2783
2784SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2785 SDOperand N1) {
2786 SDOperand Ops[] = { N1 };
2787 return getNode(Opcode, VTList, Ops, 1);
2788}
2789
2790SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2791 SDOperand N1, SDOperand N2) {
2792 SDOperand Ops[] = { N1, N2 };
2793 return getNode(Opcode, VTList, Ops, 2);
2794}
2795
2796SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2797 SDOperand N1, SDOperand N2, SDOperand N3) {
2798 SDOperand Ops[] = { N1, N2, N3 };
2799 return getNode(Opcode, VTList, Ops, 3);
2800}
2801
2802SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2803 SDOperand N1, SDOperand N2, SDOperand N3,
2804 SDOperand N4) {
2805 SDOperand Ops[] = { N1, N2, N3, N4 };
2806 return getNode(Opcode, VTList, Ops, 4);
2807}
2808
2809SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2810 SDOperand N1, SDOperand N2, SDOperand N3,
2811 SDOperand N4, SDOperand N5) {
2812 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2813 return getNode(Opcode, VTList, Ops, 5);
2814}
2815
Chris Lattner70046e92006-08-15 17:46:01 +00002816SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002817 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002818}
2819
Chris Lattner70046e92006-08-15 17:46:01 +00002820SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002821 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2822 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002823 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002824 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002825 }
2826 std::vector<MVT::ValueType> V;
2827 V.push_back(VT1);
2828 V.push_back(VT2);
2829 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002830 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002831}
Chris Lattner70046e92006-08-15 17:46:01 +00002832SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2833 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002834 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002835 E = VTList.end(); I != E; ++I) {
2836 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2837 (*I)[2] == VT3)
2838 return makeVTList(&(*I)[0], 3);
2839 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002840 std::vector<MVT::ValueType> V;
2841 V.push_back(VT1);
2842 V.push_back(VT2);
2843 V.push_back(VT3);
2844 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002845 return makeVTList(&(*VTList.begin())[0], 3);
2846}
2847
2848SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2849 switch (NumVTs) {
2850 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002851 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002852 case 2: return getVTList(VTs[0], VTs[1]);
2853 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2854 default: break;
2855 }
2856
2857 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2858 E = VTList.end(); I != E; ++I) {
2859 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2860
2861 bool NoMatch = false;
2862 for (unsigned i = 2; i != NumVTs; ++i)
2863 if (VTs[i] != (*I)[i]) {
2864 NoMatch = true;
2865 break;
2866 }
2867 if (!NoMatch)
2868 return makeVTList(&*I->begin(), NumVTs);
2869 }
2870
2871 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2872 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002873}
2874
2875
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002876/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2877/// specified operands. If the resultant node already exists in the DAG,
2878/// this does not modify the specified node, instead it returns the node that
2879/// already exists. If the resultant node does not exist in the DAG, the
2880/// input node is returned. As a degenerate case, if you specify the same
2881/// input operands as the node already has, the input node is returned.
2882SDOperand SelectionDAG::
2883UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2884 SDNode *N = InN.Val;
2885 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2886
2887 // Check to see if there is no change.
2888 if (Op == N->getOperand(0)) return InN;
2889
2890 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002891 void *InsertPos = 0;
2892 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2893 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002894
2895 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002896 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002897 RemoveNodeFromCSEMaps(N);
2898
2899 // Now we update the operands.
2900 N->OperandList[0].Val->removeUser(N);
2901 Op.Val->addUser(N);
2902 N->OperandList[0] = Op;
2903
2904 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002905 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002906 return InN;
2907}
2908
2909SDOperand SelectionDAG::
2910UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2911 SDNode *N = InN.Val;
2912 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2913
2914 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002915 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2916 return InN; // No operands changed, just return the input node.
2917
2918 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002919 void *InsertPos = 0;
2920 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2921 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002922
2923 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002924 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002925 RemoveNodeFromCSEMaps(N);
2926
2927 // Now we update the operands.
2928 if (N->OperandList[0] != Op1) {
2929 N->OperandList[0].Val->removeUser(N);
2930 Op1.Val->addUser(N);
2931 N->OperandList[0] = Op1;
2932 }
2933 if (N->OperandList[1] != Op2) {
2934 N->OperandList[1].Val->removeUser(N);
2935 Op2.Val->addUser(N);
2936 N->OperandList[1] = Op2;
2937 }
2938
2939 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002940 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002941 return InN;
2942}
2943
2944SDOperand SelectionDAG::
2945UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002946 SDOperand Ops[] = { Op1, Op2, Op3 };
2947 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002948}
2949
2950SDOperand SelectionDAG::
2951UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2952 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002953 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2954 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002955}
2956
2957SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002958UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2959 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002960 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2961 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002962}
2963
2964
2965SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002966UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002967 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002968 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002969 "Update with wrong number of operands");
2970
2971 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002972 bool AnyChange = false;
2973 for (unsigned i = 0; i != NumOps; ++i) {
2974 if (Ops[i] != N->getOperand(i)) {
2975 AnyChange = true;
2976 break;
2977 }
2978 }
2979
2980 // No operands changed, just return the input node.
2981 if (!AnyChange) return InN;
2982
2983 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002984 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002985 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002986 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002987
2988 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002989 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002990 RemoveNodeFromCSEMaps(N);
2991
2992 // Now we update the operands.
2993 for (unsigned i = 0; i != NumOps; ++i) {
2994 if (N->OperandList[i] != Ops[i]) {
2995 N->OperandList[i].Val->removeUser(N);
2996 Ops[i].Val->addUser(N);
2997 N->OperandList[i] = Ops[i];
2998 }
2999 }
3000
3001 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003002 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003003 return InN;
3004}
3005
3006
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003007/// MorphNodeTo - This frees the operands of the current node, resets the
3008/// opcode, types, and operands to the specified value. This should only be
3009/// used by the SelectionDAG class.
3010void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3011 const SDOperand *Ops, unsigned NumOps) {
3012 NodeType = Opc;
3013 ValueList = L.VTs;
3014 NumValues = L.NumVTs;
3015
3016 // Clear the operands list, updating used nodes to remove this from their
3017 // use list.
3018 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3019 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003020
3021 // If NumOps is larger than the # of operands we currently have, reallocate
3022 // the operand list.
3023 if (NumOps > NumOperands) {
3024 if (OperandsNeedDelete)
3025 delete [] OperandList;
3026 OperandList = new SDOperand[NumOps];
3027 OperandsNeedDelete = true;
3028 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003029
3030 // Assign the new operands.
3031 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003032
3033 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3034 OperandList[i] = Ops[i];
3035 SDNode *N = OperandList[i].Val;
3036 N->Uses.push_back(this);
3037 }
3038}
Chris Lattner149c58c2005-08-16 18:17:10 +00003039
3040/// SelectNodeTo - These are used for target selectors to *mutate* the
3041/// specified node to have the specified return type, Target opcode, and
3042/// operands. Note that target opcodes are stored as
3043/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003044///
3045/// Note that SelectNodeTo returns the resultant node. If there is already a
3046/// node of the specified opcode and operands, it returns that node instead of
3047/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003048SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3049 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003050 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003051 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003052 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003053 void *IP = 0;
3054 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003055 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003056
Chris Lattner7651fa42005-08-24 23:00:29 +00003057 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003058
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003059 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003060
Chris Lattner4a283e92006-08-11 18:38:11 +00003061 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003062 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003063}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003064
Evan Cheng95514ba2006-08-26 08:00:10 +00003065SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3066 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003067 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003068 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003069 SDOperand Ops[] = { Op1 };
3070
Jim Laskey583bd472006-10-27 23:46:08 +00003071 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003072 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003073 void *IP = 0;
3074 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003075 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003076
Chris Lattner149c58c2005-08-16 18:17:10 +00003077 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003078 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003079 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003080 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003081}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003082
Evan Cheng95514ba2006-08-26 08:00:10 +00003083SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3084 MVT::ValueType VT, SDOperand Op1,
3085 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003086 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003087 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003088 SDOperand Ops[] = { Op1, Op2 };
3089
Jim Laskey583bd472006-10-27 23:46:08 +00003090 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003091 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003092 void *IP = 0;
3093 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003094 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003095
Chris Lattner149c58c2005-08-16 18:17:10 +00003096 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003097
Chris Lattner63e3f142007-02-04 07:28:00 +00003098 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003099
Chris Lattnera5682852006-08-07 23:03:03 +00003100 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003101 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003102}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003103
Evan Cheng95514ba2006-08-26 08:00:10 +00003104SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3105 MVT::ValueType VT, SDOperand Op1,
3106 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003107 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003108 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003109 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003110 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003111 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003112 void *IP = 0;
3113 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003114 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003115
Chris Lattner149c58c2005-08-16 18:17:10 +00003116 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003117
Chris Lattner63e3f142007-02-04 07:28:00 +00003118 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003119
Chris Lattnera5682852006-08-07 23:03:03 +00003120 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003121 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003122}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003123
Evan Cheng95514ba2006-08-26 08:00:10 +00003124SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003125 MVT::ValueType VT, const SDOperand *Ops,
3126 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003127 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003128 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003129 FoldingSetNodeID ID;
3130 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003131 void *IP = 0;
3132 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003133 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003134
Chris Lattner6b09a292005-08-21 18:49:33 +00003135 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003136 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003137
Chris Lattnera5682852006-08-07 23:03:03 +00003138 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003139 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003140}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003141
Evan Cheng95514ba2006-08-26 08:00:10 +00003142SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3143 MVT::ValueType VT1, MVT::ValueType VT2,
3144 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003145 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003146 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003147 SDOperand Ops[] = { Op1, Op2 };
3148 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003149 void *IP = 0;
3150 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003151 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003152
Chris Lattner0fb094f2005-11-19 01:44:53 +00003153 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003154 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003155 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003156 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003157}
3158
Evan Cheng95514ba2006-08-26 08:00:10 +00003159SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3160 MVT::ValueType VT1, MVT::ValueType VT2,
3161 SDOperand Op1, SDOperand Op2,
3162 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003163 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003164 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003165 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003166 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003167 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003168 void *IP = 0;
3169 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003170 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003171
Chris Lattner0fb094f2005-11-19 01:44:53 +00003172 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003173
Chris Lattner63e3f142007-02-04 07:28:00 +00003174 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003175 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003176 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003177}
3178
Chris Lattner0fb094f2005-11-19 01:44:53 +00003179
Evan Cheng6ae46c42006-02-09 07:15:23 +00003180/// getTargetNode - These are used for target selectors to create a new node
3181/// with specified return type(s), target opcode, and operands.
3182///
3183/// Note that getTargetNode returns the resultant node. If there is already a
3184/// node of the specified opcode and operands, it returns that node instead of
3185/// the current one.
3186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3187 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3188}
3189SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3190 SDOperand Op1) {
3191 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3192}
3193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3194 SDOperand Op1, SDOperand Op2) {
3195 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3196}
3197SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003198 SDOperand Op1, SDOperand Op2,
3199 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3201}
3202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003203 const SDOperand *Ops, unsigned NumOps) {
3204 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003205}
3206SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003207 MVT::ValueType VT2) {
3208 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3209 SDOperand Op;
3210 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3211}
3212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003213 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003214 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003215 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003216}
3217SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003218 MVT::ValueType VT2, SDOperand Op1,
3219 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003220 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003221 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003222 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003223}
3224SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003225 MVT::ValueType VT2, SDOperand Op1,
3226 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003227 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003228 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003229 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003230}
Evan Cheng694481e2006-08-27 08:08:54 +00003231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3232 MVT::ValueType VT2,
3233 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003234 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003235 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003236}
3237SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3238 MVT::ValueType VT2, MVT::ValueType VT3,
3239 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003240 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003241 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003242 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003243}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003244SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3245 MVT::ValueType VT2, MVT::ValueType VT3,
3246 SDOperand Op1, SDOperand Op2,
3247 SDOperand Op3) {
3248 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3249 SDOperand Ops[] = { Op1, Op2, Op3 };
3250 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3251}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003252SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003253 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003254 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003255 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3256 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003257}
Evan Cheng05e69c12007-09-12 23:39:49 +00003258SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3259 MVT::ValueType VT2, MVT::ValueType VT3,
3260 MVT::ValueType VT4,
3261 const SDOperand *Ops, unsigned NumOps) {
3262 std::vector<MVT::ValueType> VTList;
3263 VTList.push_back(VT1);
3264 VTList.push_back(VT2);
3265 VTList.push_back(VT3);
3266 VTList.push_back(VT4);
3267 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3268 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3269}
Evan Cheng39305cf2007-10-05 01:10:49 +00003270SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3271 std::vector<MVT::ValueType> &ResultTys,
3272 const SDOperand *Ops, unsigned NumOps) {
3273 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3274 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3275 Ops, NumOps).Val;
3276}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003277
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003278
Evan Cheng99157a02006-08-07 22:13:29 +00003279/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003280/// This can cause recursive merging of nodes in the DAG.
3281///
Chris Lattner11d049c2008-02-03 03:35:22 +00003282/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003283///
Chris Lattner11d049c2008-02-03 03:35:22 +00003284void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003285 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003286 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003287 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003288 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003289 assert(From != To.Val && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003290
Chris Lattner8b8749f2005-08-17 19:00:20 +00003291 while (!From->use_empty()) {
3292 // Process users until they are all gone.
3293 SDNode *U = *From->use_begin();
3294
3295 // This node is about to morph, remove its old self from the CSE maps.
3296 RemoveNodeFromCSEMaps(U);
3297
Chris Lattner65113b22005-11-08 22:07:03 +00003298 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3299 I != E; ++I)
3300 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003301 From->removeUser(U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003302 *I = To;
3303 To.Val->addUser(U);
Chris Lattner8b52f212005-08-26 18:36:28 +00003304 }
3305
3306 // Now that we have modified U, add it back to the CSE maps. If it already
3307 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003308 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003309 ReplaceAllUsesWith(U, Existing, UpdateListener);
3310 // U is now dead. Inform the listener if it exists and delete it.
3311 if (UpdateListener)
3312 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003313 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003314 } else {
3315 // If the node doesn't already exist, we updated it. Inform a listener if
3316 // it exists.
3317 if (UpdateListener)
3318 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003319 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003320 }
3321}
3322
3323/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3324/// This can cause recursive merging of nodes in the DAG.
3325///
3326/// This version assumes From/To have matching types and numbers of result
3327/// values.
3328///
Chris Lattner1e111c72005-09-07 05:37:01 +00003329void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003330 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003331 assert(From != To && "Cannot replace uses of with self");
3332 assert(From->getNumValues() == To->getNumValues() &&
3333 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003334 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003335 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3336 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003337
3338 while (!From->use_empty()) {
3339 // Process users until they are all gone.
3340 SDNode *U = *From->use_begin();
3341
3342 // This node is about to morph, remove its old self from the CSE maps.
3343 RemoveNodeFromCSEMaps(U);
3344
Chris Lattner65113b22005-11-08 22:07:03 +00003345 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3346 I != E; ++I)
3347 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003348 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003349 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003350 To->addUser(U);
3351 }
3352
3353 // Now that we have modified U, add it back to the CSE maps. If it already
3354 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003355 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003356 ReplaceAllUsesWith(U, Existing, UpdateListener);
3357 // U is now dead. Inform the listener if it exists and delete it.
3358 if (UpdateListener)
3359 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003360 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003361 } else {
3362 // If the node doesn't already exist, we updated it. Inform a listener if
3363 // it exists.
3364 if (UpdateListener)
3365 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003366 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003367 }
3368}
3369
Chris Lattner8b52f212005-08-26 18:36:28 +00003370/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3371/// This can cause recursive merging of nodes in the DAG.
3372///
3373/// This version can replace From with any result values. To must match the
3374/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003375void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003376 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003377 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003378 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003379 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003380
3381 while (!From->use_empty()) {
3382 // Process users until they are all gone.
3383 SDNode *U = *From->use_begin();
3384
3385 // This node is about to morph, remove its old self from the CSE maps.
3386 RemoveNodeFromCSEMaps(U);
3387
Chris Lattner65113b22005-11-08 22:07:03 +00003388 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3389 I != E; ++I)
3390 if (I->Val == From) {
3391 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003392 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003393 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003394 ToOp.Val->addUser(U);
3395 }
3396
3397 // Now that we have modified U, add it back to the CSE maps. If it already
3398 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003399 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003400 ReplaceAllUsesWith(U, Existing, UpdateListener);
3401 // U is now dead. Inform the listener if it exists and delete it.
3402 if (UpdateListener)
3403 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003404 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003405 } else {
3406 // If the node doesn't already exist, we updated it. Inform a listener if
3407 // it exists.
3408 if (UpdateListener)
3409 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003410 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003411 }
3412}
3413
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003414namespace {
3415 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3416 /// any deleted nodes from the set passed into its constructor and recursively
3417 /// notifies another update listener if specified.
3418 class ChainedSetUpdaterListener :
3419 public SelectionDAG::DAGUpdateListener {
3420 SmallSetVector<SDNode*, 16> &Set;
3421 SelectionDAG::DAGUpdateListener *Chain;
3422 public:
3423 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3424 SelectionDAG::DAGUpdateListener *chain)
3425 : Set(set), Chain(chain) {}
3426
3427 virtual void NodeDeleted(SDNode *N) {
3428 Set.remove(N);
3429 if (Chain) Chain->NodeDeleted(N);
3430 }
3431 virtual void NodeUpdated(SDNode *N) {
3432 if (Chain) Chain->NodeUpdated(N);
3433 }
3434 };
3435}
3436
Chris Lattner012f2412006-02-17 21:58:01 +00003437/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3438/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003439/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003440void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003441 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003442 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003443
Chris Lattner012f2412006-02-17 21:58:01 +00003444 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003445 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003446 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003447 return;
3448 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003449
3450 if (From.use_empty()) return;
3451
Chris Lattnercf5640b2007-02-04 00:14:31 +00003452 // Get all of the users of From.Val. We want these in a nice,
3453 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3454 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003455
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003456 // When one of the recursive merges deletes nodes from the graph, we need to
3457 // make sure that UpdateListener is notified *and* that the node is removed
3458 // from Users if present. CSUL does this.
3459 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003460
Chris Lattner012f2412006-02-17 21:58:01 +00003461 while (!Users.empty()) {
3462 // We know that this user uses some value of From. If it is the right
3463 // value, update it.
3464 SDNode *User = Users.back();
3465 Users.pop_back();
3466
Chris Lattner01d029b2007-10-15 06:10:22 +00003467 // Scan for an operand that matches From.
3468 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3469 for (; Op != E; ++Op)
3470 if (*Op == From) break;
3471
3472 // If there are no matches, the user must use some other result of From.
3473 if (Op == E) continue;
3474
3475 // Okay, we know this user needs to be updated. Remove its old self
3476 // from the CSE maps.
3477 RemoveNodeFromCSEMaps(User);
3478
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003479 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003480 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003481 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003482 From.Val->removeUser(User);
3483 *Op = To;
3484 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003485 }
3486 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003487
3488 // Now that we have modified User, add it back to the CSE maps. If it
3489 // already exists there, recursively merge the results together.
3490 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003491 if (!Existing) {
3492 if (UpdateListener) UpdateListener->NodeUpdated(User);
3493 continue; // Continue on to next user.
3494 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003495
3496 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003497 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003498 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003499 // can cause deletion of nodes that used the old value. To handle this, we
3500 // use CSUL to remove them from the Users set.
3501 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003502
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003503 // User is now dead. Notify a listener if present.
3504 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003505 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003506 }
3507}
3508
Chris Lattner7b2880c2005-08-24 22:44:39 +00003509
Evan Chenge6f35d82006-08-01 08:20:41 +00003510/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3511/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003512unsigned SelectionDAG::AssignNodeIds() {
3513 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003514 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3515 SDNode *N = I;
3516 N->setNodeId(Id++);
3517 }
3518 return Id;
3519}
3520
Evan Chenge6f35d82006-08-01 08:20:41 +00003521/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003522/// based on their topological order. It returns the maximum id and a vector
3523/// of the SDNodes* in assigned order by reference.
3524unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003525 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003526 std::vector<unsigned> InDegree(DAGSize);
3527 std::vector<SDNode*> Sources;
3528
3529 // Use a two pass approach to avoid using a std::map which is slow.
3530 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003531 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3532 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003533 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003534 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003535 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003536 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003537 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003538 }
3539
Evan Cheng99157a02006-08-07 22:13:29 +00003540 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003541 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003542 SDNode *N = Sources.back();
3543 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003544 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003545 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3546 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003547 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003548 if (Degree == 0)
3549 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003550 }
3551 }
3552
Evan Chengc384d6c2006-08-02 22:00:34 +00003553 // Second pass, assign the actual topological order as node ids.
3554 Id = 0;
3555 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3556 TI != TE; ++TI)
3557 (*TI)->setNodeId(Id++);
3558
3559 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003560}
3561
3562
Evan Cheng091cba12006-07-27 06:39:06 +00003563
Jim Laskey58b968b2005-08-17 20:08:02 +00003564//===----------------------------------------------------------------------===//
3565// SDNode Class
3566//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003567
Chris Lattner917d2c92006-07-19 00:00:37 +00003568// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003569void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003570void UnarySDNode::ANCHOR() {}
3571void BinarySDNode::ANCHOR() {}
3572void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003573void HandleSDNode::ANCHOR() {}
3574void StringSDNode::ANCHOR() {}
3575void ConstantSDNode::ANCHOR() {}
3576void ConstantFPSDNode::ANCHOR() {}
3577void GlobalAddressSDNode::ANCHOR() {}
3578void FrameIndexSDNode::ANCHOR() {}
3579void JumpTableSDNode::ANCHOR() {}
3580void ConstantPoolSDNode::ANCHOR() {}
3581void BasicBlockSDNode::ANCHOR() {}
3582void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003583void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003584void RegisterSDNode::ANCHOR() {}
3585void ExternalSymbolSDNode::ANCHOR() {}
3586void CondCodeSDNode::ANCHOR() {}
3587void VTSDNode::ANCHOR() {}
3588void LoadSDNode::ANCHOR() {}
3589void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003590void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003591
Chris Lattner48b85922007-02-04 02:41:42 +00003592HandleSDNode::~HandleSDNode() {
3593 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003594 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003595}
3596
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003597GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3598 MVT::ValueType VT, int o)
3599 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003600 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003601 // Thread Local
3602 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3603 // Non Thread Local
3604 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3605 getSDVTList(VT)), Offset(o) {
3606 TheGlobal = const_cast<GlobalValue*>(GA);
3607}
Chris Lattner48b85922007-02-04 02:41:42 +00003608
Dan Gohman69de1932008-02-06 22:27:42 +00003609/// getMemOperand - Return a MemOperand object describing the memory
3610/// reference performed by this load or store.
3611MemOperand LSBaseSDNode::getMemOperand() const {
3612 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3613 int Flags =
3614 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3615 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3616
3617 // Check if the load references a frame index, and does not have
3618 // an SV attached.
3619 const FrameIndexSDNode *FI =
3620 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3621 if (!getSrcValue() && FI)
Dan Gohman3069b872008-02-07 18:41:25 +00003622 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
Dan Gohman69de1932008-02-06 22:27:42 +00003623 FI->getIndex(), Size, Alignment);
3624 else
3625 return MemOperand(getSrcValue(), Flags,
3626 getSrcValueOffset(), Size, Alignment);
3627}
3628
Jim Laskey583bd472006-10-27 23:46:08 +00003629/// Profile - Gather unique data for the node.
3630///
3631void SDNode::Profile(FoldingSetNodeID &ID) {
3632 AddNodeIDNode(ID, this);
3633}
3634
Chris Lattnera3255112005-11-08 23:30:28 +00003635/// getValueTypeList - Return a pointer to the specified value type.
3636///
Dan Gohman547ca532008-02-08 03:26:46 +00003637const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003638 if (MVT::isExtendedVT(VT)) {
3639 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00003640 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00003641 } else {
3642 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3643 VTs[VT] = VT;
3644 return &VTs[VT];
3645 }
Chris Lattnera3255112005-11-08 23:30:28 +00003646}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003647
Chris Lattner5c884562005-01-12 18:37:47 +00003648/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3649/// indicated value. This method ignores uses of other values defined by this
3650/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003651bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003652 assert(Value < getNumValues() && "Bad value!");
3653
3654 // If there is only one value, this is easy.
3655 if (getNumValues() == 1)
3656 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003657 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003658
Evan Cheng4ee62112006-02-05 06:29:23 +00003659 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003660
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003661 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003662
Chris Lattnerf83482d2006-08-16 20:59:32 +00003663 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003664 SDNode *User = *UI;
3665 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003666 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003667 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3668 if (User->getOperand(i) == TheValue) {
3669 if (NUses == 0)
3670 return false; // too many uses
3671 --NUses;
3672 }
3673 }
3674
3675 // Found exactly the right number of uses?
3676 return NUses == 0;
3677}
3678
3679
Evan Cheng33d55952007-08-02 05:29:38 +00003680/// hasAnyUseOfValue - Return true if there are any use of the indicated
3681/// value. This method ignores uses of other values defined by this operation.
3682bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3683 assert(Value < getNumValues() && "Bad value!");
3684
Dan Gohman30359592008-01-29 13:02:09 +00003685 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003686
3687 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3688
3689 SmallPtrSet<SDNode*, 32> UsersHandled;
3690
3691 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3692 SDNode *User = *UI;
3693 if (User->getNumOperands() == 1 ||
3694 UsersHandled.insert(User)) // First time we've seen this?
3695 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3696 if (User->getOperand(i) == TheValue) {
3697 return true;
3698 }
3699 }
3700
3701 return false;
3702}
3703
3704
Evan Cheng917be682008-03-04 00:41:45 +00003705/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00003706///
Evan Cheng917be682008-03-04 00:41:45 +00003707bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00003708 bool Seen = false;
3709 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3710 SDNode *User = *I;
3711 if (User == this)
3712 Seen = true;
3713 else
3714 return false;
3715 }
3716
3717 return Seen;
3718}
3719
Evan Chenge6e97e62006-11-03 07:31:32 +00003720/// isOperand - Return true if this node is an operand of N.
3721///
Evan Cheng917be682008-03-04 00:41:45 +00003722bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00003723 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3724 if (*this == N->getOperand(i))
3725 return true;
3726 return false;
3727}
3728
Evan Cheng917be682008-03-04 00:41:45 +00003729bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003730 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3731 if (this == N->OperandList[i].Val)
3732 return true;
3733 return false;
3734}
Evan Cheng4ee62112006-02-05 06:29:23 +00003735
Chris Lattner572dee72008-01-16 05:49:24 +00003736/// reachesChainWithoutSideEffects - Return true if this operand (which must
3737/// be a chain) reaches the specified operand without crossing any
3738/// side-effecting instructions. In practice, this looks through token
3739/// factors and non-volatile loads. In order to remain efficient, this only
3740/// looks a couple of nodes in, it does not do an exhaustive search.
3741bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3742 unsigned Depth) const {
3743 if (*this == Dest) return true;
3744
3745 // Don't search too deeply, we just want to be able to see through
3746 // TokenFactor's etc.
3747 if (Depth == 0) return false;
3748
3749 // If this is a token factor, all inputs to the TF happen in parallel. If any
3750 // of the operands of the TF reach dest, then we can do the xform.
3751 if (getOpcode() == ISD::TokenFactor) {
3752 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3753 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3754 return true;
3755 return false;
3756 }
3757
3758 // Loads don't have side effects, look through them.
3759 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3760 if (!Ld->isVolatile())
3761 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3762 }
3763 return false;
3764}
3765
3766
Evan Chengc5fc57d2006-11-03 03:05:24 +00003767static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003768 SmallPtrSet<SDNode *, 32> &Visited) {
3769 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003770 return;
3771
3772 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3773 SDNode *Op = N->getOperand(i).Val;
3774 if (Op == P) {
3775 found = true;
3776 return;
3777 }
3778 findPredecessor(Op, P, found, Visited);
3779 }
3780}
3781
Evan Cheng917be682008-03-04 00:41:45 +00003782/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00003783/// is either an operand of N or it can be reached by recursively traversing
3784/// up the operands.
3785/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00003786bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003787 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003788 bool found = false;
3789 findPredecessor(N, this, found, Visited);
3790 return found;
3791}
3792
Evan Chengc5484282006-10-04 00:56:09 +00003793uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3794 assert(Num < NumOperands && "Invalid child # of SDNode!");
3795 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3796}
3797
Reid Spencer577cc322007-04-01 07:32:19 +00003798std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003799 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003800 default:
3801 if (getOpcode() < ISD::BUILTIN_OP_END)
3802 return "<<Unknown DAG Node>>";
3803 else {
Evan Cheng72261582005-12-20 06:22:03 +00003804 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003805 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003806 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003807 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003808
Evan Cheng72261582005-12-20 06:22:03 +00003809 TargetLowering &TLI = G->getTargetLoweringInfo();
3810 const char *Name =
3811 TLI.getTargetNodeName(getOpcode());
3812 if (Name) return Name;
3813 }
3814
3815 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003816 }
3817
Evan Cheng27b7db52008-03-08 00:58:38 +00003818 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00003819 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003820 case ISD::ATOMIC_LCS: return "AtomicLCS";
3821 case ISD::ATOMIC_LAS: return "AtomicLAS";
3822 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00003823 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003824 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003825 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00003826 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003827 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003828 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003829 case ISD::AssertSext: return "AssertSext";
3830 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003831
3832 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003833 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003834 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003835 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003836
3837 case ISD::Constant: return "Constant";
3838 case ISD::ConstantFP: return "ConstantFP";
3839 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003840 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003841 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003842 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003843 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003844 case ISD::RETURNADDR: return "RETURNADDR";
3845 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003846 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003847 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3848 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003849 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003850 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003851 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003852 case ISD::INTRINSIC_WO_CHAIN: {
3853 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3854 return Intrinsic::getName((Intrinsic::ID)IID);
3855 }
3856 case ISD::INTRINSIC_VOID:
3857 case ISD::INTRINSIC_W_CHAIN: {
3858 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003859 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003860 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003861
Chris Lattnerb2827b02006-03-19 00:52:58 +00003862 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003863 case ISD::TargetConstant: return "TargetConstant";
3864 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003865 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003866 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003867 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003868 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003869 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003870 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003871
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003872 case ISD::CopyToReg: return "CopyToReg";
3873 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003874 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003875 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003876 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003877 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003878 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003879 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003880 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003881 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003882
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003883 // Unary operators
3884 case ISD::FABS: return "fabs";
3885 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003886 case ISD::FSQRT: return "fsqrt";
3887 case ISD::FSIN: return "fsin";
3888 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003889 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003890 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003891
3892 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003893 case ISD::ADD: return "add";
3894 case ISD::SUB: return "sub";
3895 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003896 case ISD::MULHU: return "mulhu";
3897 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003898 case ISD::SDIV: return "sdiv";
3899 case ISD::UDIV: return "udiv";
3900 case ISD::SREM: return "srem";
3901 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003902 case ISD::SMUL_LOHI: return "smul_lohi";
3903 case ISD::UMUL_LOHI: return "umul_lohi";
3904 case ISD::SDIVREM: return "sdivrem";
3905 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003906 case ISD::AND: return "and";
3907 case ISD::OR: return "or";
3908 case ISD::XOR: return "xor";
3909 case ISD::SHL: return "shl";
3910 case ISD::SRA: return "sra";
3911 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003912 case ISD::ROTL: return "rotl";
3913 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003914 case ISD::FADD: return "fadd";
3915 case ISD::FSUB: return "fsub";
3916 case ISD::FMUL: return "fmul";
3917 case ISD::FDIV: return "fdiv";
3918 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003919 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003920 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003921
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003922 case ISD::SETCC: return "setcc";
3923 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003924 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003925 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003926 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003927 case ISD::CONCAT_VECTORS: return "concat_vectors";
3928 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003929 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003930 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003931 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003932 case ISD::ADDC: return "addc";
3933 case ISD::ADDE: return "adde";
3934 case ISD::SUBC: return "subc";
3935 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003936 case ISD::SHL_PARTS: return "shl_parts";
3937 case ISD::SRA_PARTS: return "sra_parts";
3938 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003939
3940 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3941 case ISD::INSERT_SUBREG: return "insert_subreg";
3942
Chris Lattner7f644642005-04-28 21:44:03 +00003943 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003944 case ISD::SIGN_EXTEND: return "sign_extend";
3945 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003946 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003947 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003948 case ISD::TRUNCATE: return "truncate";
3949 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003950 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003951 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003952 case ISD::FP_EXTEND: return "fp_extend";
3953
3954 case ISD::SINT_TO_FP: return "sint_to_fp";
3955 case ISD::UINT_TO_FP: return "uint_to_fp";
3956 case ISD::FP_TO_SINT: return "fp_to_sint";
3957 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003958 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003959
3960 // Control flow instructions
3961 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003962 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003963 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003964 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003965 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003966 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003967 case ISD::CALLSEQ_START: return "callseq_start";
3968 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003969
3970 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003971 case ISD::LOAD: return "load";
3972 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003973 case ISD::VAARG: return "vaarg";
3974 case ISD::VACOPY: return "vacopy";
3975 case ISD::VAEND: return "vaend";
3976 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003977 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003978 case ISD::EXTRACT_ELEMENT: return "extract_element";
3979 case ISD::BUILD_PAIR: return "build_pair";
3980 case ISD::STACKSAVE: return "stacksave";
3981 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003982 case ISD::TRAP: return "trap";
3983
Chris Lattner5a67afc2006-01-13 02:39:42 +00003984 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003985 case ISD::MEMSET: return "memset";
3986 case ISD::MEMCPY: return "memcpy";
3987 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003988
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003989 // Bit manipulation
3990 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003991 case ISD::CTPOP: return "ctpop";
3992 case ISD::CTTZ: return "cttz";
3993 case ISD::CTLZ: return "ctlz";
3994
Chris Lattner36ce6912005-11-29 06:21:05 +00003995 // Debug info
3996 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003997 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003998
Duncan Sands36397f52007-07-27 12:58:54 +00003999 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004000 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004001
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004002 case ISD::CONDCODE:
4003 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004004 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004005 case ISD::SETOEQ: return "setoeq";
4006 case ISD::SETOGT: return "setogt";
4007 case ISD::SETOGE: return "setoge";
4008 case ISD::SETOLT: return "setolt";
4009 case ISD::SETOLE: return "setole";
4010 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004011
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004012 case ISD::SETO: return "seto";
4013 case ISD::SETUO: return "setuo";
4014 case ISD::SETUEQ: return "setue";
4015 case ISD::SETUGT: return "setugt";
4016 case ISD::SETUGE: return "setuge";
4017 case ISD::SETULT: return "setult";
4018 case ISD::SETULE: return "setule";
4019 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004020
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004021 case ISD::SETEQ: return "seteq";
4022 case ISD::SETGT: return "setgt";
4023 case ISD::SETGE: return "setge";
4024 case ISD::SETLT: return "setlt";
4025 case ISD::SETLE: return "setle";
4026 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004027 }
4028 }
4029}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004030
Evan Cheng144d8f02006-11-09 17:55:04 +00004031const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004032 switch (AM) {
4033 default:
4034 return "";
4035 case ISD::PRE_INC:
4036 return "<pre-inc>";
4037 case ISD::PRE_DEC:
4038 return "<pre-dec>";
4039 case ISD::POST_INC:
4040 return "<post-inc>";
4041 case ISD::POST_DEC:
4042 return "<post-dec>";
4043 }
4044}
4045
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004046void SDNode::dump() const { dump(0); }
4047void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004048 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004049
4050 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004051 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004052 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004053 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004054 else
Bill Wendling832171c2006-12-07 20:04:42 +00004055 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004056 }
Bill Wendling832171c2006-12-07 20:04:42 +00004057 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004058
Bill Wendling832171c2006-12-07 20:04:42 +00004059 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004060 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004061 if (i) cerr << ", ";
4062 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004063 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004064 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004065 }
4066
Evan Chengce254432007-12-11 02:08:35 +00004067 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4068 SDNode *Mask = getOperand(2).Val;
4069 cerr << "<";
4070 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4071 if (i) cerr << ",";
4072 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4073 cerr << "u";
4074 else
4075 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4076 }
4077 cerr << ">";
4078 }
4079
Chris Lattnerc3aae252005-01-07 07:46:32 +00004080 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004081 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004082 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004083 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4084 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4085 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4086 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4087 else {
4088 cerr << "<APFloat(";
4089 CSDN->getValueAPF().convertToAPInt().dump();
4090 cerr << ")>";
4091 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004092 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004093 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004094 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004095 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004096 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004097 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004098 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004099 else
Bill Wendling832171c2006-12-07 20:04:42 +00004100 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004101 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004102 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004103 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004104 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004105 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004106 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004107 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004108 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004109 else
Bill Wendling832171c2006-12-07 20:04:42 +00004110 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004111 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004112 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004113 else
Bill Wendling832171c2006-12-07 20:04:42 +00004114 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004115 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004116 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004117 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4118 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004119 cerr << LBB->getName() << " ";
4120 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004121 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004122 if (G && R->getReg() &&
4123 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004124 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004125 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004126 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004127 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004128 } else if (const ExternalSymbolSDNode *ES =
4129 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004130 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004131 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4132 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004133 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004134 else
Dan Gohman69de1932008-02-06 22:27:42 +00004135 cerr << "<null>";
4136 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4137 if (M->MO.getValue())
4138 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4139 else
4140 cerr << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00004141 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004142 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004143 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004144 const Value *SrcValue = LD->getSrcValue();
4145 int SrcOffset = LD->getSrcValueOffset();
4146 cerr << " <";
4147 if (SrcValue)
4148 cerr << SrcValue;
4149 else
4150 cerr << "null";
4151 cerr << ":" << SrcOffset << ">";
4152
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004153 bool doExt = true;
4154 switch (LD->getExtensionType()) {
4155 default: doExt = false; break;
4156 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004157 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004158 break;
4159 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004160 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004161 break;
4162 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004163 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004164 break;
4165 }
4166 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004167 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004168
Evan Cheng144d8f02006-11-09 17:55:04 +00004169 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004170 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004171 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004172 if (LD->isVolatile())
4173 cerr << " <volatile>";
4174 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004175 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004176 const Value *SrcValue = ST->getSrcValue();
4177 int SrcOffset = ST->getSrcValueOffset();
4178 cerr << " <";
4179 if (SrcValue)
4180 cerr << SrcValue;
4181 else
4182 cerr << "null";
4183 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004184
4185 if (ST->isTruncatingStore())
4186 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004187 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004188
4189 const char *AM = getIndexedModeName(ST->getAddressingMode());
4190 if (*AM)
4191 cerr << " " << AM;
4192 if (ST->isVolatile())
4193 cerr << " <volatile>";
4194 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004195 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004196}
4197
Chris Lattnerde202b32005-11-09 23:47:37 +00004198static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004199 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4200 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004201 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004202 else
Bill Wendling832171c2006-12-07 20:04:42 +00004203 cerr << "\n" << std::string(indent+2, ' ')
4204 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004205
Chris Lattnerea946cd2005-01-09 20:38:33 +00004206
Bill Wendling832171c2006-12-07 20:04:42 +00004207 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004208 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004209}
4210
Chris Lattnerc3aae252005-01-07 07:46:32 +00004211void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004212 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004213 std::vector<const SDNode*> Nodes;
4214 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4215 I != E; ++I)
4216 Nodes.push_back(I);
4217
Chris Lattner49d24712005-01-09 20:26:36 +00004218 std::sort(Nodes.begin(), Nodes.end());
4219
4220 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004221 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004222 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004223 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004224
Jim Laskey26f7fa72006-10-17 19:33:52 +00004225 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004226
Bill Wendling832171c2006-12-07 20:04:42 +00004227 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004228}
4229
Evan Chengd6594ae2006-09-12 21:00:35 +00004230const Type *ConstantPoolSDNode::getType() const {
4231 if (isMachineConstantPoolEntry())
4232 return Val.MachineCPVal->getType();
4233 return Val.ConstVal->getType();
4234}