blob: 327a8fe897642c140fb8ae46885efbda4bf191ff [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"
Dan Gohman707e0182008-04-12 04:36:06 +000020#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000026#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000032#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000033#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000034#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000035#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000036#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000037#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000038#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner0b3e5252006-08-15 19:11:05 +000041/// makeVTList - Return an instance of the SDVTList struct initialized with the
42/// specified members.
43static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
44 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Chris Lattnerec4a5672008-03-05 06:48:13 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
49 switch (VT) {
50 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
56 }
57}
58
Chris Lattnerf8dc0612008-02-03 06:49:24 +000059SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000069bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000070 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000071}
72
Dale Johannesenf04afdb2007-08-30 00:23:21 +000073bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
74 const APFloat& Val) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000075 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
76
77 // Anything can be extended to ppc long double.
78 if (VT == MVT::ppcf128)
79 return true;
80
81 // PPC long double cannot be shrunk to anything though.
82 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
83 return false;
84
Dale Johannesenf04afdb2007-08-30 00:23:21 +000085 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000087 return Val2.convert(*MVTToAPFloatSemantics(VT),
88 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
99 if (N->getOpcode() == ISD::BIT_CONVERT)
100 N = N->getOperand(0).Val;
101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000103
104 unsigned i = 0, e = N->getNumOperands();
105
106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
109
110 // Do not accept an all-undef vector.
111 if (i == e) return false;
112
113 // Do not accept build_vectors that aren't all constants or which have non-~0
114 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000115 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000116 if (isa<ConstantSDNode>(NotZero)) {
117 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
118 return false;
119 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000120 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
121 convertToAPInt().isAllOnesValue())
122 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000173}
174
Evan Chengefec7512008-02-18 23:04:32 +0000175/// isScalarToVector - Return true if the specified node is a
176/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
177/// element is not an undef.
178bool ISD::isScalarToVector(const SDNode *N) {
179 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
180 return true;
181
182 if (N->getOpcode() != ISD::BUILD_VECTOR)
183 return false;
184 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
185 return false;
186 unsigned NumElems = N->getNumOperands();
187 for (unsigned i = 1; i < NumElems; ++i) {
188 SDOperand V = N->getOperand(i);
189 if (V.getOpcode() != ISD::UNDEF)
190 return false;
191 }
192 return true;
193}
194
195
Evan Chengbb81d972008-01-31 09:59:15 +0000196/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000197/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000198/// is 0).
199bool ISD::isDebugLabel(const SDNode *N) {
200 SDOperand Zero;
201 if (N->getOpcode() == ISD::LABEL)
202 Zero = N->getOperand(2);
203 else if (N->isTargetOpcode() &&
204 N->getTargetOpcode() == TargetInstrInfo::LABEL)
205 // Chain moved to last operand.
206 Zero = N->getOperand(1);
207 else
208 return false;
209 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
210}
211
Chris Lattnerc3aae252005-01-07 07:46:32 +0000212/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
213/// when given the operation for (X op Y).
214ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
215 // To perform this operation, we just need to swap the L and G bits of the
216 // operation.
217 unsigned OldL = (Operation >> 2) & 1;
218 unsigned OldG = (Operation >> 1) & 1;
219 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
220 (OldL << 1) | // New G bit
221 (OldG << 2)); // New L bit.
222}
223
224/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
225/// 'op' is a valid SetCC operation.
226ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
227 unsigned Operation = Op;
228 if (isInteger)
229 Operation ^= 7; // Flip L, G, E bits, but not U.
230 else
231 Operation ^= 15; // Flip all of the condition bits.
232 if (Operation > ISD::SETTRUE2)
233 Operation &= ~8; // Don't let N and U bits get set.
234 return ISD::CondCode(Operation);
235}
236
237
238/// isSignedOp - For an integer comparison, return 1 if the comparison is a
239/// signed operation and 2 if the result is an unsigned comparison. Return zero
240/// if the operation does not depend on the sign of the input (setne and seteq).
241static int isSignedOp(ISD::CondCode Opcode) {
242 switch (Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
244 case ISD::SETEQ:
245 case ISD::SETNE: return 0;
246 case ISD::SETLT:
247 case ISD::SETLE:
248 case ISD::SETGT:
249 case ISD::SETGE: return 1;
250 case ISD::SETULT:
251 case ISD::SETULE:
252 case ISD::SETUGT:
253 case ISD::SETUGE: return 2;
254 }
255}
256
257/// getSetCCOrOperation - Return the result of a logical OR between different
258/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259/// returns SETCC_INVALID if it is not possible to represent the resultant
260/// comparison.
261ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
262 bool isInteger) {
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000272 Op &= ~16; // Clear the U bit if the N bit is set.
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
276 Op = ISD::SETNE;
277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 return ISD::CondCode(Op);
279}
280
281/// getSetCCAndOperation - Return the result of a logical AND between different
282/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283/// function returns zero if it is not possible to represent the resultant
284/// comparison.
285ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
286 bool isInteger) {
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000289 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000290
291 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
293
294 // Canonicalize illegal integer setcc's.
295 if (isInteger) {
296 switch (Result) {
297 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
299 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
300 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
301 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000302 }
303 }
304
305 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000306}
307
Chris Lattnerb48da392005-01-23 04:39:44 +0000308const TargetMachine &SelectionDAG::getTarget() const {
309 return TLI.getTargetMachine();
310}
311
Jim Laskey58b968b2005-08-17 20:08:02 +0000312//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000313// SDNode Profile Support
314//===----------------------------------------------------------------------===//
315
Jim Laskeydef69b92006-10-27 23:52:51 +0000316/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
317///
Jim Laskey583bd472006-10-27 23:46:08 +0000318static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
319 ID.AddInteger(OpC);
320}
321
322/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
323/// solely with their pointer.
324void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
325 ID.AddPointer(VTList.VTs);
326}
327
Jim Laskeydef69b92006-10-27 23:52:51 +0000328/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
329///
Jim Laskey583bd472006-10-27 23:46:08 +0000330static void AddNodeIDOperands(FoldingSetNodeID &ID,
331 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000332 for (; NumOps; --NumOps, ++Ops) {
333 ID.AddPointer(Ops->Val);
334 ID.AddInteger(Ops->ResNo);
335 }
Jim Laskey583bd472006-10-27 23:46:08 +0000336}
337
Jim Laskey583bd472006-10-27 23:46:08 +0000338static void AddNodeIDNode(FoldingSetNodeID &ID,
339 unsigned short OpC, SDVTList VTList,
340 const SDOperand *OpList, unsigned N) {
341 AddNodeIDOpcode(ID, OpC);
342 AddNodeIDValueTypes(ID, VTList);
343 AddNodeIDOperands(ID, OpList, N);
344}
345
Jim Laskeydef69b92006-10-27 23:52:51 +0000346/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
347/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000348static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
354
355 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000358 case ISD::ARG_FLAGS:
359 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
360 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 case ISD::TargetConstant:
362 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000363 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 break;
365 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000366 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000367 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000369 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000371 case ISD::GlobalAddress:
372 case ISD::TargetGlobalTLSAddress:
373 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
375 ID.AddPointer(GA->getGlobal());
376 ID.AddInteger(GA->getOffset());
377 break;
378 }
379 case ISD::BasicBlock:
380 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
381 break;
382 case ISD::Register:
383 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
384 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000385 case ISD::SRCVALUE:
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
387 break;
388 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000389 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000390 ID.AddPointer(MO.getValue());
391 ID.AddInteger(MO.getFlags());
392 ID.AddInteger(MO.getOffset());
393 ID.AddInteger(MO.getSize());
394 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000395 break;
396 }
397 case ISD::FrameIndex:
398 case ISD::TargetFrameIndex:
399 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
400 break;
401 case ISD::JumpTable:
402 case ISD::TargetJumpTable:
403 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
404 break;
405 case ISD::ConstantPool:
406 case ISD::TargetConstantPool: {
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
408 ID.AddInteger(CP->getAlignment());
409 ID.AddInteger(CP->getOffset());
410 if (CP->isMachineConstantPoolEntry())
411 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
412 else
413 ID.AddPointer(CP->getConstVal());
414 break;
415 }
416 case ISD::LOAD: {
417 LoadSDNode *LD = cast<LoadSDNode>(N);
418 ID.AddInteger(LD->getAddressingMode());
419 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000420 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 ID.AddInteger(LD->getAlignment());
422 ID.AddInteger(LD->isVolatile());
423 break;
424 }
425 case ISD::STORE: {
426 StoreSDNode *ST = cast<StoreSDNode>(N);
427 ID.AddInteger(ST->getAddressingMode());
428 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000429 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 ID.AddInteger(ST->getAlignment());
431 ID.AddInteger(ST->isVolatile());
432 break;
433 }
Jim Laskey583bd472006-10-27 23:46:08 +0000434 }
435}
436
437//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000438// SelectionDAG Class
439//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000440
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000442/// SelectionDAG.
443void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444 // Create a dummy node (which is not added to allnodes), that adds a reference
445 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000446 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000447
Chris Lattner190a4182006-08-04 17:45:20 +0000448 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000449
Chris Lattner190a4182006-08-04 17:45:20 +0000450 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000451 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000452 if (I->use_empty())
453 DeadNodes.push_back(I);
454
455 // Process the worklist, deleting the nodes and adding their uses to the
456 // worklist.
457 while (!DeadNodes.empty()) {
458 SDNode *N = DeadNodes.back();
459 DeadNodes.pop_back();
460
461 // Take the node out of the appropriate CSE map.
462 RemoveNodeFromCSEMaps(N);
463
464 // Next, brutally remove the operand list. This is safe to do, as there are
465 // no cycles in the graph.
466 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
467 SDNode *Operand = I->Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000468 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000469
470 // Now that we removed this operand, see if there are no uses of it left.
471 if (Operand->use_empty())
472 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000473 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000474 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000475 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000476 }
Chris Lattner190a4182006-08-04 17:45:20 +0000477 N->OperandList = 0;
478 N->NumOperands = 0;
479
480 // Finally, remove N itself.
481 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000482 }
483
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000484 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000485 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000486}
487
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000488void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000489 SmallVector<SDNode*, 16> DeadNodes;
490 DeadNodes.push_back(N);
491
492 // Process the worklist, deleting the nodes and adding their uses to the
493 // worklist.
494 while (!DeadNodes.empty()) {
495 SDNode *N = DeadNodes.back();
496 DeadNodes.pop_back();
497
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000498 if (UpdateListener)
499 UpdateListener->NodeDeleted(N);
500
Evan Cheng130a6472006-10-12 20:34:05 +0000501 // Take the node out of the appropriate CSE map.
502 RemoveNodeFromCSEMaps(N);
503
504 // Next, brutally remove the operand list. This is safe to do, as there are
505 // no cycles in the graph.
506 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
507 SDNode *Operand = I->Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000508 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000509
510 // Now that we removed this operand, see if there are no uses of it left.
511 if (Operand->use_empty())
512 DeadNodes.push_back(Operand);
513 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000514 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000515 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000516 }
Evan Cheng130a6472006-10-12 20:34:05 +0000517 N->OperandList = 0;
518 N->NumOperands = 0;
519
520 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000521 AllNodes.erase(N);
522 }
523}
524
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000525void SelectionDAG::DeleteNode(SDNode *N) {
526 assert(N->use_empty() && "Cannot delete a node that is not dead!");
527
528 // First take this out of the appropriate CSE map.
529 RemoveNodeFromCSEMaps(N);
530
Chris Lattner1e111c72005-09-07 05:37:01 +0000531 // Finally, remove uses due to operands of this node, remove from the
532 // AllNodes list, and delete the node.
533 DeleteNodeNotInCSEMaps(N);
534}
535
536void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
537
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000538 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000539 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000540
541 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000542 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000543 I->Val->removeUser(std::distance(N->op_begin(), I), N);
544 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000545 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000546 }
Chris Lattner65113b22005-11-08 22:07:03 +0000547 N->OperandList = 0;
548 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000549
550 delete N;
551}
552
Chris Lattner149c58c2005-08-16 18:17:10 +0000553/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
554/// correspond to it. This is useful when we're about to delete or repurpose
555/// the node. We don't want future request for structurally identical nodes
556/// to return N anymore.
557void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000558 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000559 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000560 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000561 case ISD::STRING:
562 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
563 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000564 case ISD::CONDCODE:
565 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
566 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000567 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000568 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
569 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000570 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000571 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000572 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000573 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000574 Erased =
575 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000576 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000577 case ISD::VALUETYPE: {
578 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
579 if (MVT::isExtendedVT(VT)) {
580 Erased = ExtendedValueTypeNodes.erase(VT);
581 } else {
582 Erased = ValueTypeNodes[VT] != 0;
583 ValueTypeNodes[VT] = 0;
584 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000585 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000586 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000587 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000588 // Remove it from the CSE Map.
589 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000590 break;
591 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000592#ifndef NDEBUG
593 // Verify that the node was actually in one of the CSE maps, unless it has a
594 // flag result (which cannot be CSE'd) or is one of the special cases that are
595 // not subject to CSE.
596 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000597 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000598 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000599 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000600 assert(0 && "Node is not in map!");
601 }
602#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000603}
604
Chris Lattner8b8749f2005-08-17 19:00:20 +0000605/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
606/// has been taken out and modified in some way. If the specified node already
607/// exists in the CSE maps, do not modify the maps, but return the existing node
608/// instead. If it doesn't exist, add it and return null.
609///
610SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
611 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000612 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000613 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000614
Chris Lattner9f8cc692005-12-19 22:21:21 +0000615 // Check that remaining values produced are not flags.
616 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
617 if (N->getValueType(i) == MVT::Flag)
618 return 0; // Never CSE anything that produces a flag.
619
Chris Lattnera5682852006-08-07 23:03:03 +0000620 SDNode *New = CSEMap.GetOrInsertNode(N);
621 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000622 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000623}
624
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000625/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
626/// were replaced with those specified. If this node is never memoized,
627/// return null, otherwise return a pointer to the slot it would take. If a
628/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000629SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
630 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000631 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000632 return 0; // Never add these nodes.
633
634 // Check that remaining values produced are not flags.
635 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
636 if (N->getValueType(i) == MVT::Flag)
637 return 0; // Never CSE anything that produces a flag.
638
Chris Lattner63e3f142007-02-04 07:28:00 +0000639 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000640 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000641 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000642 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000643}
644
645/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
646/// were replaced with those specified. If this node is never memoized,
647/// return null, otherwise return a pointer to the slot it would take. If a
648/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000649SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
650 SDOperand Op1, SDOperand Op2,
651 void *&InsertPos) {
652 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
653 return 0; // Never add these nodes.
654
655 // Check that remaining values produced are not flags.
656 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
657 if (N->getValueType(i) == MVT::Flag)
658 return 0; // Never CSE anything that produces a flag.
659
Chris Lattner63e3f142007-02-04 07:28:00 +0000660 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000661 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000662 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000663 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
664}
665
666
667/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
668/// were replaced with those specified. If this node is never memoized,
669/// return null, otherwise return a pointer to the slot it would take. If a
670/// node already exists with these operands, the slot will be non-null.
671SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000672 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000673 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000674 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000675 return 0; // Never add these nodes.
676
677 // Check that remaining values produced are not flags.
678 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
679 if (N->getValueType(i) == MVT::Flag)
680 return 0; // Never CSE anything that produces a flag.
681
Jim Laskey583bd472006-10-27 23:46:08 +0000682 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000683 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000684
Evan Cheng9629aba2006-10-11 01:47:58 +0000685 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
686 ID.AddInteger(LD->getAddressingMode());
687 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000688 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000689 ID.AddInteger(LD->getAlignment());
690 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000691 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
692 ID.AddInteger(ST->getAddressingMode());
693 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000694 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000695 ID.AddInteger(ST->getAlignment());
696 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000697 }
Jim Laskey583bd472006-10-27 23:46:08 +0000698
Chris Lattnera5682852006-08-07 23:03:03 +0000699 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000700}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000701
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000702
Chris Lattner78ec3112003-08-11 14:57:33 +0000703SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000704 while (!AllNodes.empty()) {
705 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000706 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000707 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000708 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000709 }
Chris Lattner65113b22005-11-08 22:07:03 +0000710 N->OperandList = 0;
711 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000712 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000713 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000714}
715
Chris Lattner0f2287b2005-04-13 02:38:18 +0000716SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000717 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000718 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
719 MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000720 return getNode(ISD::AND, Op.getValueType(), Op,
721 getConstant(Imm, Op.getValueType()));
722}
723
Chris Lattner36ce6912005-11-29 06:21:05 +0000724SDOperand SelectionDAG::getString(const std::string &Val) {
725 StringSDNode *&N = StringNodes[Val];
726 if (!N) {
727 N = new StringSDNode(Val);
728 AllNodes.push_back(N);
729 }
730 return SDOperand(N, 0);
731}
732
Chris Lattner61b09412006-08-11 21:01:22 +0000733SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000734 MVT::ValueType EltVT =
735 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
736
737 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
738}
739
740SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000741 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000742
743 MVT::ValueType EltVT =
744 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000745
Dan Gohman6394b092008-02-08 22:59:30 +0000746 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
747 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000748
749 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000750 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000751 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000752 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000753 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000754 SDNode *N = NULL;
755 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
756 if (!MVT::isVector(VT))
757 return SDOperand(N, 0);
758 if (!N) {
759 N = new ConstantSDNode(isT, Val, EltVT);
760 CSEMap.InsertNode(N, IP);
761 AllNodes.push_back(N);
762 }
763
764 SDOperand Result(N, 0);
765 if (MVT::isVector(VT)) {
766 SmallVector<SDOperand, 8> Ops;
767 Ops.assign(MVT::getVectorNumElements(VT), Result);
768 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
769 }
770 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000771}
772
Chris Lattner0bd48932008-01-17 07:00:52 +0000773SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
774 return getConstant(Val, TLI.getPointerTy(), isTarget);
775}
776
777
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000778SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000779 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000780 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000781
Dan Gohman7f321562007-06-25 16:23:39 +0000782 MVT::ValueType EltVT =
783 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784
Chris Lattnerd8658612005-02-17 20:17:32 +0000785 // Do the map lookup using the actual bit pattern for the floating point
786 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
787 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000788 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000789 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000790 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000791 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000792 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000793 SDNode *N = NULL;
794 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
795 if (!MVT::isVector(VT))
796 return SDOperand(N, 0);
797 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000798 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000799 CSEMap.InsertNode(N, IP);
800 AllNodes.push_back(N);
801 }
802
Dan Gohman7f321562007-06-25 16:23:39 +0000803 SDOperand Result(N, 0);
804 if (MVT::isVector(VT)) {
805 SmallVector<SDOperand, 8> Ops;
806 Ops.assign(MVT::getVectorNumElements(VT), Result);
807 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
808 }
809 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810}
811
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000812SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
813 bool isTarget) {
814 MVT::ValueType EltVT =
815 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
816 if (EltVT==MVT::f32)
817 return getConstantFP(APFloat((float)Val), VT, isTarget);
818 else
819 return getConstantFP(APFloat(Val), VT, isTarget);
820}
821
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000823 MVT::ValueType VT, int Offset,
824 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000825 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000826
827 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
828 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000829 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000830 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
831 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
832 }
833
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 if (GVar && GVar->isThreadLocal())
835 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
836 else
837 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000838
Jim Laskey583bd472006-10-27 23:46:08 +0000839 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000840 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000841 ID.AddPointer(GV);
842 ID.AddInteger(Offset);
843 void *IP = 0;
844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
845 return SDOperand(E, 0);
846 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
847 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000848 AllNodes.push_back(N);
849 return SDOperand(N, 0);
850}
851
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000852SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
853 bool isTarget) {
854 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000855 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000856 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000857 ID.AddInteger(FI);
858 void *IP = 0;
859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
860 return SDOperand(E, 0);
861 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
862 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000863 AllNodes.push_back(N);
864 return SDOperand(N, 0);
865}
866
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000867SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
868 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000869 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000870 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000871 ID.AddInteger(JTI);
872 void *IP = 0;
873 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
874 return SDOperand(E, 0);
875 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
876 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
879}
880
Evan Chengb8973bd2006-01-31 22:23:14 +0000881SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000882 unsigned Alignment, int Offset,
883 bool isTarget) {
884 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000885 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000886 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000887 ID.AddInteger(Alignment);
888 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000889 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000890 void *IP = 0;
891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
892 return SDOperand(E, 0);
893 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
894 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
897}
898
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899
Evan Chengd6594ae2006-09-12 21:00:35 +0000900SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
901 MVT::ValueType VT,
902 unsigned Alignment, int Offset,
903 bool isTarget) {
904 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000905 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000906 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000907 ID.AddInteger(Alignment);
908 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000909 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000910 void *IP = 0;
911 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
912 return SDOperand(E, 0);
913 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
914 CSEMap.InsertNode(N, IP);
915 AllNodes.push_back(N);
916 return SDOperand(N, 0);
917}
918
919
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000921 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000922 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000923 ID.AddPointer(MBB);
924 void *IP = 0;
925 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
926 return SDOperand(E, 0);
927 SDNode *N = new BasicBlockSDNode(MBB);
928 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 AllNodes.push_back(N);
930 return SDOperand(N, 0);
931}
932
Duncan Sands276dcbd2008-03-21 09:14:45 +0000933SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
934 FoldingSetNodeID ID;
935 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
936 ID.AddInteger(Flags.getRawBits());
937 void *IP = 0;
938 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
939 return SDOperand(E, 0);
940 SDNode *N = new ARG_FLAGSSDNode(Flags);
941 CSEMap.InsertNode(N, IP);
942 AllNodes.push_back(N);
943 return SDOperand(N, 0);
944}
945
Chris Lattner15e4b012005-07-10 00:07:11 +0000946SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000947 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000948 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000949
Duncan Sandsf411b832007-10-17 13:49:58 +0000950 SDNode *&N = MVT::isExtendedVT(VT) ?
951 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
952
953 if (N) return SDOperand(N, 0);
954 N = new VTSDNode(VT);
955 AllNodes.push_back(N);
956 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000957}
958
Chris Lattnerc3aae252005-01-07 07:46:32 +0000959SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
960 SDNode *&N = ExternalSymbols[Sym];
961 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000962 N = new ExternalSymbolSDNode(false, Sym, VT);
963 AllNodes.push_back(N);
964 return SDOperand(N, 0);
965}
966
Chris Lattner809ec112006-01-28 10:09:25 +0000967SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
968 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000969 SDNode *&N = TargetExternalSymbols[Sym];
970 if (N) return SDOperand(N, 0);
971 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 AllNodes.push_back(N);
973 return SDOperand(N, 0);
974}
975
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000976SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
977 if ((unsigned)Cond >= CondCodeNodes.size())
978 CondCodeNodes.resize(Cond+1);
979
Chris Lattner079a27a2005-08-09 20:40:02 +0000980 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000981 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000982 AllNodes.push_back(CondCodeNodes[Cond]);
983 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000984 return SDOperand(CondCodeNodes[Cond], 0);
985}
986
Chris Lattner0fdd7682005-08-30 22:38:38 +0000987SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000988 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000989 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000990 ID.AddInteger(RegNo);
991 void *IP = 0;
992 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
993 return SDOperand(E, 0);
994 SDNode *N = new RegisterSDNode(RegNo, VT);
995 CSEMap.InsertNode(N, IP);
996 AllNodes.push_back(N);
997 return SDOperand(N, 0);
998}
999
Dan Gohman69de1932008-02-06 22:27:42 +00001000SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001001 assert((!V || isa<PointerType>(V->getType())) &&
1002 "SrcValue is not a pointer?");
1003
Jim Laskey583bd472006-10-27 23:46:08 +00001004 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001005 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001006 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001007
Chris Lattner61b09412006-08-11 21:01:22 +00001008 void *IP = 0;
1009 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1010 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001011
1012 SDNode *N = new SrcValueSDNode(V);
1013 CSEMap.InsertNode(N, IP);
1014 AllNodes.push_back(N);
1015 return SDOperand(N, 0);
1016}
1017
Dan Gohman36b5c132008-04-07 19:35:22 +00001018SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001019 const Value *v = MO.getValue();
1020 assert((!v || isa<PointerType>(v->getType())) &&
1021 "SrcValue is not a pointer?");
1022
1023 FoldingSetNodeID ID;
1024 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1025 ID.AddPointer(v);
1026 ID.AddInteger(MO.getFlags());
1027 ID.AddInteger(MO.getOffset());
1028 ID.AddInteger(MO.getSize());
1029 ID.AddInteger(MO.getAlignment());
1030
1031 void *IP = 0;
1032 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1033 return SDOperand(E, 0);
1034
1035 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001036 CSEMap.InsertNode(N, IP);
1037 AllNodes.push_back(N);
1038 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001039}
1040
Chris Lattner37ce9df2007-10-15 17:47:20 +00001041/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1042/// specified value type.
1043SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1044 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1045 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1046 const Type *Ty = MVT::getTypeForValueType(VT);
1047 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1048 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1049 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1050}
1051
1052
Chris Lattner51dabfb2006-10-14 00:41:01 +00001053SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1054 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 // These setcc operations always fold.
1056 switch (Cond) {
1057 default: break;
1058 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001059 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001061 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001062
1063 case ISD::SETOEQ:
1064 case ISD::SETOGT:
1065 case ISD::SETOGE:
1066 case ISD::SETOLT:
1067 case ISD::SETOLE:
1068 case ISD::SETONE:
1069 case ISD::SETO:
1070 case ISD::SETUO:
1071 case ISD::SETUEQ:
1072 case ISD::SETUNE:
1073 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1074 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001076
Chris Lattner67255a12005-04-07 18:14:58 +00001077 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001078 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001079 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001080 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001081
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 switch (Cond) {
1083 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001084 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1085 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001086 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1087 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1088 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1089 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1090 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1091 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1092 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1093 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095 }
Chris Lattner67255a12005-04-07 18:14:58 +00001096 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001097 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001099 // No compile time operations on this type yet.
1100 if (N1C->getValueType(0) == MVT::ppcf128)
1101 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001102
1103 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001105 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001106 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1107 return getNode(ISD::UNDEF, VT);
1108 // fall through
1109 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1110 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1111 return getNode(ISD::UNDEF, VT);
1112 // fall through
1113 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001114 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001115 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1116 return getNode(ISD::UNDEF, VT);
1117 // fall through
1118 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1119 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1120 return getNode(ISD::UNDEF, VT);
1121 // fall through
1122 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1123 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1124 return getNode(ISD::UNDEF, VT);
1125 // fall through
1126 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001127 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001128 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1129 return getNode(ISD::UNDEF, VT);
1130 // fall through
1131 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001132 R==APFloat::cmpEqual, VT);
1133 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1134 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1135 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1136 R==APFloat::cmpEqual, VT);
1137 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1138 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1139 R==APFloat::cmpLessThan, VT);
1140 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1141 R==APFloat::cmpUnordered, VT);
1142 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1143 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 }
1145 } else {
1146 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001147 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001148 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001149 }
1150
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001151 // Could not fold it.
1152 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153}
1154
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001155/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1156/// use this predicate to simplify operations downstream.
1157bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1158 unsigned BitWidth = Op.getValueSizeInBits();
1159 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1160}
1161
Dan Gohmanea859be2007-06-22 14:59:07 +00001162/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1163/// this predicate to simplify operations downstream. Mask is known to be zero
1164/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001165bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001166 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001167 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001168 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1169 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1170 return (KnownZero & Mask) == Mask;
1171}
1172
1173/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1174/// known to be either zero or one and return them in the KnownZero/KnownOne
1175/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1176/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001177void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001178 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001179 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001180 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001181 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1182 "Mask size mismatches value type size!");
1183
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001184 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001185 if (Depth == 6 || Mask == 0)
1186 return; // Limit search depth.
1187
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001188 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001189
1190 switch (Op.getOpcode()) {
1191 case ISD::Constant:
1192 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001193 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001194 KnownZero = ~KnownOne & Mask;
1195 return;
1196 case ISD::AND:
1197 // If either the LHS or the RHS are Zero, the result is zero.
1198 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001199 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1200 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001201 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1202 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1203
1204 // Output known-1 bits are only known if set in both the LHS & RHS.
1205 KnownOne &= KnownOne2;
1206 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1207 KnownZero |= KnownZero2;
1208 return;
1209 case ISD::OR:
1210 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001211 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1212 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001213 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1214 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1215
1216 // Output known-0 bits are only known if clear in both the LHS & RHS.
1217 KnownZero &= KnownZero2;
1218 // Output known-1 are known to be set if set in either the LHS | RHS.
1219 KnownOne |= KnownOne2;
1220 return;
1221 case ISD::XOR: {
1222 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1223 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1224 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1225 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1226
1227 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001228 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001229 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1230 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1231 KnownZero = KnownZeroOut;
1232 return;
1233 }
1234 case ISD::SELECT:
1235 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1236 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1237 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1238 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1239
1240 // Only known if known in both the LHS and RHS.
1241 KnownOne &= KnownOne2;
1242 KnownZero &= KnownZero2;
1243 return;
1244 case ISD::SELECT_CC:
1245 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1246 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1247 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1248 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1249
1250 // Only known if known in both the LHS and RHS.
1251 KnownOne &= KnownOne2;
1252 KnownZero &= KnownZero2;
1253 return;
1254 case ISD::SETCC:
1255 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001256 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1257 BitWidth > 1)
1258 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001259 return;
1260 case ISD::SHL:
1261 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1262 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001263 unsigned ShAmt = SA->getValue();
1264
1265 // If the shift count is an invalid immediate, don't do anything.
1266 if (ShAmt >= BitWidth)
1267 return;
1268
1269 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001270 KnownZero, KnownOne, Depth+1);
1271 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001272 KnownZero <<= ShAmt;
1273 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001274 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001275 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001276 }
1277 return;
1278 case ISD::SRL:
1279 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1280 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001281 unsigned ShAmt = SA->getValue();
1282
Dan Gohmand4cf9922008-02-26 18:50:50 +00001283 // If the shift count is an invalid immediate, don't do anything.
1284 if (ShAmt >= BitWidth)
1285 return;
1286
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001287 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001288 KnownZero, KnownOne, Depth+1);
1289 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001290 KnownZero = KnownZero.lshr(ShAmt);
1291 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001292
Dan Gohman72d2fd52008-02-13 22:43:25 +00001293 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001294 KnownZero |= HighBits; // High bits known zero.
1295 }
1296 return;
1297 case ISD::SRA:
1298 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001299 unsigned ShAmt = SA->getValue();
1300
Dan Gohmand4cf9922008-02-26 18:50:50 +00001301 // If the shift count is an invalid immediate, don't do anything.
1302 if (ShAmt >= BitWidth)
1303 return;
1304
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001305 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001306 // If any of the demanded bits are produced by the sign extension, we also
1307 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001308 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1309 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001310 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001311
1312 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1313 Depth+1);
1314 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001315 KnownZero = KnownZero.lshr(ShAmt);
1316 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001317
1318 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001319 APInt SignBit = APInt::getSignBit(BitWidth);
1320 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001321
Dan Gohmanca93a432008-02-20 16:30:17 +00001322 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001323 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001324 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001325 KnownOne |= HighBits; // New bits are known one.
1326 }
1327 }
1328 return;
1329 case ISD::SIGN_EXTEND_INREG: {
1330 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001331 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001332
1333 // Sign extension. Compute the demanded bits in the result that are not
1334 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001335 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001336
Dan Gohman977a76f2008-02-13 22:28:48 +00001337 APInt InSignBit = APInt::getSignBit(EBits);
1338 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001339
1340 // If the sign extended bits are demanded, we know that the sign
1341 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001342 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001343 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001344 InputDemandedBits |= InSignBit;
1345
1346 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1347 KnownZero, KnownOne, Depth+1);
1348 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1349
1350 // If the sign bit of the input is known set or clear, then we know the
1351 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001352 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001353 KnownZero |= NewBits;
1354 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001355 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001356 KnownOne |= NewBits;
1357 KnownZero &= ~NewBits;
1358 } else { // Input sign bit unknown
1359 KnownZero &= ~NewBits;
1360 KnownOne &= ~NewBits;
1361 }
1362 return;
1363 }
1364 case ISD::CTTZ:
1365 case ISD::CTLZ:
1366 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001367 unsigned LowBits = Log2_32(BitWidth)+1;
1368 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1369 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001370 return;
1371 }
1372 case ISD::LOAD: {
1373 if (ISD::isZEXTLoad(Op.Val)) {
1374 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001375 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001376 unsigned MemBits = MVT::getSizeInBits(VT);
1377 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001378 }
1379 return;
1380 }
1381 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001382 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1383 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001384 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1385 APInt InMask = Mask;
1386 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001387 KnownZero.trunc(InBits);
1388 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001389 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001390 KnownZero.zext(BitWidth);
1391 KnownOne.zext(BitWidth);
1392 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001393 return;
1394 }
1395 case ISD::SIGN_EXTEND: {
1396 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001397 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001398 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001399 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1400 APInt InMask = Mask;
1401 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001402
1403 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001404 // bit is demanded. Temporarily set this bit in the mask for our callee.
1405 if (NewBits.getBoolValue())
1406 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001407
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001408 KnownZero.trunc(InBits);
1409 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001410 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1411
1412 // Note if the sign bit is known to be zero or one.
1413 bool SignBitKnownZero = KnownZero.isNegative();
1414 bool SignBitKnownOne = KnownOne.isNegative();
1415 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1416 "Sign bit can't be known to be both zero and one!");
1417
1418 // If the sign bit wasn't actually demanded by our caller, we don't
1419 // want it set in the KnownZero and KnownOne result values. Reset the
1420 // mask and reapply it to the result values.
1421 InMask = Mask;
1422 InMask.trunc(InBits);
1423 KnownZero &= InMask;
1424 KnownOne &= InMask;
1425
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001426 KnownZero.zext(BitWidth);
1427 KnownOne.zext(BitWidth);
1428
Dan Gohman977a76f2008-02-13 22:28:48 +00001429 // If the sign bit is known zero or one, the top bits match.
1430 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001431 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001432 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001433 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001434 return;
1435 }
1436 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001437 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1438 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001439 APInt InMask = Mask;
1440 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001441 KnownZero.trunc(InBits);
1442 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001443 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001444 KnownZero.zext(BitWidth);
1445 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001446 return;
1447 }
1448 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001449 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1450 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001451 APInt InMask = Mask;
1452 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001453 KnownZero.zext(InBits);
1454 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001455 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001456 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001457 KnownZero.trunc(BitWidth);
1458 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001459 break;
1460 }
1461 case ISD::AssertZext: {
1462 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001463 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001464 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1465 KnownOne, Depth+1);
1466 KnownZero |= (~InMask) & Mask;
1467 return;
1468 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001469 case ISD::FGETSIGN:
1470 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001471 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001472 return;
1473
Dan Gohmanea859be2007-06-22 14:59:07 +00001474 case ISD::ADD: {
1475 // If either the LHS or the RHS are Zero, the result is zero.
1476 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1477 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1478 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1479 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1480
1481 // Output known-0 bits are known if clear or set in both the low clear bits
1482 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1483 // low 3 bits clear.
Dan Gohman977a76f2008-02-13 22:28:48 +00001484 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1485 KnownZero2.countTrailingOnes());
Dan Gohmanea859be2007-06-22 14:59:07 +00001486
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001487 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1488 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001489 return;
1490 }
1491 case ISD::SUB: {
1492 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1493 if (!CLHS) return;
1494
1495 // We know that the top bits of C-X are clear if X contains less bits
1496 // than C (i.e. no wrap-around can happen). For example, 20-X is
1497 // positive if we can prove that X is >= 0 and < 16.
Dan Gohman977a76f2008-02-13 22:28:48 +00001498 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001499 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1500 // NLZ can't be BitWidth with no sign bit
Chris Lattner423be622008-02-14 18:48:56 +00001501 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001502 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1503
1504 // If all of the MaskV bits are known to be zero, then we know the output
1505 // top bits are zero, because we now know that the output is from [0-C].
1506 if ((KnownZero & MaskV) == MaskV) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001507 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1508 // Top bits known zero.
1509 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1510 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001511 } else {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001512 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001513 }
1514 }
1515 return;
1516 }
1517 default:
1518 // Allow the target to implement this method for its nodes.
1519 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1520 case ISD::INTRINSIC_WO_CHAIN:
1521 case ISD::INTRINSIC_W_CHAIN:
1522 case ISD::INTRINSIC_VOID:
1523 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1524 }
1525 return;
1526 }
1527}
1528
1529/// ComputeNumSignBits - Return the number of times the sign bit of the
1530/// register is replicated into the other bits. We know that at least 1 bit
1531/// is always equal to the sign bit (itself), but other cases can give us
1532/// information. For example, immediately after an "SRA X, 2", we know that
1533/// the top 3 bits are all equal to each other, so we return 3.
1534unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1535 MVT::ValueType VT = Op.getValueType();
1536 assert(MVT::isInteger(VT) && "Invalid VT!");
1537 unsigned VTBits = MVT::getSizeInBits(VT);
1538 unsigned Tmp, Tmp2;
1539
1540 if (Depth == 6)
1541 return 1; // Limit search depth.
1542
1543 switch (Op.getOpcode()) {
1544 default: break;
1545 case ISD::AssertSext:
1546 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1547 return VTBits-Tmp+1;
1548 case ISD::AssertZext:
1549 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1550 return VTBits-Tmp;
1551
1552 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001553 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1554 // If negative, return # leading ones.
1555 if (Val.isNegative())
1556 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001557
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001558 // Return # leading zeros.
1559 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001560 }
1561
1562 case ISD::SIGN_EXTEND:
1563 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1564 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1565
1566 case ISD::SIGN_EXTEND_INREG:
1567 // Max of the input and what this extends.
1568 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1569 Tmp = VTBits-Tmp+1;
1570
1571 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1572 return std::max(Tmp, Tmp2);
1573
1574 case ISD::SRA:
1575 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1576 // SRA X, C -> adds C sign bits.
1577 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1578 Tmp += C->getValue();
1579 if (Tmp > VTBits) Tmp = VTBits;
1580 }
1581 return Tmp;
1582 case ISD::SHL:
1583 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1584 // shl destroys sign bits.
1585 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1586 if (C->getValue() >= VTBits || // Bad shift.
1587 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1588 return Tmp - C->getValue();
1589 }
1590 break;
1591 case ISD::AND:
1592 case ISD::OR:
1593 case ISD::XOR: // NOT is handled here.
1594 // Logical binary ops preserve the number of sign bits.
1595 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1596 if (Tmp == 1) return 1; // Early out.
1597 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1598 return std::min(Tmp, Tmp2);
1599
1600 case ISD::SELECT:
1601 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1602 if (Tmp == 1) return 1; // Early out.
1603 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1604 return std::min(Tmp, Tmp2);
1605
1606 case ISD::SETCC:
1607 // If setcc returns 0/-1, all bits are sign bits.
1608 if (TLI.getSetCCResultContents() ==
1609 TargetLowering::ZeroOrNegativeOneSetCCResult)
1610 return VTBits;
1611 break;
1612 case ISD::ROTL:
1613 case ISD::ROTR:
1614 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1615 unsigned RotAmt = C->getValue() & (VTBits-1);
1616
1617 // Handle rotate right by N like a rotate left by 32-N.
1618 if (Op.getOpcode() == ISD::ROTR)
1619 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1620
1621 // If we aren't rotating out all of the known-in sign bits, return the
1622 // number that are left. This handles rotl(sext(x), 1) for example.
1623 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1624 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1625 }
1626 break;
1627 case ISD::ADD:
1628 // Add can have at most one carry bit. Thus we know that the output
1629 // is, at worst, one more bit than the inputs.
1630 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1631 if (Tmp == 1) return 1; // Early out.
1632
1633 // Special case decrementing a value (ADD X, -1):
1634 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1635 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001636 APInt KnownZero, KnownOne;
1637 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001638 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1639
1640 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1641 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001642 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001643 return VTBits;
1644
1645 // If we are subtracting one from a positive number, there is no carry
1646 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001647 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001648 return Tmp;
1649 }
1650
1651 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1652 if (Tmp2 == 1) return 1;
1653 return std::min(Tmp, Tmp2)-1;
1654 break;
1655
1656 case ISD::SUB:
1657 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1658 if (Tmp2 == 1) return 1;
1659
1660 // Handle NEG.
1661 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001662 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001663 APInt KnownZero, KnownOne;
1664 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001665 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1666 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1667 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001668 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 return VTBits;
1670
1671 // If the input is known to be positive (the sign bit is known clear),
1672 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001673 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 return Tmp2;
1675
1676 // Otherwise, we treat this like a SUB.
1677 }
1678
1679 // Sub can have at most one carry bit. Thus we know that the output
1680 // is, at worst, one more bit than the inputs.
1681 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1682 if (Tmp == 1) return 1; // Early out.
1683 return std::min(Tmp, Tmp2)-1;
1684 break;
1685 case ISD::TRUNCATE:
1686 // FIXME: it's tricky to do anything useful for this, but it is an important
1687 // case for targets like X86.
1688 break;
1689 }
1690
1691 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1692 if (Op.getOpcode() == ISD::LOAD) {
1693 LoadSDNode *LD = cast<LoadSDNode>(Op);
1694 unsigned ExtType = LD->getExtensionType();
1695 switch (ExtType) {
1696 default: break;
1697 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001698 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 return VTBits-Tmp+1;
1700 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001701 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001702 return VTBits-Tmp;
1703 }
1704 }
1705
1706 // Allow the target to implement this method for its nodes.
1707 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1708 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1709 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1710 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1711 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1712 if (NumBits > 1) return NumBits;
1713 }
1714
1715 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1716 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001717 APInt KnownZero, KnownOne;
1718 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001719 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1720
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001721 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001722 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001723 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001724 Mask = KnownOne;
1725 } else {
1726 // Nothing known.
1727 return 1;
1728 }
1729
1730 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1731 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001732 Mask = ~Mask;
1733 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001734 // Return # leading zeros. We use 'min' here in case Val was zero before
1735 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001736 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanea859be2007-06-22 14:59:07 +00001737}
1738
Chris Lattner51dabfb2006-10-14 00:41:01 +00001739
Evan Chenga844bde2008-02-02 04:07:54 +00001740bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1741 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1742 if (!GA) return false;
1743 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1744 if (!GV) return false;
1745 MachineModuleInfo *MMI = getMachineModuleInfo();
1746 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1747}
1748
1749
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001751///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001752SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001753 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001754 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001755 void *IP = 0;
1756 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1757 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001758 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001759 CSEMap.InsertNode(N, IP);
1760
1761 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001762 return SDOperand(N, 0);
1763}
1764
Chris Lattnerc3aae252005-01-07 07:46:32 +00001765SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1766 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001767 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001768 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001769 const APInt &Val = C->getAPIntValue();
1770 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001771 switch (Opcode) {
1772 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001773 case ISD::SIGN_EXTEND:
1774 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001775 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001776 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001777 case ISD::TRUNCATE:
1778 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001779 case ISD::UINT_TO_FP:
1780 case ISD::SINT_TO_FP: {
1781 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001782 // No compile time operations on this type.
1783 if (VT==MVT::ppcf128)
1784 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001785 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1786 (void)apf.convertFromAPInt(Val,
1787 Opcode==ISD::SINT_TO_FP,
1788 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001789 return getConstantFP(apf, VT);
1790 }
Chris Lattner94683772005-12-23 05:30:37 +00001791 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001792 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001793 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001794 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001795 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001796 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001797 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001798 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001799 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001800 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001801 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001802 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001803 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001804 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001805 }
1806 }
1807
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001808 // Constant fold unary operations with a floating point constant operand.
1809 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1810 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001811 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001812 switch (Opcode) {
1813 case ISD::FNEG:
1814 V.changeSign();
1815 return getConstantFP(V, VT);
1816 case ISD::FABS:
1817 V.clearSign();
1818 return getConstantFP(V, VT);
1819 case ISD::FP_ROUND:
1820 case ISD::FP_EXTEND:
1821 // This can return overflow, underflow, or inexact; we don't care.
1822 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001823 (void)V.convert(*MVTToAPFloatSemantics(VT),
1824 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001825 return getConstantFP(V, VT);
1826 case ISD::FP_TO_SINT:
1827 case ISD::FP_TO_UINT: {
1828 integerPart x;
1829 assert(integerPartWidth >= 64);
1830 // FIXME need to be more flexible about rounding mode.
1831 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1832 Opcode==ISD::FP_TO_SINT,
1833 APFloat::rmTowardZero);
1834 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1835 break;
1836 return getConstant(x, VT);
1837 }
1838 case ISD::BIT_CONVERT:
1839 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1840 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1841 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1842 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001843 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001844 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001845 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001846 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001847
1848 unsigned OpOpcode = Operand.Val->getOpcode();
1849 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001850 case ISD::TokenFactor:
1851 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001852 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001853 case ISD::FP_EXTEND:
1854 assert(MVT::isFloatingPoint(VT) &&
1855 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001856 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001857 if (Operand.getOpcode() == ISD::UNDEF)
1858 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001859 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001860 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001861 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1862 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001863 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001864 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1865 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001866 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1867 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1868 break;
1869 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001870 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1871 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001872 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001873 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1874 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001875 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001876 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001877 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001878 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001879 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1880 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001881 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001882 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1883 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001884 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1885 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1886 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1887 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001888 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001889 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1890 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001891 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001892 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1893 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001894 if (OpOpcode == ISD::TRUNCATE)
1895 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001896 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1897 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001898 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001899 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1900 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001901 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001902 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1903 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001904 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1905 else
1906 return Operand.Val->getOperand(0);
1907 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001908 break;
Chris Lattner35481892005-12-23 00:16:34 +00001909 case ISD::BIT_CONVERT:
1910 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001911 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001912 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001913 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001914 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1915 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001916 if (OpOpcode == ISD::UNDEF)
1917 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001918 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001919 case ISD::SCALAR_TO_VECTOR:
1920 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001921 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001922 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001923 if (OpOpcode == ISD::UNDEF)
1924 return getNode(ISD::UNDEF, VT);
1925 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1926 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1927 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1928 Operand.getConstantOperandVal(1) == 0 &&
1929 Operand.getOperand(0).getValueType() == VT)
1930 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001931 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001932 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001933 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1934 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001935 Operand.Val->getOperand(0));
1936 if (OpOpcode == ISD::FNEG) // --X -> X
1937 return Operand.Val->getOperand(0);
1938 break;
1939 case ISD::FABS:
1940 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1941 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1942 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001943 }
1944
Chris Lattner43247a12005-08-25 19:12:10 +00001945 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001946 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001947 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001948 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001949 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001950 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001951 void *IP = 0;
1952 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1953 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001954 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001955 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001956 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001957 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001958 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001959 AllNodes.push_back(N);
1960 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001961}
1962
Chris Lattner36019aa2005-04-18 03:48:41 +00001963
1964
Chris Lattnerc3aae252005-01-07 07:46:32 +00001965SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1966 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1968 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001969 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001970 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001971 case ISD::TokenFactor:
1972 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1973 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001974 // Fold trivial token factors.
1975 if (N1.getOpcode() == ISD::EntryToken) return N2;
1976 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001977 break;
Chris Lattner76365122005-01-16 02:23:22 +00001978 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001979 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1980 N1.getValueType() == VT && "Binary operator types must match!");
1981 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1982 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001983 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001984 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001985 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1986 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001987 break;
Chris Lattner76365122005-01-16 02:23:22 +00001988 case ISD::OR:
1989 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001990 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1991 N1.getValueType() == VT && "Binary operator types must match!");
1992 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1993 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001994 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001995 return N1;
1996 break;
Chris Lattner76365122005-01-16 02:23:22 +00001997 case ISD::UDIV:
1998 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001999 case ISD::MULHU:
2000 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002001 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2002 // fall through
2003 case ISD::ADD:
2004 case ISD::SUB:
2005 case ISD::MUL:
2006 case ISD::SDIV:
2007 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002008 case ISD::FADD:
2009 case ISD::FSUB:
2010 case ISD::FMUL:
2011 case ISD::FDIV:
2012 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002013 assert(N1.getValueType() == N2.getValueType() &&
2014 N1.getValueType() == VT && "Binary operator types must match!");
2015 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002016 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2017 assert(N1.getValueType() == VT &&
2018 MVT::isFloatingPoint(N1.getValueType()) &&
2019 MVT::isFloatingPoint(N2.getValueType()) &&
2020 "Invalid FCOPYSIGN!");
2021 break;
Chris Lattner76365122005-01-16 02:23:22 +00002022 case ISD::SHL:
2023 case ISD::SRA:
2024 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002025 case ISD::ROTL:
2026 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002027 assert(VT == N1.getValueType() &&
2028 "Shift operators return type must be the same as their first arg");
2029 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002030 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002031 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002032 case ISD::FP_ROUND_INREG: {
2033 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2034 assert(VT == N1.getValueType() && "Not an inreg round!");
2035 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2036 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002037 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2038 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002039 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002040 break;
2041 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002042 case ISD::FP_ROUND:
2043 assert(MVT::isFloatingPoint(VT) &&
2044 MVT::isFloatingPoint(N1.getValueType()) &&
2045 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2046 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002047 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002048 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002049 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002050 case ISD::AssertZext: {
2051 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2052 assert(VT == N1.getValueType() && "Not an inreg extend!");
2053 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2054 "Cannot *_EXTEND_INREG FP types");
2055 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2056 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002057 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002058 break;
2059 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002060 case ISD::SIGN_EXTEND_INREG: {
2061 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2062 assert(VT == N1.getValueType() && "Not an inreg extend!");
2063 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2064 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002065 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2066 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002067 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002068
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002069 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002070 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002071 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002072 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002073 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002074 return getConstant(Val, VT);
2075 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002076 break;
2077 }
2078 case ISD::EXTRACT_VECTOR_ELT:
2079 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2080
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002081 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2082 if (N1.getOpcode() == ISD::UNDEF)
2083 return getNode(ISD::UNDEF, VT);
2084
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002085 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2086 // expanding copies of large vectors from registers.
2087 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2088 N1.getNumOperands() > 0) {
2089 unsigned Factor =
2090 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2091 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2092 N1.getOperand(N2C->getValue() / Factor),
2093 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2094 }
2095
2096 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2097 // expanding large vector constants.
2098 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2099 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002100
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002101 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2102 // operations are lowered to scalars.
2103 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2104 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2105 if (IEC == N2C)
2106 return N1.getOperand(1);
2107 else
2108 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2109 }
2110 break;
2111 case ISD::EXTRACT_ELEMENT:
2112 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002113 assert(!MVT::isVector(N1.getValueType()) &&
2114 MVT::isInteger(N1.getValueType()) &&
2115 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2116 "EXTRACT_ELEMENT only applies to integers!");
2117
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002118 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2119 // 64-bit integers into 32-bit parts. Instead of building the extract of
2120 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2121 if (N1.getOpcode() == ISD::BUILD_PAIR)
2122 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002123
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002124 // EXTRACT_ELEMENT of a constant int is also very common.
2125 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002126 unsigned ElementSize = MVT::getSizeInBits(VT);
2127 unsigned Shift = ElementSize * N2C->getValue();
2128 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2129 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002130 }
2131 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002132 case ISD::EXTRACT_SUBVECTOR:
2133 if (N1.getValueType() == VT) // Trivial extraction.
2134 return N1;
2135 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002136 }
2137
2138 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002139 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002140 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002141 switch (Opcode) {
2142 case ISD::ADD: return getConstant(C1 + C2, VT);
2143 case ISD::SUB: return getConstant(C1 - C2, VT);
2144 case ISD::MUL: return getConstant(C1 * C2, VT);
2145 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002146 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002147 break;
2148 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002149 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002150 break;
2151 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002152 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002153 break;
2154 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002155 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002156 break;
2157 case ISD::AND : return getConstant(C1 & C2, VT);
2158 case ISD::OR : return getConstant(C1 | C2, VT);
2159 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002160 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002161 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2162 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2163 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2164 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002165 default: break;
2166 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002167 } else { // Cannonicalize constant to RHS if commutative
2168 if (isCommutativeBinOp(Opcode)) {
2169 std::swap(N1C, N2C);
2170 std::swap(N1, N2);
2171 }
2172 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002173 }
2174
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002175 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002176 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2177 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002178 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002179 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2180 // Cannonicalize constant to RHS if commutative
2181 std::swap(N1CFP, N2CFP);
2182 std::swap(N1, N2);
2183 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002184 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2185 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002186 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002187 case ISD::FADD:
2188 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002189 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002190 return getConstantFP(V1, VT);
2191 break;
2192 case ISD::FSUB:
2193 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2194 if (s!=APFloat::opInvalidOp)
2195 return getConstantFP(V1, VT);
2196 break;
2197 case ISD::FMUL:
2198 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2199 if (s!=APFloat::opInvalidOp)
2200 return getConstantFP(V1, VT);
2201 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002202 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002203 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2204 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2205 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002206 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002207 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002208 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2209 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2210 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002211 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002212 case ISD::FCOPYSIGN:
2213 V1.copySign(V2);
2214 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 default: break;
2216 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002217 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002218 }
Chris Lattner62b57722006-04-20 05:39:12 +00002219
2220 // Canonicalize an UNDEF to the RHS, even over a constant.
2221 if (N1.getOpcode() == ISD::UNDEF) {
2222 if (isCommutativeBinOp(Opcode)) {
2223 std::swap(N1, N2);
2224 } else {
2225 switch (Opcode) {
2226 case ISD::FP_ROUND_INREG:
2227 case ISD::SIGN_EXTEND_INREG:
2228 case ISD::SUB:
2229 case ISD::FSUB:
2230 case ISD::FDIV:
2231 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002232 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002233 return N1; // fold op(undef, arg2) -> undef
2234 case ISD::UDIV:
2235 case ISD::SDIV:
2236 case ISD::UREM:
2237 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002238 case ISD::SRL:
2239 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002240 if (!MVT::isVector(VT))
2241 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2242 // For vectors, we can't easily build an all zero vector, just return
2243 // the LHS.
2244 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002245 }
2246 }
2247 }
2248
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002249 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002250 if (N2.getOpcode() == ISD::UNDEF) {
2251 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002252 case ISD::XOR:
2253 if (N1.getOpcode() == ISD::UNDEF)
2254 // Handle undef ^ undef -> 0 special case. This is a common
2255 // idiom (misuse).
2256 return getConstant(0, VT);
2257 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002258 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002259 case ISD::ADDC:
2260 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002261 case ISD::SUB:
2262 case ISD::FADD:
2263 case ISD::FSUB:
2264 case ISD::FMUL:
2265 case ISD::FDIV:
2266 case ISD::FREM:
2267 case ISD::UDIV:
2268 case ISD::SDIV:
2269 case ISD::UREM:
2270 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002271 return N2; // fold op(arg1, undef) -> undef
2272 case ISD::MUL:
2273 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002274 case ISD::SRL:
2275 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002276 if (!MVT::isVector(VT))
2277 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2278 // For vectors, we can't easily build an all zero vector, just return
2279 // the LHS.
2280 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002281 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002282 if (!MVT::isVector(VT))
2283 return getConstant(MVT::getIntVTBitMask(VT), VT);
2284 // For vectors, we can't easily build an all one vector, just return
2285 // the LHS.
2286 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002287 case ISD::SRA:
2288 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002289 }
2290 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002291
Chris Lattner27e9b412005-05-11 18:57:39 +00002292 // Memoize this node if possible.
2293 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002294 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002295 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002296 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002297 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002298 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002299 void *IP = 0;
2300 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2301 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002302 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002303 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002304 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002305 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002306 }
2307
Chris Lattnerc3aae252005-01-07 07:46:32 +00002308 AllNodes.push_back(N);
2309 return SDOperand(N, 0);
2310}
2311
Chris Lattnerc3aae252005-01-07 07:46:32 +00002312SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2313 SDOperand N1, SDOperand N2, SDOperand N3) {
2314 // Perform various simplifications.
2315 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2316 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002317 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002318 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002319 // Use FoldSetCC to simplify SETCC's.
2320 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002321 if (Simp.Val) return Simp;
2322 break;
2323 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002324 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002325 if (N1C) {
2326 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002327 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002328 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002329 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002330 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331
2332 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002333 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002334 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002335 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002336 if (N2C->getValue()) // Unconditional branch
2337 return getNode(ISD::BR, MVT::Other, N1, N3);
2338 else
2339 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002340 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002341 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002342 case ISD::VECTOR_SHUFFLE:
2343 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2344 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2345 N3.getOpcode() == ISD::BUILD_VECTOR &&
2346 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2347 "Illegal VECTOR_SHUFFLE node!");
2348 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002349 case ISD::BIT_CONVERT:
2350 // Fold bit_convert nodes from a type to themselves.
2351 if (N1.getValueType() == VT)
2352 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002353 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354 }
2355
Chris Lattner43247a12005-08-25 19:12:10 +00002356 // Memoize node if it doesn't produce a flag.
2357 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002358 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002359 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002360 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002361 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002362 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002363 void *IP = 0;
2364 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2365 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002366 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002367 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002368 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002369 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002370 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002371 AllNodes.push_back(N);
2372 return SDOperand(N, 0);
2373}
2374
2375SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002376 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002377 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002378 SDOperand Ops[] = { N1, N2, N3, N4 };
2379 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002380}
2381
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002382SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2383 SDOperand N1, SDOperand N2, SDOperand N3,
2384 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002385 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2386 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002387}
2388
Dan Gohman707e0182008-04-12 04:36:06 +00002389/// getMemsetValue - Vectorized representation of the memset value
2390/// operand.
2391static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
2392 SelectionDAG &DAG) {
2393 MVT::ValueType CurVT = VT;
2394 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
2395 uint64_t Val = C->getValue() & 255;
2396 unsigned Shift = 8;
2397 while (CurVT != MVT::i8) {
2398 Val = (Val << Shift) | Val;
2399 Shift <<= 1;
2400 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2401 }
2402 return DAG.getConstant(Val, VT);
2403 } else {
2404 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2405 unsigned Shift = 8;
2406 while (CurVT != MVT::i8) {
2407 Value =
2408 DAG.getNode(ISD::OR, VT,
2409 DAG.getNode(ISD::SHL, VT, Value,
2410 DAG.getConstant(Shift, MVT::i8)), Value);
2411 Shift <<= 1;
2412 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2413 }
2414
2415 return Value;
2416 }
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002417}
2418
Dan Gohman707e0182008-04-12 04:36:06 +00002419/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2420/// used when a memcpy is turned into a memset when the source is a constant
2421/// string ptr.
2422static SDOperand getMemsetStringVal(MVT::ValueType VT,
2423 SelectionDAG &DAG,
2424 const TargetLowering &TLI,
2425 std::string &Str, unsigned Offset) {
2426 uint64_t Val = 0;
2427 unsigned MSB = MVT::getSizeInBits(VT) / 8;
2428 if (TLI.isLittleEndian())
2429 Offset = Offset + MSB - 1;
2430 for (unsigned i = 0; i != MSB; ++i) {
2431 Val = (Val << 8) | (unsigned char)Str[Offset];
2432 Offset += TLI.isLittleEndian() ? -1 : 1;
2433 }
2434 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002435}
2436
Dan Gohman707e0182008-04-12 04:36:06 +00002437/// getMemBasePlusOffset - Returns base and offset node for the
2438static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2439 SelectionDAG &DAG) {
2440 MVT::ValueType VT = Base.getValueType();
2441 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2442}
2443
2444/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2445/// to replace the memset / memcpy is below the threshold. It also returns the
2446/// types of the sequence of memory ops to perform memset / memcpy.
2447static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2448 unsigned Limit, uint64_t Size,
2449 unsigned Align,
2450 const TargetLowering &TLI) {
2451 MVT::ValueType VT;
2452
2453 if (TLI.allowsUnalignedMemoryAccesses()) {
2454 VT = MVT::i64;
2455 } else {
2456 switch (Align & 7) {
2457 case 0:
2458 VT = MVT::i64;
2459 break;
2460 case 4:
2461 VT = MVT::i32;
2462 break;
2463 case 2:
2464 VT = MVT::i16;
2465 break;
2466 default:
2467 VT = MVT::i8;
2468 break;
2469 }
2470 }
2471
2472 MVT::ValueType LVT = MVT::i64;
2473 while (!TLI.isTypeLegal(LVT))
2474 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2475 assert(MVT::isInteger(LVT));
2476
2477 if (VT > LVT)
2478 VT = LVT;
2479
2480 unsigned NumMemOps = 0;
2481 while (Size != 0) {
2482 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2483 while (VTSize > Size) {
2484 VT = (MVT::ValueType)((unsigned)VT - 1);
2485 VTSize >>= 1;
2486 }
2487 assert(MVT::isInteger(VT));
2488
2489 if (++NumMemOps > Limit)
2490 return false;
2491 MemOps.push_back(VT);
2492 Size -= VTSize;
2493 }
2494
2495 return true;
2496}
2497
2498static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2499 SDOperand Chain, SDOperand Dst,
2500 SDOperand Src, uint64_t Size,
2501 unsigned Align,
2502 bool AlwaysInline,
2503 Value *DstSV, uint64_t DstOff,
2504 Value *SrcSV, uint64_t SrcOff) {
2505 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2506
2507 // Expand memcpy to a series of store ops if the size operand falls below
2508 // a certain threshold.
2509 std::vector<MVT::ValueType> MemOps;
2510 uint64_t Limit = -1;
2511 if (!AlwaysInline)
2512 Limit = TLI.getMaxStoresPerMemcpy();
2513 if (!MeetsMaxMemopRequirement(MemOps, Limit, Size, Align, TLI))
2514 return SDOperand();
2515
2516 SmallVector<SDOperand, 8> OutChains;
2517
2518 unsigned NumMemOps = MemOps.size();
2519 unsigned SrcDelta = 0;
2520 GlobalAddressSDNode *G = NULL;
2521 std::string Str;
2522 bool CopyFromStr = false;
2523
2524 if (Src.getOpcode() == ISD::GlobalAddress)
2525 G = cast<GlobalAddressSDNode>(Src);
2526 else if (Src.getOpcode() == ISD::ADD &&
2527 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2528 Src.getOperand(1).getOpcode() == ISD::Constant) {
2529 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2530 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2531 }
2532 if (G) {
2533 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2534 if (GV && GV->isConstant()) {
2535 Str = GV->getStringValue(false);
2536 if (!Str.empty()) {
2537 CopyFromStr = true;
2538 SrcOff += SrcDelta;
2539 }
2540 }
2541 }
2542
2543 for (unsigned i = 0; i < NumMemOps; i++) {
2544 MVT::ValueType VT = MemOps[i];
2545 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2546 SDOperand Value, Store;
2547
2548 if (CopyFromStr) {
2549 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
2550 Store =
2551 DAG.getStore(Chain, Value,
2552 getMemBasePlusOffset(Dst, DstOff, DAG),
2553 DstSV, DstOff);
2554 } else {
2555 Value = DAG.getLoad(VT, Chain,
2556 getMemBasePlusOffset(Src, SrcOff, DAG),
2557 SrcSV, SrcOff, false, Align);
2558 Store =
2559 DAG.getStore(Chain, Value,
2560 getMemBasePlusOffset(Dst, DstOff, DAG),
2561 DstSV, DstOff, false, Align);
2562 }
2563 OutChains.push_back(Store);
2564 SrcOff += VTSize;
2565 DstOff += VTSize;
2566 }
2567
2568 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2569 &OutChains[0], OutChains.size());
2570}
2571
2572static SDOperand getMemsetStores(SelectionDAG &DAG,
2573 SDOperand Chain, SDOperand Dst,
2574 SDOperand Src, uint64_t Size,
2575 unsigned Align,
2576 Value *DstSV, uint64_t DstOff) {
2577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2578
2579 // Expand memset to a series of load/store ops if the size operand
2580 // falls below a certain threshold.
2581 std::vector<MVT::ValueType> MemOps;
2582 if (!MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
2583 Size, Align, TLI))
2584 return SDOperand();
2585
2586 SmallVector<SDOperand, 8> OutChains;
2587
2588 unsigned NumMemOps = MemOps.size();
2589 for (unsigned i = 0; i < NumMemOps; i++) {
2590 MVT::ValueType VT = MemOps[i];
2591 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2592 SDOperand Value = getMemsetValue(Src, VT, DAG);
2593 SDOperand Store = DAG.getStore(Chain, Value,
2594 getMemBasePlusOffset(Dst, DstOff, DAG),
2595 DstSV, DstOff);
2596 OutChains.push_back(Store);
2597 DstOff += VTSize;
2598 }
2599
2600 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2601 &OutChains[0], OutChains.size());
2602}
2603
2604SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002605 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002606 unsigned Align, bool AlwaysInline,
2607 Value *DstSV, uint64_t DstOff,
2608 Value *SrcSV, uint64_t SrcOff) {
2609
2610 // Check to see if we should lower the memcpy to loads and stores first.
2611 // For cases within the target-specified limits, this is the best choice.
2612 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2613 if (ConstantSize) {
2614 // Memcpy with size zero? Just return the original chain.
2615 if (ConstantSize->isNullValue())
2616 return Chain;
2617
2618 SDOperand Result =
2619 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2620 Align, false, DstSV, DstOff, SrcSV, SrcOff);
2621 if (Result.Val)
2622 return Result;
2623 }
2624
2625 // Then check to see if we should lower the memcpy with target-specific
2626 // code. If the target chooses to do this, this is the next best.
2627 SDOperand Result =
2628 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2629 AlwaysInline,
2630 DstSV, DstOff, SrcSV, SrcOff);
2631 if (Result.Val)
2632 return Result;
2633
2634 // If we really need inline code and the target declined to provide it,
2635 // use a (potentially long) sequence of loads and stores.
2636 if (AlwaysInline) {
2637 assert(ConstantSize && "AlwaysInline requires a constant size!");
2638 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2639 ConstantSize->getValue(), Align, true,
2640 DstSV, DstOff, SrcSV, SrcOff);
2641 }
2642
2643 // Emit a library call.
2644 TargetLowering::ArgListTy Args;
2645 TargetLowering::ArgListEntry Entry;
2646 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2647 Entry.Node = Dst; Args.push_back(Entry);
2648 Entry.Node = Src; Args.push_back(Entry);
2649 Entry.Node = Size; Args.push_back(Entry);
2650 std::pair<SDOperand,SDOperand> CallResult =
2651 TLI.LowerCallTo(Chain, Type::VoidTy,
2652 false, false, false, CallingConv::C, false,
2653 getExternalSymbol("memcpy", TLI.getPointerTy()),
2654 Args, *this);
2655 return CallResult.second;
2656}
2657
2658SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2659 SDOperand Src, SDOperand Size,
2660 unsigned Align,
2661 Value *DstSV, uint64_t DstOff,
2662 Value *SrcSV, uint64_t SrcOff) {
2663
2664 // TODO: Optimize small memmove cases with simple loads and stores,
2665 // ensuring that all loads precede all stores. This can cause severe
2666 // register pressure, so targets should be careful with the size limit.
2667
2668 // Then check to see if we should lower the memmove with target-specific
2669 // code. If the target chooses to do this, this is the next best.
2670 SDOperand Result =
2671 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
2672 DstSV, DstOff, SrcSV, SrcOff);
2673 if (Result.Val)
2674 return Result;
2675
2676 // Emit a library call.
2677 TargetLowering::ArgListTy Args;
2678 TargetLowering::ArgListEntry Entry;
2679 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2680 Entry.Node = Dst; Args.push_back(Entry);
2681 Entry.Node = Src; Args.push_back(Entry);
2682 Entry.Node = Size; Args.push_back(Entry);
2683 std::pair<SDOperand,SDOperand> CallResult =
2684 TLI.LowerCallTo(Chain, Type::VoidTy,
2685 false, false, false, CallingConv::C, false,
2686 getExternalSymbol("memmove", TLI.getPointerTy()),
2687 Args, *this);
2688 return CallResult.second;
2689}
2690
2691SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2692 SDOperand Src, SDOperand Size,
2693 unsigned Align,
2694 Value *DstSV, uint64_t DstOff) {
2695
2696 // Check to see if we should lower the memset to stores first.
2697 // For cases within the target-specified limits, this is the best choice.
2698 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2699 if (ConstantSize) {
2700 // Memset with size zero? Just return the original chain.
2701 if (ConstantSize->isNullValue())
2702 return Chain;
2703
2704 SDOperand Result =
2705 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
2706 DstSV, DstOff);
2707 if (Result.Val)
2708 return Result;
2709 }
2710
2711 // Then check to see if we should lower the memset with target-specific
2712 // code. If the target chooses to do this, this is the next best.
2713 SDOperand Result =
2714 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
2715 DstSV, DstOff);
2716 if (Result.Val)
2717 return Result;
2718
2719 // Emit a library call.
2720 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2721 TargetLowering::ArgListTy Args;
2722 TargetLowering::ArgListEntry Entry;
2723 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2724 Args.push_back(Entry);
2725 // Extend or truncate the argument to be an i32 value for the call.
2726 if (Src.getValueType() > MVT::i32)
2727 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2728 else
2729 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2730 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2731 Args.push_back(Entry);
2732 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2733 Args.push_back(Entry);
2734 std::pair<SDOperand,SDOperand> CallResult =
2735 TLI.LowerCallTo(Chain, Type::VoidTy,
2736 false, false, false, CallingConv::C, false,
2737 getExternalSymbol("memset", TLI.getPointerTy()),
2738 Args, *this);
2739 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002740}
2741
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002742SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002743 SDOperand Ptr, SDOperand Cmp,
2744 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002745 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002746 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2747 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002748 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002749 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002750 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2751 ID.AddInteger((unsigned int)VT);
2752 void* IP = 0;
2753 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2754 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002755 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002756 CSEMap.InsertNode(N, IP);
2757 AllNodes.push_back(N);
2758 return SDOperand(N, 0);
2759}
2760
2761SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002762 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002763 MVT::ValueType VT) {
2764 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2765 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002766 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002767 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002768 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002769 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2770 ID.AddInteger((unsigned int)VT);
2771 void* IP = 0;
2772 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2773 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002774 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002775 CSEMap.InsertNode(N, IP);
2776 AllNodes.push_back(N);
2777 return SDOperand(N, 0);
2778}
2779
Duncan Sands14ea39c2008-03-27 20:23:40 +00002780SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00002781SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2782 MVT::ValueType VT, SDOperand Chain,
2783 SDOperand Ptr, SDOperand Offset,
2784 const Value *SV, int SVOffset, MVT::ValueType EVT,
2785 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002786 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2787 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002788 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002789 Ty = MVT::getTypeForValueType(VT);
2790 } else if (SV) {
2791 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2792 assert(PT && "Value for load must be a pointer");
2793 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00002794 }
Dan Gohman575e2f42007-06-04 15:49:41 +00002795 assert(Ty && "Could not get type information for load");
2796 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2797 }
Evan Cheng466685d2006-10-09 20:57:25 +00002798
Duncan Sands14ea39c2008-03-27 20:23:40 +00002799 if (VT == EVT) {
2800 ExtType = ISD::NON_EXTLOAD;
2801 } else if (ExtType == ISD::NON_EXTLOAD) {
2802 assert(VT == EVT && "Non-extending load from different memory type!");
2803 } else {
2804 // Extending load.
2805 if (MVT::isVector(VT))
2806 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2807 else
2808 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2809 "Should only be an extending load, not truncating!");
2810 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2811 "Cannot sign/zero extend a FP/Vector load!");
2812 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2813 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00002814 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00002815
2816 bool Indexed = AM != ISD::UNINDEXED;
2817 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2818 "Unindexed load with an offset!");
2819
2820 SDVTList VTs = Indexed ?
2821 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2822 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002823 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002824 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002825 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00002826 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002827 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002828 ID.AddInteger(Alignment);
2829 ID.AddInteger(isVolatile);
2830 void *IP = 0;
2831 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2832 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002833 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
2834 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002835 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002836 AllNodes.push_back(N);
2837 return SDOperand(N, 0);
2838}
2839
Duncan Sands14ea39c2008-03-27 20:23:40 +00002840SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2841 SDOperand Chain, SDOperand Ptr,
2842 const Value *SV, int SVOffset,
2843 bool isVolatile, unsigned Alignment) {
2844 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002845 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
2846 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002847}
2848
2849SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2850 SDOperand Chain, SDOperand Ptr,
2851 const Value *SV,
2852 int SVOffset, MVT::ValueType EVT,
2853 bool isVolatile, unsigned Alignment) {
2854 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002855 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
2856 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002857}
2858
Evan Cheng144d8f02006-11-09 17:55:04 +00002859SDOperand
2860SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2861 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002862 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002863 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2864 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00002865 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
2866 LD->getChain(), Base, Offset, LD->getSrcValue(),
2867 LD->getSrcValueOffset(), LD->getMemoryVT(),
2868 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00002869}
2870
Jeff Cohend41b30d2006-11-05 19:31:28 +00002871SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002872 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002873 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002874 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002875
Dan Gohman575e2f42007-06-04 15:49:41 +00002876 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2877 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002878 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002879 Ty = MVT::getTypeForValueType(VT);
2880 } else if (SV) {
2881 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2882 assert(PT && "Value for store must be a pointer");
2883 Ty = PT->getElementType();
2884 }
2885 assert(Ty && "Could not get type information for store");
2886 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2887 }
Evan Chengad071e12006-10-05 22:57:11 +00002888 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002889 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002890 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002891 FoldingSetNodeID ID;
2892 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002893 ID.AddInteger(ISD::UNINDEXED);
2894 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002895 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002896 ID.AddInteger(Alignment);
2897 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002898 void *IP = 0;
2899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2900 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002901 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002902 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002903 CSEMap.InsertNode(N, IP);
2904 AllNodes.push_back(N);
2905 return SDOperand(N, 0);
2906}
2907
Jeff Cohend41b30d2006-11-05 19:31:28 +00002908SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002909 SDOperand Ptr, const Value *SV,
2910 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002911 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002912 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002913
2914 if (VT == SVT)
2915 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002916
Duncan Sandsaf47b112007-10-16 09:56:48 +00002917 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2918 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002919 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2920 "Can't do FP-INT conversion!");
2921
Dan Gohman575e2f42007-06-04 15:49:41 +00002922 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2923 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002924 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002925 Ty = MVT::getTypeForValueType(VT);
2926 } else if (SV) {
2927 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2928 assert(PT && "Value for store must be a pointer");
2929 Ty = PT->getElementType();
2930 }
2931 assert(Ty && "Could not get type information for store");
2932 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2933 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002934 SDVTList VTs = getVTList(MVT::Other);
2935 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002936 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002937 FoldingSetNodeID ID;
2938 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002939 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002940 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002941 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002942 ID.AddInteger(Alignment);
2943 ID.AddInteger(isVolatile);
2944 void *IP = 0;
2945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2946 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002947 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002948 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002949 CSEMap.InsertNode(N, IP);
2950 AllNodes.push_back(N);
2951 return SDOperand(N, 0);
2952}
2953
Evan Cheng144d8f02006-11-09 17:55:04 +00002954SDOperand
2955SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2956 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002957 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2958 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2959 "Store is already a indexed store!");
2960 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2961 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2962 FoldingSetNodeID ID;
2963 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2964 ID.AddInteger(AM);
2965 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002966 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002967 ID.AddInteger(ST->getAlignment());
2968 ID.AddInteger(ST->isVolatile());
2969 void *IP = 0;
2970 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2971 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002972 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002973 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002974 ST->getSrcValue(), ST->getSrcValueOffset(),
2975 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002976 CSEMap.InsertNode(N, IP);
2977 AllNodes.push_back(N);
2978 return SDOperand(N, 0);
2979}
2980
Nate Begemanacc398c2006-01-25 18:21:52 +00002981SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2982 SDOperand Chain, SDOperand Ptr,
2983 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002984 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002985 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002986}
2987
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002988SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002989 const SDOperand *Ops, unsigned NumOps) {
2990 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002991 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002992 case 1: return getNode(Opcode, VT, Ops[0]);
2993 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2994 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002995 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002996 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002997
Chris Lattneref847df2005-04-09 03:27:28 +00002998 switch (Opcode) {
2999 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003000 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003001 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003002 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3003 "LHS and RHS of condition must have same type!");
3004 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3005 "True and False arms of SelectCC must have same type!");
3006 assert(Ops[2].getValueType() == VT &&
3007 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003008 break;
3009 }
3010 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003011 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003012 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3013 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003014 break;
3015 }
Chris Lattneref847df2005-04-09 03:27:28 +00003016 }
3017
Chris Lattner385328c2005-05-14 07:42:29 +00003018 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003019 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003020 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003021 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003022 FoldingSetNodeID ID;
3023 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003024 void *IP = 0;
3025 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3026 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003027 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003028 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003029 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003030 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003031 }
Chris Lattneref847df2005-04-09 03:27:28 +00003032 AllNodes.push_back(N);
3033 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003034}
3035
Chris Lattner89c34632005-05-14 06:20:26 +00003036SDOperand SelectionDAG::getNode(unsigned Opcode,
3037 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003038 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003039 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3040 Ops, NumOps);
3041}
3042
3043SDOperand SelectionDAG::getNode(unsigned Opcode,
3044 const MVT::ValueType *VTs, unsigned NumVTs,
3045 const SDOperand *Ops, unsigned NumOps) {
3046 if (NumVTs == 1)
3047 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003048 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3049}
3050
3051SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3052 const SDOperand *Ops, unsigned NumOps) {
3053 if (VTList.NumVTs == 1)
3054 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003055
Chris Lattner5f056bf2005-07-10 01:55:33 +00003056 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003057 // FIXME: figure out how to safely handle things like
3058 // int foo(int x) { return 1 << (x & 255); }
3059 // int bar() { return foo(256); }
3060#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003061 case ISD::SRA_PARTS:
3062 case ISD::SRL_PARTS:
3063 case ISD::SHL_PARTS:
3064 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003065 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003066 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3067 else if (N3.getOpcode() == ISD::AND)
3068 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3069 // If the and is only masking out bits that cannot effect the shift,
3070 // eliminate the and.
3071 unsigned NumBits = MVT::getSizeInBits(VT)*2;
3072 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3073 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3074 }
3075 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003076#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003077 }
Chris Lattner89c34632005-05-14 06:20:26 +00003078
Chris Lattner43247a12005-08-25 19:12:10 +00003079 // Memoize the node unless it returns a flag.
3080 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003081 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003082 FoldingSetNodeID ID;
3083 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003084 void *IP = 0;
3085 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3086 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003087 if (NumOps == 1)
3088 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3089 else if (NumOps == 2)
3090 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3091 else if (NumOps == 3)
3092 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3093 else
3094 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003095 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003096 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003097 if (NumOps == 1)
3098 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3099 else if (NumOps == 2)
3100 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3101 else if (NumOps == 3)
3102 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3103 else
3104 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003105 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003106 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003107 return SDOperand(N, 0);
3108}
3109
Dan Gohman08ce9762007-10-08 15:49:58 +00003110SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
3111 return getNode(Opcode, VTList, 0, 0);
3112}
3113
3114SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3115 SDOperand N1) {
3116 SDOperand Ops[] = { N1 };
3117 return getNode(Opcode, VTList, Ops, 1);
3118}
3119
3120SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3121 SDOperand N1, SDOperand N2) {
3122 SDOperand Ops[] = { N1, N2 };
3123 return getNode(Opcode, VTList, Ops, 2);
3124}
3125
3126SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3127 SDOperand N1, SDOperand N2, SDOperand N3) {
3128 SDOperand Ops[] = { N1, N2, N3 };
3129 return getNode(Opcode, VTList, Ops, 3);
3130}
3131
3132SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3133 SDOperand N1, SDOperand N2, SDOperand N3,
3134 SDOperand N4) {
3135 SDOperand Ops[] = { N1, N2, N3, N4 };
3136 return getNode(Opcode, VTList, Ops, 4);
3137}
3138
3139SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3140 SDOperand N1, SDOperand N2, SDOperand N3,
3141 SDOperand N4, SDOperand N5) {
3142 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3143 return getNode(Opcode, VTList, Ops, 5);
3144}
3145
Chris Lattner70046e92006-08-15 17:46:01 +00003146SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003147 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003148}
3149
Chris Lattner70046e92006-08-15 17:46:01 +00003150SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00003151 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3152 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003153 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003154 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003155 }
3156 std::vector<MVT::ValueType> V;
3157 V.push_back(VT1);
3158 V.push_back(VT2);
3159 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003160 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003161}
Chris Lattner70046e92006-08-15 17:46:01 +00003162SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
3163 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003164 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003165 E = VTList.end(); I != E; ++I) {
3166 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3167 (*I)[2] == VT3)
3168 return makeVTList(&(*I)[0], 3);
3169 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003170 std::vector<MVT::ValueType> V;
3171 V.push_back(VT1);
3172 V.push_back(VT2);
3173 V.push_back(VT3);
3174 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003175 return makeVTList(&(*VTList.begin())[0], 3);
3176}
3177
3178SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
3179 switch (NumVTs) {
3180 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003181 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003182 case 2: return getVTList(VTs[0], VTs[1]);
3183 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3184 default: break;
3185 }
3186
3187 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3188 E = VTList.end(); I != E; ++I) {
3189 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3190
3191 bool NoMatch = false;
3192 for (unsigned i = 2; i != NumVTs; ++i)
3193 if (VTs[i] != (*I)[i]) {
3194 NoMatch = true;
3195 break;
3196 }
3197 if (!NoMatch)
3198 return makeVTList(&*I->begin(), NumVTs);
3199 }
3200
3201 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
3202 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003203}
3204
3205
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003206/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3207/// specified operands. If the resultant node already exists in the DAG,
3208/// this does not modify the specified node, instead it returns the node that
3209/// already exists. If the resultant node does not exist in the DAG, the
3210/// input node is returned. As a degenerate case, if you specify the same
3211/// input operands as the node already has, the input node is returned.
3212SDOperand SelectionDAG::
3213UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3214 SDNode *N = InN.Val;
3215 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3216
3217 // Check to see if there is no change.
3218 if (Op == N->getOperand(0)) return InN;
3219
3220 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003221 void *InsertPos = 0;
3222 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3223 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003224
3225 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003226 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003227 RemoveNodeFromCSEMaps(N);
3228
3229 // Now we update the operands.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003230 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003231 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003232 N->OperandList[0].setUser(N);
3233 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003234
3235 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003236 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003237 return InN;
3238}
3239
3240SDOperand SelectionDAG::
3241UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3242 SDNode *N = InN.Val;
3243 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3244
3245 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003246 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3247 return InN; // No operands changed, just return the input node.
3248
3249 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003250 void *InsertPos = 0;
3251 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3252 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003253
3254 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003255 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003256 RemoveNodeFromCSEMaps(N);
3257
3258 // Now we update the operands.
3259 if (N->OperandList[0] != Op1) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003260 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003261 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003262 N->OperandList[0].setUser(N);
3263 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003264 }
3265 if (N->OperandList[1] != Op2) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003266 N->OperandList[1].Val->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003267 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003268 N->OperandList[1].setUser(N);
3269 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003270 }
3271
3272 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003273 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003274 return InN;
3275}
3276
3277SDOperand SelectionDAG::
3278UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003279 SDOperand Ops[] = { Op1, Op2, Op3 };
3280 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003281}
3282
3283SDOperand SelectionDAG::
3284UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3285 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003286 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3287 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003288}
3289
3290SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003291UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3292 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003293 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3294 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003295}
3296
Chris Lattner809ec112006-01-28 10:09:25 +00003297SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003298UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003299 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003300 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003301 "Update with wrong number of operands");
3302
3303 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003304 bool AnyChange = false;
3305 for (unsigned i = 0; i != NumOps; ++i) {
3306 if (Ops[i] != N->getOperand(i)) {
3307 AnyChange = true;
3308 break;
3309 }
3310 }
3311
3312 // No operands changed, just return the input node.
3313 if (!AnyChange) return InN;
3314
3315 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003316 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003317 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003318 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003319
3320 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003321 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003322 RemoveNodeFromCSEMaps(N);
3323
3324 // Now we update the operands.
3325 for (unsigned i = 0; i != NumOps; ++i) {
3326 if (N->OperandList[i] != Ops[i]) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003327 N->OperandList[i].Val->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003328 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003329 N->OperandList[i].setUser(N);
3330 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003331 }
3332 }
3333
3334 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003335 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003336 return InN;
3337}
3338
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003339/// MorphNodeTo - This frees the operands of the current node, resets the
3340/// opcode, types, and operands to the specified value. This should only be
3341/// used by the SelectionDAG class.
3342void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3343 const SDOperand *Ops, unsigned NumOps) {
3344 NodeType = Opc;
3345 ValueList = L.VTs;
3346 NumValues = L.NumVTs;
3347
3348 // Clear the operands list, updating used nodes to remove this from their
3349 // use list.
3350 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levensteindc1adac2008-04-07 10:06:32 +00003351 I->Val->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003352
3353 // If NumOps is larger than the # of operands we currently have, reallocate
3354 // the operand list.
3355 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003356 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003357 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003358 }
Chris Lattner63e3f142007-02-04 07:28:00 +00003359 OperandList = new SDOperand[NumOps];
3360 OperandsNeedDelete = true;
3361 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003362
3363 // Assign the new operands.
3364 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003365
3366 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3367 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003368 OperandList[i].setUser(this);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003369 SDNode *N = OperandList[i].Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003370 N->addUser(i, this);
3371 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003372 }
3373}
Chris Lattner149c58c2005-08-16 18:17:10 +00003374
3375/// SelectNodeTo - These are used for target selectors to *mutate* the
3376/// specified node to have the specified return type, Target opcode, and
3377/// operands. Note that target opcodes are stored as
3378/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003379///
3380/// Note that SelectNodeTo returns the resultant node. If there is already a
3381/// node of the specified opcode and operands, it returns that node instead of
3382/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003383SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3384 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003385 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003386 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003387 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003388 void *IP = 0;
3389 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003390 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003391
Chris Lattner7651fa42005-08-24 23:00:29 +00003392 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003393
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003394 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003395
Chris Lattner4a283e92006-08-11 18:38:11 +00003396 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003397 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003398}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003399
Evan Cheng95514ba2006-08-26 08:00:10 +00003400SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3401 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003402 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003403 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003404 SDOperand Ops[] = { Op1 };
3405
Jim Laskey583bd472006-10-27 23:46:08 +00003406 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003407 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003408 void *IP = 0;
3409 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003410 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003411
Chris Lattner149c58c2005-08-16 18:17:10 +00003412 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003413 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003414 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003415 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003416}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003417
Evan Cheng95514ba2006-08-26 08:00:10 +00003418SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3419 MVT::ValueType VT, SDOperand Op1,
3420 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003421 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003422 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003423 SDOperand Ops[] = { Op1, Op2 };
3424
Jim Laskey583bd472006-10-27 23:46:08 +00003425 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003426 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003427 void *IP = 0;
3428 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003429 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003430
Chris Lattner149c58c2005-08-16 18:17:10 +00003431 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003432
Chris Lattner63e3f142007-02-04 07:28:00 +00003433 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003434
Chris Lattnera5682852006-08-07 23:03:03 +00003435 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003436 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003437}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003438
Evan Cheng95514ba2006-08-26 08:00:10 +00003439SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3440 MVT::ValueType VT, SDOperand Op1,
3441 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003442 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003443 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003444 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003445 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003446 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003447 void *IP = 0;
3448 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003449 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003450
Chris Lattner149c58c2005-08-16 18:17:10 +00003451 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003452
Chris Lattner63e3f142007-02-04 07:28:00 +00003453 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003454
Chris Lattnera5682852006-08-07 23:03:03 +00003455 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003456 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003457}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003458
Evan Cheng95514ba2006-08-26 08:00:10 +00003459SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003460 MVT::ValueType VT, const SDOperand *Ops,
3461 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003462 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003463 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003464 FoldingSetNodeID ID;
3465 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003466 void *IP = 0;
3467 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003468 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003469
Chris Lattner6b09a292005-08-21 18:49:33 +00003470 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003471 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003472
Chris Lattnera5682852006-08-07 23:03:03 +00003473 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003474 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003475}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003476
Evan Cheng95514ba2006-08-26 08:00:10 +00003477SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3478 MVT::ValueType VT1, MVT::ValueType VT2,
3479 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003480 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003481 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003482 SDOperand Ops[] = { Op1, Op2 };
3483 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003484 void *IP = 0;
3485 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003486 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003487
Chris Lattner0fb094f2005-11-19 01:44:53 +00003488 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003489 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003490 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003491 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003492}
3493
Evan Cheng95514ba2006-08-26 08:00:10 +00003494SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3495 MVT::ValueType VT1, MVT::ValueType VT2,
3496 SDOperand Op1, SDOperand Op2,
3497 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003498 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003499 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003500 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003501 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003502 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003503 void *IP = 0;
3504 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003505 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003506
Chris Lattner0fb094f2005-11-19 01:44:53 +00003507 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003508
Chris Lattner63e3f142007-02-04 07:28:00 +00003509 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003510 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003511 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003512}
3513
Chris Lattner0fb094f2005-11-19 01:44:53 +00003514
Evan Cheng6ae46c42006-02-09 07:15:23 +00003515/// getTargetNode - These are used for target selectors to create a new node
3516/// with specified return type(s), target opcode, and operands.
3517///
3518/// Note that getTargetNode returns the resultant node. If there is already a
3519/// node of the specified opcode and operands, it returns that node instead of
3520/// the current one.
3521SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3522 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3523}
3524SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3525 SDOperand Op1) {
3526 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3527}
3528SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3529 SDOperand Op1, SDOperand Op2) {
3530 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3531}
3532SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003533 SDOperand Op1, SDOperand Op2,
3534 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003535 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3536}
3537SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003538 const SDOperand *Ops, unsigned NumOps) {
3539 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003540}
3541SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003542 MVT::ValueType VT2) {
3543 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3544 SDOperand Op;
3545 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3546}
3547SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003548 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003549 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003550 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003551}
3552SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003553 MVT::ValueType VT2, SDOperand Op1,
3554 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003555 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003556 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003557 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003558}
3559SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003560 MVT::ValueType VT2, SDOperand Op1,
3561 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003562 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003563 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003564 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003565}
Evan Cheng694481e2006-08-27 08:08:54 +00003566SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3567 MVT::ValueType VT2,
3568 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003569 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003570 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003571}
3572SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3573 MVT::ValueType VT2, MVT::ValueType VT3,
3574 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003575 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003576 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003577 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003578}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003579SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3580 MVT::ValueType VT2, MVT::ValueType VT3,
3581 SDOperand Op1, SDOperand Op2,
3582 SDOperand Op3) {
3583 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3584 SDOperand Ops[] = { Op1, Op2, Op3 };
3585 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3586}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003587SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003588 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003589 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003590 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3591 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003592}
Evan Cheng05e69c12007-09-12 23:39:49 +00003593SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3594 MVT::ValueType VT2, MVT::ValueType VT3,
3595 MVT::ValueType VT4,
3596 const SDOperand *Ops, unsigned NumOps) {
3597 std::vector<MVT::ValueType> VTList;
3598 VTList.push_back(VT1);
3599 VTList.push_back(VT2);
3600 VTList.push_back(VT3);
3601 VTList.push_back(VT4);
3602 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3603 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3604}
Evan Cheng39305cf2007-10-05 01:10:49 +00003605SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3606 std::vector<MVT::ValueType> &ResultTys,
3607 const SDOperand *Ops, unsigned NumOps) {
3608 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3609 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3610 Ops, NumOps).Val;
3611}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003612
Evan Cheng08b11732008-03-22 01:55:50 +00003613/// getNodeIfExists - Get the specified node if it's already available, or
3614/// else return NULL.
3615SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
3616 const SDOperand *Ops, unsigned NumOps) {
3617 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3618 FoldingSetNodeID ID;
3619 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3620 void *IP = 0;
3621 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3622 return E;
3623 }
3624 return NULL;
3625}
3626
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003627
Evan Cheng99157a02006-08-07 22:13:29 +00003628/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003629/// This can cause recursive merging of nodes in the DAG.
3630///
Chris Lattner11d049c2008-02-03 03:35:22 +00003631/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003632///
Chris Lattner11d049c2008-02-03 03:35:22 +00003633void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003634 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003635 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003636 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003637 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003638 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003639
Chris Lattner8b8749f2005-08-17 19:00:20 +00003640 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003641 SDNode::use_iterator UI = From->use_begin();
3642 SDNode *U = UI->getUser();
3643
Chris Lattner8b8749f2005-08-17 19:00:20 +00003644 // This node is about to morph, remove its old self from the CSE maps.
3645 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003646 int operandNum = 0;
3647 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3648 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003649 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003650 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003651 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003652 I->setUser(U);
3653 To.Val->addUser(operandNum, U);
3654 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003655
3656 // Now that we have modified U, add it back to the CSE maps. If it already
3657 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003658 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003659 ReplaceAllUsesWith(U, Existing, UpdateListener);
3660 // U is now dead. Inform the listener if it exists and delete it.
3661 if (UpdateListener)
3662 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003663 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003664 } else {
3665 // If the node doesn't already exist, we updated it. Inform a listener if
3666 // it exists.
3667 if (UpdateListener)
3668 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003669 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003670 }
3671}
3672
3673/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3674/// This can cause recursive merging of nodes in the DAG.
3675///
3676/// This version assumes From/To have matching types and numbers of result
3677/// values.
3678///
Chris Lattner1e111c72005-09-07 05:37:01 +00003679void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003680 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003681 assert(From != To && "Cannot replace uses of with self");
3682 assert(From->getNumValues() == To->getNumValues() &&
3683 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003684 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003685 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3686 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003687
3688 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003689 SDNode::use_iterator UI = From->use_begin();
3690 SDNode *U = UI->getUser();
3691
Chris Lattner8b52f212005-08-26 18:36:28 +00003692 // This node is about to morph, remove its old self from the CSE maps.
3693 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003694 int operandNum = 0;
3695 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3696 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003697 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003698 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003699 I->Val = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003700 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003701 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003702
Chris Lattner8b8749f2005-08-17 19:00:20 +00003703 // Now that we have modified U, add it back to the CSE maps. If it already
3704 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003705 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003706 ReplaceAllUsesWith(U, Existing, UpdateListener);
3707 // U is now dead. Inform the listener if it exists and delete it.
3708 if (UpdateListener)
3709 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003710 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003711 } else {
3712 // If the node doesn't already exist, we updated it. Inform a listener if
3713 // it exists.
3714 if (UpdateListener)
3715 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003716 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003717 }
3718}
3719
Chris Lattner8b52f212005-08-26 18:36:28 +00003720/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3721/// This can cause recursive merging of nodes in the DAG.
3722///
3723/// This version can replace From with any result values. To must match the
3724/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003725void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003726 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003727 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003728 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003729 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003730
3731 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003732 SDNode::use_iterator UI = From->use_begin();
3733 SDNode *U = UI->getUser();
3734
Chris Lattner7b2880c2005-08-24 22:44:39 +00003735 // This node is about to morph, remove its old self from the CSE maps.
3736 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003737 int operandNum = 0;
3738 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3739 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003740 if (I->Val == From) {
3741 const SDOperand &ToOp = To[I->ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003742 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003743 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003744 I->setUser(U);
3745 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003746 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003747
Chris Lattner7b2880c2005-08-24 22:44:39 +00003748 // Now that we have modified U, add it back to the CSE maps. If it already
3749 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003750 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003751 ReplaceAllUsesWith(U, Existing, UpdateListener);
3752 // U is now dead. Inform the listener if it exists and delete it.
3753 if (UpdateListener)
3754 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003755 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003756 } else {
3757 // If the node doesn't already exist, we updated it. Inform a listener if
3758 // it exists.
3759 if (UpdateListener)
3760 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003761 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003762 }
3763}
3764
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003765namespace {
3766 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3767 /// any deleted nodes from the set passed into its constructor and recursively
3768 /// notifies another update listener if specified.
3769 class ChainedSetUpdaterListener :
3770 public SelectionDAG::DAGUpdateListener {
3771 SmallSetVector<SDNode*, 16> &Set;
3772 SelectionDAG::DAGUpdateListener *Chain;
3773 public:
3774 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3775 SelectionDAG::DAGUpdateListener *chain)
3776 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00003777
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003778 virtual void NodeDeleted(SDNode *N) {
3779 Set.remove(N);
3780 if (Chain) Chain->NodeDeleted(N);
3781 }
3782 virtual void NodeUpdated(SDNode *N) {
3783 if (Chain) Chain->NodeUpdated(N);
3784 }
3785 };
3786}
3787
Chris Lattner012f2412006-02-17 21:58:01 +00003788/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3789/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003790/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003791void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003792 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003793 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003794
Chris Lattner012f2412006-02-17 21:58:01 +00003795 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003796 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003797 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003798 return;
3799 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003800
3801 if (From.use_empty()) return;
3802
Chris Lattnercf5640b2007-02-04 00:14:31 +00003803 // Get all of the users of From.Val. We want these in a nice,
3804 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003805 SmallSetVector<SDNode*, 16> Users;
3806 for (SDNode::use_iterator UI = From.Val->use_begin(),
3807 E = From.Val->use_end(); UI != E; ++UI) {
3808 SDNode *User = UI->getUser();
3809 if (!Users.count(User))
3810 Users.insert(User);
3811 }
Chris Lattner012f2412006-02-17 21:58:01 +00003812
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003813 // When one of the recursive merges deletes nodes from the graph, we need to
3814 // make sure that UpdateListener is notified *and* that the node is removed
3815 // from Users if present. CSUL does this.
3816 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003817
Chris Lattner012f2412006-02-17 21:58:01 +00003818 while (!Users.empty()) {
3819 // We know that this user uses some value of From. If it is the right
3820 // value, update it.
3821 SDNode *User = Users.back();
3822 Users.pop_back();
3823
Chris Lattner01d029b2007-10-15 06:10:22 +00003824 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003825 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00003826 for (; Op != E; ++Op)
3827 if (*Op == From) break;
3828
3829 // If there are no matches, the user must use some other result of From.
3830 if (Op == E) continue;
3831
3832 // Okay, we know this user needs to be updated. Remove its old self
3833 // from the CSE maps.
3834 RemoveNodeFromCSEMaps(User);
3835
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003836 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003837 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003838 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003839 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00003840 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003841 Op->setUser(User);
3842 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00003843 }
3844 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003845
3846 // Now that we have modified User, add it back to the CSE maps. If it
3847 // already exists there, recursively merge the results together.
3848 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003849 if (!Existing) {
3850 if (UpdateListener) UpdateListener->NodeUpdated(User);
3851 continue; // Continue on to next user.
3852 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003853
3854 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003855 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003856 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003857 // can cause deletion of nodes that used the old value. To handle this, we
3858 // use CSUL to remove them from the Users set.
3859 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003860
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003861 // User is now dead. Notify a listener if present.
3862 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003863 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003864 }
3865}
3866
Chris Lattner7b2880c2005-08-24 22:44:39 +00003867
Evan Chenge6f35d82006-08-01 08:20:41 +00003868/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3869/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003870unsigned SelectionDAG::AssignNodeIds() {
3871 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003872 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3873 SDNode *N = I;
3874 N->setNodeId(Id++);
3875 }
3876 return Id;
3877}
3878
Evan Chenge6f35d82006-08-01 08:20:41 +00003879/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003880/// based on their topological order. It returns the maximum id and a vector
3881/// of the SDNodes* in assigned order by reference.
3882unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003883 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003884 std::vector<unsigned> InDegree(DAGSize);
3885 std::vector<SDNode*> Sources;
3886
3887 // Use a two pass approach to avoid using a std::map which is slow.
3888 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003889 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3890 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003891 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003892 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003893 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003894 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003895 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003896 }
3897
Evan Cheng99157a02006-08-07 22:13:29 +00003898 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003899 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003900 SDNode *N = Sources.back();
3901 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003902 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003903 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3904 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003905 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003906 if (Degree == 0)
3907 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003908 }
3909 }
3910
Evan Chengc384d6c2006-08-02 22:00:34 +00003911 // Second pass, assign the actual topological order as node ids.
3912 Id = 0;
3913 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3914 TI != TE; ++TI)
3915 (*TI)->setNodeId(Id++);
3916
3917 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003918}
3919
3920
Evan Cheng091cba12006-07-27 06:39:06 +00003921
Jim Laskey58b968b2005-08-17 20:08:02 +00003922//===----------------------------------------------------------------------===//
3923// SDNode Class
3924//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003925
Chris Lattner917d2c92006-07-19 00:00:37 +00003926// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003927void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003928void UnarySDNode::ANCHOR() {}
3929void BinarySDNode::ANCHOR() {}
3930void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003931void HandleSDNode::ANCHOR() {}
3932void StringSDNode::ANCHOR() {}
3933void ConstantSDNode::ANCHOR() {}
3934void ConstantFPSDNode::ANCHOR() {}
3935void GlobalAddressSDNode::ANCHOR() {}
3936void FrameIndexSDNode::ANCHOR() {}
3937void JumpTableSDNode::ANCHOR() {}
3938void ConstantPoolSDNode::ANCHOR() {}
3939void BasicBlockSDNode::ANCHOR() {}
3940void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003941void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003942void RegisterSDNode::ANCHOR() {}
3943void ExternalSymbolSDNode::ANCHOR() {}
3944void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00003945void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003946void VTSDNode::ANCHOR() {}
3947void LoadSDNode::ANCHOR() {}
3948void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003949void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003950
Chris Lattner48b85922007-02-04 02:41:42 +00003951HandleSDNode::~HandleSDNode() {
3952 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003953 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003954}
3955
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003956GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3957 MVT::ValueType VT, int o)
3958 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003959 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003960 // Thread Local
3961 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3962 // Non Thread Local
3963 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3964 getSDVTList(VT)), Offset(o) {
3965 TheGlobal = const_cast<GlobalValue*>(GA);
3966}
Chris Lattner48b85922007-02-04 02:41:42 +00003967
Dan Gohman36b5c132008-04-07 19:35:22 +00003968/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00003969/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00003970MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman69de1932008-02-06 22:27:42 +00003971 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3972 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00003973 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
3974 MachineMemOperand::MOStore;
3975 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00003976
3977 // Check if the load references a frame index, and does not have
3978 // an SV attached.
3979 const FrameIndexSDNode *FI =
3980 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3981 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00003982 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
3983 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003984 else
Dan Gohman36b5c132008-04-07 19:35:22 +00003985 return MachineMemOperand(getSrcValue(), Flags,
3986 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003987}
3988
Jim Laskey583bd472006-10-27 23:46:08 +00003989/// Profile - Gather unique data for the node.
3990///
3991void SDNode::Profile(FoldingSetNodeID &ID) {
3992 AddNodeIDNode(ID, this);
3993}
3994
Chris Lattnera3255112005-11-08 23:30:28 +00003995/// getValueTypeList - Return a pointer to the specified value type.
3996///
Dan Gohman547ca532008-02-08 03:26:46 +00003997const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003998 if (MVT::isExtendedVT(VT)) {
3999 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004000 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004001 } else {
4002 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
4003 VTs[VT] = VT;
4004 return &VTs[VT];
4005 }
Chris Lattnera3255112005-11-08 23:30:28 +00004006}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004007
Chris Lattner5c884562005-01-12 18:37:47 +00004008/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4009/// indicated value. This method ignores uses of other values defined by this
4010/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004011bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004012 assert(Value < getNumValues() && "Bad value!");
4013
4014 // If there is only one value, this is easy.
4015 if (getNumValues() == 1)
4016 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004017 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004018
Evan Cheng4ee62112006-02-05 06:29:23 +00004019 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004020
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004021 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004022
Roman Levensteindc1adac2008-04-07 10:06:32 +00004023 // TODO: Only iterate over uses of a given value of the node
4024 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4025 if (*UI == TheValue) {
4026 if (NUses == 0)
4027 return false;
4028 --NUses;
4029 }
Chris Lattner5c884562005-01-12 18:37:47 +00004030 }
4031
4032 // Found exactly the right number of uses?
4033 return NUses == 0;
4034}
4035
4036
Evan Cheng33d55952007-08-02 05:29:38 +00004037/// hasAnyUseOfValue - Return true if there are any use of the indicated
4038/// value. This method ignores uses of other values defined by this operation.
4039bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4040 assert(Value < getNumValues() && "Bad value!");
4041
Dan Gohman30359592008-01-29 13:02:09 +00004042 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004043
4044 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4045
4046 SmallPtrSet<SDNode*, 32> UsersHandled;
4047
Roman Levensteindc1adac2008-04-07 10:06:32 +00004048 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4049 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004050 if (User->getNumOperands() == 1 ||
4051 UsersHandled.insert(User)) // First time we've seen this?
4052 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4053 if (User->getOperand(i) == TheValue) {
4054 return true;
4055 }
4056 }
4057
4058 return false;
4059}
4060
4061
Evan Cheng917be682008-03-04 00:41:45 +00004062/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004063///
Evan Cheng917be682008-03-04 00:41:45 +00004064bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004065 bool Seen = false;
4066 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004067 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004068 if (User == this)
4069 Seen = true;
4070 else
4071 return false;
4072 }
4073
4074 return Seen;
4075}
4076
Evan Chenge6e97e62006-11-03 07:31:32 +00004077/// isOperand - Return true if this node is an operand of N.
4078///
Roman Levensteindc1adac2008-04-07 10:06:32 +00004079bool SDOperandImpl::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004080 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4081 if (*this == N->getOperand(i))
4082 return true;
4083 return false;
4084}
4085
Evan Cheng917be682008-03-04 00:41:45 +00004086bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004087 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
4088 if (this == N->OperandList[i].Val)
4089 return true;
4090 return false;
4091}
Evan Cheng4ee62112006-02-05 06:29:23 +00004092
Chris Lattner572dee72008-01-16 05:49:24 +00004093/// reachesChainWithoutSideEffects - Return true if this operand (which must
4094/// be a chain) reaches the specified operand without crossing any
4095/// side-effecting instructions. In practice, this looks through token
4096/// factors and non-volatile loads. In order to remain efficient, this only
4097/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004098bool SDOperandImpl::reachesChainWithoutSideEffects(SDOperandImpl Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004099 unsigned Depth) const {
4100 if (*this == Dest) return true;
4101
4102 // Don't search too deeply, we just want to be able to see through
4103 // TokenFactor's etc.
4104 if (Depth == 0) return false;
4105
4106 // If this is a token factor, all inputs to the TF happen in parallel. If any
4107 // of the operands of the TF reach dest, then we can do the xform.
4108 if (getOpcode() == ISD::TokenFactor) {
4109 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4110 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4111 return true;
4112 return false;
4113 }
4114
4115 // Loads don't have side effects, look through them.
4116 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4117 if (!Ld->isVolatile())
4118 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4119 }
4120 return false;
4121}
4122
4123
Evan Chengc5fc57d2006-11-03 03:05:24 +00004124static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004125 SmallPtrSet<SDNode *, 32> &Visited) {
4126 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004127 return;
4128
4129 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4130 SDNode *Op = N->getOperand(i).Val;
4131 if (Op == P) {
4132 found = true;
4133 return;
4134 }
4135 findPredecessor(Op, P, found, Visited);
4136 }
4137}
4138
Evan Cheng917be682008-03-04 00:41:45 +00004139/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004140/// is either an operand of N or it can be reached by recursively traversing
4141/// up the operands.
4142/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004143bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004144 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004145 bool found = false;
4146 findPredecessor(N, this, found, Visited);
4147 return found;
4148}
4149
Evan Chengc5484282006-10-04 00:56:09 +00004150uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4151 assert(Num < NumOperands && "Invalid child # of SDNode!");
4152 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4153}
4154
Reid Spencer577cc322007-04-01 07:32:19 +00004155std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004156 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004157 default:
4158 if (getOpcode() < ISD::BUILTIN_OP_END)
4159 return "<<Unknown DAG Node>>";
4160 else {
Evan Cheng72261582005-12-20 06:22:03 +00004161 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004162 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004163 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004164 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004165
Evan Cheng72261582005-12-20 06:22:03 +00004166 TargetLowering &TLI = G->getTargetLoweringInfo();
4167 const char *Name =
4168 TLI.getTargetNodeName(getOpcode());
4169 if (Name) return Name;
4170 }
4171
4172 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004173 }
4174
Evan Cheng27b7db52008-03-08 00:58:38 +00004175 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004176 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004177 case ISD::ATOMIC_LCS: return "AtomicLCS";
4178 case ISD::ATOMIC_LAS: return "AtomicLAS";
4179 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004180 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004181 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004182 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004183 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004184 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004185 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004186 case ISD::AssertSext: return "AssertSext";
4187 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004188
4189 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004190 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004191 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004192 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004193 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004194
4195 case ISD::Constant: return "Constant";
4196 case ISD::ConstantFP: return "ConstantFP";
4197 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004198 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004199 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004200 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004201 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004202 case ISD::RETURNADDR: return "RETURNADDR";
4203 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004204 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004205 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4206 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004207 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004208 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004209 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004210 case ISD::INTRINSIC_WO_CHAIN: {
4211 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4212 return Intrinsic::getName((Intrinsic::ID)IID);
4213 }
4214 case ISD::INTRINSIC_VOID:
4215 case ISD::INTRINSIC_W_CHAIN: {
4216 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004217 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004218 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004219
Chris Lattnerb2827b02006-03-19 00:52:58 +00004220 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004221 case ISD::TargetConstant: return "TargetConstant";
4222 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004223 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004224 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004225 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004226 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004227 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004228 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004229
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004230 case ISD::CopyToReg: return "CopyToReg";
4231 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004232 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004233 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004234 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004235 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004236 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004237 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004238 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004239 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004240
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004241 // Unary operators
4242 case ISD::FABS: return "fabs";
4243 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004244 case ISD::FSQRT: return "fsqrt";
4245 case ISD::FSIN: return "fsin";
4246 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004247 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004248 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004249
4250 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004251 case ISD::ADD: return "add";
4252 case ISD::SUB: return "sub";
4253 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004254 case ISD::MULHU: return "mulhu";
4255 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004256 case ISD::SDIV: return "sdiv";
4257 case ISD::UDIV: return "udiv";
4258 case ISD::SREM: return "srem";
4259 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004260 case ISD::SMUL_LOHI: return "smul_lohi";
4261 case ISD::UMUL_LOHI: return "umul_lohi";
4262 case ISD::SDIVREM: return "sdivrem";
4263 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004264 case ISD::AND: return "and";
4265 case ISD::OR: return "or";
4266 case ISD::XOR: return "xor";
4267 case ISD::SHL: return "shl";
4268 case ISD::SRA: return "sra";
4269 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004270 case ISD::ROTL: return "rotl";
4271 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004272 case ISD::FADD: return "fadd";
4273 case ISD::FSUB: return "fsub";
4274 case ISD::FMUL: return "fmul";
4275 case ISD::FDIV: return "fdiv";
4276 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004277 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004278 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004279
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004280 case ISD::SETCC: return "setcc";
4281 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004282 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004283 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004284 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004285 case ISD::CONCAT_VECTORS: return "concat_vectors";
4286 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004287 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004288 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004289 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004290 case ISD::ADDC: return "addc";
4291 case ISD::ADDE: return "adde";
4292 case ISD::SUBC: return "subc";
4293 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004294 case ISD::SHL_PARTS: return "shl_parts";
4295 case ISD::SRA_PARTS: return "sra_parts";
4296 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004297
4298 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4299 case ISD::INSERT_SUBREG: return "insert_subreg";
4300
Chris Lattner7f644642005-04-28 21:44:03 +00004301 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004302 case ISD::SIGN_EXTEND: return "sign_extend";
4303 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004304 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004305 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004306 case ISD::TRUNCATE: return "truncate";
4307 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004308 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004309 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004310 case ISD::FP_EXTEND: return "fp_extend";
4311
4312 case ISD::SINT_TO_FP: return "sint_to_fp";
4313 case ISD::UINT_TO_FP: return "uint_to_fp";
4314 case ISD::FP_TO_SINT: return "fp_to_sint";
4315 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004316 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004317
4318 // Control flow instructions
4319 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004320 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004321 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004322 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004323 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004324 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004325 case ISD::CALLSEQ_START: return "callseq_start";
4326 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004327
4328 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004329 case ISD::LOAD: return "load";
4330 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004331 case ISD::VAARG: return "vaarg";
4332 case ISD::VACOPY: return "vacopy";
4333 case ISD::VAEND: return "vaend";
4334 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004335 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004336 case ISD::EXTRACT_ELEMENT: return "extract_element";
4337 case ISD::BUILD_PAIR: return "build_pair";
4338 case ISD::STACKSAVE: return "stacksave";
4339 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004340 case ISD::TRAP: return "trap";
4341
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004342 // Bit manipulation
4343 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004344 case ISD::CTPOP: return "ctpop";
4345 case ISD::CTTZ: return "cttz";
4346 case ISD::CTLZ: return "ctlz";
4347
Chris Lattner36ce6912005-11-29 06:21:05 +00004348 // Debug info
4349 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004350 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004351
Duncan Sands36397f52007-07-27 12:58:54 +00004352 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004353 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004354
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004355 case ISD::CONDCODE:
4356 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004357 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004358 case ISD::SETOEQ: return "setoeq";
4359 case ISD::SETOGT: return "setogt";
4360 case ISD::SETOGE: return "setoge";
4361 case ISD::SETOLT: return "setolt";
4362 case ISD::SETOLE: return "setole";
4363 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004364
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004365 case ISD::SETO: return "seto";
4366 case ISD::SETUO: return "setuo";
4367 case ISD::SETUEQ: return "setue";
4368 case ISD::SETUGT: return "setugt";
4369 case ISD::SETUGE: return "setuge";
4370 case ISD::SETULT: return "setult";
4371 case ISD::SETULE: return "setule";
4372 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004373
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004374 case ISD::SETEQ: return "seteq";
4375 case ISD::SETGT: return "setgt";
4376 case ISD::SETGE: return "setge";
4377 case ISD::SETLT: return "setlt";
4378 case ISD::SETLE: return "setle";
4379 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004380 }
4381 }
4382}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004383
Evan Cheng144d8f02006-11-09 17:55:04 +00004384const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004385 switch (AM) {
4386 default:
4387 return "";
4388 case ISD::PRE_INC:
4389 return "<pre-inc>";
4390 case ISD::PRE_DEC:
4391 return "<pre-dec>";
4392 case ISD::POST_INC:
4393 return "<post-inc>";
4394 case ISD::POST_DEC:
4395 return "<post-dec>";
4396 }
4397}
4398
Duncan Sands276dcbd2008-03-21 09:14:45 +00004399std::string ISD::ArgFlagsTy::getArgFlagsString() {
4400 std::string S = "< ";
4401
4402 if (isZExt())
4403 S += "zext ";
4404 if (isSExt())
4405 S += "sext ";
4406 if (isInReg())
4407 S += "inreg ";
4408 if (isSRet())
4409 S += "sret ";
4410 if (isByVal())
4411 S += "byval ";
4412 if (isNest())
4413 S += "nest ";
4414 if (getByValAlign())
4415 S += "byval-align:" + utostr(getByValAlign()) + " ";
4416 if (getOrigAlign())
4417 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4418 if (getByValSize())
4419 S += "byval-size:" + utostr(getByValSize()) + " ";
4420 return S + ">";
4421}
4422
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004423void SDNode::dump() const { dump(0); }
4424void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004425 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004426
4427 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004428 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004429 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004430 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004431 else
Bill Wendling832171c2006-12-07 20:04:42 +00004432 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004433 }
Bill Wendling832171c2006-12-07 20:04:42 +00004434 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004435
Bill Wendling832171c2006-12-07 20:04:42 +00004436 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004437 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004438 if (i) cerr << ", ";
4439 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004440 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004441 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004442 }
4443
Evan Chengce254432007-12-11 02:08:35 +00004444 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4445 SDNode *Mask = getOperand(2).Val;
4446 cerr << "<";
4447 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4448 if (i) cerr << ",";
4449 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4450 cerr << "u";
4451 else
4452 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4453 }
4454 cerr << ">";
4455 }
4456
Chris Lattnerc3aae252005-01-07 07:46:32 +00004457 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004458 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004459 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004460 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4461 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4462 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4463 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4464 else {
4465 cerr << "<APFloat(";
4466 CSDN->getValueAPF().convertToAPInt().dump();
4467 cerr << ")>";
4468 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004469 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004470 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004471 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004472 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004473 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004474 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004475 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004476 else
Bill Wendling832171c2006-12-07 20:04:42 +00004477 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004478 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004479 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004480 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004481 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004482 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004483 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004484 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004485 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004486 else
Bill Wendling832171c2006-12-07 20:04:42 +00004487 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004488 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004489 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004490 else
Bill Wendling832171c2006-12-07 20:04:42 +00004491 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004492 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004493 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004494 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4495 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004496 cerr << LBB->getName() << " ";
4497 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004498 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004499 if (G && R->getReg() &&
4500 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004501 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004502 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004503 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004504 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004505 } else if (const ExternalSymbolSDNode *ES =
4506 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004507 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004508 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4509 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004510 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004511 else
Dan Gohman69de1932008-02-06 22:27:42 +00004512 cerr << "<null>";
4513 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4514 if (M->MO.getValue())
4515 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4516 else
4517 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004518 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4519 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004520 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004521 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004522 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004523 const Value *SrcValue = LD->getSrcValue();
4524 int SrcOffset = LD->getSrcValueOffset();
4525 cerr << " <";
4526 if (SrcValue)
4527 cerr << SrcValue;
4528 else
4529 cerr << "null";
4530 cerr << ":" << SrcOffset << ">";
4531
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004532 bool doExt = true;
4533 switch (LD->getExtensionType()) {
4534 default: doExt = false; break;
4535 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004536 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004537 break;
4538 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004539 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004540 break;
4541 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004542 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004543 break;
4544 }
4545 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004546 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004547
Evan Cheng144d8f02006-11-09 17:55:04 +00004548 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004549 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004550 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004551 if (LD->isVolatile())
4552 cerr << " <volatile>";
4553 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004554 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004555 const Value *SrcValue = ST->getSrcValue();
4556 int SrcOffset = ST->getSrcValueOffset();
4557 cerr << " <";
4558 if (SrcValue)
4559 cerr << SrcValue;
4560 else
4561 cerr << "null";
4562 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004563
4564 if (ST->isTruncatingStore())
4565 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004566 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004567
4568 const char *AM = getIndexedModeName(ST->getAddressingMode());
4569 if (*AM)
4570 cerr << " " << AM;
4571 if (ST->isVolatile())
4572 cerr << " <volatile>";
4573 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004574 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004575}
4576
Chris Lattnerde202b32005-11-09 23:47:37 +00004577static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004578 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4579 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004580 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004581 else
Bill Wendling832171c2006-12-07 20:04:42 +00004582 cerr << "\n" << std::string(indent+2, ' ')
4583 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004584
Chris Lattnerea946cd2005-01-09 20:38:33 +00004585
Bill Wendling832171c2006-12-07 20:04:42 +00004586 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004587 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004588}
4589
Chris Lattnerc3aae252005-01-07 07:46:32 +00004590void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004591 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004592 std::vector<const SDNode*> Nodes;
4593 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4594 I != E; ++I)
4595 Nodes.push_back(I);
4596
Chris Lattner49d24712005-01-09 20:26:36 +00004597 std::sort(Nodes.begin(), Nodes.end());
4598
4599 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004600 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004601 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004602 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004603
Jim Laskey26f7fa72006-10-17 19:33:52 +00004604 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004605
Bill Wendling832171c2006-12-07 20:04:42 +00004606 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004607}
4608
Evan Chengd6594ae2006-09-12 21:00:35 +00004609const Type *ConstantPoolSDNode::getType() const {
4610 if (isMachineConstantPoolEntry())
4611 return Val.MachineCPVal->getType();
4612 return Val.ConstVal->getType();
4613}