blob: f259b2b60d1165391bc65b40382ee1c44f46bbdf [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//===----------------------------------------------------------------------===//
Evan Cheng6397c642008-04-03 03:13:16 +000013
Chris Lattner78ec3112003-08-11 14:57:33 +000014#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.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000358 case ISD::ARG_FLAGS:
359 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
360 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 case ISD::TargetConstant:
362 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000363 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 break;
365 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000366 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000367 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000369 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000371 case ISD::GlobalAddress:
372 case ISD::TargetGlobalTLSAddress:
373 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
375 ID.AddPointer(GA->getGlobal());
376 ID.AddInteger(GA->getOffset());
377 break;
378 }
379 case ISD::BasicBlock:
380 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
381 break;
382 case ISD::Register:
383 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
384 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000385 case ISD::SRCVALUE:
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
387 break;
388 case ISD::MEMOPERAND: {
389 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
390 ID.AddPointer(MO.getValue());
391 ID.AddInteger(MO.getFlags());
392 ID.AddInteger(MO.getOffset());
393 ID.AddInteger(MO.getSize());
394 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000395 break;
396 }
397 case ISD::FrameIndex:
398 case ISD::TargetFrameIndex:
399 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
400 break;
401 case ISD::JumpTable:
402 case ISD::TargetJumpTable:
403 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
404 break;
405 case ISD::ConstantPool:
406 case ISD::TargetConstantPool: {
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
408 ID.AddInteger(CP->getAlignment());
409 ID.AddInteger(CP->getOffset());
410 if (CP->isMachineConstantPoolEntry())
411 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
412 else
413 ID.AddPointer(CP->getConstVal());
414 break;
415 }
416 case ISD::LOAD: {
417 LoadSDNode *LD = cast<LoadSDNode>(N);
418 ID.AddInteger(LD->getAddressingMode());
419 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000420 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 ID.AddInteger(LD->getAlignment());
422 ID.AddInteger(LD->isVolatile());
423 break;
424 }
425 case ISD::STORE: {
426 StoreSDNode *ST = cast<StoreSDNode>(N);
427 ID.AddInteger(ST->getAddressingMode());
428 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000429 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 ID.AddInteger(ST->getAlignment());
431 ID.AddInteger(ST->isVolatile());
432 break;
433 }
Jim Laskey583bd472006-10-27 23:46:08 +0000434 }
435}
436
437//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000438// SelectionDAG Class
439//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000440
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000442/// SelectionDAG.
443void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444 // Create a dummy node (which is not added to allnodes), that adds a reference
445 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000446 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000447
Chris Lattner190a4182006-08-04 17:45:20 +0000448 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000449
Chris Lattner190a4182006-08-04 17:45:20 +0000450 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000451 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000452 if (I->use_empty())
453 DeadNodes.push_back(I);
454
455 // Process the worklist, deleting the nodes and adding their uses to the
456 // worklist.
457 while (!DeadNodes.empty()) {
458 SDNode *N = DeadNodes.back();
459 DeadNodes.pop_back();
460
461 // Take the node out of the appropriate CSE map.
462 RemoveNodeFromCSEMaps(N);
463
464 // Next, brutally remove the operand list. This is safe to do, as there are
465 // no cycles in the graph.
466 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
467 SDNode *Operand = I->Val;
Evan Cheng6397c642008-04-03 03:13:16 +0000468 Operand->removeUser(N);
Chris Lattner190a4182006-08-04 17:45:20 +0000469
470 // Now that we removed this operand, see if there are no uses of it left.
471 if (Operand->use_empty())
472 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000473 }
Evan Cheng6397c642008-04-03 03:13:16 +0000474 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000475 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000476 N->OperandList = 0;
477 N->NumOperands = 0;
478
479 // Finally, remove N itself.
480 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000481 }
482
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000483 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000484 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000485}
486
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000487void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000488 SmallVector<SDNode*, 16> DeadNodes;
489 DeadNodes.push_back(N);
490
491 // Process the worklist, deleting the nodes and adding their uses to the
492 // worklist.
493 while (!DeadNodes.empty()) {
494 SDNode *N = DeadNodes.back();
495 DeadNodes.pop_back();
496
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000497 if (UpdateListener)
498 UpdateListener->NodeDeleted(N);
499
Evan Cheng130a6472006-10-12 20:34:05 +0000500 // Take the node out of the appropriate CSE map.
501 RemoveNodeFromCSEMaps(N);
502
503 // Next, brutally remove the operand list. This is safe to do, as there are
504 // no cycles in the graph.
505 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
506 SDNode *Operand = I->Val;
Evan Cheng6397c642008-04-03 03:13:16 +0000507 Operand->removeUser(N);
Evan Cheng130a6472006-10-12 20:34:05 +0000508
509 // Now that we removed this operand, see if there are no uses of it left.
510 if (Operand->use_empty())
511 DeadNodes.push_back(Operand);
512 }
Evan Cheng6397c642008-04-03 03:13:16 +0000513 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000514 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000515 N->OperandList = 0;
516 N->NumOperands = 0;
517
518 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000519 AllNodes.erase(N);
520 }
521}
522
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000523void SelectionDAG::DeleteNode(SDNode *N) {
524 assert(N->use_empty() && "Cannot delete a node that is not dead!");
525
526 // First take this out of the appropriate CSE map.
527 RemoveNodeFromCSEMaps(N);
528
Chris Lattner1e111c72005-09-07 05:37:01 +0000529 // Finally, remove uses due to operands of this node, remove from the
530 // AllNodes list, and delete the node.
531 DeleteNodeNotInCSEMaps(N);
532}
533
534void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
535
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000536 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000537 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000538
539 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000540 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Evan Cheng6397c642008-04-03 03:13:16 +0000541 I->Val->removeUser(N);
542 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000543 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000544 N->OperandList = 0;
545 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000546
547 delete N;
548}
549
Chris Lattner149c58c2005-08-16 18:17:10 +0000550/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
551/// correspond to it. This is useful when we're about to delete or repurpose
552/// the node. We don't want future request for structurally identical nodes
553/// to return N anymore.
554void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000555 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000557 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000558 case ISD::STRING:
559 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
560 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000561 case ISD::CONDCODE:
562 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
563 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000564 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000565 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
566 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000567 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000568 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000570 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000571 Erased =
572 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000573 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000574 case ISD::VALUETYPE: {
575 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
576 if (MVT::isExtendedVT(VT)) {
577 Erased = ExtendedValueTypeNodes.erase(VT);
578 } else {
579 Erased = ValueTypeNodes[VT] != 0;
580 ValueTypeNodes[VT] = 0;
581 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000582 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000583 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000584 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000585 // Remove it from the CSE Map.
586 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000587 break;
588 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000589#ifndef NDEBUG
590 // Verify that the node was actually in one of the CSE maps, unless it has a
591 // flag result (which cannot be CSE'd) or is one of the special cases that are
592 // not subject to CSE.
593 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000594 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000595 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000596 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000597 assert(0 && "Node is not in map!");
598 }
599#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000600}
601
Chris Lattner8b8749f2005-08-17 19:00:20 +0000602/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
603/// has been taken out and modified in some way. If the specified node already
604/// exists in the CSE maps, do not modify the maps, but return the existing node
605/// instead. If it doesn't exist, add it and return null.
606///
607SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
608 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000609 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000610 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000611
Chris Lattner9f8cc692005-12-19 22:21:21 +0000612 // Check that remaining values produced are not flags.
613 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
614 if (N->getValueType(i) == MVT::Flag)
615 return 0; // Never CSE anything that produces a flag.
616
Chris Lattnera5682852006-08-07 23:03:03 +0000617 SDNode *New = CSEMap.GetOrInsertNode(N);
618 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000619 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000620}
621
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000622/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
623/// were replaced with those specified. If this node is never memoized,
624/// return null, otherwise return a pointer to the slot it would take. If a
625/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000626SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
627 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000628 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000629 return 0; // Never add these nodes.
630
631 // Check that remaining values produced are not flags.
632 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
633 if (N->getValueType(i) == MVT::Flag)
634 return 0; // Never CSE anything that produces a flag.
635
Chris Lattner63e3f142007-02-04 07:28:00 +0000636 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000637 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000638 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000639 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000640}
641
642/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
643/// were replaced with those specified. If this node is never memoized,
644/// return null, otherwise return a pointer to the slot it would take. If a
645/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000646SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
647 SDOperand Op1, SDOperand Op2,
648 void *&InsertPos) {
649 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
650 return 0; // Never add these nodes.
651
652 // Check that remaining values produced are not flags.
653 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
654 if (N->getValueType(i) == MVT::Flag)
655 return 0; // Never CSE anything that produces a flag.
656
Chris Lattner63e3f142007-02-04 07:28:00 +0000657 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000658 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000659 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000660 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
661}
662
663
664/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
665/// were replaced with those specified. If this node is never memoized,
666/// return null, otherwise return a pointer to the slot it would take. If a
667/// node already exists with these operands, the slot will be non-null.
668SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000669 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000670 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000671 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000672 return 0; // Never add these nodes.
673
674 // Check that remaining values produced are not flags.
675 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
676 if (N->getValueType(i) == MVT::Flag)
677 return 0; // Never CSE anything that produces a flag.
678
Jim Laskey583bd472006-10-27 23:46:08 +0000679 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000680 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000681
Evan Cheng9629aba2006-10-11 01:47:58 +0000682 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
683 ID.AddInteger(LD->getAddressingMode());
684 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000685 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000686 ID.AddInteger(LD->getAlignment());
687 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000688 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
689 ID.AddInteger(ST->getAddressingMode());
690 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000691 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000692 ID.AddInteger(ST->getAlignment());
693 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000694 }
Jim Laskey583bd472006-10-27 23:46:08 +0000695
Chris Lattnera5682852006-08-07 23:03:03 +0000696 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000697}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000698
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000699
Chris Lattner78ec3112003-08-11 14:57:33 +0000700SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000701 while (!AllNodes.empty()) {
702 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000703 N->SetNextInBucket(0);
Evan Cheng6397c642008-04-03 03:13:16 +0000704 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000705 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000706 N->OperandList = 0;
707 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000708 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000709 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000710}
711
Chris Lattner0f2287b2005-04-13 02:38:18 +0000712SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000713 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000714 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
715 MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000716 return getNode(ISD::AND, Op.getValueType(), Op,
717 getConstant(Imm, Op.getValueType()));
718}
719
Chris Lattner36ce6912005-11-29 06:21:05 +0000720SDOperand SelectionDAG::getString(const std::string &Val) {
721 StringSDNode *&N = StringNodes[Val];
722 if (!N) {
723 N = new StringSDNode(Val);
724 AllNodes.push_back(N);
725 }
726 return SDOperand(N, 0);
727}
728
Chris Lattner61b09412006-08-11 21:01:22 +0000729SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000730 MVT::ValueType EltVT =
731 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
732
733 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
734}
735
736SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000737 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000738
739 MVT::ValueType EltVT =
740 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000741
Dan Gohman6394b092008-02-08 22:59:30 +0000742 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
743 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000744
745 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000746 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000747 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000748 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000749 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000750 SDNode *N = NULL;
751 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
752 if (!MVT::isVector(VT))
753 return SDOperand(N, 0);
754 if (!N) {
755 N = new ConstantSDNode(isT, Val, EltVT);
756 CSEMap.InsertNode(N, IP);
757 AllNodes.push_back(N);
758 }
759
760 SDOperand Result(N, 0);
761 if (MVT::isVector(VT)) {
762 SmallVector<SDOperand, 8> Ops;
763 Ops.assign(MVT::getVectorNumElements(VT), Result);
764 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
765 }
766 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000767}
768
Chris Lattner0bd48932008-01-17 07:00:52 +0000769SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
770 return getConstant(Val, TLI.getPointerTy(), isTarget);
771}
772
773
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000774SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000775 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000776 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000777
Dan Gohman7f321562007-06-25 16:23:39 +0000778 MVT::ValueType EltVT =
779 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000780
Chris Lattnerd8658612005-02-17 20:17:32 +0000781 // Do the map lookup using the actual bit pattern for the floating point
782 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
783 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000784 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000785 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000786 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000787 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000788 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000789 SDNode *N = NULL;
790 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
791 if (!MVT::isVector(VT))
792 return SDOperand(N, 0);
793 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000794 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000795 CSEMap.InsertNode(N, IP);
796 AllNodes.push_back(N);
797 }
798
Dan Gohman7f321562007-06-25 16:23:39 +0000799 SDOperand Result(N, 0);
800 if (MVT::isVector(VT)) {
801 SmallVector<SDOperand, 8> Ops;
802 Ops.assign(MVT::getVectorNumElements(VT), Result);
803 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
804 }
805 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000806}
807
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000808SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
809 bool isTarget) {
810 MVT::ValueType EltVT =
811 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
812 if (EltVT==MVT::f32)
813 return getConstantFP(APFloat((float)Val), VT, isTarget);
814 else
815 return getConstantFP(APFloat(Val), VT, isTarget);
816}
817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000819 MVT::ValueType VT, int Offset,
820 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000821 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000822
823 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
824 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000825 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000826 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
827 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
828 }
829
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000830 if (GVar && GVar->isThreadLocal())
831 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
832 else
833 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000834
Jim Laskey583bd472006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000836 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000837 ID.AddPointer(GV);
838 ID.AddInteger(Offset);
839 void *IP = 0;
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
843 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
846}
847
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000848SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
849 bool isTarget) {
850 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000851 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000852 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000853 ID.AddInteger(FI);
854 void *IP = 0;
855 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
856 return SDOperand(E, 0);
857 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
858 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000859 AllNodes.push_back(N);
860 return SDOperand(N, 0);
861}
862
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000863SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
864 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000865 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000866 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000867 ID.AddInteger(JTI);
868 void *IP = 0;
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
872 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
Evan Chengb8973bd2006-01-31 22:23:14 +0000877SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000878 unsigned Alignment, int Offset,
879 bool isTarget) {
880 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000881 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000882 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000883 ID.AddInteger(Alignment);
884 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000885 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 void *IP = 0;
887 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
888 return SDOperand(E, 0);
889 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
890 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
893}
894
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895
Evan Chengd6594ae2006-09-12 21:00:35 +0000896SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
897 MVT::ValueType VT,
898 unsigned Alignment, int Offset,
899 bool isTarget) {
900 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000901 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000902 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000903 ID.AddInteger(Alignment);
904 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000905 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000906 void *IP = 0;
907 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
908 return SDOperand(E, 0);
909 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
910 CSEMap.InsertNode(N, IP);
911 AllNodes.push_back(N);
912 return SDOperand(N, 0);
913}
914
915
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000917 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000918 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000919 ID.AddPointer(MBB);
920 void *IP = 0;
921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
922 return SDOperand(E, 0);
923 SDNode *N = new BasicBlockSDNode(MBB);
924 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 AllNodes.push_back(N);
926 return SDOperand(N, 0);
927}
928
Duncan Sands276dcbd2008-03-21 09:14:45 +0000929SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
930 FoldingSetNodeID ID;
931 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
932 ID.AddInteger(Flags.getRawBits());
933 void *IP = 0;
934 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
935 return SDOperand(E, 0);
936 SDNode *N = new ARG_FLAGSSDNode(Flags);
937 CSEMap.InsertNode(N, IP);
938 AllNodes.push_back(N);
939 return SDOperand(N, 0);
940}
941
Chris Lattner15e4b012005-07-10 00:07:11 +0000942SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000943 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000944 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000945
Duncan Sandsf411b832007-10-17 13:49:58 +0000946 SDNode *&N = MVT::isExtendedVT(VT) ?
947 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
948
949 if (N) return SDOperand(N, 0);
950 N = new VTSDNode(VT);
951 AllNodes.push_back(N);
952 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000953}
954
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
956 SDNode *&N = ExternalSymbols[Sym];
957 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000958 N = new ExternalSymbolSDNode(false, Sym, VT);
959 AllNodes.push_back(N);
960 return SDOperand(N, 0);
961}
962
Chris Lattner809ec112006-01-28 10:09:25 +0000963SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
964 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000965 SDNode *&N = TargetExternalSymbols[Sym];
966 if (N) return SDOperand(N, 0);
967 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000968 AllNodes.push_back(N);
969 return SDOperand(N, 0);
970}
971
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000972SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
973 if ((unsigned)Cond >= CondCodeNodes.size())
974 CondCodeNodes.resize(Cond+1);
975
Chris Lattner079a27a2005-08-09 20:40:02 +0000976 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000977 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000978 AllNodes.push_back(CondCodeNodes[Cond]);
979 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000980 return SDOperand(CondCodeNodes[Cond], 0);
981}
982
Chris Lattner0fdd7682005-08-30 22:38:38 +0000983SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000984 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000985 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000986 ID.AddInteger(RegNo);
987 void *IP = 0;
988 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
989 return SDOperand(E, 0);
990 SDNode *N = new RegisterSDNode(RegNo, VT);
991 CSEMap.InsertNode(N, IP);
992 AllNodes.push_back(N);
993 return SDOperand(N, 0);
994}
995
Dan Gohman69de1932008-02-06 22:27:42 +0000996SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000997 assert((!V || isa<PointerType>(V->getType())) &&
998 "SrcValue is not a pointer?");
999
Jim Laskey583bd472006-10-27 23:46:08 +00001000 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001001 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001002 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001003
Chris Lattner61b09412006-08-11 21:01:22 +00001004 void *IP = 0;
1005 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1006 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001007
1008 SDNode *N = new SrcValueSDNode(V);
1009 CSEMap.InsertNode(N, IP);
1010 AllNodes.push_back(N);
1011 return SDOperand(N, 0);
1012}
1013
1014SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
1015 const Value *v = MO.getValue();
1016 assert((!v || isa<PointerType>(v->getType())) &&
1017 "SrcValue is not a pointer?");
1018
1019 FoldingSetNodeID ID;
1020 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1021 ID.AddPointer(v);
1022 ID.AddInteger(MO.getFlags());
1023 ID.AddInteger(MO.getOffset());
1024 ID.AddInteger(MO.getSize());
1025 ID.AddInteger(MO.getAlignment());
1026
1027 void *IP = 0;
1028 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1029 return SDOperand(E, 0);
1030
1031 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001032 CSEMap.InsertNode(N, IP);
1033 AllNodes.push_back(N);
1034 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001035}
1036
Chris Lattner37ce9df2007-10-15 17:47:20 +00001037/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1038/// specified value type.
1039SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1040 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1041 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1042 const Type *Ty = MVT::getTypeForValueType(VT);
1043 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1044 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1045 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1046}
1047
1048
Chris Lattner51dabfb2006-10-14 00:41:01 +00001049SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1050 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 // These setcc operations always fold.
1052 switch (Cond) {
1053 default: break;
1054 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001055 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001057 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001058
1059 case ISD::SETOEQ:
1060 case ISD::SETOGT:
1061 case ISD::SETOGE:
1062 case ISD::SETOLT:
1063 case ISD::SETOLE:
1064 case ISD::SETONE:
1065 case ISD::SETO:
1066 case ISD::SETUO:
1067 case ISD::SETUEQ:
1068 case ISD::SETUNE:
1069 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1070 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001072
Chris Lattner67255a12005-04-07 18:14:58 +00001073 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001074 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001075 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001076 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001077
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 switch (Cond) {
1079 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001080 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1081 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001082 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1083 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1084 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1085 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1086 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1087 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1088 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1089 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 }
Chris Lattner67255a12005-04-07 18:14:58 +00001092 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001093 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001095 // No compile time operations on this type yet.
1096 if (N1C->getValueType(0) == MVT::ppcf128)
1097 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001098
1099 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001101 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001102 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1103 return getNode(ISD::UNDEF, VT);
1104 // fall through
1105 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1106 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1107 return getNode(ISD::UNDEF, VT);
1108 // fall through
1109 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001110 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001111 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1112 return getNode(ISD::UNDEF, VT);
1113 // fall through
1114 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1115 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1116 return getNode(ISD::UNDEF, VT);
1117 // fall through
1118 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1119 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1120 return getNode(ISD::UNDEF, VT);
1121 // fall through
1122 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001123 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001124 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1125 return getNode(ISD::UNDEF, VT);
1126 // fall through
1127 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001128 R==APFloat::cmpEqual, VT);
1129 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1130 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1131 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1132 R==APFloat::cmpEqual, VT);
1133 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1134 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1135 R==APFloat::cmpLessThan, VT);
1136 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1137 R==APFloat::cmpUnordered, VT);
1138 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1139 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 }
1141 } else {
1142 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001143 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001145 }
1146
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001147 // Could not fold it.
1148 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149}
1150
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001151/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1152/// use this predicate to simplify operations downstream.
1153bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1154 unsigned BitWidth = Op.getValueSizeInBits();
1155 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1156}
1157
Dan Gohmanea859be2007-06-22 14:59:07 +00001158/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1159/// this predicate to simplify operations downstream. Mask is known to be zero
1160/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001161bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001162 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001163 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001164 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1165 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1166 return (KnownZero & Mask) == Mask;
1167}
1168
1169/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1170/// known to be either zero or one and return them in the KnownZero/KnownOne
1171/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1172/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001173void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001174 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001175 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001176 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001177 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1178 "Mask size mismatches value type size!");
1179
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001180 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001181 if (Depth == 6 || Mask == 0)
1182 return; // Limit search depth.
1183
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001184 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001185
1186 switch (Op.getOpcode()) {
1187 case ISD::Constant:
1188 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001189 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001190 KnownZero = ~KnownOne & Mask;
1191 return;
1192 case ISD::AND:
1193 // If either the LHS or the RHS are Zero, the result is zero.
1194 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001195 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1196 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001197 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1198 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1199
1200 // Output known-1 bits are only known if set in both the LHS & RHS.
1201 KnownOne &= KnownOne2;
1202 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1203 KnownZero |= KnownZero2;
1204 return;
1205 case ISD::OR:
1206 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001207 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1208 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001209 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1210 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1211
1212 // Output known-0 bits are only known if clear in both the LHS & RHS.
1213 KnownZero &= KnownZero2;
1214 // Output known-1 are known to be set if set in either the LHS | RHS.
1215 KnownOne |= KnownOne2;
1216 return;
1217 case ISD::XOR: {
1218 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1219 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1221 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1222
1223 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001224 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001225 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1226 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1227 KnownZero = KnownZeroOut;
1228 return;
1229 }
1230 case ISD::SELECT:
1231 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1232 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1233 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1234 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1235
1236 // Only known if known in both the LHS and RHS.
1237 KnownOne &= KnownOne2;
1238 KnownZero &= KnownZero2;
1239 return;
1240 case ISD::SELECT_CC:
1241 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1242 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1243 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1244 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1245
1246 // Only known if known in both the LHS and RHS.
1247 KnownOne &= KnownOne2;
1248 KnownZero &= KnownZero2;
1249 return;
1250 case ISD::SETCC:
1251 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001252 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1253 BitWidth > 1)
1254 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001255 return;
1256 case ISD::SHL:
1257 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1258 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001259 unsigned ShAmt = SA->getValue();
1260
1261 // If the shift count is an invalid immediate, don't do anything.
1262 if (ShAmt >= BitWidth)
1263 return;
1264
1265 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001266 KnownZero, KnownOne, Depth+1);
1267 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001268 KnownZero <<= ShAmt;
1269 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001270 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001271 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001272 }
1273 return;
1274 case ISD::SRL:
1275 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1276 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001277 unsigned ShAmt = SA->getValue();
1278
Dan Gohmand4cf9922008-02-26 18:50:50 +00001279 // If the shift count is an invalid immediate, don't do anything.
1280 if (ShAmt >= BitWidth)
1281 return;
1282
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001283 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001284 KnownZero, KnownOne, Depth+1);
1285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001286 KnownZero = KnownZero.lshr(ShAmt);
1287 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001288
Dan Gohman72d2fd52008-02-13 22:43:25 +00001289 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001290 KnownZero |= HighBits; // High bits known zero.
1291 }
1292 return;
1293 case ISD::SRA:
1294 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001295 unsigned ShAmt = SA->getValue();
1296
Dan Gohmand4cf9922008-02-26 18:50:50 +00001297 // If the shift count is an invalid immediate, don't do anything.
1298 if (ShAmt >= BitWidth)
1299 return;
1300
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001301 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001302 // If any of the demanded bits are produced by the sign extension, we also
1303 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001304 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1305 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001306 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001307
1308 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1309 Depth+1);
1310 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001311 KnownZero = KnownZero.lshr(ShAmt);
1312 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001313
1314 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001315 APInt SignBit = APInt::getSignBit(BitWidth);
1316 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001317
Dan Gohmanca93a432008-02-20 16:30:17 +00001318 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001319 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001320 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001321 KnownOne |= HighBits; // New bits are known one.
1322 }
1323 }
1324 return;
1325 case ISD::SIGN_EXTEND_INREG: {
1326 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001327 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001328
1329 // Sign extension. Compute the demanded bits in the result that are not
1330 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001331 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001332
Dan Gohman977a76f2008-02-13 22:28:48 +00001333 APInt InSignBit = APInt::getSignBit(EBits);
1334 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001335
1336 // If the sign extended bits are demanded, we know that the sign
1337 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001338 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001339 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001340 InputDemandedBits |= InSignBit;
1341
1342 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1343 KnownZero, KnownOne, Depth+1);
1344 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1345
1346 // If the sign bit of the input is known set or clear, then we know the
1347 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001348 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001349 KnownZero |= NewBits;
1350 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001351 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001352 KnownOne |= NewBits;
1353 KnownZero &= ~NewBits;
1354 } else { // Input sign bit unknown
1355 KnownZero &= ~NewBits;
1356 KnownOne &= ~NewBits;
1357 }
1358 return;
1359 }
1360 case ISD::CTTZ:
1361 case ISD::CTLZ:
1362 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001363 unsigned LowBits = Log2_32(BitWidth)+1;
1364 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1365 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001366 return;
1367 }
1368 case ISD::LOAD: {
1369 if (ISD::isZEXTLoad(Op.Val)) {
1370 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001371 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001372 unsigned MemBits = MVT::getSizeInBits(VT);
1373 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001374 }
1375 return;
1376 }
1377 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001378 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1379 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001380 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1381 APInt InMask = Mask;
1382 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001383 KnownZero.trunc(InBits);
1384 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001385 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001386 KnownZero.zext(BitWidth);
1387 KnownOne.zext(BitWidth);
1388 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001389 return;
1390 }
1391 case ISD::SIGN_EXTEND: {
1392 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001393 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001394 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001395 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1396 APInt InMask = Mask;
1397 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001398
1399 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001400 // bit is demanded. Temporarily set this bit in the mask for our callee.
1401 if (NewBits.getBoolValue())
1402 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001403
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001404 KnownZero.trunc(InBits);
1405 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001406 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1407
1408 // Note if the sign bit is known to be zero or one.
1409 bool SignBitKnownZero = KnownZero.isNegative();
1410 bool SignBitKnownOne = KnownOne.isNegative();
1411 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1412 "Sign bit can't be known to be both zero and one!");
1413
1414 // If the sign bit wasn't actually demanded by our caller, we don't
1415 // want it set in the KnownZero and KnownOne result values. Reset the
1416 // mask and reapply it to the result values.
1417 InMask = Mask;
1418 InMask.trunc(InBits);
1419 KnownZero &= InMask;
1420 KnownOne &= InMask;
1421
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001422 KnownZero.zext(BitWidth);
1423 KnownOne.zext(BitWidth);
1424
Dan Gohman977a76f2008-02-13 22:28:48 +00001425 // If the sign bit is known zero or one, the top bits match.
1426 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001427 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001428 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001429 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 return;
1431 }
1432 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001433 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1434 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001435 APInt InMask = Mask;
1436 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001437 KnownZero.trunc(InBits);
1438 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001439 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001440 KnownZero.zext(BitWidth);
1441 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001442 return;
1443 }
1444 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001445 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1446 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001447 APInt InMask = Mask;
1448 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001449 KnownZero.zext(InBits);
1450 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001451 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001452 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001453 KnownZero.trunc(BitWidth);
1454 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001455 break;
1456 }
1457 case ISD::AssertZext: {
1458 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001459 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1461 KnownOne, Depth+1);
1462 KnownZero |= (~InMask) & Mask;
1463 return;
1464 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001465 case ISD::FGETSIGN:
1466 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001467 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001468 return;
1469
Dan Gohmanea859be2007-06-22 14:59:07 +00001470 case ISD::ADD: {
1471 // If either the LHS or the RHS are Zero, the result is zero.
1472 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1473 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1474 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1475 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1476
1477 // Output known-0 bits are known if clear or set in both the low clear bits
1478 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1479 // low 3 bits clear.
Dan Gohman977a76f2008-02-13 22:28:48 +00001480 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1481 KnownZero2.countTrailingOnes());
Dan Gohmanea859be2007-06-22 14:59:07 +00001482
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001483 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1484 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001485 return;
1486 }
1487 case ISD::SUB: {
1488 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1489 if (!CLHS) return;
1490
1491 // We know that the top bits of C-X are clear if X contains less bits
1492 // than C (i.e. no wrap-around can happen). For example, 20-X is
1493 // positive if we can prove that X is >= 0 and < 16.
Dan Gohman977a76f2008-02-13 22:28:48 +00001494 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001495 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1496 // NLZ can't be BitWidth with no sign bit
Chris Lattner423be622008-02-14 18:48:56 +00001497 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001498 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1499
1500 // If all of the MaskV bits are known to be zero, then we know the output
1501 // top bits are zero, because we now know that the output is from [0-C].
1502 if ((KnownZero & MaskV) == MaskV) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001503 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1504 // Top bits known zero.
1505 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1506 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001507 } else {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001508 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001509 }
1510 }
1511 return;
1512 }
1513 default:
1514 // Allow the target to implement this method for its nodes.
1515 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1516 case ISD::INTRINSIC_WO_CHAIN:
1517 case ISD::INTRINSIC_W_CHAIN:
1518 case ISD::INTRINSIC_VOID:
1519 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1520 }
1521 return;
1522 }
1523}
1524
1525/// ComputeNumSignBits - Return the number of times the sign bit of the
1526/// register is replicated into the other bits. We know that at least 1 bit
1527/// is always equal to the sign bit (itself), but other cases can give us
1528/// information. For example, immediately after an "SRA X, 2", we know that
1529/// the top 3 bits are all equal to each other, so we return 3.
1530unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1531 MVT::ValueType VT = Op.getValueType();
1532 assert(MVT::isInteger(VT) && "Invalid VT!");
1533 unsigned VTBits = MVT::getSizeInBits(VT);
1534 unsigned Tmp, Tmp2;
1535
1536 if (Depth == 6)
1537 return 1; // Limit search depth.
1538
1539 switch (Op.getOpcode()) {
1540 default: break;
1541 case ISD::AssertSext:
1542 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1543 return VTBits-Tmp+1;
1544 case ISD::AssertZext:
1545 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1546 return VTBits-Tmp;
1547
1548 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001549 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1550 // If negative, return # leading ones.
1551 if (Val.isNegative())
1552 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001553
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001554 // Return # leading zeros.
1555 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001556 }
1557
1558 case ISD::SIGN_EXTEND:
1559 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1560 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1561
1562 case ISD::SIGN_EXTEND_INREG:
1563 // Max of the input and what this extends.
1564 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1565 Tmp = VTBits-Tmp+1;
1566
1567 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1568 return std::max(Tmp, Tmp2);
1569
1570 case ISD::SRA:
1571 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1572 // SRA X, C -> adds C sign bits.
1573 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1574 Tmp += C->getValue();
1575 if (Tmp > VTBits) Tmp = VTBits;
1576 }
1577 return Tmp;
1578 case ISD::SHL:
1579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1580 // shl destroys sign bits.
1581 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1582 if (C->getValue() >= VTBits || // Bad shift.
1583 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1584 return Tmp - C->getValue();
1585 }
1586 break;
1587 case ISD::AND:
1588 case ISD::OR:
1589 case ISD::XOR: // NOT is handled here.
1590 // Logical binary ops preserve the number of sign bits.
1591 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1592 if (Tmp == 1) return 1; // Early out.
1593 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1594 return std::min(Tmp, Tmp2);
1595
1596 case ISD::SELECT:
1597 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1598 if (Tmp == 1) return 1; // Early out.
1599 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1600 return std::min(Tmp, Tmp2);
1601
1602 case ISD::SETCC:
1603 // If setcc returns 0/-1, all bits are sign bits.
1604 if (TLI.getSetCCResultContents() ==
1605 TargetLowering::ZeroOrNegativeOneSetCCResult)
1606 return VTBits;
1607 break;
1608 case ISD::ROTL:
1609 case ISD::ROTR:
1610 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1611 unsigned RotAmt = C->getValue() & (VTBits-1);
1612
1613 // Handle rotate right by N like a rotate left by 32-N.
1614 if (Op.getOpcode() == ISD::ROTR)
1615 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1616
1617 // If we aren't rotating out all of the known-in sign bits, return the
1618 // number that are left. This handles rotl(sext(x), 1) for example.
1619 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1620 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1621 }
1622 break;
1623 case ISD::ADD:
1624 // Add can have at most one carry bit. Thus we know that the output
1625 // is, at worst, one more bit than the inputs.
1626 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1627 if (Tmp == 1) return 1; // Early out.
1628
1629 // Special case decrementing a value (ADD X, -1):
1630 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1631 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001632 APInt KnownZero, KnownOne;
1633 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001634 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1635
1636 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1637 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001638 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001639 return VTBits;
1640
1641 // If we are subtracting one from a positive number, there is no carry
1642 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001643 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 return Tmp;
1645 }
1646
1647 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1648 if (Tmp2 == 1) return 1;
1649 return std::min(Tmp, Tmp2)-1;
1650 break;
1651
1652 case ISD::SUB:
1653 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1654 if (Tmp2 == 1) return 1;
1655
1656 // Handle NEG.
1657 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001658 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001659 APInt KnownZero, KnownOne;
1660 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001661 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1662 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1663 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001664 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001665 return VTBits;
1666
1667 // If the input is known to be positive (the sign bit is known clear),
1668 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001669 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001670 return Tmp2;
1671
1672 // Otherwise, we treat this like a SUB.
1673 }
1674
1675 // Sub can have at most one carry bit. Thus we know that the output
1676 // is, at worst, one more bit than the inputs.
1677 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1678 if (Tmp == 1) return 1; // Early out.
1679 return std::min(Tmp, Tmp2)-1;
1680 break;
1681 case ISD::TRUNCATE:
1682 // FIXME: it's tricky to do anything useful for this, but it is an important
1683 // case for targets like X86.
1684 break;
1685 }
1686
1687 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1688 if (Op.getOpcode() == ISD::LOAD) {
1689 LoadSDNode *LD = cast<LoadSDNode>(Op);
1690 unsigned ExtType = LD->getExtensionType();
1691 switch (ExtType) {
1692 default: break;
1693 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001694 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 return VTBits-Tmp+1;
1696 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001697 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001698 return VTBits-Tmp;
1699 }
1700 }
1701
1702 // Allow the target to implement this method for its nodes.
1703 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1704 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1705 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1706 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1707 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1708 if (NumBits > 1) return NumBits;
1709 }
1710
1711 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1712 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001713 APInt KnownZero, KnownOne;
1714 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1716
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001717 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001718 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001719 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001720 Mask = KnownOne;
1721 } else {
1722 // Nothing known.
1723 return 1;
1724 }
1725
1726 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1727 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001728 Mask = ~Mask;
1729 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001730 // Return # leading zeros. We use 'min' here in case Val was zero before
1731 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001732 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanea859be2007-06-22 14:59:07 +00001733}
1734
Chris Lattner51dabfb2006-10-14 00:41:01 +00001735
Evan Chenga844bde2008-02-02 04:07:54 +00001736bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1737 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1738 if (!GA) return false;
1739 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1740 if (!GV) return false;
1741 MachineModuleInfo *MMI = getMachineModuleInfo();
1742 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1743}
1744
1745
Chris Lattnerc3aae252005-01-07 07:46:32 +00001746/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001747///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001748SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001749 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001750 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001751 void *IP = 0;
1752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1753 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001754 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001755 CSEMap.InsertNode(N, IP);
1756
1757 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 return SDOperand(N, 0);
1759}
1760
Chris Lattnerc3aae252005-01-07 07:46:32 +00001761SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1762 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001763 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001764 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001765 const APInt &Val = C->getAPIntValue();
1766 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001767 switch (Opcode) {
1768 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001769 case ISD::SIGN_EXTEND:
1770 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001771 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001772 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001773 case ISD::TRUNCATE:
1774 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001775 case ISD::UINT_TO_FP:
1776 case ISD::SINT_TO_FP: {
1777 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001778 // No compile time operations on this type.
1779 if (VT==MVT::ppcf128)
1780 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001781 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1782 (void)apf.convertFromAPInt(Val,
1783 Opcode==ISD::SINT_TO_FP,
1784 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001785 return getConstantFP(apf, VT);
1786 }
Chris Lattner94683772005-12-23 05:30:37 +00001787 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001788 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001789 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001790 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001791 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001792 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001793 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001794 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001795 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001796 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001797 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001798 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001799 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001800 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001801 }
1802 }
1803
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001804 // Constant fold unary operations with a floating point constant operand.
1805 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1806 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001807 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001808 switch (Opcode) {
1809 case ISD::FNEG:
1810 V.changeSign();
1811 return getConstantFP(V, VT);
1812 case ISD::FABS:
1813 V.clearSign();
1814 return getConstantFP(V, VT);
1815 case ISD::FP_ROUND:
1816 case ISD::FP_EXTEND:
1817 // This can return overflow, underflow, or inexact; we don't care.
1818 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001819 (void)V.convert(*MVTToAPFloatSemantics(VT),
1820 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001821 return getConstantFP(V, VT);
1822 case ISD::FP_TO_SINT:
1823 case ISD::FP_TO_UINT: {
1824 integerPart x;
1825 assert(integerPartWidth >= 64);
1826 // FIXME need to be more flexible about rounding mode.
1827 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1828 Opcode==ISD::FP_TO_SINT,
1829 APFloat::rmTowardZero);
1830 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1831 break;
1832 return getConstant(x, VT);
1833 }
1834 case ISD::BIT_CONVERT:
1835 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1836 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1837 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1838 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001839 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001840 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001841 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001842 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001843
1844 unsigned OpOpcode = Operand.Val->getOpcode();
1845 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001846 case ISD::TokenFactor:
1847 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001848 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001849 case ISD::FP_EXTEND:
1850 assert(MVT::isFloatingPoint(VT) &&
1851 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001852 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001853 if (Operand.getOpcode() == ISD::UNDEF)
1854 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001855 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001856 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001857 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1858 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001859 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001860 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1861 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001862 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1863 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1864 break;
1865 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001866 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1867 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001868 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001869 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1870 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001871 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001872 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001873 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001874 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001875 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1876 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001877 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001878 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1879 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001880 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1881 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1882 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1883 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001884 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001885 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1886 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001887 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001888 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1889 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001890 if (OpOpcode == ISD::TRUNCATE)
1891 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001892 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1893 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001894 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001895 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1896 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001897 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001898 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1899 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001900 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1901 else
1902 return Operand.Val->getOperand(0);
1903 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001904 break;
Chris Lattner35481892005-12-23 00:16:34 +00001905 case ISD::BIT_CONVERT:
1906 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001907 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001908 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001909 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001910 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1911 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001912 if (OpOpcode == ISD::UNDEF)
1913 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001914 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001915 case ISD::SCALAR_TO_VECTOR:
1916 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001917 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001918 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001919 if (OpOpcode == ISD::UNDEF)
1920 return getNode(ISD::UNDEF, VT);
1921 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1922 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1923 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1924 Operand.getConstantOperandVal(1) == 0 &&
1925 Operand.getOperand(0).getValueType() == VT)
1926 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001927 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001928 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001929 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1930 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001931 Operand.Val->getOperand(0));
1932 if (OpOpcode == ISD::FNEG) // --X -> X
1933 return Operand.Val->getOperand(0);
1934 break;
1935 case ISD::FABS:
1936 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1937 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1938 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001939 }
1940
Chris Lattner43247a12005-08-25 19:12:10 +00001941 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001942 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001943 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001944 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001945 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001946 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001947 void *IP = 0;
1948 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1949 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001950 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001951 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001952 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001953 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001954 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001955 AllNodes.push_back(N);
1956 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001957}
1958
Chris Lattner36019aa2005-04-18 03:48:41 +00001959
1960
Chris Lattnerc3aae252005-01-07 07:46:32 +00001961SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1962 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001963 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1964 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001965 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001966 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001967 case ISD::TokenFactor:
1968 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1969 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001970 // Fold trivial token factors.
1971 if (N1.getOpcode() == ISD::EntryToken) return N2;
1972 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001973 break;
Chris Lattner76365122005-01-16 02:23:22 +00001974 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001975 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1976 N1.getValueType() == VT && "Binary operator types must match!");
1977 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1978 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001979 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001980 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001981 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1982 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001983 break;
Chris Lattner76365122005-01-16 02:23:22 +00001984 case ISD::OR:
1985 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001986 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1987 N1.getValueType() == VT && "Binary operator types must match!");
1988 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1989 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001990 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001991 return N1;
1992 break;
Chris Lattner76365122005-01-16 02:23:22 +00001993 case ISD::UDIV:
1994 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001995 case ISD::MULHU:
1996 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001997 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1998 // fall through
1999 case ISD::ADD:
2000 case ISD::SUB:
2001 case ISD::MUL:
2002 case ISD::SDIV:
2003 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002004 case ISD::FADD:
2005 case ISD::FSUB:
2006 case ISD::FMUL:
2007 case ISD::FDIV:
2008 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002009 assert(N1.getValueType() == N2.getValueType() &&
2010 N1.getValueType() == VT && "Binary operator types must match!");
2011 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002012 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2013 assert(N1.getValueType() == VT &&
2014 MVT::isFloatingPoint(N1.getValueType()) &&
2015 MVT::isFloatingPoint(N2.getValueType()) &&
2016 "Invalid FCOPYSIGN!");
2017 break;
Chris Lattner76365122005-01-16 02:23:22 +00002018 case ISD::SHL:
2019 case ISD::SRA:
2020 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002021 case ISD::ROTL:
2022 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002023 assert(VT == N1.getValueType() &&
2024 "Shift operators return type must be the same as their first arg");
2025 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002026 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002027 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002028 case ISD::FP_ROUND_INREG: {
2029 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2030 assert(VT == N1.getValueType() && "Not an inreg round!");
2031 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2032 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002033 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2034 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002035 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002036 break;
2037 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002038 case ISD::FP_ROUND:
2039 assert(MVT::isFloatingPoint(VT) &&
2040 MVT::isFloatingPoint(N1.getValueType()) &&
2041 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2042 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002043 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002044 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002045 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002046 case ISD::AssertZext: {
2047 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2048 assert(VT == N1.getValueType() && "Not an inreg extend!");
2049 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2050 "Cannot *_EXTEND_INREG FP types");
2051 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2052 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002053 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002054 break;
2055 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002056 case ISD::SIGN_EXTEND_INREG: {
2057 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2058 assert(VT == N1.getValueType() && "Not an inreg extend!");
2059 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2060 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002061 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2062 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002063 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002064
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002065 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002066 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002067 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002068 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002069 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002070 return getConstant(Val, VT);
2071 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002072 break;
2073 }
2074 case ISD::EXTRACT_VECTOR_ELT:
2075 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2076
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002077 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2078 if (N1.getOpcode() == ISD::UNDEF)
2079 return getNode(ISD::UNDEF, VT);
2080
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002081 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2082 // expanding copies of large vectors from registers.
2083 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2084 N1.getNumOperands() > 0) {
2085 unsigned Factor =
2086 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2087 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2088 N1.getOperand(N2C->getValue() / Factor),
2089 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2090 }
2091
2092 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2093 // expanding large vector constants.
2094 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2095 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002096
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002097 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2098 // operations are lowered to scalars.
2099 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2100 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2101 if (IEC == N2C)
2102 return N1.getOperand(1);
2103 else
2104 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2105 }
2106 break;
2107 case ISD::EXTRACT_ELEMENT:
2108 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002109 assert(!MVT::isVector(N1.getValueType()) &&
2110 MVT::isInteger(N1.getValueType()) &&
2111 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2112 "EXTRACT_ELEMENT only applies to integers!");
2113
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002114 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2115 // 64-bit integers into 32-bit parts. Instead of building the extract of
2116 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2117 if (N1.getOpcode() == ISD::BUILD_PAIR)
2118 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002119
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002120 // EXTRACT_ELEMENT of a constant int is also very common.
2121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002122 unsigned ElementSize = MVT::getSizeInBits(VT);
2123 unsigned Shift = ElementSize * N2C->getValue();
2124 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2125 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002126 }
2127 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002128 case ISD::EXTRACT_SUBVECTOR:
2129 if (N1.getValueType() == VT) // Trivial extraction.
2130 return N1;
2131 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002132 }
2133
2134 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002135 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002136 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002137 switch (Opcode) {
2138 case ISD::ADD: return getConstant(C1 + C2, VT);
2139 case ISD::SUB: return getConstant(C1 - C2, VT);
2140 case ISD::MUL: return getConstant(C1 * C2, VT);
2141 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002142 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143 break;
2144 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002145 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002146 break;
2147 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002148 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 break;
2150 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002151 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 break;
2153 case ISD::AND : return getConstant(C1 & C2, VT);
2154 case ISD::OR : return getConstant(C1 | C2, VT);
2155 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002156 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002157 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2158 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2159 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2160 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002161 default: break;
2162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 } else { // Cannonicalize constant to RHS if commutative
2164 if (isCommutativeBinOp(Opcode)) {
2165 std::swap(N1C, N2C);
2166 std::swap(N1, N2);
2167 }
2168 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002169 }
2170
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002171 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002172 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2173 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002174 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002175 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2176 // Cannonicalize constant to RHS if commutative
2177 std::swap(N1CFP, N2CFP);
2178 std::swap(N1, N2);
2179 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002180 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2181 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002182 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002183 case ISD::FADD:
2184 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002185 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002186 return getConstantFP(V1, VT);
2187 break;
2188 case ISD::FSUB:
2189 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2190 if (s!=APFloat::opInvalidOp)
2191 return getConstantFP(V1, VT);
2192 break;
2193 case ISD::FMUL:
2194 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2195 if (s!=APFloat::opInvalidOp)
2196 return getConstantFP(V1, VT);
2197 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002198 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002199 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2200 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2201 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002203 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002204 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2205 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2206 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002207 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002208 case ISD::FCOPYSIGN:
2209 V1.copySign(V2);
2210 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002211 default: break;
2212 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002213 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002214 }
Chris Lattner62b57722006-04-20 05:39:12 +00002215
2216 // Canonicalize an UNDEF to the RHS, even over a constant.
2217 if (N1.getOpcode() == ISD::UNDEF) {
2218 if (isCommutativeBinOp(Opcode)) {
2219 std::swap(N1, N2);
2220 } else {
2221 switch (Opcode) {
2222 case ISD::FP_ROUND_INREG:
2223 case ISD::SIGN_EXTEND_INREG:
2224 case ISD::SUB:
2225 case ISD::FSUB:
2226 case ISD::FDIV:
2227 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002228 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002229 return N1; // fold op(undef, arg2) -> undef
2230 case ISD::UDIV:
2231 case ISD::SDIV:
2232 case ISD::UREM:
2233 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002234 case ISD::SRL:
2235 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002236 if (!MVT::isVector(VT))
2237 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2238 // For vectors, we can't easily build an all zero vector, just return
2239 // the LHS.
2240 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002241 }
2242 }
2243 }
2244
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002245 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002246 if (N2.getOpcode() == ISD::UNDEF) {
2247 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002248 case ISD::XOR:
2249 if (N1.getOpcode() == ISD::UNDEF)
2250 // Handle undef ^ undef -> 0 special case. This is a common
2251 // idiom (misuse).
2252 return getConstant(0, VT);
2253 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002254 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002255 case ISD::ADDC:
2256 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002257 case ISD::SUB:
2258 case ISD::FADD:
2259 case ISD::FSUB:
2260 case ISD::FMUL:
2261 case ISD::FDIV:
2262 case ISD::FREM:
2263 case ISD::UDIV:
2264 case ISD::SDIV:
2265 case ISD::UREM:
2266 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002267 return N2; // fold op(arg1, undef) -> undef
2268 case ISD::MUL:
2269 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002270 case ISD::SRL:
2271 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002272 if (!MVT::isVector(VT))
2273 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2274 // For vectors, we can't easily build an all zero vector, just return
2275 // the LHS.
2276 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002277 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002278 if (!MVT::isVector(VT))
2279 return getConstant(MVT::getIntVTBitMask(VT), VT);
2280 // For vectors, we can't easily build an all one vector, just return
2281 // the LHS.
2282 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002283 case ISD::SRA:
2284 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002285 }
2286 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002287
Chris Lattner27e9b412005-05-11 18:57:39 +00002288 // Memoize this node if possible.
2289 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002290 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002291 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002292 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002293 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002294 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002295 void *IP = 0;
2296 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2297 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002298 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002299 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002300 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002301 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002302 }
2303
Chris Lattnerc3aae252005-01-07 07:46:32 +00002304 AllNodes.push_back(N);
2305 return SDOperand(N, 0);
2306}
2307
Chris Lattnerc3aae252005-01-07 07:46:32 +00002308SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2309 SDOperand N1, SDOperand N2, SDOperand N3) {
2310 // Perform various simplifications.
2311 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2312 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002313 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002314 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002315 // Use FoldSetCC to simplify SETCC's.
2316 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002317 if (Simp.Val) return Simp;
2318 break;
2319 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002321 if (N1C) {
2322 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002324 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002325 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002326 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002327
2328 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002329 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002330 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002331 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002332 if (N2C->getValue()) // Unconditional branch
2333 return getNode(ISD::BR, MVT::Other, N1, N3);
2334 else
2335 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002336 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002337 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002338 case ISD::VECTOR_SHUFFLE:
2339 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2340 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2341 N3.getOpcode() == ISD::BUILD_VECTOR &&
2342 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2343 "Illegal VECTOR_SHUFFLE node!");
2344 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002345 case ISD::BIT_CONVERT:
2346 // Fold bit_convert nodes from a type to themselves.
2347 if (N1.getValueType() == VT)
2348 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002349 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002350 }
2351
Chris Lattner43247a12005-08-25 19:12:10 +00002352 // Memoize node if it doesn't produce a flag.
2353 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002354 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002355 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002356 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002357 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002358 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002359 void *IP = 0;
2360 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2361 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002362 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002363 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002364 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002365 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002366 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002367 AllNodes.push_back(N);
2368 return SDOperand(N, 0);
2369}
2370
2371SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002372 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002373 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002374 SDOperand Ops[] = { N1, N2, N3, N4 };
2375 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002376}
2377
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002378SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2379 SDOperand N1, SDOperand N2, SDOperand N3,
2380 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002381 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2382 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002383}
2384
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002385SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2386 SDOperand Src, SDOperand Size,
2387 SDOperand Align,
2388 SDOperand AlwaysInline) {
2389 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2390 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2391}
2392
2393SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2394 SDOperand Src, SDOperand Size,
2395 SDOperand Align,
2396 SDOperand AlwaysInline) {
2397 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2398 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2399}
2400
2401SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2402 SDOperand Src, SDOperand Size,
2403 SDOperand Align,
2404 SDOperand AlwaysInline) {
2405 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2406 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2407}
2408
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002409SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002410 SDOperand Ptr, SDOperand Cmp,
2411 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002412 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002413 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2414 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002415 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002416 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002417 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2418 ID.AddInteger((unsigned int)VT);
2419 void* IP = 0;
2420 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2421 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002422 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002423 CSEMap.InsertNode(N, IP);
2424 AllNodes.push_back(N);
2425 return SDOperand(N, 0);
2426}
2427
2428SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002429 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002430 MVT::ValueType VT) {
2431 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2432 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002433 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002434 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002435 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002436 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2437 ID.AddInteger((unsigned int)VT);
2438 void* IP = 0;
2439 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2440 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002441 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002442 CSEMap.InsertNode(N, IP);
2443 AllNodes.push_back(N);
2444 return SDOperand(N, 0);
2445}
2446
Duncan Sands14ea39c2008-03-27 20:23:40 +00002447SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00002448SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2449 MVT::ValueType VT, SDOperand Chain,
2450 SDOperand Ptr, SDOperand Offset,
2451 const Value *SV, int SVOffset, MVT::ValueType EVT,
2452 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002453 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2454 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002455 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002456 Ty = MVT::getTypeForValueType(VT);
2457 } else if (SV) {
2458 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2459 assert(PT && "Value for load must be a pointer");
2460 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00002461 }
Dan Gohman575e2f42007-06-04 15:49:41 +00002462 assert(Ty && "Could not get type information for load");
2463 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2464 }
Evan Cheng466685d2006-10-09 20:57:25 +00002465
Duncan Sands14ea39c2008-03-27 20:23:40 +00002466 if (VT == EVT) {
2467 ExtType = ISD::NON_EXTLOAD;
2468 } else if (ExtType == ISD::NON_EXTLOAD) {
2469 assert(VT == EVT && "Non-extending load from different memory type!");
2470 } else {
2471 // Extending load.
2472 if (MVT::isVector(VT))
2473 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2474 else
2475 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2476 "Should only be an extending load, not truncating!");
2477 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2478 "Cannot sign/zero extend a FP/Vector load!");
2479 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2480 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00002481 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00002482
2483 bool Indexed = AM != ISD::UNINDEXED;
2484 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2485 "Unindexed load with an offset!");
2486
2487 SDVTList VTs = Indexed ?
2488 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2489 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002490 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002491 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002492 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00002493 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002494 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002495 ID.AddInteger(Alignment);
2496 ID.AddInteger(isVolatile);
2497 void *IP = 0;
2498 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2499 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002500 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
2501 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002502 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002503 AllNodes.push_back(N);
2504 return SDOperand(N, 0);
2505}
2506
Duncan Sands14ea39c2008-03-27 20:23:40 +00002507SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2508 SDOperand Chain, SDOperand Ptr,
2509 const Value *SV, int SVOffset,
2510 bool isVolatile, unsigned Alignment) {
2511 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002512 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
2513 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002514}
2515
2516SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2517 SDOperand Chain, SDOperand Ptr,
2518 const Value *SV,
2519 int SVOffset, MVT::ValueType EVT,
2520 bool isVolatile, unsigned Alignment) {
2521 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002522 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
2523 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002524}
2525
Evan Cheng144d8f02006-11-09 17:55:04 +00002526SDOperand
2527SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2528 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002529 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002530 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2531 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00002532 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
2533 LD->getChain(), Base, Offset, LD->getSrcValue(),
2534 LD->getSrcValueOffset(), LD->getMemoryVT(),
2535 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00002536}
2537
Jeff Cohend41b30d2006-11-05 19:31:28 +00002538SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002539 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002540 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002541 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002542
Dan Gohman575e2f42007-06-04 15:49:41 +00002543 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2544 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002545 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002546 Ty = MVT::getTypeForValueType(VT);
2547 } else if (SV) {
2548 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2549 assert(PT && "Value for store must be a pointer");
2550 Ty = PT->getElementType();
2551 }
2552 assert(Ty && "Could not get type information for store");
2553 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2554 }
Evan Chengad071e12006-10-05 22:57:11 +00002555 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002556 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002557 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002558 FoldingSetNodeID ID;
2559 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002560 ID.AddInteger(ISD::UNINDEXED);
2561 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002562 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563 ID.AddInteger(Alignment);
2564 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002565 void *IP = 0;
2566 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2567 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002568 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002569 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002570 CSEMap.InsertNode(N, IP);
2571 AllNodes.push_back(N);
2572 return SDOperand(N, 0);
2573}
2574
Jeff Cohend41b30d2006-11-05 19:31:28 +00002575SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002576 SDOperand Ptr, const Value *SV,
2577 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002578 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002579 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002580
2581 if (VT == SVT)
2582 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002583
Duncan Sandsaf47b112007-10-16 09:56:48 +00002584 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2585 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2587 "Can't do FP-INT conversion!");
2588
Dan Gohman575e2f42007-06-04 15:49:41 +00002589 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2590 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002591 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002592 Ty = MVT::getTypeForValueType(VT);
2593 } else if (SV) {
2594 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2595 assert(PT && "Value for store must be a pointer");
2596 Ty = PT->getElementType();
2597 }
2598 assert(Ty && "Could not get type information for store");
2599 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2600 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002601 SDVTList VTs = getVTList(MVT::Other);
2602 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002603 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002604 FoldingSetNodeID ID;
2605 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002606 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002607 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002608 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002609 ID.AddInteger(Alignment);
2610 ID.AddInteger(isVolatile);
2611 void *IP = 0;
2612 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2613 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002614 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002615 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002616 CSEMap.InsertNode(N, IP);
2617 AllNodes.push_back(N);
2618 return SDOperand(N, 0);
2619}
2620
Evan Cheng144d8f02006-11-09 17:55:04 +00002621SDOperand
2622SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2623 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002624 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2625 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2626 "Store is already a indexed store!");
2627 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2628 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2629 FoldingSetNodeID ID;
2630 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2631 ID.AddInteger(AM);
2632 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002633 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002634 ID.AddInteger(ST->getAlignment());
2635 ID.AddInteger(ST->isVolatile());
2636 void *IP = 0;
2637 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2638 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002639 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002640 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002641 ST->getSrcValue(), ST->getSrcValueOffset(),
2642 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002643 CSEMap.InsertNode(N, IP);
2644 AllNodes.push_back(N);
2645 return SDOperand(N, 0);
2646}
2647
Nate Begemanacc398c2006-01-25 18:21:52 +00002648SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2649 SDOperand Chain, SDOperand Ptr,
2650 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002651 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002652 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002653}
2654
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002655SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002656 const SDOperand *Ops, unsigned NumOps) {
2657 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002658 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002659 case 1: return getNode(Opcode, VT, Ops[0]);
2660 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2661 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002662 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002663 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002664
Chris Lattneref847df2005-04-09 03:27:28 +00002665 switch (Opcode) {
2666 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002667 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002668 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002669 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2670 "LHS and RHS of condition must have same type!");
2671 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2672 "True and False arms of SelectCC must have same type!");
2673 assert(Ops[2].getValueType() == VT &&
2674 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002675 break;
2676 }
2677 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002678 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002679 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2680 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002681 break;
2682 }
Chris Lattneref847df2005-04-09 03:27:28 +00002683 }
2684
Chris Lattner385328c2005-05-14 07:42:29 +00002685 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002686 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002687 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002688 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002689 FoldingSetNodeID ID;
2690 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002691 void *IP = 0;
2692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2693 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002694 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002695 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002696 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002697 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002698 }
Chris Lattneref847df2005-04-09 03:27:28 +00002699 AllNodes.push_back(N);
2700 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002701}
2702
Chris Lattner89c34632005-05-14 06:20:26 +00002703SDOperand SelectionDAG::getNode(unsigned Opcode,
2704 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002705 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002706 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2707 Ops, NumOps);
2708}
2709
2710SDOperand SelectionDAG::getNode(unsigned Opcode,
2711 const MVT::ValueType *VTs, unsigned NumVTs,
2712 const SDOperand *Ops, unsigned NumOps) {
2713 if (NumVTs == 1)
2714 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002715 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2716}
2717
2718SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2719 const SDOperand *Ops, unsigned NumOps) {
2720 if (VTList.NumVTs == 1)
2721 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002722
Chris Lattner5f056bf2005-07-10 01:55:33 +00002723 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002724 // FIXME: figure out how to safely handle things like
2725 // int foo(int x) { return 1 << (x & 255); }
2726 // int bar() { return foo(256); }
2727#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002728 case ISD::SRA_PARTS:
2729 case ISD::SRL_PARTS:
2730 case ISD::SHL_PARTS:
2731 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002732 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002733 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2734 else if (N3.getOpcode() == ISD::AND)
2735 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2736 // If the and is only masking out bits that cannot effect the shift,
2737 // eliminate the and.
2738 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2739 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2740 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2741 }
2742 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002743#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002744 }
Chris Lattner89c34632005-05-14 06:20:26 +00002745
Chris Lattner43247a12005-08-25 19:12:10 +00002746 // Memoize the node unless it returns a flag.
2747 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002748 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002749 FoldingSetNodeID ID;
2750 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002751 void *IP = 0;
2752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2753 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002754 if (NumOps == 1)
2755 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2756 else if (NumOps == 2)
2757 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2758 else if (NumOps == 3)
2759 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2760 else
2761 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002762 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002763 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002764 if (NumOps == 1)
2765 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2766 else if (NumOps == 2)
2767 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2768 else if (NumOps == 3)
2769 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2770 else
2771 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002772 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002773 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002774 return SDOperand(N, 0);
2775}
2776
Dan Gohman08ce9762007-10-08 15:49:58 +00002777SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2778 return getNode(Opcode, VTList, 0, 0);
2779}
2780
2781SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2782 SDOperand N1) {
2783 SDOperand Ops[] = { N1 };
2784 return getNode(Opcode, VTList, Ops, 1);
2785}
2786
2787SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2788 SDOperand N1, SDOperand N2) {
2789 SDOperand Ops[] = { N1, N2 };
2790 return getNode(Opcode, VTList, Ops, 2);
2791}
2792
2793SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2794 SDOperand N1, SDOperand N2, SDOperand N3) {
2795 SDOperand Ops[] = { N1, N2, N3 };
2796 return getNode(Opcode, VTList, Ops, 3);
2797}
2798
2799SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2800 SDOperand N1, SDOperand N2, SDOperand N3,
2801 SDOperand N4) {
2802 SDOperand Ops[] = { N1, N2, N3, N4 };
2803 return getNode(Opcode, VTList, Ops, 4);
2804}
2805
2806SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2807 SDOperand N1, SDOperand N2, SDOperand N3,
2808 SDOperand N4, SDOperand N5) {
2809 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2810 return getNode(Opcode, VTList, Ops, 5);
2811}
2812
Chris Lattner70046e92006-08-15 17:46:01 +00002813SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002814 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002815}
2816
Chris Lattner70046e92006-08-15 17:46:01 +00002817SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002818 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2819 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002820 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002821 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002822 }
2823 std::vector<MVT::ValueType> V;
2824 V.push_back(VT1);
2825 V.push_back(VT2);
2826 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002827 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002828}
Chris Lattner70046e92006-08-15 17:46:01 +00002829SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2830 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002831 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002832 E = VTList.end(); I != E; ++I) {
2833 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2834 (*I)[2] == VT3)
2835 return makeVTList(&(*I)[0], 3);
2836 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002837 std::vector<MVT::ValueType> V;
2838 V.push_back(VT1);
2839 V.push_back(VT2);
2840 V.push_back(VT3);
2841 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002842 return makeVTList(&(*VTList.begin())[0], 3);
2843}
2844
2845SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2846 switch (NumVTs) {
2847 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002848 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002849 case 2: return getVTList(VTs[0], VTs[1]);
2850 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2851 default: break;
2852 }
2853
2854 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2855 E = VTList.end(); I != E; ++I) {
2856 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2857
2858 bool NoMatch = false;
2859 for (unsigned i = 2; i != NumVTs; ++i)
2860 if (VTs[i] != (*I)[i]) {
2861 NoMatch = true;
2862 break;
2863 }
2864 if (!NoMatch)
2865 return makeVTList(&*I->begin(), NumVTs);
2866 }
2867
2868 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2869 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002870}
2871
2872
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002873/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2874/// specified operands. If the resultant node already exists in the DAG,
2875/// this does not modify the specified node, instead it returns the node that
2876/// already exists. If the resultant node does not exist in the DAG, the
2877/// input node is returned. As a degenerate case, if you specify the same
2878/// input operands as the node already has, the input node is returned.
2879SDOperand SelectionDAG::
2880UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2881 SDNode *N = InN.Val;
2882 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2883
2884 // Check to see if there is no change.
2885 if (Op == N->getOperand(0)) return InN;
2886
2887 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002888 void *InsertPos = 0;
2889 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2890 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002891
2892 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002893 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002894 RemoveNodeFromCSEMaps(N);
2895
2896 // Now we update the operands.
Evan Cheng6397c642008-04-03 03:13:16 +00002897 N->OperandList[0].Val->removeUser(N);
2898 Op.Val->addUser(N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002899 N->OperandList[0] = Op;
2900
2901 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002902 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002903 return InN;
2904}
2905
2906SDOperand SelectionDAG::
2907UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2908 SDNode *N = InN.Val;
2909 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2910
2911 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002912 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2913 return InN; // No operands changed, just return the input node.
2914
2915 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002916 void *InsertPos = 0;
2917 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2918 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002919
2920 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002921 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002922 RemoveNodeFromCSEMaps(N);
2923
2924 // Now we update the operands.
2925 if (N->OperandList[0] != Op1) {
Evan Cheng6397c642008-04-03 03:13:16 +00002926 N->OperandList[0].Val->removeUser(N);
2927 Op1.Val->addUser(N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002928 N->OperandList[0] = Op1;
2929 }
2930 if (N->OperandList[1] != Op2) {
Evan Cheng6397c642008-04-03 03:13:16 +00002931 N->OperandList[1].Val->removeUser(N);
2932 Op2.Val->addUser(N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002933 N->OperandList[1] = Op2;
2934 }
2935
2936 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002937 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002938 return InN;
2939}
2940
2941SDOperand SelectionDAG::
2942UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002943 SDOperand Ops[] = { Op1, Op2, Op3 };
2944 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002945}
2946
2947SDOperand SelectionDAG::
2948UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2949 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002950 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2951 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002952}
2953
2954SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002955UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2956 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002957 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2958 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002959}
2960
Evan Cheng6397c642008-04-03 03:13:16 +00002961
Chris Lattner809ec112006-01-28 10:09:25 +00002962SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002963UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002964 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002965 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002966 "Update with wrong number of operands");
2967
2968 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002969 bool AnyChange = false;
2970 for (unsigned i = 0; i != NumOps; ++i) {
2971 if (Ops[i] != N->getOperand(i)) {
2972 AnyChange = true;
2973 break;
2974 }
2975 }
2976
2977 // No operands changed, just return the input node.
2978 if (!AnyChange) return InN;
2979
2980 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002981 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002982 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002983 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002984
2985 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002986 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002987 RemoveNodeFromCSEMaps(N);
2988
2989 // Now we update the operands.
2990 for (unsigned i = 0; i != NumOps; ++i) {
2991 if (N->OperandList[i] != Ops[i]) {
Evan Cheng6397c642008-04-03 03:13:16 +00002992 N->OperandList[i].Val->removeUser(N);
2993 Ops[i].Val->addUser(N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002994 N->OperandList[i] = Ops[i];
2995 }
2996 }
2997
2998 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002999 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003000 return InN;
3001}
3002
Evan Cheng6397c642008-04-03 03:13:16 +00003003
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003004/// MorphNodeTo - This frees the operands of the current node, resets the
3005/// opcode, types, and operands to the specified value. This should only be
3006/// used by the SelectionDAG class.
3007void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3008 const SDOperand *Ops, unsigned NumOps) {
3009 NodeType = Opc;
3010 ValueList = L.VTs;
3011 NumValues = L.NumVTs;
3012
3013 // Clear the operands list, updating used nodes to remove this from their
3014 // use list.
3015 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Evan Cheng6397c642008-04-03 03:13:16 +00003016 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003017
3018 // If NumOps is larger than the # of operands we currently have, reallocate
3019 // the operand list.
3020 if (NumOps > NumOperands) {
Evan Cheng6397c642008-04-03 03:13:16 +00003021 if (OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +00003022 delete [] OperandList;
3023 OperandList = new SDOperand[NumOps];
3024 OperandsNeedDelete = true;
3025 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003026
3027 // Assign the new operands.
3028 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003029
3030 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3031 OperandList[i] = Ops[i];
3032 SDNode *N = OperandList[i].Val;
Evan Cheng6397c642008-04-03 03:13:16 +00003033 N->Uses.push_back(this);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003034 }
3035}
Chris Lattner149c58c2005-08-16 18:17:10 +00003036
3037/// SelectNodeTo - These are used for target selectors to *mutate* the
3038/// specified node to have the specified return type, Target opcode, and
3039/// operands. Note that target opcodes are stored as
3040/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003041///
3042/// Note that SelectNodeTo returns the resultant node. If there is already a
3043/// node of the specified opcode and operands, it returns that node instead of
3044/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003045SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3046 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003047 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003048 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003049 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003050 void *IP = 0;
3051 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003052 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003053
Chris Lattner7651fa42005-08-24 23:00:29 +00003054 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003055
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003056 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003057
Chris Lattner4a283e92006-08-11 18:38:11 +00003058 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003059 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003060}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003061
Evan Cheng95514ba2006-08-26 08:00:10 +00003062SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3063 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003064 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003065 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003066 SDOperand Ops[] = { Op1 };
3067
Jim Laskey583bd472006-10-27 23:46:08 +00003068 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003069 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003070 void *IP = 0;
3071 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003072 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003073
Chris Lattner149c58c2005-08-16 18:17:10 +00003074 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003075 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003076 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003077 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003078}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003079
Evan Cheng95514ba2006-08-26 08:00:10 +00003080SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3081 MVT::ValueType VT, SDOperand Op1,
3082 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003083 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003084 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003085 SDOperand Ops[] = { Op1, Op2 };
3086
Jim Laskey583bd472006-10-27 23:46:08 +00003087 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003088 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003089 void *IP = 0;
3090 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003091 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003092
Chris Lattner149c58c2005-08-16 18:17:10 +00003093 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003094
Chris Lattner63e3f142007-02-04 07:28:00 +00003095 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003096
Chris Lattnera5682852006-08-07 23:03:03 +00003097 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003098 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003099}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003100
Evan Cheng95514ba2006-08-26 08:00:10 +00003101SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3102 MVT::ValueType VT, SDOperand Op1,
3103 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003104 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003105 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003106 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003107 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003108 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003109 void *IP = 0;
3110 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003111 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003112
Chris Lattner149c58c2005-08-16 18:17:10 +00003113 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003114
Chris Lattner63e3f142007-02-04 07:28:00 +00003115 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003116
Chris Lattnera5682852006-08-07 23:03:03 +00003117 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003118 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003119}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003120
Evan Cheng95514ba2006-08-26 08:00:10 +00003121SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003122 MVT::ValueType VT, const SDOperand *Ops,
3123 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003124 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003125 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003126 FoldingSetNodeID ID;
3127 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003128 void *IP = 0;
3129 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003130 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003131
Chris Lattner6b09a292005-08-21 18:49:33 +00003132 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003133 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003134
Chris Lattnera5682852006-08-07 23:03:03 +00003135 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003136 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003137}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003138
Evan Cheng95514ba2006-08-26 08:00:10 +00003139SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3140 MVT::ValueType VT1, MVT::ValueType VT2,
3141 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003142 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003143 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003144 SDOperand Ops[] = { Op1, Op2 };
3145 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003146 void *IP = 0;
3147 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003148 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003149
Chris Lattner0fb094f2005-11-19 01:44:53 +00003150 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003151 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003152 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003153 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003154}
3155
Evan Cheng95514ba2006-08-26 08:00:10 +00003156SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3157 MVT::ValueType VT1, MVT::ValueType VT2,
3158 SDOperand Op1, SDOperand Op2,
3159 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003160 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003161 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003162 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003163 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003164 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003165 void *IP = 0;
3166 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003167 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003168
Chris Lattner0fb094f2005-11-19 01:44:53 +00003169 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003170
Chris Lattner63e3f142007-02-04 07:28:00 +00003171 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003172 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003173 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003174}
3175
Chris Lattner0fb094f2005-11-19 01:44:53 +00003176
Evan Cheng6ae46c42006-02-09 07:15:23 +00003177/// getTargetNode - These are used for target selectors to create a new node
3178/// with specified return type(s), target opcode, and operands.
3179///
3180/// Note that getTargetNode returns the resultant node. If there is already a
3181/// node of the specified opcode and operands, it returns that node instead of
3182/// the current one.
3183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3184 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3185}
3186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3187 SDOperand Op1) {
3188 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3189}
3190SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3191 SDOperand Op1, SDOperand Op2) {
3192 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3193}
3194SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003195 SDOperand Op1, SDOperand Op2,
3196 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003197 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3198}
3199SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003200 const SDOperand *Ops, unsigned NumOps) {
3201 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003202}
3203SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003204 MVT::ValueType VT2) {
3205 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3206 SDOperand Op;
3207 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3208}
3209SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003210 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003211 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003212 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003213}
3214SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003215 MVT::ValueType VT2, SDOperand Op1,
3216 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003217 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003218 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003219 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003220}
3221SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003222 MVT::ValueType VT2, SDOperand Op1,
3223 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003224 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003225 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003226 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003227}
Evan Cheng694481e2006-08-27 08:08:54 +00003228SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3229 MVT::ValueType VT2,
3230 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003231 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003232 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003233}
3234SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3235 MVT::ValueType VT2, MVT::ValueType VT3,
3236 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003237 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003238 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003239 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003240}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003241SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3242 MVT::ValueType VT2, MVT::ValueType VT3,
3243 SDOperand Op1, SDOperand Op2,
3244 SDOperand Op3) {
3245 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3246 SDOperand Ops[] = { Op1, Op2, Op3 };
3247 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3248}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003250 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003251 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003252 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3253 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003254}
Evan Cheng05e69c12007-09-12 23:39:49 +00003255SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3256 MVT::ValueType VT2, MVT::ValueType VT3,
3257 MVT::ValueType VT4,
3258 const SDOperand *Ops, unsigned NumOps) {
3259 std::vector<MVT::ValueType> VTList;
3260 VTList.push_back(VT1);
3261 VTList.push_back(VT2);
3262 VTList.push_back(VT3);
3263 VTList.push_back(VT4);
3264 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3265 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3266}
Evan Cheng39305cf2007-10-05 01:10:49 +00003267SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3268 std::vector<MVT::ValueType> &ResultTys,
3269 const SDOperand *Ops, unsigned NumOps) {
3270 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3271 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3272 Ops, NumOps).Val;
3273}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003274
Evan Cheng08b11732008-03-22 01:55:50 +00003275/// getNodeIfExists - Get the specified node if it's already available, or
3276/// else return NULL.
3277SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
3278 const SDOperand *Ops, unsigned NumOps) {
3279 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3280 FoldingSetNodeID ID;
3281 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3282 void *IP = 0;
3283 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3284 return E;
3285 }
3286 return NULL;
3287}
3288
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003289
Evan Cheng99157a02006-08-07 22:13:29 +00003290/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003291/// This can cause recursive merging of nodes in the DAG.
3292///
Chris Lattner11d049c2008-02-03 03:35:22 +00003293/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003294///
Chris Lattner11d049c2008-02-03 03:35:22 +00003295void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003296 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003297 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003298 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003299 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003300 assert(From != To.Val && "Cannot replace uses of with self");
Evan Cheng6397c642008-04-03 03:13:16 +00003301
Chris Lattner8b8749f2005-08-17 19:00:20 +00003302 while (!From->use_empty()) {
Evan Cheng6397c642008-04-03 03:13:16 +00003303 // Process users until they are all gone.
3304 SDNode *U = *From->use_begin();
3305
Chris Lattner8b8749f2005-08-17 19:00:20 +00003306 // This node is about to morph, remove its old self from the CSE maps.
3307 RemoveNodeFromCSEMaps(U);
Evan Cheng6397c642008-04-03 03:13:16 +00003308
3309 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3310 I != E; ++I)
Chris Lattner65113b22005-11-08 22:07:03 +00003311 if (I->Val == From) {
Evan Cheng6397c642008-04-03 03:13:16 +00003312 From->removeUser(U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003313 *I = To;
Evan Cheng6397c642008-04-03 03:13:16 +00003314 To.Val->addUser(U);
3315 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003316
3317 // Now that we have modified U, add it back to the CSE maps. If it already
3318 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003319 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003320 ReplaceAllUsesWith(U, Existing, UpdateListener);
3321 // U is now dead. Inform the listener if it exists and delete it.
3322 if (UpdateListener)
3323 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003324 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003325 } else {
3326 // If the node doesn't already exist, we updated it. Inform a listener if
3327 // it exists.
3328 if (UpdateListener)
3329 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003330 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003331 }
3332}
3333
3334/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3335/// This can cause recursive merging of nodes in the DAG.
3336///
3337/// This version assumes From/To have matching types and numbers of result
3338/// values.
3339///
Chris Lattner1e111c72005-09-07 05:37:01 +00003340void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003341 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003342 assert(From != To && "Cannot replace uses of with self");
3343 assert(From->getNumValues() == To->getNumValues() &&
3344 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003345 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003346 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3347 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003348
3349 while (!From->use_empty()) {
Evan Cheng6397c642008-04-03 03:13:16 +00003350 // Process users until they are all gone.
3351 SDNode *U = *From->use_begin();
3352
Chris Lattner8b52f212005-08-26 18:36:28 +00003353 // This node is about to morph, remove its old self from the CSE maps.
3354 RemoveNodeFromCSEMaps(U);
Evan Cheng6397c642008-04-03 03:13:16 +00003355
3356 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3357 I != E; ++I)
Chris Lattner65113b22005-11-08 22:07:03 +00003358 if (I->Val == From) {
Evan Cheng6397c642008-04-03 03:13:16 +00003359 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003360 I->Val = To;
Evan Cheng6397c642008-04-03 03:13:16 +00003361 To->addUser(U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003362 }
Evan Cheng6397c642008-04-03 03:13:16 +00003363
Chris Lattner8b8749f2005-08-17 19:00:20 +00003364 // Now that we have modified U, add it back to the CSE maps. If it already
3365 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003366 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003367 ReplaceAllUsesWith(U, Existing, UpdateListener);
3368 // U is now dead. Inform the listener if it exists and delete it.
3369 if (UpdateListener)
3370 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003371 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003372 } else {
3373 // If the node doesn't already exist, we updated it. Inform a listener if
3374 // it exists.
3375 if (UpdateListener)
3376 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003377 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003378 }
3379}
3380
Chris Lattner8b52f212005-08-26 18:36:28 +00003381/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3382/// This can cause recursive merging of nodes in the DAG.
3383///
3384/// This version can replace From with any result values. To must match the
3385/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003386void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003387 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003388 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003389 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003390 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003391
3392 while (!From->use_empty()) {
Evan Cheng6397c642008-04-03 03:13:16 +00003393 // Process users until they are all gone.
3394 SDNode *U = *From->use_begin();
3395
Chris Lattner7b2880c2005-08-24 22:44:39 +00003396 // This node is about to morph, remove its old self from the CSE maps.
3397 RemoveNodeFromCSEMaps(U);
Evan Cheng6397c642008-04-03 03:13:16 +00003398
3399 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3400 I != E; ++I)
Chris Lattner65113b22005-11-08 22:07:03 +00003401 if (I->Val == From) {
3402 const SDOperand &ToOp = To[I->ResNo];
Evan Cheng6397c642008-04-03 03:13:16 +00003403 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003404 *I = ToOp;
Evan Cheng6397c642008-04-03 03:13:16 +00003405 ToOp.Val->addUser(U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003406 }
Evan Cheng6397c642008-04-03 03:13:16 +00003407
Chris Lattner7b2880c2005-08-24 22:44:39 +00003408 // Now that we have modified U, add it back to the CSE maps. If it already
3409 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003410 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003411 ReplaceAllUsesWith(U, Existing, UpdateListener);
3412 // U is now dead. Inform the listener if it exists and delete it.
3413 if (UpdateListener)
3414 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003415 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003416 } else {
3417 // If the node doesn't already exist, we updated it. Inform a listener if
3418 // it exists.
3419 if (UpdateListener)
3420 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003421 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003422 }
3423}
3424
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003425namespace {
3426 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3427 /// any deleted nodes from the set passed into its constructor and recursively
3428 /// notifies another update listener if specified.
3429 class ChainedSetUpdaterListener :
3430 public SelectionDAG::DAGUpdateListener {
3431 SmallSetVector<SDNode*, 16> &Set;
3432 SelectionDAG::DAGUpdateListener *Chain;
3433 public:
3434 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3435 SelectionDAG::DAGUpdateListener *chain)
3436 : Set(set), Chain(chain) {}
Evan Cheng6397c642008-04-03 03:13:16 +00003437
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003438 virtual void NodeDeleted(SDNode *N) {
3439 Set.remove(N);
3440 if (Chain) Chain->NodeDeleted(N);
3441 }
3442 virtual void NodeUpdated(SDNode *N) {
3443 if (Chain) Chain->NodeUpdated(N);
3444 }
3445 };
3446}
3447
Chris Lattner012f2412006-02-17 21:58:01 +00003448/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3449/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003450/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003451void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003452 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003453 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003454
Chris Lattner012f2412006-02-17 21:58:01 +00003455 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003456 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003457 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003458 return;
3459 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003460
3461 if (From.use_empty()) return;
3462
Chris Lattnercf5640b2007-02-04 00:14:31 +00003463 // Get all of the users of From.Val. We want these in a nice,
3464 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Evan Cheng6397c642008-04-03 03:13:16 +00003465 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003466
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003467 // When one of the recursive merges deletes nodes from the graph, we need to
3468 // make sure that UpdateListener is notified *and* that the node is removed
3469 // from Users if present. CSUL does this.
3470 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003471
Chris Lattner012f2412006-02-17 21:58:01 +00003472 while (!Users.empty()) {
3473 // We know that this user uses some value of From. If it is the right
3474 // value, update it.
3475 SDNode *User = Users.back();
3476 Users.pop_back();
3477
Chris Lattner01d029b2007-10-15 06:10:22 +00003478 // Scan for an operand that matches From.
Evan Cheng6397c642008-04-03 03:13:16 +00003479 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
Chris Lattner01d029b2007-10-15 06:10:22 +00003480 for (; Op != E; ++Op)
3481 if (*Op == From) break;
3482
3483 // If there are no matches, the user must use some other result of From.
3484 if (Op == E) continue;
3485
3486 // Okay, we know this user needs to be updated. Remove its old self
3487 // from the CSE maps.
3488 RemoveNodeFromCSEMaps(User);
3489
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003490 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003491 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003492 if (*Op == From) {
Evan Cheng6397c642008-04-03 03:13:16 +00003493 From.Val->removeUser(User);
3494 *Op = To;
3495 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003496 }
3497 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003498
3499 // Now that we have modified User, add it back to the CSE maps. If it
3500 // already exists there, recursively merge the results together.
3501 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003502 if (!Existing) {
3503 if (UpdateListener) UpdateListener->NodeUpdated(User);
3504 continue; // Continue on to next user.
3505 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003506
3507 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003508 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003509 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003510 // can cause deletion of nodes that used the old value. To handle this, we
3511 // use CSUL to remove them from the Users set.
3512 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003513
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003514 // User is now dead. Notify a listener if present.
3515 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003516 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003517 }
3518}
3519
Chris Lattner7b2880c2005-08-24 22:44:39 +00003520
Evan Chenge6f35d82006-08-01 08:20:41 +00003521/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3522/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003523unsigned SelectionDAG::AssignNodeIds() {
3524 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003525 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3526 SDNode *N = I;
3527 N->setNodeId(Id++);
3528 }
3529 return Id;
3530}
3531
Evan Chenge6f35d82006-08-01 08:20:41 +00003532/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003533/// based on their topological order. It returns the maximum id and a vector
3534/// of the SDNodes* in assigned order by reference.
3535unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003536 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003537 std::vector<unsigned> InDegree(DAGSize);
3538 std::vector<SDNode*> Sources;
3539
3540 // Use a two pass approach to avoid using a std::map which is slow.
3541 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003542 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3543 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003544 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003545 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003546 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003547 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003548 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003549 }
3550
Evan Cheng99157a02006-08-07 22:13:29 +00003551 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003552 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003553 SDNode *N = Sources.back();
3554 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003555 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003556 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3557 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003558 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003559 if (Degree == 0)
3560 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003561 }
3562 }
3563
Evan Chengc384d6c2006-08-02 22:00:34 +00003564 // Second pass, assign the actual topological order as node ids.
3565 Id = 0;
3566 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3567 TI != TE; ++TI)
3568 (*TI)->setNodeId(Id++);
3569
3570 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003571}
3572
3573
Evan Cheng091cba12006-07-27 06:39:06 +00003574
Jim Laskey58b968b2005-08-17 20:08:02 +00003575//===----------------------------------------------------------------------===//
3576// SDNode Class
3577//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003578
Chris Lattner917d2c92006-07-19 00:00:37 +00003579// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003580void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003581void UnarySDNode::ANCHOR() {}
3582void BinarySDNode::ANCHOR() {}
3583void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003584void HandleSDNode::ANCHOR() {}
3585void StringSDNode::ANCHOR() {}
3586void ConstantSDNode::ANCHOR() {}
3587void ConstantFPSDNode::ANCHOR() {}
3588void GlobalAddressSDNode::ANCHOR() {}
3589void FrameIndexSDNode::ANCHOR() {}
3590void JumpTableSDNode::ANCHOR() {}
3591void ConstantPoolSDNode::ANCHOR() {}
3592void BasicBlockSDNode::ANCHOR() {}
3593void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003594void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003595void RegisterSDNode::ANCHOR() {}
3596void ExternalSymbolSDNode::ANCHOR() {}
3597void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00003598void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003599void VTSDNode::ANCHOR() {}
3600void LoadSDNode::ANCHOR() {}
3601void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003602void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003603
Chris Lattner48b85922007-02-04 02:41:42 +00003604HandleSDNode::~HandleSDNode() {
3605 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003606 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003607}
3608
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003609GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3610 MVT::ValueType VT, int o)
3611 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003612 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003613 // Thread Local
3614 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3615 // Non Thread Local
3616 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3617 getSDVTList(VT)), Offset(o) {
3618 TheGlobal = const_cast<GlobalValue*>(GA);
3619}
Chris Lattner48b85922007-02-04 02:41:42 +00003620
Dan Gohman69de1932008-02-06 22:27:42 +00003621/// getMemOperand - Return a MemOperand object describing the memory
3622/// reference performed by this load or store.
3623MemOperand LSBaseSDNode::getMemOperand() const {
3624 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3625 int Flags =
3626 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3627 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3628
3629 // Check if the load references a frame index, and does not have
3630 // an SV attached.
3631 const FrameIndexSDNode *FI =
3632 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3633 if (!getSrcValue() && FI)
Dan Gohman3069b872008-02-07 18:41:25 +00003634 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
Dan Gohman69de1932008-02-06 22:27:42 +00003635 FI->getIndex(), Size, Alignment);
3636 else
3637 return MemOperand(getSrcValue(), Flags,
3638 getSrcValueOffset(), Size, Alignment);
3639}
3640
Jim Laskey583bd472006-10-27 23:46:08 +00003641/// Profile - Gather unique data for the node.
3642///
3643void SDNode::Profile(FoldingSetNodeID &ID) {
3644 AddNodeIDNode(ID, this);
3645}
3646
Chris Lattnera3255112005-11-08 23:30:28 +00003647/// getValueTypeList - Return a pointer to the specified value type.
3648///
Dan Gohman547ca532008-02-08 03:26:46 +00003649const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003650 if (MVT::isExtendedVT(VT)) {
3651 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00003652 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00003653 } else {
3654 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3655 VTs[VT] = VT;
3656 return &VTs[VT];
3657 }
Chris Lattnera3255112005-11-08 23:30:28 +00003658}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003659
Chris Lattner5c884562005-01-12 18:37:47 +00003660/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3661/// indicated value. This method ignores uses of other values defined by this
3662/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003663bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003664 assert(Value < getNumValues() && "Bad value!");
3665
3666 // If there is only one value, this is easy.
3667 if (getNumValues() == 1)
3668 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003669 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003670
Evan Cheng4ee62112006-02-05 06:29:23 +00003671 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003672
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003673 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003674
Evan Cheng6397c642008-04-03 03:13:16 +00003675 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3676 SDNode *User = *UI;
3677 if (User->getNumOperands() == 1 ||
3678 UsersHandled.insert(User)) // First time we've seen this?
3679 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3680 if (User->getOperand(i) == TheValue) {
3681 if (NUses == 0)
3682 return false; // too many uses
3683 --NUses;
3684 }
Chris Lattner5c884562005-01-12 18:37:47 +00003685 }
3686
3687 // Found exactly the right number of uses?
3688 return NUses == 0;
3689}
3690
3691
Evan Cheng33d55952007-08-02 05:29:38 +00003692/// hasAnyUseOfValue - Return true if there are any use of the indicated
3693/// value. This method ignores uses of other values defined by this operation.
3694bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3695 assert(Value < getNumValues() && "Bad value!");
3696
Dan Gohman30359592008-01-29 13:02:09 +00003697 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003698
3699 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3700
3701 SmallPtrSet<SDNode*, 32> UsersHandled;
3702
Evan Cheng6397c642008-04-03 03:13:16 +00003703 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3704 SDNode *User = *UI;
Evan Cheng33d55952007-08-02 05:29:38 +00003705 if (User->getNumOperands() == 1 ||
3706 UsersHandled.insert(User)) // First time we've seen this?
3707 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3708 if (User->getOperand(i) == TheValue) {
3709 return true;
3710 }
3711 }
3712
3713 return false;
3714}
3715
3716
Evan Cheng917be682008-03-04 00:41:45 +00003717/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00003718///
Evan Cheng917be682008-03-04 00:41:45 +00003719bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00003720 bool Seen = false;
3721 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Evan Cheng6397c642008-04-03 03:13:16 +00003722 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00003723 if (User == this)
3724 Seen = true;
3725 else
3726 return false;
3727 }
3728
3729 return Seen;
3730}
3731
Evan Chenge6e97e62006-11-03 07:31:32 +00003732/// isOperand - Return true if this node is an operand of N.
3733///
Evan Cheng6397c642008-04-03 03:13:16 +00003734bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00003735 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3736 if (*this == N->getOperand(i))
3737 return true;
3738 return false;
3739}
3740
Evan Cheng917be682008-03-04 00:41:45 +00003741bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003742 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3743 if (this == N->OperandList[i].Val)
3744 return true;
3745 return false;
3746}
Evan Cheng4ee62112006-02-05 06:29:23 +00003747
Chris Lattner572dee72008-01-16 05:49:24 +00003748/// reachesChainWithoutSideEffects - Return true if this operand (which must
3749/// be a chain) reaches the specified operand without crossing any
3750/// side-effecting instructions. In practice, this looks through token
3751/// factors and non-volatile loads. In order to remain efficient, this only
3752/// looks a couple of nodes in, it does not do an exhaustive search.
Evan Cheng6397c642008-04-03 03:13:16 +00003753bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00003754 unsigned Depth) const {
3755 if (*this == Dest) return true;
3756
3757 // Don't search too deeply, we just want to be able to see through
3758 // TokenFactor's etc.
3759 if (Depth == 0) return false;
3760
3761 // If this is a token factor, all inputs to the TF happen in parallel. If any
3762 // of the operands of the TF reach dest, then we can do the xform.
3763 if (getOpcode() == ISD::TokenFactor) {
3764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3765 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3766 return true;
3767 return false;
3768 }
3769
3770 // Loads don't have side effects, look through them.
3771 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3772 if (!Ld->isVolatile())
3773 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3774 }
3775 return false;
3776}
3777
3778
Evan Chengc5fc57d2006-11-03 03:05:24 +00003779static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003780 SmallPtrSet<SDNode *, 32> &Visited) {
3781 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003782 return;
3783
3784 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3785 SDNode *Op = N->getOperand(i).Val;
3786 if (Op == P) {
3787 found = true;
3788 return;
3789 }
3790 findPredecessor(Op, P, found, Visited);
3791 }
3792}
3793
Evan Cheng917be682008-03-04 00:41:45 +00003794/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00003795/// is either an operand of N or it can be reached by recursively traversing
3796/// up the operands.
3797/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00003798bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003799 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003800 bool found = false;
3801 findPredecessor(N, this, found, Visited);
3802 return found;
3803}
3804
Evan Chengc5484282006-10-04 00:56:09 +00003805uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3806 assert(Num < NumOperands && "Invalid child # of SDNode!");
3807 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3808}
3809
Reid Spencer577cc322007-04-01 07:32:19 +00003810std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003811 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003812 default:
3813 if (getOpcode() < ISD::BUILTIN_OP_END)
3814 return "<<Unknown DAG Node>>";
3815 else {
Evan Cheng72261582005-12-20 06:22:03 +00003816 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003817 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003818 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003819 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003820
Evan Cheng72261582005-12-20 06:22:03 +00003821 TargetLowering &TLI = G->getTargetLoweringInfo();
3822 const char *Name =
3823 TLI.getTargetNodeName(getOpcode());
3824 if (Name) return Name;
3825 }
3826
3827 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003828 }
3829
Evan Cheng27b7db52008-03-08 00:58:38 +00003830 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00003831 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003832 case ISD::ATOMIC_LCS: return "AtomicLCS";
3833 case ISD::ATOMIC_LAS: return "AtomicLAS";
3834 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00003835 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003836 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003837 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00003838 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003839 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003840 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003841 case ISD::AssertSext: return "AssertSext";
3842 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003843
3844 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003845 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00003846 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003847 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003848 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003849
3850 case ISD::Constant: return "Constant";
3851 case ISD::ConstantFP: return "ConstantFP";
3852 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003853 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003854 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003855 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003856 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003857 case ISD::RETURNADDR: return "RETURNADDR";
3858 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003859 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003860 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3861 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003862 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003863 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003864 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003865 case ISD::INTRINSIC_WO_CHAIN: {
3866 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3867 return Intrinsic::getName((Intrinsic::ID)IID);
3868 }
3869 case ISD::INTRINSIC_VOID:
3870 case ISD::INTRINSIC_W_CHAIN: {
3871 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003872 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003873 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003874
Chris Lattnerb2827b02006-03-19 00:52:58 +00003875 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003876 case ISD::TargetConstant: return "TargetConstant";
3877 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003878 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003879 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003880 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003881 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003882 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003883 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003884
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003885 case ISD::CopyToReg: return "CopyToReg";
3886 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003887 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003888 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003889 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003890 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003891 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003892 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003893 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003894 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003895
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003896 // Unary operators
3897 case ISD::FABS: return "fabs";
3898 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003899 case ISD::FSQRT: return "fsqrt";
3900 case ISD::FSIN: return "fsin";
3901 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003902 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003903 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003904
3905 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003906 case ISD::ADD: return "add";
3907 case ISD::SUB: return "sub";
3908 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003909 case ISD::MULHU: return "mulhu";
3910 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003911 case ISD::SDIV: return "sdiv";
3912 case ISD::UDIV: return "udiv";
3913 case ISD::SREM: return "srem";
3914 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003915 case ISD::SMUL_LOHI: return "smul_lohi";
3916 case ISD::UMUL_LOHI: return "umul_lohi";
3917 case ISD::SDIVREM: return "sdivrem";
3918 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003919 case ISD::AND: return "and";
3920 case ISD::OR: return "or";
3921 case ISD::XOR: return "xor";
3922 case ISD::SHL: return "shl";
3923 case ISD::SRA: return "sra";
3924 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003925 case ISD::ROTL: return "rotl";
3926 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003927 case ISD::FADD: return "fadd";
3928 case ISD::FSUB: return "fsub";
3929 case ISD::FMUL: return "fmul";
3930 case ISD::FDIV: return "fdiv";
3931 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003932 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003933 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003934
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003935 case ISD::SETCC: return "setcc";
3936 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003937 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003938 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003939 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003940 case ISD::CONCAT_VECTORS: return "concat_vectors";
3941 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003942 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003943 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003944 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003945 case ISD::ADDC: return "addc";
3946 case ISD::ADDE: return "adde";
3947 case ISD::SUBC: return "subc";
3948 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003949 case ISD::SHL_PARTS: return "shl_parts";
3950 case ISD::SRA_PARTS: return "sra_parts";
3951 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003952
3953 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3954 case ISD::INSERT_SUBREG: return "insert_subreg";
3955
Chris Lattner7f644642005-04-28 21:44:03 +00003956 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003957 case ISD::SIGN_EXTEND: return "sign_extend";
3958 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003959 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003960 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003961 case ISD::TRUNCATE: return "truncate";
3962 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003963 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003964 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003965 case ISD::FP_EXTEND: return "fp_extend";
3966
3967 case ISD::SINT_TO_FP: return "sint_to_fp";
3968 case ISD::UINT_TO_FP: return "uint_to_fp";
3969 case ISD::FP_TO_SINT: return "fp_to_sint";
3970 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003971 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003972
3973 // Control flow instructions
3974 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003975 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003976 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003977 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003978 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003979 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003980 case ISD::CALLSEQ_START: return "callseq_start";
3981 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003982
3983 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003984 case ISD::LOAD: return "load";
3985 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003986 case ISD::VAARG: return "vaarg";
3987 case ISD::VACOPY: return "vacopy";
3988 case ISD::VAEND: return "vaend";
3989 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003990 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003991 case ISD::EXTRACT_ELEMENT: return "extract_element";
3992 case ISD::BUILD_PAIR: return "build_pair";
3993 case ISD::STACKSAVE: return "stacksave";
3994 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003995 case ISD::TRAP: return "trap";
3996
Chris Lattner5a67afc2006-01-13 02:39:42 +00003997 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003998 case ISD::MEMSET: return "memset";
3999 case ISD::MEMCPY: return "memcpy";
4000 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004001
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004002 // Bit manipulation
4003 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004004 case ISD::CTPOP: return "ctpop";
4005 case ISD::CTTZ: return "cttz";
4006 case ISD::CTLZ: return "ctlz";
4007
Chris Lattner36ce6912005-11-29 06:21:05 +00004008 // Debug info
4009 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004010 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004011
Duncan Sands36397f52007-07-27 12:58:54 +00004012 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004013 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004014
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004015 case ISD::CONDCODE:
4016 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004017 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004018 case ISD::SETOEQ: return "setoeq";
4019 case ISD::SETOGT: return "setogt";
4020 case ISD::SETOGE: return "setoge";
4021 case ISD::SETOLT: return "setolt";
4022 case ISD::SETOLE: return "setole";
4023 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004024
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004025 case ISD::SETO: return "seto";
4026 case ISD::SETUO: return "setuo";
4027 case ISD::SETUEQ: return "setue";
4028 case ISD::SETUGT: return "setugt";
4029 case ISD::SETUGE: return "setuge";
4030 case ISD::SETULT: return "setult";
4031 case ISD::SETULE: return "setule";
4032 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004033
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004034 case ISD::SETEQ: return "seteq";
4035 case ISD::SETGT: return "setgt";
4036 case ISD::SETGE: return "setge";
4037 case ISD::SETLT: return "setlt";
4038 case ISD::SETLE: return "setle";
4039 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004040 }
4041 }
4042}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004043
Evan Cheng144d8f02006-11-09 17:55:04 +00004044const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004045 switch (AM) {
4046 default:
4047 return "";
4048 case ISD::PRE_INC:
4049 return "<pre-inc>";
4050 case ISD::PRE_DEC:
4051 return "<pre-dec>";
4052 case ISD::POST_INC:
4053 return "<post-inc>";
4054 case ISD::POST_DEC:
4055 return "<post-dec>";
4056 }
4057}
4058
Duncan Sands276dcbd2008-03-21 09:14:45 +00004059std::string ISD::ArgFlagsTy::getArgFlagsString() {
4060 std::string S = "< ";
4061
4062 if (isZExt())
4063 S += "zext ";
4064 if (isSExt())
4065 S += "sext ";
4066 if (isInReg())
4067 S += "inreg ";
4068 if (isSRet())
4069 S += "sret ";
4070 if (isByVal())
4071 S += "byval ";
4072 if (isNest())
4073 S += "nest ";
4074 if (getByValAlign())
4075 S += "byval-align:" + utostr(getByValAlign()) + " ";
4076 if (getOrigAlign())
4077 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4078 if (getByValSize())
4079 S += "byval-size:" + utostr(getByValSize()) + " ";
4080 return S + ">";
4081}
4082
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004083void SDNode::dump() const { dump(0); }
4084void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004085 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004086
4087 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004088 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004089 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004090 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004091 else
Bill Wendling832171c2006-12-07 20:04:42 +00004092 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004093 }
Bill Wendling832171c2006-12-07 20:04:42 +00004094 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004095
Bill Wendling832171c2006-12-07 20:04:42 +00004096 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004097 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004098 if (i) cerr << ", ";
4099 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004100 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004101 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004102 }
4103
Evan Chengce254432007-12-11 02:08:35 +00004104 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4105 SDNode *Mask = getOperand(2).Val;
4106 cerr << "<";
4107 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4108 if (i) cerr << ",";
4109 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4110 cerr << "u";
4111 else
4112 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4113 }
4114 cerr << ">";
4115 }
4116
Chris Lattnerc3aae252005-01-07 07:46:32 +00004117 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004118 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004119 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004120 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4121 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4122 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4123 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4124 else {
4125 cerr << "<APFloat(";
4126 CSDN->getValueAPF().convertToAPInt().dump();
4127 cerr << ")>";
4128 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004129 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004130 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004131 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004132 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004133 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004134 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004135 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004136 else
Bill Wendling832171c2006-12-07 20:04:42 +00004137 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004138 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004139 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004140 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004141 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004142 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004143 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004144 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004145 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004146 else
Bill Wendling832171c2006-12-07 20:04:42 +00004147 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004148 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004149 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004150 else
Bill Wendling832171c2006-12-07 20:04:42 +00004151 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004152 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004153 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004154 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4155 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004156 cerr << LBB->getName() << " ";
4157 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004158 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004159 if (G && R->getReg() &&
4160 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004161 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004162 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004163 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004164 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004165 } else if (const ExternalSymbolSDNode *ES =
4166 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004167 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004168 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4169 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004170 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004171 else
Dan Gohman69de1932008-02-06 22:27:42 +00004172 cerr << "<null>";
4173 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4174 if (M->MO.getValue())
4175 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4176 else
4177 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004178 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4179 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004180 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004181 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004182 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004183 const Value *SrcValue = LD->getSrcValue();
4184 int SrcOffset = LD->getSrcValueOffset();
4185 cerr << " <";
4186 if (SrcValue)
4187 cerr << SrcValue;
4188 else
4189 cerr << "null";
4190 cerr << ":" << SrcOffset << ">";
4191
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004192 bool doExt = true;
4193 switch (LD->getExtensionType()) {
4194 default: doExt = false; break;
4195 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004196 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004197 break;
4198 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004199 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004200 break;
4201 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004202 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004203 break;
4204 }
4205 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004206 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004207
Evan Cheng144d8f02006-11-09 17:55:04 +00004208 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004209 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004210 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004211 if (LD->isVolatile())
4212 cerr << " <volatile>";
4213 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004214 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004215 const Value *SrcValue = ST->getSrcValue();
4216 int SrcOffset = ST->getSrcValueOffset();
4217 cerr << " <";
4218 if (SrcValue)
4219 cerr << SrcValue;
4220 else
4221 cerr << "null";
4222 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004223
4224 if (ST->isTruncatingStore())
4225 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004226 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004227
4228 const char *AM = getIndexedModeName(ST->getAddressingMode());
4229 if (*AM)
4230 cerr << " " << AM;
4231 if (ST->isVolatile())
4232 cerr << " <volatile>";
4233 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004234 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004235}
4236
Chris Lattnerde202b32005-11-09 23:47:37 +00004237static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004238 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4239 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004240 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004241 else
Bill Wendling832171c2006-12-07 20:04:42 +00004242 cerr << "\n" << std::string(indent+2, ' ')
4243 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004244
Chris Lattnerea946cd2005-01-09 20:38:33 +00004245
Bill Wendling832171c2006-12-07 20:04:42 +00004246 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004247 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004248}
4249
Chris Lattnerc3aae252005-01-07 07:46:32 +00004250void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004251 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004252 std::vector<const SDNode*> Nodes;
4253 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4254 I != E; ++I)
4255 Nodes.push_back(I);
4256
Chris Lattner49d24712005-01-09 20:26:36 +00004257 std::sort(Nodes.begin(), Nodes.end());
4258
4259 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004260 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004261 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004262 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004263
Jim Laskey26f7fa72006-10-17 19:33:52 +00004264 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004265
Bill Wendling832171c2006-12-07 20:04:42 +00004266 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004267}
4268
Evan Chengd6594ae2006-09-12 21:00:35 +00004269const Type *ConstantPoolSDNode::getType() const {
4270 if (isMachineConstantPoolEntry())
4271 return Val.MachineCPVal->getType();
4272 return Val.ConstVal->getType();
4273}