blob: a1d6c779b9b3ff8eb72ebbaebe5ce15fe37b31ae [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//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000015#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000025#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000027#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000028#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000031#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000032#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000033#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000034#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000035#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000036#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000037#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattner0b3e5252006-08-15 19:11:05 +000040/// makeVTList - Return an instance of the SDVTList struct initialized with the
41/// specified members.
42static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
43 SDVTList Res = {VTs, NumVTs};
44 return Res;
45}
46
Chris Lattnerec4a5672008-03-05 06:48:13 +000047static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
48 switch (VT) {
49 default: assert(0 && "Unknown FP format");
50 case MVT::f32: return &APFloat::IEEEsingle;
51 case MVT::f64: return &APFloat::IEEEdouble;
52 case MVT::f80: return &APFloat::x87DoubleExtended;
53 case MVT::f128: return &APFloat::IEEEquad;
54 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
55 }
56}
57
Chris Lattnerf8dc0612008-02-03 06:49:24 +000058SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
59
Jim Laskey58b968b2005-08-17 20:08:02 +000060//===----------------------------------------------------------------------===//
61// ConstantFPSDNode Class
62//===----------------------------------------------------------------------===//
63
64/// isExactlyValue - We don't rely on operator== working on double values, as
65/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
66/// As such, this method can be used to do an exact bit-for-bit comparison of
67/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000068bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000069 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000070}
71
Dale Johannesenf04afdb2007-08-30 00:23:21 +000072bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
73 const APFloat& Val) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000074 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
75
76 // Anything can be extended to ppc long double.
77 if (VT == MVT::ppcf128)
78 return true;
79
80 // PPC long double cannot be shrunk to anything though.
81 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
82 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
99 N = N->getOperand(0).Val;
100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000114 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
155 SDOperand Zero = N->getOperand(i);
156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
187 SDOperand V = N->getOperand(i);
188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000196/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000197/// is 0).
198bool ISD::isDebugLabel(const SDNode *N) {
199 SDOperand Zero;
200 if (N->getOpcode() == ISD::LABEL)
201 Zero = N->getOperand(2);
202 else if (N->isTargetOpcode() &&
203 N->getTargetOpcode() == TargetInstrInfo::LABEL)
204 // Chain moved to last operand.
205 Zero = N->getOperand(1);
206 else
207 return false;
208 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
209}
210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
212/// when given the operation for (X op Y).
213ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
214 // To perform this operation, we just need to swap the L and G bits of the
215 // operation.
216 unsigned OldL = (Operation >> 2) & 1;
217 unsigned OldG = (Operation >> 1) & 1;
218 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
219 (OldL << 1) | // New G bit
220 (OldG << 2)); // New L bit.
221}
222
223/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
224/// 'op' is a valid SetCC operation.
225ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
226 unsigned Operation = Op;
227 if (isInteger)
228 Operation ^= 7; // Flip L, G, E bits, but not U.
229 else
230 Operation ^= 15; // Flip all of the condition bits.
231 if (Operation > ISD::SETTRUE2)
232 Operation &= ~8; // Don't let N and U bits get set.
233 return ISD::CondCode(Operation);
234}
235
236
237/// isSignedOp - For an integer comparison, return 1 if the comparison is a
238/// signed operation and 2 if the result is an unsigned comparison. Return zero
239/// if the operation does not depend on the sign of the input (setne and seteq).
240static int isSignedOp(ISD::CondCode Opcode) {
241 switch (Opcode) {
242 default: assert(0 && "Illegal integer setcc operation!");
243 case ISD::SETEQ:
244 case ISD::SETNE: return 0;
245 case ISD::SETLT:
246 case ISD::SETLE:
247 case ISD::SETGT:
248 case ISD::SETGE: return 1;
249 case ISD::SETULT:
250 case ISD::SETULE:
251 case ISD::SETUGT:
252 case ISD::SETUGE: return 2;
253 }
254}
255
256/// getSetCCOrOperation - Return the result of a logical OR between different
257/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
258/// returns SETCC_INVALID if it is not possible to represent the resultant
259/// comparison.
260ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
261 bool isInteger) {
262 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
263 // Cannot fold a signed integer setcc with an unsigned integer setcc.
264 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000265
Chris Lattnerc3aae252005-01-07 07:46:32 +0000266 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267
Chris Lattnerc3aae252005-01-07 07:46:32 +0000268 // If the N and U bits get set then the resultant comparison DOES suddenly
269 // care about orderedness, and is true when ordered.
270 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000271 Op &= ~16; // Clear the U bit if the N bit is set.
272
273 // Canonicalize illegal integer setcc's.
274 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
275 Op = ISD::SETNE;
276
Chris Lattnerc3aae252005-01-07 07:46:32 +0000277 return ISD::CondCode(Op);
278}
279
280/// getSetCCAndOperation - Return the result of a logical AND between different
281/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
282/// function returns zero if it is not possible to represent the resultant
283/// comparison.
284ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
285 bool isInteger) {
286 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
287 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000288 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289
290 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000291 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
292
293 // Canonicalize illegal integer setcc's.
294 if (isInteger) {
295 switch (Result) {
296 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000297 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
298 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
299 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
300 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000301 }
302 }
303
304 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000305}
306
Chris Lattnerb48da392005-01-23 04:39:44 +0000307const TargetMachine &SelectionDAG::getTarget() const {
308 return TLI.getTargetMachine();
309}
310
Jim Laskey58b968b2005-08-17 20:08:02 +0000311//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000312// SDNode Profile Support
313//===----------------------------------------------------------------------===//
314
Jim Laskeydef69b92006-10-27 23:52:51 +0000315/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
316///
Jim Laskey583bd472006-10-27 23:46:08 +0000317static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
318 ID.AddInteger(OpC);
319}
320
321/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
322/// solely with their pointer.
323void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
324 ID.AddPointer(VTList.VTs);
325}
326
Jim Laskeydef69b92006-10-27 23:52:51 +0000327/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
328///
Jim Laskey583bd472006-10-27 23:46:08 +0000329static void AddNodeIDOperands(FoldingSetNodeID &ID,
330 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 for (; NumOps; --NumOps, ++Ops) {
332 ID.AddPointer(Ops->Val);
333 ID.AddInteger(Ops->ResNo);
334 }
Jim Laskey583bd472006-10-27 23:46:08 +0000335}
336
Jim Laskey583bd472006-10-27 23:46:08 +0000337static void AddNodeIDNode(FoldingSetNodeID &ID,
338 unsigned short OpC, SDVTList VTList,
339 const SDOperand *OpList, unsigned N) {
340 AddNodeIDOpcode(ID, OpC);
341 AddNodeIDValueTypes(ID, VTList);
342 AddNodeIDOperands(ID, OpList, N);
343}
344
Jim Laskeydef69b92006-10-27 23:52:51 +0000345/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
346/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000347static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
348 AddNodeIDOpcode(ID, N->getOpcode());
349 // Add the return value info.
350 AddNodeIDValueTypes(ID, N->getVTList());
351 // Add the operand info.
352 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
353
354 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000357 case ISD::ARG_FLAGS:
358 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
359 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 case ISD::TargetConstant:
361 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000362 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 break;
364 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000365 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000366 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000368 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000370 case ISD::GlobalAddress:
371 case ISD::TargetGlobalTLSAddress:
372 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
374 ID.AddPointer(GA->getGlobal());
375 ID.AddInteger(GA->getOffset());
376 break;
377 }
378 case ISD::BasicBlock:
379 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 break;
381 case ISD::Register:
382 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000384 case ISD::SRCVALUE:
385 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
386 break;
387 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000388 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000389 ID.AddPointer(MO.getValue());
390 ID.AddInteger(MO.getFlags());
391 ID.AddInteger(MO.getOffset());
392 ID.AddInteger(MO.getSize());
393 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000394 break;
395 }
396 case ISD::FrameIndex:
397 case ISD::TargetFrameIndex:
398 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
399 break;
400 case ISD::JumpTable:
401 case ISD::TargetJumpTable:
402 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
403 break;
404 case ISD::ConstantPool:
405 case ISD::TargetConstantPool: {
406 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
407 ID.AddInteger(CP->getAlignment());
408 ID.AddInteger(CP->getOffset());
409 if (CP->isMachineConstantPoolEntry())
410 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
411 else
412 ID.AddPointer(CP->getConstVal());
413 break;
414 }
415 case ISD::LOAD: {
416 LoadSDNode *LD = cast<LoadSDNode>(N);
417 ID.AddInteger(LD->getAddressingMode());
418 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000419 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 ID.AddInteger(LD->getAlignment());
421 ID.AddInteger(LD->isVolatile());
422 break;
423 }
424 case ISD::STORE: {
425 StoreSDNode *ST = cast<StoreSDNode>(N);
426 ID.AddInteger(ST->getAddressingMode());
427 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000428 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 ID.AddInteger(ST->getAlignment());
430 ID.AddInteger(ST->isVolatile());
431 break;
432 }
Jim Laskey583bd472006-10-27 23:46:08 +0000433 }
434}
435
436//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000437// SelectionDAG Class
438//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000439
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000440/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000441/// SelectionDAG.
442void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000443 // Create a dummy node (which is not added to allnodes), that adds a reference
444 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000445 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000446
Chris Lattner190a4182006-08-04 17:45:20 +0000447 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000448
Chris Lattner190a4182006-08-04 17:45:20 +0000449 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000450 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000451 if (I->use_empty())
452 DeadNodes.push_back(I);
453
454 // Process the worklist, deleting the nodes and adding their uses to the
455 // worklist.
456 while (!DeadNodes.empty()) {
457 SDNode *N = DeadNodes.back();
458 DeadNodes.pop_back();
459
460 // Take the node out of the appropriate CSE map.
461 RemoveNodeFromCSEMaps(N);
462
463 // Next, brutally remove the operand list. This is safe to do, as there are
464 // no cycles in the graph.
465 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
466 SDNode *Operand = I->Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000467 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000468
469 // Now that we removed this operand, see if there are no uses of it left.
470 if (Operand->use_empty())
471 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000472 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000473 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000474 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000475 }
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;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000507 Operand->removeUser(std::distance(N->op_begin(), I), 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 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000513 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000514 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000515 }
Evan Cheng130a6472006-10-12 20:34:05 +0000516 N->OperandList = 0;
517 N->NumOperands = 0;
518
519 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000520 AllNodes.erase(N);
521 }
522}
523
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000524void SelectionDAG::DeleteNode(SDNode *N) {
525 assert(N->use_empty() && "Cannot delete a node that is not dead!");
526
527 // First take this out of the appropriate CSE map.
528 RemoveNodeFromCSEMaps(N);
529
Chris Lattner1e111c72005-09-07 05:37:01 +0000530 // Finally, remove uses due to operands of this node, remove from the
531 // AllNodes list, and delete the node.
532 DeleteNodeNotInCSEMaps(N);
533}
534
535void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
536
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000537 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000538 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000539
540 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000541 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000542 I->Val->removeUser(std::distance(N->op_begin(), I), N);
543 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000544 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000545 }
Chris Lattner65113b22005-11-08 22:07:03 +0000546 N->OperandList = 0;
547 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000548
549 delete N;
550}
551
Chris Lattner149c58c2005-08-16 18:17:10 +0000552/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
553/// correspond to it. This is useful when we're about to delete or repurpose
554/// the node. We don't want future request for structurally identical nodes
555/// to return N anymore.
556void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000557 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000558 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000559 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000560 case ISD::STRING:
561 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
562 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000563 case ISD::CONDCODE:
564 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
565 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000566 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000567 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
568 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000571 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000572 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000573 Erased =
574 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000575 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000576 case ISD::VALUETYPE: {
577 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
578 if (MVT::isExtendedVT(VT)) {
579 Erased = ExtendedValueTypeNodes.erase(VT);
580 } else {
581 Erased = ValueTypeNodes[VT] != 0;
582 ValueTypeNodes[VT] = 0;
583 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000584 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000585 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000586 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000587 // Remove it from the CSE Map.
588 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000589 break;
590 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000591#ifndef NDEBUG
592 // Verify that the node was actually in one of the CSE maps, unless it has a
593 // flag result (which cannot be CSE'd) or is one of the special cases that are
594 // not subject to CSE.
595 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000596 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000597 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000598 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000599 assert(0 && "Node is not in map!");
600 }
601#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000602}
603
Chris Lattner8b8749f2005-08-17 19:00:20 +0000604/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
605/// has been taken out and modified in some way. If the specified node already
606/// exists in the CSE maps, do not modify the maps, but return the existing node
607/// instead. If it doesn't exist, add it and return null.
608///
609SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
610 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000611 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000612 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000613
Chris Lattner9f8cc692005-12-19 22:21:21 +0000614 // Check that remaining values produced are not flags.
615 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
616 if (N->getValueType(i) == MVT::Flag)
617 return 0; // Never CSE anything that produces a flag.
618
Chris Lattnera5682852006-08-07 23:03:03 +0000619 SDNode *New = CSEMap.GetOrInsertNode(N);
620 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000621 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000622}
623
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000624/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
625/// were replaced with those specified. If this node is never memoized,
626/// return null, otherwise return a pointer to the slot it would take. If a
627/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000628SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
629 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000630 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000631 return 0; // Never add these nodes.
632
633 // Check that remaining values produced are not flags.
634 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
635 if (N->getValueType(i) == MVT::Flag)
636 return 0; // Never CSE anything that produces a flag.
637
Chris Lattner63e3f142007-02-04 07:28:00 +0000638 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000639 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000640 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000641 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000642}
643
644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
649 SDOperand Op1, SDOperand Op2,
650 void *&InsertPos) {
651 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
652 return 0; // Never add these nodes.
653
654 // Check that remaining values produced are not flags.
655 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
656 if (N->getValueType(i) == MVT::Flag)
657 return 0; // Never CSE anything that produces a flag.
658
Chris Lattner63e3f142007-02-04 07:28:00 +0000659 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000660 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000661 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000662 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
663}
664
665
666/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
667/// were replaced with those specified. If this node is never memoized,
668/// return null, otherwise return a pointer to the slot it would take. If a
669/// node already exists with these operands, the slot will be non-null.
670SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000671 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000672 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000673 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000674 return 0; // Never add these nodes.
675
676 // Check that remaining values produced are not flags.
677 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
678 if (N->getValueType(i) == MVT::Flag)
679 return 0; // Never CSE anything that produces a flag.
680
Jim Laskey583bd472006-10-27 23:46:08 +0000681 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000682 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000683
Evan Cheng9629aba2006-10-11 01:47:58 +0000684 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
685 ID.AddInteger(LD->getAddressingMode());
686 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000687 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000688 ID.AddInteger(LD->getAlignment());
689 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000690 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
691 ID.AddInteger(ST->getAddressingMode());
692 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000693 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000694 ID.AddInteger(ST->getAlignment());
695 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000696 }
Jim Laskey583bd472006-10-27 23:46:08 +0000697
Chris Lattnera5682852006-08-07 23:03:03 +0000698 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000700
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000701
Chris Lattner78ec3112003-08-11 14:57:33 +0000702SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000703 while (!AllNodes.empty()) {
704 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000705 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000706 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000707 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000708 }
Chris Lattner65113b22005-11-08 22:07:03 +0000709 N->OperandList = 0;
710 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000711 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000712 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000713}
714
Chris Lattner0f2287b2005-04-13 02:38:18 +0000715SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000716 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000717 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
718 MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000719 return getNode(ISD::AND, Op.getValueType(), Op,
720 getConstant(Imm, Op.getValueType()));
721}
722
Chris Lattner36ce6912005-11-29 06:21:05 +0000723SDOperand SelectionDAG::getString(const std::string &Val) {
724 StringSDNode *&N = StringNodes[Val];
725 if (!N) {
726 N = new StringSDNode(Val);
727 AllNodes.push_back(N);
728 }
729 return SDOperand(N, 0);
730}
731
Chris Lattner61b09412006-08-11 21:01:22 +0000732SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000733 MVT::ValueType EltVT =
734 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
735
736 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
737}
738
739SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000740 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000741
742 MVT::ValueType EltVT =
743 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000744
Dan Gohman6394b092008-02-08 22:59:30 +0000745 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
746 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000747
748 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000749 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000750 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000751 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000752 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000753 SDNode *N = NULL;
754 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
755 if (!MVT::isVector(VT))
756 return SDOperand(N, 0);
757 if (!N) {
758 N = new ConstantSDNode(isT, Val, EltVT);
759 CSEMap.InsertNode(N, IP);
760 AllNodes.push_back(N);
761 }
762
763 SDOperand Result(N, 0);
764 if (MVT::isVector(VT)) {
765 SmallVector<SDOperand, 8> Ops;
766 Ops.assign(MVT::getVectorNumElements(VT), Result);
767 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
768 }
769 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000770}
771
Chris Lattner0bd48932008-01-17 07:00:52 +0000772SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
773 return getConstant(Val, TLI.getPointerTy(), isTarget);
774}
775
776
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000777SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000778 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000780
Dan Gohman7f321562007-06-25 16:23:39 +0000781 MVT::ValueType EltVT =
782 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783
Chris Lattnerd8658612005-02-17 20:17:32 +0000784 // Do the map lookup using the actual bit pattern for the floating point
785 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
786 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000787 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000789 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000790 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000791 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000792 SDNode *N = NULL;
793 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
794 if (!MVT::isVector(VT))
795 return SDOperand(N, 0);
796 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000797 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000798 CSEMap.InsertNode(N, IP);
799 AllNodes.push_back(N);
800 }
801
Dan Gohman7f321562007-06-25 16:23:39 +0000802 SDOperand Result(N, 0);
803 if (MVT::isVector(VT)) {
804 SmallVector<SDOperand, 8> Ops;
805 Ops.assign(MVT::getVectorNumElements(VT), Result);
806 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
807 }
808 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000809}
810
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000811SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
812 bool isTarget) {
813 MVT::ValueType EltVT =
814 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
815 if (EltVT==MVT::f32)
816 return getConstantFP(APFloat((float)Val), VT, isTarget);
817 else
818 return getConstantFP(APFloat(Val), VT, isTarget);
819}
820
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000822 MVT::ValueType VT, int Offset,
823 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000824 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000825
826 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
827 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000828 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000829 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
830 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
831 }
832
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000833 if (GVar && GVar->isThreadLocal())
834 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
835 else
836 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000837
Jim Laskey583bd472006-10-27 23:46:08 +0000838 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000839 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000840 ID.AddPointer(GV);
841 ID.AddInteger(Offset);
842 void *IP = 0;
843 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
844 return SDOperand(E, 0);
845 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
846 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000847 AllNodes.push_back(N);
848 return SDOperand(N, 0);
849}
850
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000851SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
852 bool isTarget) {
853 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000854 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000855 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000856 ID.AddInteger(FI);
857 void *IP = 0;
858 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
859 return SDOperand(E, 0);
860 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
861 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
864}
865
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000866SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
867 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000868 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000869 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000870 ID.AddInteger(JTI);
871 void *IP = 0;
872 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
873 return SDOperand(E, 0);
874 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
875 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000876 AllNodes.push_back(N);
877 return SDOperand(N, 0);
878}
879
Evan Chengb8973bd2006-01-31 22:23:14 +0000880SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000881 unsigned Alignment, int Offset,
882 bool isTarget) {
883 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000884 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000885 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 ID.AddInteger(Alignment);
887 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000888 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000889 void *IP = 0;
890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
891 return SDOperand(E, 0);
892 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
893 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000894 AllNodes.push_back(N);
895 return SDOperand(N, 0);
896}
897
Chris Lattnerc3aae252005-01-07 07:46:32 +0000898
Evan Chengd6594ae2006-09-12 21:00:35 +0000899SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
900 MVT::ValueType VT,
901 unsigned Alignment, int Offset,
902 bool isTarget) {
903 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000904 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000905 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000906 ID.AddInteger(Alignment);
907 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000908 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000909 void *IP = 0;
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
911 return SDOperand(E, 0);
912 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
913 CSEMap.InsertNode(N, IP);
914 AllNodes.push_back(N);
915 return SDOperand(N, 0);
916}
917
918
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000920 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000921 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000922 ID.AddPointer(MBB);
923 void *IP = 0;
924 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
925 return SDOperand(E, 0);
926 SDNode *N = new BasicBlockSDNode(MBB);
927 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 AllNodes.push_back(N);
929 return SDOperand(N, 0);
930}
931
Duncan Sands276dcbd2008-03-21 09:14:45 +0000932SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
933 FoldingSetNodeID ID;
934 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
935 ID.AddInteger(Flags.getRawBits());
936 void *IP = 0;
937 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
938 return SDOperand(E, 0);
939 SDNode *N = new ARG_FLAGSSDNode(Flags);
940 CSEMap.InsertNode(N, IP);
941 AllNodes.push_back(N);
942 return SDOperand(N, 0);
943}
944
Chris Lattner15e4b012005-07-10 00:07:11 +0000945SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000946 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000947 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000948
Duncan Sandsf411b832007-10-17 13:49:58 +0000949 SDNode *&N = MVT::isExtendedVT(VT) ?
950 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
951
952 if (N) return SDOperand(N, 0);
953 N = new VTSDNode(VT);
954 AllNodes.push_back(N);
955 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000956}
957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
959 SDNode *&N = ExternalSymbols[Sym];
960 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000961 N = new ExternalSymbolSDNode(false, Sym, VT);
962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
964}
965
Chris Lattner809ec112006-01-28 10:09:25 +0000966SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
967 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000968 SDNode *&N = TargetExternalSymbols[Sym];
969 if (N) return SDOperand(N, 0);
970 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 AllNodes.push_back(N);
972 return SDOperand(N, 0);
973}
974
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000975SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
976 if ((unsigned)Cond >= CondCodeNodes.size())
977 CondCodeNodes.resize(Cond+1);
978
Chris Lattner079a27a2005-08-09 20:40:02 +0000979 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000980 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000981 AllNodes.push_back(CondCodeNodes[Cond]);
982 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000983 return SDOperand(CondCodeNodes[Cond], 0);
984}
985
Chris Lattner0fdd7682005-08-30 22:38:38 +0000986SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000987 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000988 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000989 ID.AddInteger(RegNo);
990 void *IP = 0;
991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
992 return SDOperand(E, 0);
993 SDNode *N = new RegisterSDNode(RegNo, VT);
994 CSEMap.InsertNode(N, IP);
995 AllNodes.push_back(N);
996 return SDOperand(N, 0);
997}
998
Dan Gohman69de1932008-02-06 22:27:42 +0000999SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001000 assert((!V || isa<PointerType>(V->getType())) &&
1001 "SrcValue is not a pointer?");
1002
Jim Laskey583bd472006-10-27 23:46:08 +00001003 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001004 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001005 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001006
Chris Lattner61b09412006-08-11 21:01:22 +00001007 void *IP = 0;
1008 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1009 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001010
1011 SDNode *N = new SrcValueSDNode(V);
1012 CSEMap.InsertNode(N, IP);
1013 AllNodes.push_back(N);
1014 return SDOperand(N, 0);
1015}
1016
Dan Gohman36b5c132008-04-07 19:35:22 +00001017SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001018 const Value *v = MO.getValue();
1019 assert((!v || isa<PointerType>(v->getType())) &&
1020 "SrcValue is not a pointer?");
1021
1022 FoldingSetNodeID ID;
1023 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1024 ID.AddPointer(v);
1025 ID.AddInteger(MO.getFlags());
1026 ID.AddInteger(MO.getOffset());
1027 ID.AddInteger(MO.getSize());
1028 ID.AddInteger(MO.getAlignment());
1029
1030 void *IP = 0;
1031 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1032 return SDOperand(E, 0);
1033
1034 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001035 CSEMap.InsertNode(N, IP);
1036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001038}
1039
Chris Lattner37ce9df2007-10-15 17:47:20 +00001040/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1041/// specified value type.
1042SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1043 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1044 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1045 const Type *Ty = MVT::getTypeForValueType(VT);
1046 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1047 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1048 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1049}
1050
1051
Chris Lattner51dabfb2006-10-14 00:41:01 +00001052SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1053 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 // These setcc operations always fold.
1055 switch (Cond) {
1056 default: break;
1057 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001058 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001060 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001061
1062 case ISD::SETOEQ:
1063 case ISD::SETOGT:
1064 case ISD::SETOGE:
1065 case ISD::SETOLT:
1066 case ISD::SETOLE:
1067 case ISD::SETONE:
1068 case ISD::SETO:
1069 case ISD::SETUO:
1070 case ISD::SETUEQ:
1071 case ISD::SETUNE:
1072 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1073 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001075
Chris Lattner67255a12005-04-07 18:14:58 +00001076 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001077 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001078 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001079 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001080
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081 switch (Cond) {
1082 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001083 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1084 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001085 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1086 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1087 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1088 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1089 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1090 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1091 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1092 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 }
Chris Lattner67255a12005-04-07 18:14:58 +00001095 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001096 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001098 // No compile time operations on this type yet.
1099 if (N1C->getValueType(0) == MVT::ppcf128)
1100 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001101
1102 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001104 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001105 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1106 return getNode(ISD::UNDEF, VT);
1107 // fall through
1108 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1109 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1110 return getNode(ISD::UNDEF, VT);
1111 // fall through
1112 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001113 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001114 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1115 return getNode(ISD::UNDEF, VT);
1116 // fall through
1117 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1118 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1119 return getNode(ISD::UNDEF, VT);
1120 // fall through
1121 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1122 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1123 return getNode(ISD::UNDEF, VT);
1124 // fall through
1125 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001126 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001127 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1128 return getNode(ISD::UNDEF, VT);
1129 // fall through
1130 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001131 R==APFloat::cmpEqual, VT);
1132 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1133 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1134 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1135 R==APFloat::cmpEqual, VT);
1136 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1137 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1138 R==APFloat::cmpLessThan, VT);
1139 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1140 R==APFloat::cmpUnordered, VT);
1141 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1142 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001143 }
1144 } else {
1145 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001146 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001148 }
1149
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001150 // Could not fold it.
1151 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152}
1153
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001154/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1155/// use this predicate to simplify operations downstream.
1156bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1157 unsigned BitWidth = Op.getValueSizeInBits();
1158 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1159}
1160
Dan Gohmanea859be2007-06-22 14:59:07 +00001161/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1162/// this predicate to simplify operations downstream. Mask is known to be zero
1163/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001164bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001165 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001166 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001167 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1168 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1169 return (KnownZero & Mask) == Mask;
1170}
1171
1172/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1173/// known to be either zero or one and return them in the KnownZero/KnownOne
1174/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1175/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001176void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001177 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001178 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001179 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001180 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1181 "Mask size mismatches value type size!");
1182
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001183 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001184 if (Depth == 6 || Mask == 0)
1185 return; // Limit search depth.
1186
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001187 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001188
1189 switch (Op.getOpcode()) {
1190 case ISD::Constant:
1191 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001192 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001193 KnownZero = ~KnownOne & Mask;
1194 return;
1195 case ISD::AND:
1196 // If either the LHS or the RHS are Zero, the result is zero.
1197 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001198 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1199 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001200 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1201 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1202
1203 // Output known-1 bits are only known if set in both the LHS & RHS.
1204 KnownOne &= KnownOne2;
1205 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1206 KnownZero |= KnownZero2;
1207 return;
1208 case ISD::OR:
1209 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001210 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1211 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001212 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1213 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1214
1215 // Output known-0 bits are only known if clear in both the LHS & RHS.
1216 KnownZero &= KnownZero2;
1217 // Output known-1 are known to be set if set in either the LHS | RHS.
1218 KnownOne |= KnownOne2;
1219 return;
1220 case ISD::XOR: {
1221 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1222 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1223 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1224 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1225
1226 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001227 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001228 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1229 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1230 KnownZero = KnownZeroOut;
1231 return;
1232 }
1233 case ISD::SELECT:
1234 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1235 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1236 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1237 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1238
1239 // Only known if known in both the LHS and RHS.
1240 KnownOne &= KnownOne2;
1241 KnownZero &= KnownZero2;
1242 return;
1243 case ISD::SELECT_CC:
1244 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1245 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1246 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1247 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1248
1249 // Only known if known in both the LHS and RHS.
1250 KnownOne &= KnownOne2;
1251 KnownZero &= KnownZero2;
1252 return;
1253 case ISD::SETCC:
1254 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001255 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1256 BitWidth > 1)
1257 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001258 return;
1259 case ISD::SHL:
1260 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1261 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001262 unsigned ShAmt = SA->getValue();
1263
1264 // If the shift count is an invalid immediate, don't do anything.
1265 if (ShAmt >= BitWidth)
1266 return;
1267
1268 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001269 KnownZero, KnownOne, Depth+1);
1270 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001271 KnownZero <<= ShAmt;
1272 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001273 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001274 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001275 }
1276 return;
1277 case ISD::SRL:
1278 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1279 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001280 unsigned ShAmt = SA->getValue();
1281
Dan Gohmand4cf9922008-02-26 18:50:50 +00001282 // If the shift count is an invalid immediate, don't do anything.
1283 if (ShAmt >= BitWidth)
1284 return;
1285
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001286 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001287 KnownZero, KnownOne, Depth+1);
1288 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001289 KnownZero = KnownZero.lshr(ShAmt);
1290 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001291
Dan Gohman72d2fd52008-02-13 22:43:25 +00001292 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001293 KnownZero |= HighBits; // High bits known zero.
1294 }
1295 return;
1296 case ISD::SRA:
1297 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001298 unsigned ShAmt = SA->getValue();
1299
Dan Gohmand4cf9922008-02-26 18:50:50 +00001300 // If the shift count is an invalid immediate, don't do anything.
1301 if (ShAmt >= BitWidth)
1302 return;
1303
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001304 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001305 // If any of the demanded bits are produced by the sign extension, we also
1306 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001307 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1308 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001309 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001310
1311 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1312 Depth+1);
1313 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001314 KnownZero = KnownZero.lshr(ShAmt);
1315 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001316
1317 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001318 APInt SignBit = APInt::getSignBit(BitWidth);
1319 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001320
Dan Gohmanca93a432008-02-20 16:30:17 +00001321 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001322 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001323 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001324 KnownOne |= HighBits; // New bits are known one.
1325 }
1326 }
1327 return;
1328 case ISD::SIGN_EXTEND_INREG: {
1329 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001330 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001331
1332 // Sign extension. Compute the demanded bits in the result that are not
1333 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001334 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001335
Dan Gohman977a76f2008-02-13 22:28:48 +00001336 APInt InSignBit = APInt::getSignBit(EBits);
1337 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001338
1339 // If the sign extended bits are demanded, we know that the sign
1340 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001341 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001342 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001343 InputDemandedBits |= InSignBit;
1344
1345 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1346 KnownZero, KnownOne, Depth+1);
1347 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1348
1349 // If the sign bit of the input is known set or clear, then we know the
1350 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001351 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001352 KnownZero |= NewBits;
1353 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001354 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001355 KnownOne |= NewBits;
1356 KnownZero &= ~NewBits;
1357 } else { // Input sign bit unknown
1358 KnownZero &= ~NewBits;
1359 KnownOne &= ~NewBits;
1360 }
1361 return;
1362 }
1363 case ISD::CTTZ:
1364 case ISD::CTLZ:
1365 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001366 unsigned LowBits = Log2_32(BitWidth)+1;
1367 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1368 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001369 return;
1370 }
1371 case ISD::LOAD: {
1372 if (ISD::isZEXTLoad(Op.Val)) {
1373 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001374 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001375 unsigned MemBits = MVT::getSizeInBits(VT);
1376 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001377 }
1378 return;
1379 }
1380 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001381 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1382 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001383 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1384 APInt InMask = Mask;
1385 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001386 KnownZero.trunc(InBits);
1387 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001388 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001389 KnownZero.zext(BitWidth);
1390 KnownOne.zext(BitWidth);
1391 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001392 return;
1393 }
1394 case ISD::SIGN_EXTEND: {
1395 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001396 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001397 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001398 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1399 APInt InMask = Mask;
1400 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001401
1402 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001403 // bit is demanded. Temporarily set this bit in the mask for our callee.
1404 if (NewBits.getBoolValue())
1405 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001406
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001407 KnownZero.trunc(InBits);
1408 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001409 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1410
1411 // Note if the sign bit is known to be zero or one.
1412 bool SignBitKnownZero = KnownZero.isNegative();
1413 bool SignBitKnownOne = KnownOne.isNegative();
1414 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1415 "Sign bit can't be known to be both zero and one!");
1416
1417 // If the sign bit wasn't actually demanded by our caller, we don't
1418 // want it set in the KnownZero and KnownOne result values. Reset the
1419 // mask and reapply it to the result values.
1420 InMask = Mask;
1421 InMask.trunc(InBits);
1422 KnownZero &= InMask;
1423 KnownOne &= InMask;
1424
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001425 KnownZero.zext(BitWidth);
1426 KnownOne.zext(BitWidth);
1427
Dan Gohman977a76f2008-02-13 22:28:48 +00001428 // If the sign bit is known zero or one, the top bits match.
1429 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001431 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001433 return;
1434 }
1435 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001436 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1437 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001438 APInt InMask = Mask;
1439 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001440 KnownZero.trunc(InBits);
1441 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001442 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001443 KnownZero.zext(BitWidth);
1444 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001445 return;
1446 }
1447 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001448 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1449 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001450 APInt InMask = Mask;
1451 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 KnownZero.zext(InBits);
1453 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001454 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001455 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001456 KnownZero.trunc(BitWidth);
1457 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001458 break;
1459 }
1460 case ISD::AssertZext: {
1461 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001462 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001463 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1464 KnownOne, Depth+1);
1465 KnownZero |= (~InMask) & Mask;
1466 return;
1467 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001468 case ISD::FGETSIGN:
1469 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001470 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001471 return;
1472
Dan Gohmanea859be2007-06-22 14:59:07 +00001473 case ISD::ADD: {
1474 // If either the LHS or the RHS are Zero, the result is zero.
1475 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1476 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1477 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1478 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1479
1480 // Output known-0 bits are known if clear or set in both the low clear bits
1481 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1482 // low 3 bits clear.
Dan Gohman977a76f2008-02-13 22:28:48 +00001483 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1484 KnownZero2.countTrailingOnes());
Dan Gohmanea859be2007-06-22 14:59:07 +00001485
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001486 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1487 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001488 return;
1489 }
1490 case ISD::SUB: {
1491 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1492 if (!CLHS) return;
1493
1494 // We know that the top bits of C-X are clear if X contains less bits
1495 // than C (i.e. no wrap-around can happen). For example, 20-X is
1496 // positive if we can prove that X is >= 0 and < 16.
Dan Gohman977a76f2008-02-13 22:28:48 +00001497 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001498 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1499 // NLZ can't be BitWidth with no sign bit
Chris Lattner423be622008-02-14 18:48:56 +00001500 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001501 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1502
1503 // If all of the MaskV bits are known to be zero, then we know the output
1504 // top bits are zero, because we now know that the output is from [0-C].
1505 if ((KnownZero & MaskV) == MaskV) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001506 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1507 // Top bits known zero.
1508 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1509 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001510 } else {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001511 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 }
1513 }
1514 return;
1515 }
1516 default:
1517 // Allow the target to implement this method for its nodes.
1518 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1519 case ISD::INTRINSIC_WO_CHAIN:
1520 case ISD::INTRINSIC_W_CHAIN:
1521 case ISD::INTRINSIC_VOID:
1522 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1523 }
1524 return;
1525 }
1526}
1527
1528/// ComputeNumSignBits - Return the number of times the sign bit of the
1529/// register is replicated into the other bits. We know that at least 1 bit
1530/// is always equal to the sign bit (itself), but other cases can give us
1531/// information. For example, immediately after an "SRA X, 2", we know that
1532/// the top 3 bits are all equal to each other, so we return 3.
1533unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1534 MVT::ValueType VT = Op.getValueType();
1535 assert(MVT::isInteger(VT) && "Invalid VT!");
1536 unsigned VTBits = MVT::getSizeInBits(VT);
1537 unsigned Tmp, Tmp2;
1538
1539 if (Depth == 6)
1540 return 1; // Limit search depth.
1541
1542 switch (Op.getOpcode()) {
1543 default: break;
1544 case ISD::AssertSext:
1545 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1546 return VTBits-Tmp+1;
1547 case ISD::AssertZext:
1548 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1549 return VTBits-Tmp;
1550
1551 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001552 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1553 // If negative, return # leading ones.
1554 if (Val.isNegative())
1555 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001556
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001557 // Return # leading zeros.
1558 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001559 }
1560
1561 case ISD::SIGN_EXTEND:
1562 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1563 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1564
1565 case ISD::SIGN_EXTEND_INREG:
1566 // Max of the input and what this extends.
1567 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1568 Tmp = VTBits-Tmp+1;
1569
1570 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1571 return std::max(Tmp, Tmp2);
1572
1573 case ISD::SRA:
1574 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1575 // SRA X, C -> adds C sign bits.
1576 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1577 Tmp += C->getValue();
1578 if (Tmp > VTBits) Tmp = VTBits;
1579 }
1580 return Tmp;
1581 case ISD::SHL:
1582 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1583 // shl destroys sign bits.
1584 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1585 if (C->getValue() >= VTBits || // Bad shift.
1586 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1587 return Tmp - C->getValue();
1588 }
1589 break;
1590 case ISD::AND:
1591 case ISD::OR:
1592 case ISD::XOR: // NOT is handled here.
1593 // Logical binary ops preserve the number of sign bits.
1594 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1595 if (Tmp == 1) return 1; // Early out.
1596 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1597 return std::min(Tmp, Tmp2);
1598
1599 case ISD::SELECT:
1600 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1601 if (Tmp == 1) return 1; // Early out.
1602 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1603 return std::min(Tmp, Tmp2);
1604
1605 case ISD::SETCC:
1606 // If setcc returns 0/-1, all bits are sign bits.
1607 if (TLI.getSetCCResultContents() ==
1608 TargetLowering::ZeroOrNegativeOneSetCCResult)
1609 return VTBits;
1610 break;
1611 case ISD::ROTL:
1612 case ISD::ROTR:
1613 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1614 unsigned RotAmt = C->getValue() & (VTBits-1);
1615
1616 // Handle rotate right by N like a rotate left by 32-N.
1617 if (Op.getOpcode() == ISD::ROTR)
1618 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1619
1620 // If we aren't rotating out all of the known-in sign bits, return the
1621 // number that are left. This handles rotl(sext(x), 1) for example.
1622 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1623 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1624 }
1625 break;
1626 case ISD::ADD:
1627 // Add can have at most one carry bit. Thus we know that the output
1628 // is, at worst, one more bit than the inputs.
1629 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1630 if (Tmp == 1) return 1; // Early out.
1631
1632 // Special case decrementing a value (ADD X, -1):
1633 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1634 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001635 APInt KnownZero, KnownOne;
1636 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1638
1639 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1640 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001641 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001642 return VTBits;
1643
1644 // If we are subtracting one from a positive number, there is no carry
1645 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001646 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001647 return Tmp;
1648 }
1649
1650 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1651 if (Tmp2 == 1) return 1;
1652 return std::min(Tmp, Tmp2)-1;
1653 break;
1654
1655 case ISD::SUB:
1656 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1657 if (Tmp2 == 1) return 1;
1658
1659 // Handle NEG.
1660 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001661 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001662 APInt KnownZero, KnownOne;
1663 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001664 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1665 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1666 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001667 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001668 return VTBits;
1669
1670 // If the input is known to be positive (the sign bit is known clear),
1671 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001672 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001673 return Tmp2;
1674
1675 // Otherwise, we treat this like a SUB.
1676 }
1677
1678 // Sub can have at most one carry bit. Thus we know that the output
1679 // is, at worst, one more bit than the inputs.
1680 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1681 if (Tmp == 1) return 1; // Early out.
1682 return std::min(Tmp, Tmp2)-1;
1683 break;
1684 case ISD::TRUNCATE:
1685 // FIXME: it's tricky to do anything useful for this, but it is an important
1686 // case for targets like X86.
1687 break;
1688 }
1689
1690 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1691 if (Op.getOpcode() == ISD::LOAD) {
1692 LoadSDNode *LD = cast<LoadSDNode>(Op);
1693 unsigned ExtType = LD->getExtensionType();
1694 switch (ExtType) {
1695 default: break;
1696 case ISD::SEXTLOAD: // '17' 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+1;
1699 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001700 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001701 return VTBits-Tmp;
1702 }
1703 }
1704
1705 // Allow the target to implement this method for its nodes.
1706 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1707 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1708 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1709 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1710 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1711 if (NumBits > 1) return NumBits;
1712 }
1713
1714 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1715 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001716 APInt KnownZero, KnownOne;
1717 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001718 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1719
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001720 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001721 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001722 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001723 Mask = KnownOne;
1724 } else {
1725 // Nothing known.
1726 return 1;
1727 }
1728
1729 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1730 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001731 Mask = ~Mask;
1732 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001733 // Return # leading zeros. We use 'min' here in case Val was zero before
1734 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001735 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanea859be2007-06-22 14:59:07 +00001736}
1737
Chris Lattner51dabfb2006-10-14 00:41:01 +00001738
Evan Chenga844bde2008-02-02 04:07:54 +00001739bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1740 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1741 if (!GA) return false;
1742 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1743 if (!GV) return false;
1744 MachineModuleInfo *MMI = getMachineModuleInfo();
1745 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1746}
1747
1748
Chris Lattnerc3aae252005-01-07 07:46:32 +00001749/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001750///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001752 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001753 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001754 void *IP = 0;
1755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1756 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001757 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001758 CSEMap.InsertNode(N, IP);
1759
1760 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001761 return SDOperand(N, 0);
1762}
1763
Chris Lattnerc3aae252005-01-07 07:46:32 +00001764SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1765 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001766 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001767 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001768 const APInt &Val = C->getAPIntValue();
1769 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001770 switch (Opcode) {
1771 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001772 case ISD::SIGN_EXTEND:
1773 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001774 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001775 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001776 case ISD::TRUNCATE:
1777 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001778 case ISD::UINT_TO_FP:
1779 case ISD::SINT_TO_FP: {
1780 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001781 // No compile time operations on this type.
1782 if (VT==MVT::ppcf128)
1783 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001784 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1785 (void)apf.convertFromAPInt(Val,
1786 Opcode==ISD::SINT_TO_FP,
1787 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001788 return getConstantFP(apf, VT);
1789 }
Chris Lattner94683772005-12-23 05:30:37 +00001790 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001791 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001792 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001793 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001794 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001795 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001796 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001797 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001798 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001799 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001800 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001801 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001802 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001803 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001804 }
1805 }
1806
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001807 // Constant fold unary operations with a floating point constant operand.
1808 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1809 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001810 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001811 switch (Opcode) {
1812 case ISD::FNEG:
1813 V.changeSign();
1814 return getConstantFP(V, VT);
1815 case ISD::FABS:
1816 V.clearSign();
1817 return getConstantFP(V, VT);
1818 case ISD::FP_ROUND:
1819 case ISD::FP_EXTEND:
1820 // This can return overflow, underflow, or inexact; we don't care.
1821 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001822 (void)V.convert(*MVTToAPFloatSemantics(VT),
1823 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001824 return getConstantFP(V, VT);
1825 case ISD::FP_TO_SINT:
1826 case ISD::FP_TO_UINT: {
1827 integerPart x;
1828 assert(integerPartWidth >= 64);
1829 // FIXME need to be more flexible about rounding mode.
1830 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1831 Opcode==ISD::FP_TO_SINT,
1832 APFloat::rmTowardZero);
1833 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1834 break;
1835 return getConstant(x, VT);
1836 }
1837 case ISD::BIT_CONVERT:
1838 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1839 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1840 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1841 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001842 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001843 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001844 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001845 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001846
1847 unsigned OpOpcode = Operand.Val->getOpcode();
1848 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001849 case ISD::TokenFactor:
1850 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001851 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001852 case ISD::FP_EXTEND:
1853 assert(MVT::isFloatingPoint(VT) &&
1854 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001855 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001856 if (Operand.getOpcode() == ISD::UNDEF)
1857 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001858 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001859 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001860 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1861 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001862 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001863 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1864 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001865 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1867 break;
1868 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001869 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1870 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001871 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001872 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1873 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001874 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001875 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001876 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001877 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001878 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1879 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001880 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001881 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1882 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001883 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1884 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1885 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1886 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001887 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001888 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1889 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001890 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001891 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1892 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001893 if (OpOpcode == ISD::TRUNCATE)
1894 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001895 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1896 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001897 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001898 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1899 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001900 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001901 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1902 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001903 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1904 else
1905 return Operand.Val->getOperand(0);
1906 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001907 break;
Chris Lattner35481892005-12-23 00:16:34 +00001908 case ISD::BIT_CONVERT:
1909 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001910 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001911 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001912 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001913 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1914 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001915 if (OpOpcode == ISD::UNDEF)
1916 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001917 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001918 case ISD::SCALAR_TO_VECTOR:
1919 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001920 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001921 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001922 if (OpOpcode == ISD::UNDEF)
1923 return getNode(ISD::UNDEF, VT);
1924 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1925 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1926 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1927 Operand.getConstantOperandVal(1) == 0 &&
1928 Operand.getOperand(0).getValueType() == VT)
1929 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001930 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001931 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001932 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1933 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001934 Operand.Val->getOperand(0));
1935 if (OpOpcode == ISD::FNEG) // --X -> X
1936 return Operand.Val->getOperand(0);
1937 break;
1938 case ISD::FABS:
1939 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1940 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1941 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001942 }
1943
Chris Lattner43247a12005-08-25 19:12:10 +00001944 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001945 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001946 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001947 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001948 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001949 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001950 void *IP = 0;
1951 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1952 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001953 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001954 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001955 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001956 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001957 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001958 AllNodes.push_back(N);
1959 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001960}
1961
Chris Lattner36019aa2005-04-18 03:48:41 +00001962
1963
Chris Lattnerc3aae252005-01-07 07:46:32 +00001964SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1965 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1967 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001968 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001969 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001970 case ISD::TokenFactor:
1971 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1972 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001973 // Fold trivial token factors.
1974 if (N1.getOpcode() == ISD::EntryToken) return N2;
1975 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001976 break;
Chris Lattner76365122005-01-16 02:23:22 +00001977 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001978 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1979 N1.getValueType() == VT && "Binary operator types must match!");
1980 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1981 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001982 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001983 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001984 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1985 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001986 break;
Chris Lattner76365122005-01-16 02:23:22 +00001987 case ISD::OR:
1988 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001989 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1990 N1.getValueType() == VT && "Binary operator types must match!");
1991 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1992 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001993 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001994 return N1;
1995 break;
Chris Lattner76365122005-01-16 02:23:22 +00001996 case ISD::UDIV:
1997 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001998 case ISD::MULHU:
1999 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002000 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2001 // fall through
2002 case ISD::ADD:
2003 case ISD::SUB:
2004 case ISD::MUL:
2005 case ISD::SDIV:
2006 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002007 case ISD::FADD:
2008 case ISD::FSUB:
2009 case ISD::FMUL:
2010 case ISD::FDIV:
2011 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002012 assert(N1.getValueType() == N2.getValueType() &&
2013 N1.getValueType() == VT && "Binary operator types must match!");
2014 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002015 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2016 assert(N1.getValueType() == VT &&
2017 MVT::isFloatingPoint(N1.getValueType()) &&
2018 MVT::isFloatingPoint(N2.getValueType()) &&
2019 "Invalid FCOPYSIGN!");
2020 break;
Chris Lattner76365122005-01-16 02:23:22 +00002021 case ISD::SHL:
2022 case ISD::SRA:
2023 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002024 case ISD::ROTL:
2025 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002026 assert(VT == N1.getValueType() &&
2027 "Shift operators return type must be the same as their first arg");
2028 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002029 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002030 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002031 case ISD::FP_ROUND_INREG: {
2032 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2033 assert(VT == N1.getValueType() && "Not an inreg round!");
2034 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2035 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002036 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2037 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002038 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002039 break;
2040 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002041 case ISD::FP_ROUND:
2042 assert(MVT::isFloatingPoint(VT) &&
2043 MVT::isFloatingPoint(N1.getValueType()) &&
2044 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2045 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002046 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002047 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002048 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002049 case ISD::AssertZext: {
2050 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2051 assert(VT == N1.getValueType() && "Not an inreg extend!");
2052 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2053 "Cannot *_EXTEND_INREG FP types");
2054 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2055 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002056 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002057 break;
2058 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002059 case ISD::SIGN_EXTEND_INREG: {
2060 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2061 assert(VT == N1.getValueType() && "Not an inreg extend!");
2062 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2063 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002064 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2065 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002066 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002067
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002068 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002069 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002070 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002071 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002072 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002073 return getConstant(Val, VT);
2074 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002075 break;
2076 }
2077 case ISD::EXTRACT_VECTOR_ELT:
2078 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2079
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002080 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2081 if (N1.getOpcode() == ISD::UNDEF)
2082 return getNode(ISD::UNDEF, VT);
2083
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002084 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2085 // expanding copies of large vectors from registers.
2086 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2087 N1.getNumOperands() > 0) {
2088 unsigned Factor =
2089 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2090 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2091 N1.getOperand(N2C->getValue() / Factor),
2092 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2093 }
2094
2095 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2096 // expanding large vector constants.
2097 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2098 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002099
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002100 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2101 // operations are lowered to scalars.
2102 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2103 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2104 if (IEC == N2C)
2105 return N1.getOperand(1);
2106 else
2107 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2108 }
2109 break;
2110 case ISD::EXTRACT_ELEMENT:
2111 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002112 assert(!MVT::isVector(N1.getValueType()) &&
2113 MVT::isInteger(N1.getValueType()) &&
2114 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2115 "EXTRACT_ELEMENT only applies to integers!");
2116
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002117 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2118 // 64-bit integers into 32-bit parts. Instead of building the extract of
2119 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2120 if (N1.getOpcode() == ISD::BUILD_PAIR)
2121 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002122
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002123 // EXTRACT_ELEMENT of a constant int is also very common.
2124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002125 unsigned ElementSize = MVT::getSizeInBits(VT);
2126 unsigned Shift = ElementSize * N2C->getValue();
2127 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2128 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002129 }
2130 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002131 case ISD::EXTRACT_SUBVECTOR:
2132 if (N1.getValueType() == VT) // Trivial extraction.
2133 return N1;
2134 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002135 }
2136
2137 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002138 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002139 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002140 switch (Opcode) {
2141 case ISD::ADD: return getConstant(C1 + C2, VT);
2142 case ISD::SUB: return getConstant(C1 - C2, VT);
2143 case ISD::MUL: return getConstant(C1 * C2, VT);
2144 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002145 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002146 break;
2147 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002148 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 break;
2150 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002151 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 break;
2153 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002154 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002155 break;
2156 case ISD::AND : return getConstant(C1 & C2, VT);
2157 case ISD::OR : return getConstant(C1 | C2, VT);
2158 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002159 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002160 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2161 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2162 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2163 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002164 default: break;
2165 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002166 } else { // Cannonicalize constant to RHS if commutative
2167 if (isCommutativeBinOp(Opcode)) {
2168 std::swap(N1C, N2C);
2169 std::swap(N1, N2);
2170 }
2171 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002172 }
2173
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002174 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002175 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2176 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002177 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002178 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2179 // Cannonicalize constant to RHS if commutative
2180 std::swap(N1CFP, N2CFP);
2181 std::swap(N1, N2);
2182 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002183 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2184 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002185 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002186 case ISD::FADD:
2187 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002188 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002189 return getConstantFP(V1, VT);
2190 break;
2191 case ISD::FSUB:
2192 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2193 if (s!=APFloat::opInvalidOp)
2194 return getConstantFP(V1, VT);
2195 break;
2196 case ISD::FMUL:
2197 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2198 if (s!=APFloat::opInvalidOp)
2199 return getConstantFP(V1, VT);
2200 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002201 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002202 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2203 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2204 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002205 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002206 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002207 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2208 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2209 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002210 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002211 case ISD::FCOPYSIGN:
2212 V1.copySign(V2);
2213 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002214 default: break;
2215 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002216 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002217 }
Chris Lattner62b57722006-04-20 05:39:12 +00002218
2219 // Canonicalize an UNDEF to the RHS, even over a constant.
2220 if (N1.getOpcode() == ISD::UNDEF) {
2221 if (isCommutativeBinOp(Opcode)) {
2222 std::swap(N1, N2);
2223 } else {
2224 switch (Opcode) {
2225 case ISD::FP_ROUND_INREG:
2226 case ISD::SIGN_EXTEND_INREG:
2227 case ISD::SUB:
2228 case ISD::FSUB:
2229 case ISD::FDIV:
2230 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002231 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002232 return N1; // fold op(undef, arg2) -> undef
2233 case ISD::UDIV:
2234 case ISD::SDIV:
2235 case ISD::UREM:
2236 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002237 case ISD::SRL:
2238 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002239 if (!MVT::isVector(VT))
2240 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2241 // For vectors, we can't easily build an all zero vector, just return
2242 // the LHS.
2243 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002244 }
2245 }
2246 }
2247
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002248 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002249 if (N2.getOpcode() == ISD::UNDEF) {
2250 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002251 case ISD::XOR:
2252 if (N1.getOpcode() == ISD::UNDEF)
2253 // Handle undef ^ undef -> 0 special case. This is a common
2254 // idiom (misuse).
2255 return getConstant(0, VT);
2256 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002257 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002258 case ISD::ADDC:
2259 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002260 case ISD::SUB:
2261 case ISD::FADD:
2262 case ISD::FSUB:
2263 case ISD::FMUL:
2264 case ISD::FDIV:
2265 case ISD::FREM:
2266 case ISD::UDIV:
2267 case ISD::SDIV:
2268 case ISD::UREM:
2269 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002270 return N2; // fold op(arg1, undef) -> undef
2271 case ISD::MUL:
2272 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002273 case ISD::SRL:
2274 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002275 if (!MVT::isVector(VT))
2276 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2277 // For vectors, we can't easily build an all zero vector, just return
2278 // the LHS.
2279 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002280 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002281 if (!MVT::isVector(VT))
2282 return getConstant(MVT::getIntVTBitMask(VT), VT);
2283 // For vectors, we can't easily build an all one vector, just return
2284 // the LHS.
2285 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002286 case ISD::SRA:
2287 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002288 }
2289 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002290
Chris Lattner27e9b412005-05-11 18:57:39 +00002291 // Memoize this node if possible.
2292 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002293 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002294 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002295 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002296 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002297 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002298 void *IP = 0;
2299 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2300 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002301 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002302 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002303 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002304 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002305 }
2306
Chris Lattnerc3aae252005-01-07 07:46:32 +00002307 AllNodes.push_back(N);
2308 return SDOperand(N, 0);
2309}
2310
Chris Lattnerc3aae252005-01-07 07:46:32 +00002311SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2312 SDOperand N1, SDOperand N2, SDOperand N3) {
2313 // Perform various simplifications.
2314 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2315 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002316 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002317 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002318 // Use FoldSetCC to simplify SETCC's.
2319 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002320 if (Simp.Val) return Simp;
2321 break;
2322 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002324 if (N1C) {
2325 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002326 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002327 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002329 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002330
2331 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002332 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002333 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002334 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002335 if (N2C->getValue()) // Unconditional branch
2336 return getNode(ISD::BR, MVT::Other, N1, N3);
2337 else
2338 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002339 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002340 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002341 case ISD::VECTOR_SHUFFLE:
2342 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2343 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2344 N3.getOpcode() == ISD::BUILD_VECTOR &&
2345 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2346 "Illegal VECTOR_SHUFFLE node!");
2347 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002348 case ISD::BIT_CONVERT:
2349 // Fold bit_convert nodes from a type to themselves.
2350 if (N1.getValueType() == VT)
2351 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002352 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002353 }
2354
Chris Lattner43247a12005-08-25 19:12:10 +00002355 // Memoize node if it doesn't produce a flag.
2356 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002357 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002358 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002359 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002360 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002361 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002362 void *IP = 0;
2363 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2364 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002365 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002366 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002367 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002368 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002369 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002370 AllNodes.push_back(N);
2371 return SDOperand(N, 0);
2372}
2373
2374SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002375 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002376 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002377 SDOperand Ops[] = { N1, N2, N3, N4 };
2378 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002379}
2380
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002381SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2382 SDOperand N1, SDOperand N2, SDOperand N3,
2383 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002384 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2385 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002386}
2387
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002388SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2389 SDOperand Src, SDOperand Size,
2390 SDOperand Align,
2391 SDOperand AlwaysInline) {
2392 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2393 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2394}
2395
2396SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2397 SDOperand Src, SDOperand Size,
2398 SDOperand Align,
2399 SDOperand AlwaysInline) {
2400 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2401 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2402}
2403
2404SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2405 SDOperand Src, SDOperand Size,
2406 SDOperand Align,
2407 SDOperand AlwaysInline) {
2408 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2409 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2410}
2411
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002412SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002413 SDOperand Ptr, SDOperand Cmp,
2414 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002415 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002416 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2417 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002418 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002419 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002420 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2421 ID.AddInteger((unsigned int)VT);
2422 void* IP = 0;
2423 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2424 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002425 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002426 CSEMap.InsertNode(N, IP);
2427 AllNodes.push_back(N);
2428 return SDOperand(N, 0);
2429}
2430
2431SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002432 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002433 MVT::ValueType VT) {
2434 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2435 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002436 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002437 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002438 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002439 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2440 ID.AddInteger((unsigned int)VT);
2441 void* IP = 0;
2442 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2443 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002444 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002445 CSEMap.InsertNode(N, IP);
2446 AllNodes.push_back(N);
2447 return SDOperand(N, 0);
2448}
2449
Duncan Sands14ea39c2008-03-27 20:23:40 +00002450SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00002451SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2452 MVT::ValueType VT, SDOperand Chain,
2453 SDOperand Ptr, SDOperand Offset,
2454 const Value *SV, int SVOffset, MVT::ValueType EVT,
2455 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002456 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2457 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002458 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002459 Ty = MVT::getTypeForValueType(VT);
2460 } else if (SV) {
2461 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2462 assert(PT && "Value for load must be a pointer");
2463 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00002464 }
Dan Gohman575e2f42007-06-04 15:49:41 +00002465 assert(Ty && "Could not get type information for load");
2466 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2467 }
Evan Cheng466685d2006-10-09 20:57:25 +00002468
Duncan Sands14ea39c2008-03-27 20:23:40 +00002469 if (VT == EVT) {
2470 ExtType = ISD::NON_EXTLOAD;
2471 } else if (ExtType == ISD::NON_EXTLOAD) {
2472 assert(VT == EVT && "Non-extending load from different memory type!");
2473 } else {
2474 // Extending load.
2475 if (MVT::isVector(VT))
2476 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2477 else
2478 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2479 "Should only be an extending load, not truncating!");
2480 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2481 "Cannot sign/zero extend a FP/Vector load!");
2482 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2483 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00002484 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00002485
2486 bool Indexed = AM != ISD::UNINDEXED;
2487 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2488 "Unindexed load with an offset!");
2489
2490 SDVTList VTs = Indexed ?
2491 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2492 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002493 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002494 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002495 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00002496 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002497 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002498 ID.AddInteger(Alignment);
2499 ID.AddInteger(isVolatile);
2500 void *IP = 0;
2501 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2502 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002503 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
2504 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002505 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002506 AllNodes.push_back(N);
2507 return SDOperand(N, 0);
2508}
2509
Duncan Sands14ea39c2008-03-27 20:23:40 +00002510SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2511 SDOperand Chain, SDOperand Ptr,
2512 const Value *SV, int SVOffset,
2513 bool isVolatile, unsigned Alignment) {
2514 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002515 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
2516 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002517}
2518
2519SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2520 SDOperand Chain, SDOperand Ptr,
2521 const Value *SV,
2522 int SVOffset, MVT::ValueType EVT,
2523 bool isVolatile, unsigned Alignment) {
2524 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002525 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
2526 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002527}
2528
Evan Cheng144d8f02006-11-09 17:55:04 +00002529SDOperand
2530SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2531 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002532 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002533 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2534 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00002535 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
2536 LD->getChain(), Base, Offset, LD->getSrcValue(),
2537 LD->getSrcValueOffset(), LD->getMemoryVT(),
2538 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00002539}
2540
Jeff Cohend41b30d2006-11-05 19:31:28 +00002541SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002542 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002543 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002544 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002545
Dan Gohman575e2f42007-06-04 15:49:41 +00002546 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2547 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002548 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002549 Ty = MVT::getTypeForValueType(VT);
2550 } else if (SV) {
2551 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2552 assert(PT && "Value for store must be a pointer");
2553 Ty = PT->getElementType();
2554 }
2555 assert(Ty && "Could not get type information for store");
2556 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2557 }
Evan Chengad071e12006-10-05 22:57:11 +00002558 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002559 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002560 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002561 FoldingSetNodeID ID;
2562 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563 ID.AddInteger(ISD::UNINDEXED);
2564 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002565 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002566 ID.AddInteger(Alignment);
2567 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002568 void *IP = 0;
2569 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2570 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002571 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002572 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002573 CSEMap.InsertNode(N, IP);
2574 AllNodes.push_back(N);
2575 return SDOperand(N, 0);
2576}
2577
Jeff Cohend41b30d2006-11-05 19:31:28 +00002578SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002579 SDOperand Ptr, const Value *SV,
2580 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002581 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002582 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002583
2584 if (VT == SVT)
2585 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586
Duncan Sandsaf47b112007-10-16 09:56:48 +00002587 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2588 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002589 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2590 "Can't do FP-INT conversion!");
2591
Dan Gohman575e2f42007-06-04 15:49:41 +00002592 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2593 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002594 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002595 Ty = MVT::getTypeForValueType(VT);
2596 } else if (SV) {
2597 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2598 assert(PT && "Value for store must be a pointer");
2599 Ty = PT->getElementType();
2600 }
2601 assert(Ty && "Could not get type information for store");
2602 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2603 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002604 SDVTList VTs = getVTList(MVT::Other);
2605 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002606 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002607 FoldingSetNodeID ID;
2608 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002609 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002610 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002611 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002612 ID.AddInteger(Alignment);
2613 ID.AddInteger(isVolatile);
2614 void *IP = 0;
2615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2616 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002617 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002618 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002619 CSEMap.InsertNode(N, IP);
2620 AllNodes.push_back(N);
2621 return SDOperand(N, 0);
2622}
2623
Evan Cheng144d8f02006-11-09 17:55:04 +00002624SDOperand
2625SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2626 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002627 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2628 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2629 "Store is already a indexed store!");
2630 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2631 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2632 FoldingSetNodeID ID;
2633 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2634 ID.AddInteger(AM);
2635 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002636 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002637 ID.AddInteger(ST->getAlignment());
2638 ID.AddInteger(ST->isVolatile());
2639 void *IP = 0;
2640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2641 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002642 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002643 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002644 ST->getSrcValue(), ST->getSrcValueOffset(),
2645 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002646 CSEMap.InsertNode(N, IP);
2647 AllNodes.push_back(N);
2648 return SDOperand(N, 0);
2649}
2650
Nate Begemanacc398c2006-01-25 18:21:52 +00002651SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2652 SDOperand Chain, SDOperand Ptr,
2653 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002654 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002655 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002656}
2657
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002658SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002659 const SDOperand *Ops, unsigned NumOps) {
2660 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002661 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002662 case 1: return getNode(Opcode, VT, Ops[0]);
2663 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2664 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002665 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002666 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002667
Chris Lattneref847df2005-04-09 03:27:28 +00002668 switch (Opcode) {
2669 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002670 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002671 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002672 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2673 "LHS and RHS of condition must have same type!");
2674 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2675 "True and False arms of SelectCC must have same type!");
2676 assert(Ops[2].getValueType() == VT &&
2677 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002678 break;
2679 }
2680 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002681 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002682 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2683 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002684 break;
2685 }
Chris Lattneref847df2005-04-09 03:27:28 +00002686 }
2687
Chris Lattner385328c2005-05-14 07:42:29 +00002688 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002689 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002690 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002691 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002692 FoldingSetNodeID ID;
2693 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002694 void *IP = 0;
2695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2696 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002697 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002698 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002699 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002700 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002701 }
Chris Lattneref847df2005-04-09 03:27:28 +00002702 AllNodes.push_back(N);
2703 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002704}
2705
Chris Lattner89c34632005-05-14 06:20:26 +00002706SDOperand SelectionDAG::getNode(unsigned Opcode,
2707 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002708 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002709 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2710 Ops, NumOps);
2711}
2712
2713SDOperand SelectionDAG::getNode(unsigned Opcode,
2714 const MVT::ValueType *VTs, unsigned NumVTs,
2715 const SDOperand *Ops, unsigned NumOps) {
2716 if (NumVTs == 1)
2717 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002718 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2719}
2720
2721SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2722 const SDOperand *Ops, unsigned NumOps) {
2723 if (VTList.NumVTs == 1)
2724 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002725
Chris Lattner5f056bf2005-07-10 01:55:33 +00002726 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002727 // FIXME: figure out how to safely handle things like
2728 // int foo(int x) { return 1 << (x & 255); }
2729 // int bar() { return foo(256); }
2730#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002731 case ISD::SRA_PARTS:
2732 case ISD::SRL_PARTS:
2733 case ISD::SHL_PARTS:
2734 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002735 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002736 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2737 else if (N3.getOpcode() == ISD::AND)
2738 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2739 // If the and is only masking out bits that cannot effect the shift,
2740 // eliminate the and.
2741 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2742 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2743 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2744 }
2745 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002746#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002747 }
Chris Lattner89c34632005-05-14 06:20:26 +00002748
Chris Lattner43247a12005-08-25 19:12:10 +00002749 // Memoize the node unless it returns a flag.
2750 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002751 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002752 FoldingSetNodeID ID;
2753 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002754 void *IP = 0;
2755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2756 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002757 if (NumOps == 1)
2758 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2759 else if (NumOps == 2)
2760 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2761 else if (NumOps == 3)
2762 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2763 else
2764 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002765 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002766 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002767 if (NumOps == 1)
2768 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2769 else if (NumOps == 2)
2770 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2771 else if (NumOps == 3)
2772 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2773 else
2774 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002775 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002776 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002777 return SDOperand(N, 0);
2778}
2779
Dan Gohman08ce9762007-10-08 15:49:58 +00002780SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2781 return getNode(Opcode, VTList, 0, 0);
2782}
2783
2784SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2785 SDOperand N1) {
2786 SDOperand Ops[] = { N1 };
2787 return getNode(Opcode, VTList, Ops, 1);
2788}
2789
2790SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2791 SDOperand N1, SDOperand N2) {
2792 SDOperand Ops[] = { N1, N2 };
2793 return getNode(Opcode, VTList, Ops, 2);
2794}
2795
2796SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2797 SDOperand N1, SDOperand N2, SDOperand N3) {
2798 SDOperand Ops[] = { N1, N2, N3 };
2799 return getNode(Opcode, VTList, Ops, 3);
2800}
2801
2802SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2803 SDOperand N1, SDOperand N2, SDOperand N3,
2804 SDOperand N4) {
2805 SDOperand Ops[] = { N1, N2, N3, N4 };
2806 return getNode(Opcode, VTList, Ops, 4);
2807}
2808
2809SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2810 SDOperand N1, SDOperand N2, SDOperand N3,
2811 SDOperand N4, SDOperand N5) {
2812 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2813 return getNode(Opcode, VTList, Ops, 5);
2814}
2815
Chris Lattner70046e92006-08-15 17:46:01 +00002816SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002817 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002818}
2819
Chris Lattner70046e92006-08-15 17:46:01 +00002820SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002821 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2822 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002823 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002824 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002825 }
2826 std::vector<MVT::ValueType> V;
2827 V.push_back(VT1);
2828 V.push_back(VT2);
2829 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002830 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002831}
Chris Lattner70046e92006-08-15 17:46:01 +00002832SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2833 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002834 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002835 E = VTList.end(); I != E; ++I) {
2836 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2837 (*I)[2] == VT3)
2838 return makeVTList(&(*I)[0], 3);
2839 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002840 std::vector<MVT::ValueType> V;
2841 V.push_back(VT1);
2842 V.push_back(VT2);
2843 V.push_back(VT3);
2844 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002845 return makeVTList(&(*VTList.begin())[0], 3);
2846}
2847
2848SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2849 switch (NumVTs) {
2850 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002851 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002852 case 2: return getVTList(VTs[0], VTs[1]);
2853 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2854 default: break;
2855 }
2856
2857 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2858 E = VTList.end(); I != E; ++I) {
2859 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2860
2861 bool NoMatch = false;
2862 for (unsigned i = 2; i != NumVTs; ++i)
2863 if (VTs[i] != (*I)[i]) {
2864 NoMatch = true;
2865 break;
2866 }
2867 if (!NoMatch)
2868 return makeVTList(&*I->begin(), NumVTs);
2869 }
2870
2871 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2872 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002873}
2874
2875
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002876/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2877/// specified operands. If the resultant node already exists in the DAG,
2878/// this does not modify the specified node, instead it returns the node that
2879/// already exists. If the resultant node does not exist in the DAG, the
2880/// input node is returned. As a degenerate case, if you specify the same
2881/// input operands as the node already has, the input node is returned.
2882SDOperand SelectionDAG::
2883UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2884 SDNode *N = InN.Val;
2885 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2886
2887 // Check to see if there is no change.
2888 if (Op == N->getOperand(0)) return InN;
2889
2890 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002891 void *InsertPos = 0;
2892 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2893 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002894
2895 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002896 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002897 RemoveNodeFromCSEMaps(N);
2898
2899 // Now we update the operands.
Roman Levensteindc1adac2008-04-07 10:06:32 +00002900 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002901 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00002902 N->OperandList[0].setUser(N);
2903 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002904
2905 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002906 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002907 return InN;
2908}
2909
2910SDOperand SelectionDAG::
2911UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2912 SDNode *N = InN.Val;
2913 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2914
2915 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002916 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2917 return InN; // No operands changed, just return the input node.
2918
2919 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002920 void *InsertPos = 0;
2921 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2922 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002923
2924 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002925 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002926 RemoveNodeFromCSEMaps(N);
2927
2928 // Now we update the operands.
2929 if (N->OperandList[0] != Op1) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002930 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002931 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00002932 N->OperandList[0].setUser(N);
2933 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002934 }
2935 if (N->OperandList[1] != Op2) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002936 N->OperandList[1].Val->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002937 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00002938 N->OperandList[1].setUser(N);
2939 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002940 }
2941
2942 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002943 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002944 return InN;
2945}
2946
2947SDOperand SelectionDAG::
2948UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002949 SDOperand Ops[] = { Op1, Op2, Op3 };
2950 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002951}
2952
2953SDOperand SelectionDAG::
2954UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2955 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002956 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2957 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002958}
2959
2960SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002961UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2962 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002963 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2964 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002965}
2966
Chris Lattner809ec112006-01-28 10:09:25 +00002967SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002968UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002969 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002970 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002971 "Update with wrong number of operands");
2972
2973 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002974 bool AnyChange = false;
2975 for (unsigned i = 0; i != NumOps; ++i) {
2976 if (Ops[i] != N->getOperand(i)) {
2977 AnyChange = true;
2978 break;
2979 }
2980 }
2981
2982 // No operands changed, just return the input node.
2983 if (!AnyChange) return InN;
2984
2985 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002986 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002987 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002988 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002989
2990 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002991 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002992 RemoveNodeFromCSEMaps(N);
2993
2994 // Now we update the operands.
2995 for (unsigned i = 0; i != NumOps; ++i) {
2996 if (N->OperandList[i] != Ops[i]) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002997 N->OperandList[i].Val->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002998 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00002999 N->OperandList[i].setUser(N);
3000 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003001 }
3002 }
3003
3004 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003005 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003006 return InN;
3007}
3008
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003009/// MorphNodeTo - This frees the operands of the current node, resets the
3010/// opcode, types, and operands to the specified value. This should only be
3011/// used by the SelectionDAG class.
3012void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3013 const SDOperand *Ops, unsigned NumOps) {
3014 NodeType = Opc;
3015 ValueList = L.VTs;
3016 NumValues = L.NumVTs;
3017
3018 // Clear the operands list, updating used nodes to remove this from their
3019 // use list.
3020 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levensteindc1adac2008-04-07 10:06:32 +00003021 I->Val->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003022
3023 // If NumOps is larger than the # of operands we currently have, reallocate
3024 // the operand list.
3025 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003026 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003027 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003028 }
Chris Lattner63e3f142007-02-04 07:28:00 +00003029 OperandList = new SDOperand[NumOps];
3030 OperandsNeedDelete = true;
3031 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003032
3033 // Assign the new operands.
3034 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003035
3036 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3037 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003038 OperandList[i].setUser(this);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003039 SDNode *N = OperandList[i].Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003040 N->addUser(i, this);
3041 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003042 }
3043}
Chris Lattner149c58c2005-08-16 18:17:10 +00003044
3045/// SelectNodeTo - These are used for target selectors to *mutate* the
3046/// specified node to have the specified return type, Target opcode, and
3047/// operands. Note that target opcodes are stored as
3048/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003049///
3050/// Note that SelectNodeTo returns the resultant node. If there is already a
3051/// node of the specified opcode and operands, it returns that node instead of
3052/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003053SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3054 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003055 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003056 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003057 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003058 void *IP = 0;
3059 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003060 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003061
Chris Lattner7651fa42005-08-24 23:00:29 +00003062 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003063
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003064 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003065
Chris Lattner4a283e92006-08-11 18:38:11 +00003066 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003067 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003068}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003069
Evan Cheng95514ba2006-08-26 08:00:10 +00003070SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3071 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003072 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003073 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003074 SDOperand Ops[] = { Op1 };
3075
Jim Laskey583bd472006-10-27 23:46:08 +00003076 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003077 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003078 void *IP = 0;
3079 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003080 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003081
Chris Lattner149c58c2005-08-16 18:17:10 +00003082 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003083 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003084 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003085 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003086}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003087
Evan Cheng95514ba2006-08-26 08:00:10 +00003088SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3089 MVT::ValueType VT, SDOperand Op1,
3090 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003091 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003092 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003093 SDOperand Ops[] = { Op1, Op2 };
3094
Jim Laskey583bd472006-10-27 23:46:08 +00003095 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003096 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003097 void *IP = 0;
3098 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003099 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003100
Chris Lattner149c58c2005-08-16 18:17:10 +00003101 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003102
Chris Lattner63e3f142007-02-04 07:28:00 +00003103 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003104
Chris Lattnera5682852006-08-07 23:03:03 +00003105 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003106 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003107}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003108
Evan Cheng95514ba2006-08-26 08:00:10 +00003109SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3110 MVT::ValueType VT, SDOperand Op1,
3111 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003112 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003113 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003114 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003115 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003116 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003117 void *IP = 0;
3118 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003119 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003120
Chris Lattner149c58c2005-08-16 18:17:10 +00003121 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003122
Chris Lattner63e3f142007-02-04 07:28:00 +00003123 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003124
Chris Lattnera5682852006-08-07 23:03:03 +00003125 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003126 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003127}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003128
Evan Cheng95514ba2006-08-26 08:00:10 +00003129SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003130 MVT::ValueType VT, const SDOperand *Ops,
3131 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003132 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003133 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003134 FoldingSetNodeID ID;
3135 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003136 void *IP = 0;
3137 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003138 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003139
Chris Lattner6b09a292005-08-21 18:49:33 +00003140 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003141 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003142
Chris Lattnera5682852006-08-07 23:03:03 +00003143 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003144 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003145}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003146
Evan Cheng95514ba2006-08-26 08:00:10 +00003147SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3148 MVT::ValueType VT1, MVT::ValueType VT2,
3149 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003150 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003151 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003152 SDOperand Ops[] = { Op1, Op2 };
3153 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003154 void *IP = 0;
3155 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003156 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003157
Chris Lattner0fb094f2005-11-19 01:44:53 +00003158 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003159 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003160 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003161 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003162}
3163
Evan Cheng95514ba2006-08-26 08:00:10 +00003164SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3165 MVT::ValueType VT1, MVT::ValueType VT2,
3166 SDOperand Op1, SDOperand Op2,
3167 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003168 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003169 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003170 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003171 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003172 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003173 void *IP = 0;
3174 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003175 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003176
Chris Lattner0fb094f2005-11-19 01:44:53 +00003177 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003178
Chris Lattner63e3f142007-02-04 07:28:00 +00003179 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003180 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003181 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003182}
3183
Chris Lattner0fb094f2005-11-19 01:44:53 +00003184
Evan Cheng6ae46c42006-02-09 07:15:23 +00003185/// getTargetNode - These are used for target selectors to create a new node
3186/// with specified return type(s), target opcode, and operands.
3187///
3188/// Note that getTargetNode returns the resultant node. If there is already a
3189/// node of the specified opcode and operands, it returns that node instead of
3190/// the current one.
3191SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3192 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3193}
3194SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3195 SDOperand Op1) {
3196 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3197}
3198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3199 SDOperand Op1, SDOperand Op2) {
3200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3201}
3202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003203 SDOperand Op1, SDOperand Op2,
3204 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003205 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3206}
3207SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003208 const SDOperand *Ops, unsigned NumOps) {
3209 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003210}
3211SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003212 MVT::ValueType VT2) {
3213 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3214 SDOperand Op;
3215 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3216}
3217SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003218 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003219 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003220 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003221}
3222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003223 MVT::ValueType VT2, SDOperand Op1,
3224 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003225 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003226 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003227 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003228}
3229SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003230 MVT::ValueType VT2, SDOperand Op1,
3231 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003232 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003233 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003234 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003235}
Evan Cheng694481e2006-08-27 08:08:54 +00003236SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3237 MVT::ValueType VT2,
3238 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003239 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003240 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003241}
3242SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3243 MVT::ValueType VT2, MVT::ValueType VT3,
3244 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003245 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003246 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003247 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003248}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3250 MVT::ValueType VT2, MVT::ValueType VT3,
3251 SDOperand Op1, SDOperand Op2,
3252 SDOperand Op3) {
3253 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3254 SDOperand Ops[] = { Op1, Op2, Op3 };
3255 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3256}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003257SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003258 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003259 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003260 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3261 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003262}
Evan Cheng05e69c12007-09-12 23:39:49 +00003263SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3264 MVT::ValueType VT2, MVT::ValueType VT3,
3265 MVT::ValueType VT4,
3266 const SDOperand *Ops, unsigned NumOps) {
3267 std::vector<MVT::ValueType> VTList;
3268 VTList.push_back(VT1);
3269 VTList.push_back(VT2);
3270 VTList.push_back(VT3);
3271 VTList.push_back(VT4);
3272 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3273 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3274}
Evan Cheng39305cf2007-10-05 01:10:49 +00003275SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3276 std::vector<MVT::ValueType> &ResultTys,
3277 const SDOperand *Ops, unsigned NumOps) {
3278 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3279 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3280 Ops, NumOps).Val;
3281}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003282
Evan Cheng08b11732008-03-22 01:55:50 +00003283/// getNodeIfExists - Get the specified node if it's already available, or
3284/// else return NULL.
3285SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
3286 const SDOperand *Ops, unsigned NumOps) {
3287 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3288 FoldingSetNodeID ID;
3289 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3290 void *IP = 0;
3291 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3292 return E;
3293 }
3294 return NULL;
3295}
3296
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003297
Evan Cheng99157a02006-08-07 22:13:29 +00003298/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003299/// This can cause recursive merging of nodes in the DAG.
3300///
Chris Lattner11d049c2008-02-03 03:35:22 +00003301/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003302///
Chris Lattner11d049c2008-02-03 03:35:22 +00003303void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003304 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003305 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003306 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003307 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003308 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003309
Chris Lattner8b8749f2005-08-17 19:00:20 +00003310 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003311 SDNode::use_iterator UI = From->use_begin();
3312 SDNode *U = UI->getUser();
3313
Chris Lattner8b8749f2005-08-17 19:00:20 +00003314 // This node is about to morph, remove its old self from the CSE maps.
3315 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003316 int operandNum = 0;
3317 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3318 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003319 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003320 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003321 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003322 I->setUser(U);
3323 To.Val->addUser(operandNum, U);
3324 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003325
3326 // Now that we have modified U, add it back to the CSE maps. If it already
3327 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003328 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003329 ReplaceAllUsesWith(U, Existing, UpdateListener);
3330 // U is now dead. Inform the listener if it exists and delete it.
3331 if (UpdateListener)
3332 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003333 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003334 } else {
3335 // If the node doesn't already exist, we updated it. Inform a listener if
3336 // it exists.
3337 if (UpdateListener)
3338 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003339 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003340 }
3341}
3342
3343/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3344/// This can cause recursive merging of nodes in the DAG.
3345///
3346/// This version assumes From/To have matching types and numbers of result
3347/// values.
3348///
Chris Lattner1e111c72005-09-07 05:37:01 +00003349void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003350 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003351 assert(From != To && "Cannot replace uses of with self");
3352 assert(From->getNumValues() == To->getNumValues() &&
3353 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003354 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003355 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3356 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003357
3358 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003359 SDNode::use_iterator UI = From->use_begin();
3360 SDNode *U = UI->getUser();
3361
Chris Lattner8b52f212005-08-26 18:36:28 +00003362 // This node is about to morph, remove its old self from the CSE maps.
3363 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003364 int operandNum = 0;
3365 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3366 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003367 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003368 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003369 I->Val = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003370 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003371 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003372
Chris Lattner8b8749f2005-08-17 19:00:20 +00003373 // Now that we have modified U, add it back to the CSE maps. If it already
3374 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003375 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003376 ReplaceAllUsesWith(U, Existing, UpdateListener);
3377 // U is now dead. Inform the listener if it exists and delete it.
3378 if (UpdateListener)
3379 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003380 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003381 } else {
3382 // If the node doesn't already exist, we updated it. Inform a listener if
3383 // it exists.
3384 if (UpdateListener)
3385 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003386 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003387 }
3388}
3389
Chris Lattner8b52f212005-08-26 18:36:28 +00003390/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3391/// This can cause recursive merging of nodes in the DAG.
3392///
3393/// This version can replace From with any result values. To must match the
3394/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003395void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003396 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003397 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003398 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003399 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003400
3401 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003402 SDNode::use_iterator UI = From->use_begin();
3403 SDNode *U = UI->getUser();
3404
Chris Lattner7b2880c2005-08-24 22:44:39 +00003405 // This node is about to morph, remove its old self from the CSE maps.
3406 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003407 int operandNum = 0;
3408 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3409 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003410 if (I->Val == From) {
3411 const SDOperand &ToOp = To[I->ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003412 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003413 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003414 I->setUser(U);
3415 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003416 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003417
Chris Lattner7b2880c2005-08-24 22:44:39 +00003418 // Now that we have modified U, add it back to the CSE maps. If it already
3419 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003420 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003421 ReplaceAllUsesWith(U, Existing, UpdateListener);
3422 // U is now dead. Inform the listener if it exists and delete it.
3423 if (UpdateListener)
3424 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003425 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003426 } else {
3427 // If the node doesn't already exist, we updated it. Inform a listener if
3428 // it exists.
3429 if (UpdateListener)
3430 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003431 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003432 }
3433}
3434
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003435namespace {
3436 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3437 /// any deleted nodes from the set passed into its constructor and recursively
3438 /// notifies another update listener if specified.
3439 class ChainedSetUpdaterListener :
3440 public SelectionDAG::DAGUpdateListener {
3441 SmallSetVector<SDNode*, 16> &Set;
3442 SelectionDAG::DAGUpdateListener *Chain;
3443 public:
3444 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3445 SelectionDAG::DAGUpdateListener *chain)
3446 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00003447
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003448 virtual void NodeDeleted(SDNode *N) {
3449 Set.remove(N);
3450 if (Chain) Chain->NodeDeleted(N);
3451 }
3452 virtual void NodeUpdated(SDNode *N) {
3453 if (Chain) Chain->NodeUpdated(N);
3454 }
3455 };
3456}
3457
Chris Lattner012f2412006-02-17 21:58:01 +00003458/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3459/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003460/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003461void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003462 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003463 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003464
Chris Lattner012f2412006-02-17 21:58:01 +00003465 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003466 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003467 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003468 return;
3469 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003470
3471 if (From.use_empty()) return;
3472
Chris Lattnercf5640b2007-02-04 00:14:31 +00003473 // Get all of the users of From.Val. We want these in a nice,
3474 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003475 SmallSetVector<SDNode*, 16> Users;
3476 for (SDNode::use_iterator UI = From.Val->use_begin(),
3477 E = From.Val->use_end(); UI != E; ++UI) {
3478 SDNode *User = UI->getUser();
3479 if (!Users.count(User))
3480 Users.insert(User);
3481 }
Chris Lattner012f2412006-02-17 21:58:01 +00003482
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003483 // When one of the recursive merges deletes nodes from the graph, we need to
3484 // make sure that UpdateListener is notified *and* that the node is removed
3485 // from Users if present. CSUL does this.
3486 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003487
Chris Lattner012f2412006-02-17 21:58:01 +00003488 while (!Users.empty()) {
3489 // We know that this user uses some value of From. If it is the right
3490 // value, update it.
3491 SDNode *User = Users.back();
3492 Users.pop_back();
3493
Chris Lattner01d029b2007-10-15 06:10:22 +00003494 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003495 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00003496 for (; Op != E; ++Op)
3497 if (*Op == From) break;
3498
3499 // If there are no matches, the user must use some other result of From.
3500 if (Op == E) continue;
3501
3502 // Okay, we know this user needs to be updated. Remove its old self
3503 // from the CSE maps.
3504 RemoveNodeFromCSEMaps(User);
3505
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003506 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003507 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003508 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003509 From.Val->removeUser(Op-User->op_begin(), User);
3510 *Op = To;
3511 Op->setUser(User);
3512 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00003513 }
3514 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003515
3516 // Now that we have modified User, add it back to the CSE maps. If it
3517 // already exists there, recursively merge the results together.
3518 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003519 if (!Existing) {
3520 if (UpdateListener) UpdateListener->NodeUpdated(User);
3521 continue; // Continue on to next user.
3522 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003523
3524 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003525 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003526 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003527 // can cause deletion of nodes that used the old value. To handle this, we
3528 // use CSUL to remove them from the Users set.
3529 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003530
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003531 // User is now dead. Notify a listener if present.
3532 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003533 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003534 }
3535}
3536
Chris Lattner7b2880c2005-08-24 22:44:39 +00003537
Evan Chenge6f35d82006-08-01 08:20:41 +00003538/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3539/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003540unsigned SelectionDAG::AssignNodeIds() {
3541 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003542 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3543 SDNode *N = I;
3544 N->setNodeId(Id++);
3545 }
3546 return Id;
3547}
3548
Evan Chenge6f35d82006-08-01 08:20:41 +00003549/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003550/// based on their topological order. It returns the maximum id and a vector
3551/// of the SDNodes* in assigned order by reference.
3552unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003553 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003554 std::vector<unsigned> InDegree(DAGSize);
3555 std::vector<SDNode*> Sources;
3556
3557 // Use a two pass approach to avoid using a std::map which is slow.
3558 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003559 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3560 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003561 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003562 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003563 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003564 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003565 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003566 }
3567
Evan Cheng99157a02006-08-07 22:13:29 +00003568 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003569 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003570 SDNode *N = Sources.back();
3571 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003572 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003573 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3574 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003575 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003576 if (Degree == 0)
3577 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003578 }
3579 }
3580
Evan Chengc384d6c2006-08-02 22:00:34 +00003581 // Second pass, assign the actual topological order as node ids.
3582 Id = 0;
3583 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3584 TI != TE; ++TI)
3585 (*TI)->setNodeId(Id++);
3586
3587 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003588}
3589
3590
Evan Cheng091cba12006-07-27 06:39:06 +00003591
Jim Laskey58b968b2005-08-17 20:08:02 +00003592//===----------------------------------------------------------------------===//
3593// SDNode Class
3594//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003595
Chris Lattner917d2c92006-07-19 00:00:37 +00003596// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003597void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003598void UnarySDNode::ANCHOR() {}
3599void BinarySDNode::ANCHOR() {}
3600void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003601void HandleSDNode::ANCHOR() {}
3602void StringSDNode::ANCHOR() {}
3603void ConstantSDNode::ANCHOR() {}
3604void ConstantFPSDNode::ANCHOR() {}
3605void GlobalAddressSDNode::ANCHOR() {}
3606void FrameIndexSDNode::ANCHOR() {}
3607void JumpTableSDNode::ANCHOR() {}
3608void ConstantPoolSDNode::ANCHOR() {}
3609void BasicBlockSDNode::ANCHOR() {}
3610void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003611void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003612void RegisterSDNode::ANCHOR() {}
3613void ExternalSymbolSDNode::ANCHOR() {}
3614void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00003615void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003616void VTSDNode::ANCHOR() {}
3617void LoadSDNode::ANCHOR() {}
3618void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003619void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003620
Chris Lattner48b85922007-02-04 02:41:42 +00003621HandleSDNode::~HandleSDNode() {
3622 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003623 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003624}
3625
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003626GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3627 MVT::ValueType VT, int o)
3628 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003629 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003630 // Thread Local
3631 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3632 // Non Thread Local
3633 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3634 getSDVTList(VT)), Offset(o) {
3635 TheGlobal = const_cast<GlobalValue*>(GA);
3636}
Chris Lattner48b85922007-02-04 02:41:42 +00003637
Dan Gohman36b5c132008-04-07 19:35:22 +00003638/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00003639/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00003640MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman69de1932008-02-06 22:27:42 +00003641 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3642 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00003643 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
3644 MachineMemOperand::MOStore;
3645 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00003646
3647 // Check if the load references a frame index, and does not have
3648 // an SV attached.
3649 const FrameIndexSDNode *FI =
3650 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3651 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00003652 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
3653 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003654 else
Dan Gohman36b5c132008-04-07 19:35:22 +00003655 return MachineMemOperand(getSrcValue(), Flags,
3656 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003657}
3658
Jim Laskey583bd472006-10-27 23:46:08 +00003659/// Profile - Gather unique data for the node.
3660///
3661void SDNode::Profile(FoldingSetNodeID &ID) {
3662 AddNodeIDNode(ID, this);
3663}
3664
Chris Lattnera3255112005-11-08 23:30:28 +00003665/// getValueTypeList - Return a pointer to the specified value type.
3666///
Dan Gohman547ca532008-02-08 03:26:46 +00003667const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003668 if (MVT::isExtendedVT(VT)) {
3669 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00003670 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00003671 } else {
3672 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3673 VTs[VT] = VT;
3674 return &VTs[VT];
3675 }
Chris Lattnera3255112005-11-08 23:30:28 +00003676}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003677
Chris Lattner5c884562005-01-12 18:37:47 +00003678/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3679/// indicated value. This method ignores uses of other values defined by this
3680/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003681bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003682 assert(Value < getNumValues() && "Bad value!");
3683
3684 // If there is only one value, this is easy.
3685 if (getNumValues() == 1)
3686 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003687 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003688
Evan Cheng4ee62112006-02-05 06:29:23 +00003689 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003690
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003691 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003692
Roman Levensteindc1adac2008-04-07 10:06:32 +00003693 // TODO: Only iterate over uses of a given value of the node
3694 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
3695 if (*UI == TheValue) {
3696 if (NUses == 0)
3697 return false;
3698 --NUses;
3699 }
Chris Lattner5c884562005-01-12 18:37:47 +00003700 }
3701
3702 // Found exactly the right number of uses?
3703 return NUses == 0;
3704}
3705
3706
Evan Cheng33d55952007-08-02 05:29:38 +00003707/// hasAnyUseOfValue - Return true if there are any use of the indicated
3708/// value. This method ignores uses of other values defined by this operation.
3709bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3710 assert(Value < getNumValues() && "Bad value!");
3711
Dan Gohman30359592008-01-29 13:02:09 +00003712 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003713
3714 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3715
3716 SmallPtrSet<SDNode*, 32> UsersHandled;
3717
Roman Levensteindc1adac2008-04-07 10:06:32 +00003718 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
3719 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00003720 if (User->getNumOperands() == 1 ||
3721 UsersHandled.insert(User)) // First time we've seen this?
3722 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3723 if (User->getOperand(i) == TheValue) {
3724 return true;
3725 }
3726 }
3727
3728 return false;
3729}
3730
3731
Evan Cheng917be682008-03-04 00:41:45 +00003732/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00003733///
Evan Cheng917be682008-03-04 00:41:45 +00003734bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00003735 bool Seen = false;
3736 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003737 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00003738 if (User == this)
3739 Seen = true;
3740 else
3741 return false;
3742 }
3743
3744 return Seen;
3745}
3746
Evan Chenge6e97e62006-11-03 07:31:32 +00003747/// isOperand - Return true if this node is an operand of N.
3748///
Roman Levensteindc1adac2008-04-07 10:06:32 +00003749bool SDOperandImpl::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00003750 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3751 if (*this == N->getOperand(i))
3752 return true;
3753 return false;
3754}
3755
Evan Cheng917be682008-03-04 00:41:45 +00003756bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003757 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3758 if (this == N->OperandList[i].Val)
3759 return true;
3760 return false;
3761}
Evan Cheng4ee62112006-02-05 06:29:23 +00003762
Chris Lattner572dee72008-01-16 05:49:24 +00003763/// reachesChainWithoutSideEffects - Return true if this operand (which must
3764/// be a chain) reaches the specified operand without crossing any
3765/// side-effecting instructions. In practice, this looks through token
3766/// factors and non-volatile loads. In order to remain efficient, this only
3767/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003768bool SDOperandImpl::reachesChainWithoutSideEffects(SDOperandImpl Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00003769 unsigned Depth) const {
3770 if (*this == Dest) return true;
3771
3772 // Don't search too deeply, we just want to be able to see through
3773 // TokenFactor's etc.
3774 if (Depth == 0) return false;
3775
3776 // If this is a token factor, all inputs to the TF happen in parallel. If any
3777 // of the operands of the TF reach dest, then we can do the xform.
3778 if (getOpcode() == ISD::TokenFactor) {
3779 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3780 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3781 return true;
3782 return false;
3783 }
3784
3785 // Loads don't have side effects, look through them.
3786 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3787 if (!Ld->isVolatile())
3788 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3789 }
3790 return false;
3791}
3792
3793
Evan Chengc5fc57d2006-11-03 03:05:24 +00003794static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003795 SmallPtrSet<SDNode *, 32> &Visited) {
3796 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003797 return;
3798
3799 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3800 SDNode *Op = N->getOperand(i).Val;
3801 if (Op == P) {
3802 found = true;
3803 return;
3804 }
3805 findPredecessor(Op, P, found, Visited);
3806 }
3807}
3808
Evan Cheng917be682008-03-04 00:41:45 +00003809/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00003810/// is either an operand of N or it can be reached by recursively traversing
3811/// up the operands.
3812/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00003813bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003814 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003815 bool found = false;
3816 findPredecessor(N, this, found, Visited);
3817 return found;
3818}
3819
Evan Chengc5484282006-10-04 00:56:09 +00003820uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3821 assert(Num < NumOperands && "Invalid child # of SDNode!");
3822 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3823}
3824
Reid Spencer577cc322007-04-01 07:32:19 +00003825std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003826 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003827 default:
3828 if (getOpcode() < ISD::BUILTIN_OP_END)
3829 return "<<Unknown DAG Node>>";
3830 else {
Evan Cheng72261582005-12-20 06:22:03 +00003831 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003832 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003833 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003834 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003835
Evan Cheng72261582005-12-20 06:22:03 +00003836 TargetLowering &TLI = G->getTargetLoweringInfo();
3837 const char *Name =
3838 TLI.getTargetNodeName(getOpcode());
3839 if (Name) return Name;
3840 }
3841
3842 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003843 }
3844
Evan Cheng27b7db52008-03-08 00:58:38 +00003845 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00003846 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003847 case ISD::ATOMIC_LCS: return "AtomicLCS";
3848 case ISD::ATOMIC_LAS: return "AtomicLAS";
3849 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00003850 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003851 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003852 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00003853 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003854 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003855 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003856 case ISD::AssertSext: return "AssertSext";
3857 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003858
3859 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003860 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00003861 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003862 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003863 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003864
3865 case ISD::Constant: return "Constant";
3866 case ISD::ConstantFP: return "ConstantFP";
3867 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003868 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003869 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003870 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003871 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003872 case ISD::RETURNADDR: return "RETURNADDR";
3873 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003874 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003875 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3876 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003877 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003878 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003879 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003880 case ISD::INTRINSIC_WO_CHAIN: {
3881 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3882 return Intrinsic::getName((Intrinsic::ID)IID);
3883 }
3884 case ISD::INTRINSIC_VOID:
3885 case ISD::INTRINSIC_W_CHAIN: {
3886 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003887 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003888 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003889
Chris Lattnerb2827b02006-03-19 00:52:58 +00003890 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003891 case ISD::TargetConstant: return "TargetConstant";
3892 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003893 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003894 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003895 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003896 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003897 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003898 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003899
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003900 case ISD::CopyToReg: return "CopyToReg";
3901 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003902 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003903 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003904 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003905 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003906 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003907 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003908 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003909 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003910
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003911 // Unary operators
3912 case ISD::FABS: return "fabs";
3913 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003914 case ISD::FSQRT: return "fsqrt";
3915 case ISD::FSIN: return "fsin";
3916 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003917 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003918 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003919
3920 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003921 case ISD::ADD: return "add";
3922 case ISD::SUB: return "sub";
3923 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003924 case ISD::MULHU: return "mulhu";
3925 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003926 case ISD::SDIV: return "sdiv";
3927 case ISD::UDIV: return "udiv";
3928 case ISD::SREM: return "srem";
3929 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003930 case ISD::SMUL_LOHI: return "smul_lohi";
3931 case ISD::UMUL_LOHI: return "umul_lohi";
3932 case ISD::SDIVREM: return "sdivrem";
3933 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003934 case ISD::AND: return "and";
3935 case ISD::OR: return "or";
3936 case ISD::XOR: return "xor";
3937 case ISD::SHL: return "shl";
3938 case ISD::SRA: return "sra";
3939 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003940 case ISD::ROTL: return "rotl";
3941 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003942 case ISD::FADD: return "fadd";
3943 case ISD::FSUB: return "fsub";
3944 case ISD::FMUL: return "fmul";
3945 case ISD::FDIV: return "fdiv";
3946 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003947 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003948 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003949
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003950 case ISD::SETCC: return "setcc";
3951 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003952 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003953 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003954 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003955 case ISD::CONCAT_VECTORS: return "concat_vectors";
3956 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003957 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003958 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003959 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003960 case ISD::ADDC: return "addc";
3961 case ISD::ADDE: return "adde";
3962 case ISD::SUBC: return "subc";
3963 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003964 case ISD::SHL_PARTS: return "shl_parts";
3965 case ISD::SRA_PARTS: return "sra_parts";
3966 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003967
3968 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3969 case ISD::INSERT_SUBREG: return "insert_subreg";
3970
Chris Lattner7f644642005-04-28 21:44:03 +00003971 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003972 case ISD::SIGN_EXTEND: return "sign_extend";
3973 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003974 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003975 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003976 case ISD::TRUNCATE: return "truncate";
3977 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003978 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003979 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003980 case ISD::FP_EXTEND: return "fp_extend";
3981
3982 case ISD::SINT_TO_FP: return "sint_to_fp";
3983 case ISD::UINT_TO_FP: return "uint_to_fp";
3984 case ISD::FP_TO_SINT: return "fp_to_sint";
3985 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003986 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003987
3988 // Control flow instructions
3989 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003990 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003991 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003992 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003993 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003994 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003995 case ISD::CALLSEQ_START: return "callseq_start";
3996 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003997
3998 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003999 case ISD::LOAD: return "load";
4000 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004001 case ISD::VAARG: return "vaarg";
4002 case ISD::VACOPY: return "vacopy";
4003 case ISD::VAEND: return "vaend";
4004 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004005 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004006 case ISD::EXTRACT_ELEMENT: return "extract_element";
4007 case ISD::BUILD_PAIR: return "build_pair";
4008 case ISD::STACKSAVE: return "stacksave";
4009 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004010 case ISD::TRAP: return "trap";
4011
Chris Lattner5a67afc2006-01-13 02:39:42 +00004012 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00004013 case ISD::MEMSET: return "memset";
4014 case ISD::MEMCPY: return "memcpy";
4015 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004016
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004017 // Bit manipulation
4018 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004019 case ISD::CTPOP: return "ctpop";
4020 case ISD::CTTZ: return "cttz";
4021 case ISD::CTLZ: return "ctlz";
4022
Chris Lattner36ce6912005-11-29 06:21:05 +00004023 // Debug info
4024 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004025 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004026
Duncan Sands36397f52007-07-27 12:58:54 +00004027 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004028 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004029
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004030 case ISD::CONDCODE:
4031 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004032 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004033 case ISD::SETOEQ: return "setoeq";
4034 case ISD::SETOGT: return "setogt";
4035 case ISD::SETOGE: return "setoge";
4036 case ISD::SETOLT: return "setolt";
4037 case ISD::SETOLE: return "setole";
4038 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004039
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004040 case ISD::SETO: return "seto";
4041 case ISD::SETUO: return "setuo";
4042 case ISD::SETUEQ: return "setue";
4043 case ISD::SETUGT: return "setugt";
4044 case ISD::SETUGE: return "setuge";
4045 case ISD::SETULT: return "setult";
4046 case ISD::SETULE: return "setule";
4047 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004048
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004049 case ISD::SETEQ: return "seteq";
4050 case ISD::SETGT: return "setgt";
4051 case ISD::SETGE: return "setge";
4052 case ISD::SETLT: return "setlt";
4053 case ISD::SETLE: return "setle";
4054 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004055 }
4056 }
4057}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004058
Evan Cheng144d8f02006-11-09 17:55:04 +00004059const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004060 switch (AM) {
4061 default:
4062 return "";
4063 case ISD::PRE_INC:
4064 return "<pre-inc>";
4065 case ISD::PRE_DEC:
4066 return "<pre-dec>";
4067 case ISD::POST_INC:
4068 return "<post-inc>";
4069 case ISD::POST_DEC:
4070 return "<post-dec>";
4071 }
4072}
4073
Duncan Sands276dcbd2008-03-21 09:14:45 +00004074std::string ISD::ArgFlagsTy::getArgFlagsString() {
4075 std::string S = "< ";
4076
4077 if (isZExt())
4078 S += "zext ";
4079 if (isSExt())
4080 S += "sext ";
4081 if (isInReg())
4082 S += "inreg ";
4083 if (isSRet())
4084 S += "sret ";
4085 if (isByVal())
4086 S += "byval ";
4087 if (isNest())
4088 S += "nest ";
4089 if (getByValAlign())
4090 S += "byval-align:" + utostr(getByValAlign()) + " ";
4091 if (getOrigAlign())
4092 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4093 if (getByValSize())
4094 S += "byval-size:" + utostr(getByValSize()) + " ";
4095 return S + ">";
4096}
4097
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004098void SDNode::dump() const { dump(0); }
4099void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004100 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004101
4102 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004103 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004104 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004105 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004106 else
Bill Wendling832171c2006-12-07 20:04:42 +00004107 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004108 }
Bill Wendling832171c2006-12-07 20:04:42 +00004109 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004110
Bill Wendling832171c2006-12-07 20:04:42 +00004111 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004112 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004113 if (i) cerr << ", ";
4114 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004115 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004116 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004117 }
4118
Evan Chengce254432007-12-11 02:08:35 +00004119 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4120 SDNode *Mask = getOperand(2).Val;
4121 cerr << "<";
4122 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4123 if (i) cerr << ",";
4124 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4125 cerr << "u";
4126 else
4127 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4128 }
4129 cerr << ">";
4130 }
4131
Chris Lattnerc3aae252005-01-07 07:46:32 +00004132 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004133 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004134 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004135 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4136 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4137 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4138 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4139 else {
4140 cerr << "<APFloat(";
4141 CSDN->getValueAPF().convertToAPInt().dump();
4142 cerr << ")>";
4143 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004144 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004145 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004146 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004147 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004148 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004149 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004150 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004151 else
Bill Wendling832171c2006-12-07 20:04:42 +00004152 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004153 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004154 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004155 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004156 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004157 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004158 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004159 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004160 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004161 else
Bill Wendling832171c2006-12-07 20:04:42 +00004162 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004163 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004164 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004165 else
Bill Wendling832171c2006-12-07 20:04:42 +00004166 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004167 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004168 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004169 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4170 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004171 cerr << LBB->getName() << " ";
4172 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004173 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004174 if (G && R->getReg() &&
4175 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004176 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004177 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004178 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004179 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004180 } else if (const ExternalSymbolSDNode *ES =
4181 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004182 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004183 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4184 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004185 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004186 else
Dan Gohman69de1932008-02-06 22:27:42 +00004187 cerr << "<null>";
4188 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4189 if (M->MO.getValue())
4190 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4191 else
4192 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004193 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4194 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004195 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004196 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004197 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004198 const Value *SrcValue = LD->getSrcValue();
4199 int SrcOffset = LD->getSrcValueOffset();
4200 cerr << " <";
4201 if (SrcValue)
4202 cerr << SrcValue;
4203 else
4204 cerr << "null";
4205 cerr << ":" << SrcOffset << ">";
4206
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004207 bool doExt = true;
4208 switch (LD->getExtensionType()) {
4209 default: doExt = false; break;
4210 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004211 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004212 break;
4213 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004214 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004215 break;
4216 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004217 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004218 break;
4219 }
4220 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004221 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004222
Evan Cheng144d8f02006-11-09 17:55:04 +00004223 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004224 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004225 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004226 if (LD->isVolatile())
4227 cerr << " <volatile>";
4228 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004229 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004230 const Value *SrcValue = ST->getSrcValue();
4231 int SrcOffset = ST->getSrcValueOffset();
4232 cerr << " <";
4233 if (SrcValue)
4234 cerr << SrcValue;
4235 else
4236 cerr << "null";
4237 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004238
4239 if (ST->isTruncatingStore())
4240 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004241 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004242
4243 const char *AM = getIndexedModeName(ST->getAddressingMode());
4244 if (*AM)
4245 cerr << " " << AM;
4246 if (ST->isVolatile())
4247 cerr << " <volatile>";
4248 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004249 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004250}
4251
Chris Lattnerde202b32005-11-09 23:47:37 +00004252static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004253 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4254 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004255 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004256 else
Bill Wendling832171c2006-12-07 20:04:42 +00004257 cerr << "\n" << std::string(indent+2, ' ')
4258 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004259
Chris Lattnerea946cd2005-01-09 20:38:33 +00004260
Bill Wendling832171c2006-12-07 20:04:42 +00004261 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004262 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004263}
4264
Chris Lattnerc3aae252005-01-07 07:46:32 +00004265void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004266 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004267 std::vector<const SDNode*> Nodes;
4268 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4269 I != E; ++I)
4270 Nodes.push_back(I);
4271
Chris Lattner49d24712005-01-09 20:26:36 +00004272 std::sort(Nodes.begin(), Nodes.end());
4273
4274 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004275 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004276 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004277 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004278
Jim Laskey26f7fa72006-10-17 19:33:52 +00004279 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004280
Bill Wendling832171c2006-12-07 20:04:42 +00004281 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004282}
4283
Evan Chengd6594ae2006-09-12 21:00:35 +00004284const Type *ConstantPoolSDNode::getType() const {
4285 if (isMachineConstantPoolEntry())
4286 return Val.MachineCPVal->getType();
4287 return Val.ConstVal->getType();
4288}