blob: 1e626677f02062efede7ab124d112f0909363a69 [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:
Dan Gohmanb91c89d2008-04-14 18:43:25 +00001851 case ISD::MERGE_VALUES:
1852 return Operand; // Factor or merge of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001853 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001854 case ISD::FP_EXTEND:
1855 assert(MVT::isFloatingPoint(VT) &&
1856 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001857 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001858 if (Operand.getOpcode() == ISD::UNDEF)
1859 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001860 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001861 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001862 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1863 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001864 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001865 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1866 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001867 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1868 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1869 break;
1870 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001871 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1872 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001873 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001874 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1875 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001876 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001877 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001878 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001879 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001880 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1881 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001882 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001883 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1884 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001885 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1886 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1887 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1888 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001890 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1891 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001892 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001893 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1894 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001895 if (OpOpcode == ISD::TRUNCATE)
1896 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001897 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1898 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001899 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001900 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1901 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001902 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001903 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1904 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001905 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1906 else
1907 return Operand.Val->getOperand(0);
1908 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001909 break;
Chris Lattner35481892005-12-23 00:16:34 +00001910 case ISD::BIT_CONVERT:
1911 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001912 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001913 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001914 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001915 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1916 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001917 if (OpOpcode == ISD::UNDEF)
1918 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001919 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001920 case ISD::SCALAR_TO_VECTOR:
1921 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001922 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001923 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001924 if (OpOpcode == ISD::UNDEF)
1925 return getNode(ISD::UNDEF, VT);
1926 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1927 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1928 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1929 Operand.getConstantOperandVal(1) == 0 &&
1930 Operand.getOperand(0).getValueType() == VT)
1931 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001932 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001933 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001934 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1935 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001936 Operand.Val->getOperand(0));
1937 if (OpOpcode == ISD::FNEG) // --X -> X
1938 return Operand.Val->getOperand(0);
1939 break;
1940 case ISD::FABS:
1941 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1942 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1943 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001944 }
1945
Chris Lattner43247a12005-08-25 19:12:10 +00001946 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001947 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001948 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001949 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001950 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001951 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001952 void *IP = 0;
1953 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1954 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001955 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001956 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001957 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001958 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001959 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001960 AllNodes.push_back(N);
1961 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001962}
1963
Chris Lattner36019aa2005-04-18 03:48:41 +00001964
1965
Chris Lattnerc3aae252005-01-07 07:46:32 +00001966SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1967 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001968 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1969 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001970 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001971 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001972 case ISD::TokenFactor:
1973 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1974 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001975 // Fold trivial token factors.
1976 if (N1.getOpcode() == ISD::EntryToken) return N2;
1977 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001978 break;
Chris Lattner76365122005-01-16 02:23:22 +00001979 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001980 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1981 N1.getValueType() == VT && "Binary operator types must match!");
1982 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1983 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001984 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001985 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001986 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1987 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001988 break;
Chris Lattner76365122005-01-16 02:23:22 +00001989 case ISD::OR:
1990 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001991 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1992 N1.getValueType() == VT && "Binary operator types must match!");
1993 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1994 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001995 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001996 return N1;
1997 break;
Chris Lattner76365122005-01-16 02:23:22 +00001998 case ISD::UDIV:
1999 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002000 case ISD::MULHU:
2001 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002002 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2003 // fall through
2004 case ISD::ADD:
2005 case ISD::SUB:
2006 case ISD::MUL:
2007 case ISD::SDIV:
2008 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002009 case ISD::FADD:
2010 case ISD::FSUB:
2011 case ISD::FMUL:
2012 case ISD::FDIV:
2013 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002014 assert(N1.getValueType() == N2.getValueType() &&
2015 N1.getValueType() == VT && "Binary operator types must match!");
2016 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002017 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2018 assert(N1.getValueType() == VT &&
2019 MVT::isFloatingPoint(N1.getValueType()) &&
2020 MVT::isFloatingPoint(N2.getValueType()) &&
2021 "Invalid FCOPYSIGN!");
2022 break;
Chris Lattner76365122005-01-16 02:23:22 +00002023 case ISD::SHL:
2024 case ISD::SRA:
2025 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002026 case ISD::ROTL:
2027 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002028 assert(VT == N1.getValueType() &&
2029 "Shift operators return type must be the same as their first arg");
2030 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002031 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002032 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002033 case ISD::FP_ROUND_INREG: {
2034 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2035 assert(VT == N1.getValueType() && "Not an inreg round!");
2036 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2037 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002038 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2039 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002040 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002041 break;
2042 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002043 case ISD::FP_ROUND:
2044 assert(MVT::isFloatingPoint(VT) &&
2045 MVT::isFloatingPoint(N1.getValueType()) &&
2046 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2047 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002048 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002049 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002050 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002051 case ISD::AssertZext: {
2052 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2053 assert(VT == N1.getValueType() && "Not an inreg extend!");
2054 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2055 "Cannot *_EXTEND_INREG FP types");
2056 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2057 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002058 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002059 break;
2060 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002061 case ISD::SIGN_EXTEND_INREG: {
2062 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2063 assert(VT == N1.getValueType() && "Not an inreg extend!");
2064 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2065 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002066 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2067 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002068 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002069
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002070 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002071 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002072 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002073 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002074 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002075 return getConstant(Val, VT);
2076 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002077 break;
2078 }
2079 case ISD::EXTRACT_VECTOR_ELT:
2080 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2081
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002082 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2083 if (N1.getOpcode() == ISD::UNDEF)
2084 return getNode(ISD::UNDEF, VT);
2085
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002086 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2087 // expanding copies of large vectors from registers.
2088 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2089 N1.getNumOperands() > 0) {
2090 unsigned Factor =
2091 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2092 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2093 N1.getOperand(N2C->getValue() / Factor),
2094 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2095 }
2096
2097 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2098 // expanding large vector constants.
2099 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2100 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002101
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002102 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2103 // operations are lowered to scalars.
2104 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2105 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2106 if (IEC == N2C)
2107 return N1.getOperand(1);
2108 else
2109 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2110 }
2111 break;
2112 case ISD::EXTRACT_ELEMENT:
2113 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002114 assert(!MVT::isVector(N1.getValueType()) &&
2115 MVT::isInteger(N1.getValueType()) &&
2116 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2117 "EXTRACT_ELEMENT only applies to integers!");
2118
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002119 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2120 // 64-bit integers into 32-bit parts. Instead of building the extract of
2121 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2122 if (N1.getOpcode() == ISD::BUILD_PAIR)
2123 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002124
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002125 // EXTRACT_ELEMENT of a constant int is also very common.
2126 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002127 unsigned ElementSize = MVT::getSizeInBits(VT);
2128 unsigned Shift = ElementSize * N2C->getValue();
2129 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2130 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002131 }
2132 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002133 case ISD::EXTRACT_SUBVECTOR:
2134 if (N1.getValueType() == VT) // Trivial extraction.
2135 return N1;
2136 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002137 }
2138
2139 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002140 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002141 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002142 switch (Opcode) {
2143 case ISD::ADD: return getConstant(C1 + C2, VT);
2144 case ISD::SUB: return getConstant(C1 - C2, VT);
2145 case ISD::MUL: return getConstant(C1 * C2, VT);
2146 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002147 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002148 break;
2149 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002150 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002151 break;
2152 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002153 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002154 break;
2155 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002156 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002157 break;
2158 case ISD::AND : return getConstant(C1 & C2, VT);
2159 case ISD::OR : return getConstant(C1 | C2, VT);
2160 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002161 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002162 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2163 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2164 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2165 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002166 default: break;
2167 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002168 } else { // Cannonicalize constant to RHS if commutative
2169 if (isCommutativeBinOp(Opcode)) {
2170 std::swap(N1C, N2C);
2171 std::swap(N1, N2);
2172 }
2173 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002174 }
2175
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002176 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002177 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2178 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002179 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002180 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2181 // Cannonicalize constant to RHS if commutative
2182 std::swap(N1CFP, N2CFP);
2183 std::swap(N1, N2);
2184 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002185 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2186 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002187 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002188 case ISD::FADD:
2189 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002190 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002191 return getConstantFP(V1, VT);
2192 break;
2193 case ISD::FSUB:
2194 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2195 if (s!=APFloat::opInvalidOp)
2196 return getConstantFP(V1, VT);
2197 break;
2198 case ISD::FMUL:
2199 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2200 if (s!=APFloat::opInvalidOp)
2201 return getConstantFP(V1, VT);
2202 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002203 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002204 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2205 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2206 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002207 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002208 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002209 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2210 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2211 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002212 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002213 case ISD::FCOPYSIGN:
2214 V1.copySign(V2);
2215 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002216 default: break;
2217 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002218 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002219 }
Chris Lattner62b57722006-04-20 05:39:12 +00002220
2221 // Canonicalize an UNDEF to the RHS, even over a constant.
2222 if (N1.getOpcode() == ISD::UNDEF) {
2223 if (isCommutativeBinOp(Opcode)) {
2224 std::swap(N1, N2);
2225 } else {
2226 switch (Opcode) {
2227 case ISD::FP_ROUND_INREG:
2228 case ISD::SIGN_EXTEND_INREG:
2229 case ISD::SUB:
2230 case ISD::FSUB:
2231 case ISD::FDIV:
2232 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002233 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002234 return N1; // fold op(undef, arg2) -> undef
2235 case ISD::UDIV:
2236 case ISD::SDIV:
2237 case ISD::UREM:
2238 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002239 case ISD::SRL:
2240 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002241 if (!MVT::isVector(VT))
2242 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2243 // For vectors, we can't easily build an all zero vector, just return
2244 // the LHS.
2245 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002246 }
2247 }
2248 }
2249
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002250 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002251 if (N2.getOpcode() == ISD::UNDEF) {
2252 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002253 case ISD::XOR:
2254 if (N1.getOpcode() == ISD::UNDEF)
2255 // Handle undef ^ undef -> 0 special case. This is a common
2256 // idiom (misuse).
2257 return getConstant(0, VT);
2258 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002259 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002260 case ISD::ADDC:
2261 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002262 case ISD::SUB:
2263 case ISD::FADD:
2264 case ISD::FSUB:
2265 case ISD::FMUL:
2266 case ISD::FDIV:
2267 case ISD::FREM:
2268 case ISD::UDIV:
2269 case ISD::SDIV:
2270 case ISD::UREM:
2271 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002272 return N2; // fold op(arg1, undef) -> undef
2273 case ISD::MUL:
2274 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002275 case ISD::SRL:
2276 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002277 if (!MVT::isVector(VT))
2278 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2279 // For vectors, we can't easily build an all zero vector, just return
2280 // the LHS.
2281 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002282 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002283 if (!MVT::isVector(VT))
2284 return getConstant(MVT::getIntVTBitMask(VT), VT);
2285 // For vectors, we can't easily build an all one vector, just return
2286 // the LHS.
2287 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002288 case ISD::SRA:
2289 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002290 }
2291 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002292
Chris Lattner27e9b412005-05-11 18:57:39 +00002293 // Memoize this node if possible.
2294 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002295 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002296 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002297 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002298 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002299 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002300 void *IP = 0;
2301 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2302 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002303 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002304 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002305 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002306 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002307 }
2308
Chris Lattnerc3aae252005-01-07 07:46:32 +00002309 AllNodes.push_back(N);
2310 return SDOperand(N, 0);
2311}
2312
Chris Lattnerc3aae252005-01-07 07:46:32 +00002313SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2314 SDOperand N1, SDOperand N2, SDOperand N3) {
2315 // Perform various simplifications.
2316 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2317 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002318 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002319 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002320 // Use FoldSetCC to simplify SETCC's.
2321 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002322 if (Simp.Val) return Simp;
2323 break;
2324 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002325 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002326 if (N1C) {
2327 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002329 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002330 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002331 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002332
2333 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002335 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002336 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002337 if (N2C->getValue()) // Unconditional branch
2338 return getNode(ISD::BR, MVT::Other, N1, N3);
2339 else
2340 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002341 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002342 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002343 case ISD::VECTOR_SHUFFLE:
2344 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2345 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2346 N3.getOpcode() == ISD::BUILD_VECTOR &&
2347 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2348 "Illegal VECTOR_SHUFFLE node!");
2349 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002350 case ISD::BIT_CONVERT:
2351 // Fold bit_convert nodes from a type to themselves.
2352 if (N1.getValueType() == VT)
2353 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002354 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002355 }
2356
Chris Lattner43247a12005-08-25 19:12:10 +00002357 // Memoize node if it doesn't produce a flag.
2358 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002359 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002360 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002361 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002362 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002363 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002364 void *IP = 0;
2365 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2366 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002367 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002368 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002369 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002370 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002371 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 AllNodes.push_back(N);
2373 return SDOperand(N, 0);
2374}
2375
2376SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002377 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002378 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002379 SDOperand Ops[] = { N1, N2, N3, N4 };
2380 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002381}
2382
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002383SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2384 SDOperand N1, SDOperand N2, SDOperand N3,
2385 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002386 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2387 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002388}
2389
Dan Gohman707e0182008-04-12 04:36:06 +00002390/// getMemsetValue - Vectorized representation of the memset value
2391/// operand.
2392static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
2393 SelectionDAG &DAG) {
2394 MVT::ValueType CurVT = VT;
2395 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
2396 uint64_t Val = C->getValue() & 255;
2397 unsigned Shift = 8;
2398 while (CurVT != MVT::i8) {
2399 Val = (Val << Shift) | Val;
2400 Shift <<= 1;
2401 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2402 }
2403 return DAG.getConstant(Val, VT);
2404 } else {
2405 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2406 unsigned Shift = 8;
2407 while (CurVT != MVT::i8) {
2408 Value =
2409 DAG.getNode(ISD::OR, VT,
2410 DAG.getNode(ISD::SHL, VT, Value,
2411 DAG.getConstant(Shift, MVT::i8)), Value);
2412 Shift <<= 1;
2413 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2414 }
2415
2416 return Value;
2417 }
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002418}
2419
Dan Gohman707e0182008-04-12 04:36:06 +00002420/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2421/// used when a memcpy is turned into a memset when the source is a constant
2422/// string ptr.
2423static SDOperand getMemsetStringVal(MVT::ValueType VT,
2424 SelectionDAG &DAG,
2425 const TargetLowering &TLI,
2426 std::string &Str, unsigned Offset) {
2427 uint64_t Val = 0;
2428 unsigned MSB = MVT::getSizeInBits(VT) / 8;
2429 if (TLI.isLittleEndian())
2430 Offset = Offset + MSB - 1;
2431 for (unsigned i = 0; i != MSB; ++i) {
2432 Val = (Val << 8) | (unsigned char)Str[Offset];
2433 Offset += TLI.isLittleEndian() ? -1 : 1;
2434 }
2435 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002436}
2437
Dan Gohman707e0182008-04-12 04:36:06 +00002438/// getMemBasePlusOffset - Returns base and offset node for the
2439static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2440 SelectionDAG &DAG) {
2441 MVT::ValueType VT = Base.getValueType();
2442 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2443}
2444
2445/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2446/// to replace the memset / memcpy is below the threshold. It also returns the
2447/// types of the sequence of memory ops to perform memset / memcpy.
2448static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2449 unsigned Limit, uint64_t Size,
2450 unsigned Align,
2451 const TargetLowering &TLI) {
2452 MVT::ValueType VT;
2453
2454 if (TLI.allowsUnalignedMemoryAccesses()) {
2455 VT = MVT::i64;
2456 } else {
2457 switch (Align & 7) {
2458 case 0:
2459 VT = MVT::i64;
2460 break;
2461 case 4:
2462 VT = MVT::i32;
2463 break;
2464 case 2:
2465 VT = MVT::i16;
2466 break;
2467 default:
2468 VT = MVT::i8;
2469 break;
2470 }
2471 }
2472
2473 MVT::ValueType LVT = MVT::i64;
2474 while (!TLI.isTypeLegal(LVT))
2475 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2476 assert(MVT::isInteger(LVT));
2477
2478 if (VT > LVT)
2479 VT = LVT;
2480
2481 unsigned NumMemOps = 0;
2482 while (Size != 0) {
2483 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2484 while (VTSize > Size) {
2485 VT = (MVT::ValueType)((unsigned)VT - 1);
2486 VTSize >>= 1;
2487 }
2488 assert(MVT::isInteger(VT));
2489
2490 if (++NumMemOps > Limit)
2491 return false;
2492 MemOps.push_back(VT);
2493 Size -= VTSize;
2494 }
2495
2496 return true;
2497}
2498
2499static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2500 SDOperand Chain, SDOperand Dst,
2501 SDOperand Src, uint64_t Size,
2502 unsigned Align,
2503 bool AlwaysInline,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002504 const Value *DstSV, uint64_t DstOff,
2505 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002506 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2507
2508 // Expand memcpy to a series of store ops if the size operand falls below
2509 // a certain threshold.
2510 std::vector<MVT::ValueType> MemOps;
2511 uint64_t Limit = -1;
2512 if (!AlwaysInline)
2513 Limit = TLI.getMaxStoresPerMemcpy();
2514 if (!MeetsMaxMemopRequirement(MemOps, Limit, Size, Align, TLI))
2515 return SDOperand();
2516
2517 SmallVector<SDOperand, 8> OutChains;
2518
2519 unsigned NumMemOps = MemOps.size();
2520 unsigned SrcDelta = 0;
2521 GlobalAddressSDNode *G = NULL;
2522 std::string Str;
2523 bool CopyFromStr = false;
2524
2525 if (Src.getOpcode() == ISD::GlobalAddress)
2526 G = cast<GlobalAddressSDNode>(Src);
2527 else if (Src.getOpcode() == ISD::ADD &&
2528 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2529 Src.getOperand(1).getOpcode() == ISD::Constant) {
2530 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2531 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2532 }
2533 if (G) {
2534 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2535 if (GV && GV->isConstant()) {
2536 Str = GV->getStringValue(false);
2537 if (!Str.empty()) {
2538 CopyFromStr = true;
2539 SrcOff += SrcDelta;
2540 }
2541 }
2542 }
2543
2544 for (unsigned i = 0; i < NumMemOps; i++) {
2545 MVT::ValueType VT = MemOps[i];
2546 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2547 SDOperand Value, Store;
2548
2549 if (CopyFromStr) {
2550 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
2551 Store =
2552 DAG.getStore(Chain, Value,
2553 getMemBasePlusOffset(Dst, DstOff, DAG),
2554 DstSV, DstOff);
2555 } else {
2556 Value = DAG.getLoad(VT, Chain,
2557 getMemBasePlusOffset(Src, SrcOff, DAG),
2558 SrcSV, SrcOff, false, Align);
2559 Store =
2560 DAG.getStore(Chain, Value,
2561 getMemBasePlusOffset(Dst, DstOff, DAG),
2562 DstSV, DstOff, false, Align);
2563 }
2564 OutChains.push_back(Store);
2565 SrcOff += VTSize;
2566 DstOff += VTSize;
2567 }
2568
2569 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2570 &OutChains[0], OutChains.size());
2571}
2572
2573static SDOperand getMemsetStores(SelectionDAG &DAG,
2574 SDOperand Chain, SDOperand Dst,
2575 SDOperand Src, uint64_t Size,
2576 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002577 const Value *DstSV, uint64_t DstOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002578 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2579
2580 // Expand memset to a series of load/store ops if the size operand
2581 // falls below a certain threshold.
2582 std::vector<MVT::ValueType> MemOps;
2583 if (!MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
2584 Size, Align, TLI))
2585 return SDOperand();
2586
2587 SmallVector<SDOperand, 8> OutChains;
2588
2589 unsigned NumMemOps = MemOps.size();
2590 for (unsigned i = 0; i < NumMemOps; i++) {
2591 MVT::ValueType VT = MemOps[i];
2592 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2593 SDOperand Value = getMemsetValue(Src, VT, DAG);
2594 SDOperand Store = DAG.getStore(Chain, Value,
2595 getMemBasePlusOffset(Dst, DstOff, DAG),
2596 DstSV, DstOff);
2597 OutChains.push_back(Store);
2598 DstOff += VTSize;
2599 }
2600
2601 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2602 &OutChains[0], OutChains.size());
2603}
2604
2605SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002606 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002607 unsigned Align, bool AlwaysInline,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002608 const Value *DstSV, uint64_t DstOff,
2609 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002610
2611 // Check to see if we should lower the memcpy to loads and stores first.
2612 // For cases within the target-specified limits, this is the best choice.
2613 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2614 if (ConstantSize) {
2615 // Memcpy with size zero? Just return the original chain.
2616 if (ConstantSize->isNullValue())
2617 return Chain;
2618
2619 SDOperand Result =
2620 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2621 Align, false, DstSV, DstOff, SrcSV, SrcOff);
2622 if (Result.Val)
2623 return Result;
2624 }
2625
2626 // Then check to see if we should lower the memcpy with target-specific
2627 // code. If the target chooses to do this, this is the next best.
2628 SDOperand Result =
2629 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2630 AlwaysInline,
2631 DstSV, DstOff, SrcSV, SrcOff);
2632 if (Result.Val)
2633 return Result;
2634
2635 // If we really need inline code and the target declined to provide it,
2636 // use a (potentially long) sequence of loads and stores.
2637 if (AlwaysInline) {
2638 assert(ConstantSize && "AlwaysInline requires a constant size!");
2639 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2640 ConstantSize->getValue(), Align, true,
2641 DstSV, DstOff, SrcSV, SrcOff);
2642 }
2643
2644 // Emit a library call.
2645 TargetLowering::ArgListTy Args;
2646 TargetLowering::ArgListEntry Entry;
2647 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2648 Entry.Node = Dst; Args.push_back(Entry);
2649 Entry.Node = Src; Args.push_back(Entry);
2650 Entry.Node = Size; Args.push_back(Entry);
2651 std::pair<SDOperand,SDOperand> CallResult =
2652 TLI.LowerCallTo(Chain, Type::VoidTy,
2653 false, false, false, CallingConv::C, false,
2654 getExternalSymbol("memcpy", TLI.getPointerTy()),
2655 Args, *this);
2656 return CallResult.second;
2657}
2658
2659SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2660 SDOperand Src, SDOperand Size,
2661 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002662 const Value *DstSV, uint64_t DstOff,
2663 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002664
2665 // TODO: Optimize small memmove cases with simple loads and stores,
2666 // ensuring that all loads precede all stores. This can cause severe
2667 // register pressure, so targets should be careful with the size limit.
2668
2669 // Then check to see if we should lower the memmove with target-specific
2670 // code. If the target chooses to do this, this is the next best.
2671 SDOperand Result =
2672 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
2673 DstSV, DstOff, SrcSV, SrcOff);
2674 if (Result.Val)
2675 return Result;
2676
2677 // Emit a library call.
2678 TargetLowering::ArgListTy Args;
2679 TargetLowering::ArgListEntry Entry;
2680 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2681 Entry.Node = Dst; Args.push_back(Entry);
2682 Entry.Node = Src; Args.push_back(Entry);
2683 Entry.Node = Size; Args.push_back(Entry);
2684 std::pair<SDOperand,SDOperand> CallResult =
2685 TLI.LowerCallTo(Chain, Type::VoidTy,
2686 false, false, false, CallingConv::C, false,
2687 getExternalSymbol("memmove", TLI.getPointerTy()),
2688 Args, *this);
2689 return CallResult.second;
2690}
2691
2692SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2693 SDOperand Src, SDOperand Size,
2694 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002695 const Value *DstSV, uint64_t DstOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002696
2697 // Check to see if we should lower the memset to stores first.
2698 // For cases within the target-specified limits, this is the best choice.
2699 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2700 if (ConstantSize) {
2701 // Memset with size zero? Just return the original chain.
2702 if (ConstantSize->isNullValue())
2703 return Chain;
2704
2705 SDOperand Result =
2706 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
2707 DstSV, DstOff);
2708 if (Result.Val)
2709 return Result;
2710 }
2711
2712 // Then check to see if we should lower the memset with target-specific
2713 // code. If the target chooses to do this, this is the next best.
2714 SDOperand Result =
2715 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
2716 DstSV, DstOff);
2717 if (Result.Val)
2718 return Result;
2719
2720 // Emit a library call.
2721 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2722 TargetLowering::ArgListTy Args;
2723 TargetLowering::ArgListEntry Entry;
2724 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2725 Args.push_back(Entry);
2726 // Extend or truncate the argument to be an i32 value for the call.
2727 if (Src.getValueType() > MVT::i32)
2728 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2729 else
2730 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2731 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2732 Args.push_back(Entry);
2733 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2734 Args.push_back(Entry);
2735 std::pair<SDOperand,SDOperand> CallResult =
2736 TLI.LowerCallTo(Chain, Type::VoidTy,
2737 false, false, false, CallingConv::C, false,
2738 getExternalSymbol("memset", TLI.getPointerTy()),
2739 Args, *this);
2740 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002741}
2742
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002743SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002744 SDOperand Ptr, SDOperand Cmp,
2745 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002746 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002747 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2748 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002749 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002750 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002751 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2752 ID.AddInteger((unsigned int)VT);
2753 void* IP = 0;
2754 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2755 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002756 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002757 CSEMap.InsertNode(N, IP);
2758 AllNodes.push_back(N);
2759 return SDOperand(N, 0);
2760}
2761
2762SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002763 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002764 MVT::ValueType VT) {
2765 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2766 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002767 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002768 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002769 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002770 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2771 ID.AddInteger((unsigned int)VT);
2772 void* IP = 0;
2773 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2774 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002775 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002776 CSEMap.InsertNode(N, IP);
2777 AllNodes.push_back(N);
2778 return SDOperand(N, 0);
2779}
2780
Duncan Sands14ea39c2008-03-27 20:23:40 +00002781SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00002782SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2783 MVT::ValueType VT, SDOperand Chain,
2784 SDOperand Ptr, SDOperand Offset,
2785 const Value *SV, int SVOffset, MVT::ValueType EVT,
2786 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002787 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2788 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002789 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002790 Ty = MVT::getTypeForValueType(VT);
2791 } else if (SV) {
2792 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2793 assert(PT && "Value for load must be a pointer");
2794 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00002795 }
Dan Gohman575e2f42007-06-04 15:49:41 +00002796 assert(Ty && "Could not get type information for load");
2797 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2798 }
Evan Cheng466685d2006-10-09 20:57:25 +00002799
Duncan Sands14ea39c2008-03-27 20:23:40 +00002800 if (VT == EVT) {
2801 ExtType = ISD::NON_EXTLOAD;
2802 } else if (ExtType == ISD::NON_EXTLOAD) {
2803 assert(VT == EVT && "Non-extending load from different memory type!");
2804 } else {
2805 // Extending load.
2806 if (MVT::isVector(VT))
2807 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2808 else
2809 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2810 "Should only be an extending load, not truncating!");
2811 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2812 "Cannot sign/zero extend a FP/Vector load!");
2813 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2814 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00002815 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00002816
2817 bool Indexed = AM != ISD::UNINDEXED;
2818 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2819 "Unindexed load with an offset!");
2820
2821 SDVTList VTs = Indexed ?
2822 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2823 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002824 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002825 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002826 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00002827 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002828 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002829 ID.AddInteger(Alignment);
2830 ID.AddInteger(isVolatile);
2831 void *IP = 0;
2832 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2833 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002834 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
2835 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002836 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002837 AllNodes.push_back(N);
2838 return SDOperand(N, 0);
2839}
2840
Duncan Sands14ea39c2008-03-27 20:23:40 +00002841SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2842 SDOperand Chain, SDOperand Ptr,
2843 const Value *SV, int SVOffset,
2844 bool isVolatile, unsigned Alignment) {
2845 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002846 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
2847 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002848}
2849
2850SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2851 SDOperand Chain, SDOperand Ptr,
2852 const Value *SV,
2853 int SVOffset, MVT::ValueType EVT,
2854 bool isVolatile, unsigned Alignment) {
2855 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002856 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
2857 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002858}
2859
Evan Cheng144d8f02006-11-09 17:55:04 +00002860SDOperand
2861SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2862 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002863 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002864 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2865 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00002866 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
2867 LD->getChain(), Base, Offset, LD->getSrcValue(),
2868 LD->getSrcValueOffset(), LD->getMemoryVT(),
2869 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00002870}
2871
Jeff Cohend41b30d2006-11-05 19:31:28 +00002872SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002873 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002874 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002875 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002876
Dan Gohman575e2f42007-06-04 15:49:41 +00002877 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2878 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002879 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002880 Ty = MVT::getTypeForValueType(VT);
2881 } else if (SV) {
2882 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2883 assert(PT && "Value for store must be a pointer");
2884 Ty = PT->getElementType();
2885 }
2886 assert(Ty && "Could not get type information for store");
2887 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2888 }
Evan Chengad071e12006-10-05 22:57:11 +00002889 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002890 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002891 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002892 FoldingSetNodeID ID;
2893 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002894 ID.AddInteger(ISD::UNINDEXED);
2895 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002896 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002897 ID.AddInteger(Alignment);
2898 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002899 void *IP = 0;
2900 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2901 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002902 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002903 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002904 CSEMap.InsertNode(N, IP);
2905 AllNodes.push_back(N);
2906 return SDOperand(N, 0);
2907}
2908
Jeff Cohend41b30d2006-11-05 19:31:28 +00002909SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002910 SDOperand Ptr, const Value *SV,
2911 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002912 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002913 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002914
2915 if (VT == SVT)
2916 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002917
Duncan Sandsaf47b112007-10-16 09:56:48 +00002918 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2919 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002920 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2921 "Can't do FP-INT conversion!");
2922
Dan Gohman575e2f42007-06-04 15:49:41 +00002923 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2924 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002925 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002926 Ty = MVT::getTypeForValueType(VT);
2927 } else if (SV) {
2928 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2929 assert(PT && "Value for store must be a pointer");
2930 Ty = PT->getElementType();
2931 }
2932 assert(Ty && "Could not get type information for store");
2933 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2934 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002935 SDVTList VTs = getVTList(MVT::Other);
2936 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002937 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002938 FoldingSetNodeID ID;
2939 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002940 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002941 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002942 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002943 ID.AddInteger(Alignment);
2944 ID.AddInteger(isVolatile);
2945 void *IP = 0;
2946 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2947 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002948 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002949 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002950 CSEMap.InsertNode(N, IP);
2951 AllNodes.push_back(N);
2952 return SDOperand(N, 0);
2953}
2954
Evan Cheng144d8f02006-11-09 17:55:04 +00002955SDOperand
2956SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2957 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002958 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2959 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2960 "Store is already a indexed store!");
2961 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2962 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2963 FoldingSetNodeID ID;
2964 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2965 ID.AddInteger(AM);
2966 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002967 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002968 ID.AddInteger(ST->getAlignment());
2969 ID.AddInteger(ST->isVolatile());
2970 void *IP = 0;
2971 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2972 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002973 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002974 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002975 ST->getSrcValue(), ST->getSrcValueOffset(),
2976 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002977 CSEMap.InsertNode(N, IP);
2978 AllNodes.push_back(N);
2979 return SDOperand(N, 0);
2980}
2981
Nate Begemanacc398c2006-01-25 18:21:52 +00002982SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2983 SDOperand Chain, SDOperand Ptr,
2984 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002985 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002986 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002987}
2988
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002989SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002990 const SDOperand *Ops, unsigned NumOps) {
2991 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002992 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002993 case 1: return getNode(Opcode, VT, Ops[0]);
2994 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2995 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002996 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002997 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002998
Chris Lattneref847df2005-04-09 03:27:28 +00002999 switch (Opcode) {
3000 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003001 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003002 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003003 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3004 "LHS and RHS of condition must have same type!");
3005 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3006 "True and False arms of SelectCC must have same type!");
3007 assert(Ops[2].getValueType() == VT &&
3008 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003009 break;
3010 }
3011 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003012 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003013 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3014 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003015 break;
3016 }
Chris Lattneref847df2005-04-09 03:27:28 +00003017 }
3018
Chris Lattner385328c2005-05-14 07:42:29 +00003019 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003020 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003021 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003022 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003023 FoldingSetNodeID ID;
3024 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003025 void *IP = 0;
3026 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3027 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003028 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003029 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003030 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003031 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003032 }
Chris Lattneref847df2005-04-09 03:27:28 +00003033 AllNodes.push_back(N);
3034 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003035}
3036
Chris Lattner89c34632005-05-14 06:20:26 +00003037SDOperand SelectionDAG::getNode(unsigned Opcode,
3038 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003039 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003040 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3041 Ops, NumOps);
3042}
3043
3044SDOperand SelectionDAG::getNode(unsigned Opcode,
3045 const MVT::ValueType *VTs, unsigned NumVTs,
3046 const SDOperand *Ops, unsigned NumOps) {
3047 if (NumVTs == 1)
3048 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003049 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3050}
3051
3052SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3053 const SDOperand *Ops, unsigned NumOps) {
3054 if (VTList.NumVTs == 1)
3055 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003056
Chris Lattner5f056bf2005-07-10 01:55:33 +00003057 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003058 // FIXME: figure out how to safely handle things like
3059 // int foo(int x) { return 1 << (x & 255); }
3060 // int bar() { return foo(256); }
3061#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003062 case ISD::SRA_PARTS:
3063 case ISD::SRL_PARTS:
3064 case ISD::SHL_PARTS:
3065 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003066 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003067 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3068 else if (N3.getOpcode() == ISD::AND)
3069 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3070 // If the and is only masking out bits that cannot effect the shift,
3071 // eliminate the and.
3072 unsigned NumBits = MVT::getSizeInBits(VT)*2;
3073 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3074 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3075 }
3076 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003077#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003078 }
Chris Lattner89c34632005-05-14 06:20:26 +00003079
Chris Lattner43247a12005-08-25 19:12:10 +00003080 // Memoize the node unless it returns a flag.
3081 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003082 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003083 FoldingSetNodeID ID;
3084 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003085 void *IP = 0;
3086 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3087 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003088 if (NumOps == 1)
3089 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3090 else if (NumOps == 2)
3091 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3092 else if (NumOps == 3)
3093 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3094 else
3095 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003096 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003097 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003098 if (NumOps == 1)
3099 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3100 else if (NumOps == 2)
3101 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3102 else if (NumOps == 3)
3103 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3104 else
3105 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003106 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003107 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003108 return SDOperand(N, 0);
3109}
3110
Dan Gohman08ce9762007-10-08 15:49:58 +00003111SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
3112 return getNode(Opcode, VTList, 0, 0);
3113}
3114
3115SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3116 SDOperand N1) {
3117 SDOperand Ops[] = { N1 };
3118 return getNode(Opcode, VTList, Ops, 1);
3119}
3120
3121SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3122 SDOperand N1, SDOperand N2) {
3123 SDOperand Ops[] = { N1, N2 };
3124 return getNode(Opcode, VTList, Ops, 2);
3125}
3126
3127SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3128 SDOperand N1, SDOperand N2, SDOperand N3) {
3129 SDOperand Ops[] = { N1, N2, N3 };
3130 return getNode(Opcode, VTList, Ops, 3);
3131}
3132
3133SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3134 SDOperand N1, SDOperand N2, SDOperand N3,
3135 SDOperand N4) {
3136 SDOperand Ops[] = { N1, N2, N3, N4 };
3137 return getNode(Opcode, VTList, Ops, 4);
3138}
3139
3140SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3141 SDOperand N1, SDOperand N2, SDOperand N3,
3142 SDOperand N4, SDOperand N5) {
3143 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3144 return getNode(Opcode, VTList, Ops, 5);
3145}
3146
Chris Lattner70046e92006-08-15 17:46:01 +00003147SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003148 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003149}
3150
Chris Lattner70046e92006-08-15 17:46:01 +00003151SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00003152 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3153 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003154 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003155 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003156 }
3157 std::vector<MVT::ValueType> V;
3158 V.push_back(VT1);
3159 V.push_back(VT2);
3160 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003161 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003162}
Chris Lattner70046e92006-08-15 17:46:01 +00003163SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
3164 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003165 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003166 E = VTList.end(); I != E; ++I) {
3167 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3168 (*I)[2] == VT3)
3169 return makeVTList(&(*I)[0], 3);
3170 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003171 std::vector<MVT::ValueType> V;
3172 V.push_back(VT1);
3173 V.push_back(VT2);
3174 V.push_back(VT3);
3175 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003176 return makeVTList(&(*VTList.begin())[0], 3);
3177}
3178
3179SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
3180 switch (NumVTs) {
3181 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003182 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003183 case 2: return getVTList(VTs[0], VTs[1]);
3184 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3185 default: break;
3186 }
3187
3188 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3189 E = VTList.end(); I != E; ++I) {
3190 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3191
3192 bool NoMatch = false;
3193 for (unsigned i = 2; i != NumVTs; ++i)
3194 if (VTs[i] != (*I)[i]) {
3195 NoMatch = true;
3196 break;
3197 }
3198 if (!NoMatch)
3199 return makeVTList(&*I->begin(), NumVTs);
3200 }
3201
3202 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
3203 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003204}
3205
3206
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003207/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3208/// specified operands. If the resultant node already exists in the DAG,
3209/// this does not modify the specified node, instead it returns the node that
3210/// already exists. If the resultant node does not exist in the DAG, the
3211/// input node is returned. As a degenerate case, if you specify the same
3212/// input operands as the node already has, the input node is returned.
3213SDOperand SelectionDAG::
3214UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3215 SDNode *N = InN.Val;
3216 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3217
3218 // Check to see if there is no change.
3219 if (Op == N->getOperand(0)) return InN;
3220
3221 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003222 void *InsertPos = 0;
3223 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3224 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003225
3226 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003227 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003228 RemoveNodeFromCSEMaps(N);
3229
3230 // Now we update the operands.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003231 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003232 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003233 N->OperandList[0].setUser(N);
3234 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003235
3236 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003237 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003238 return InN;
3239}
3240
3241SDOperand SelectionDAG::
3242UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3243 SDNode *N = InN.Val;
3244 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3245
3246 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003247 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3248 return InN; // No operands changed, just return the input node.
3249
3250 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003251 void *InsertPos = 0;
3252 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3253 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003254
3255 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003256 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003257 RemoveNodeFromCSEMaps(N);
3258
3259 // Now we update the operands.
3260 if (N->OperandList[0] != Op1) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003261 N->OperandList[0].Val->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003262 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003263 N->OperandList[0].setUser(N);
3264 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003265 }
3266 if (N->OperandList[1] != Op2) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003267 N->OperandList[1].Val->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003268 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003269 N->OperandList[1].setUser(N);
3270 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003271 }
3272
3273 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003274 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003275 return InN;
3276}
3277
3278SDOperand SelectionDAG::
3279UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003280 SDOperand Ops[] = { Op1, Op2, Op3 };
3281 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003282}
3283
3284SDOperand SelectionDAG::
3285UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3286 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003287 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3288 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003289}
3290
3291SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003292UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3293 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003294 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3295 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003296}
3297
Chris Lattner809ec112006-01-28 10:09:25 +00003298SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003299UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003300 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003301 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003302 "Update with wrong number of operands");
3303
3304 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003305 bool AnyChange = false;
3306 for (unsigned i = 0; i != NumOps; ++i) {
3307 if (Ops[i] != N->getOperand(i)) {
3308 AnyChange = true;
3309 break;
3310 }
3311 }
3312
3313 // No operands changed, just return the input node.
3314 if (!AnyChange) return InN;
3315
3316 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003317 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003318 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003319 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003320
3321 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003322 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003323 RemoveNodeFromCSEMaps(N);
3324
3325 // Now we update the operands.
3326 for (unsigned i = 0; i != NumOps; ++i) {
3327 if (N->OperandList[i] != Ops[i]) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003328 N->OperandList[i].Val->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003329 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003330 N->OperandList[i].setUser(N);
3331 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003332 }
3333 }
3334
3335 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003336 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003337 return InN;
3338}
3339
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003340/// MorphNodeTo - This frees the operands of the current node, resets the
3341/// opcode, types, and operands to the specified value. This should only be
3342/// used by the SelectionDAG class.
3343void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3344 const SDOperand *Ops, unsigned NumOps) {
3345 NodeType = Opc;
3346 ValueList = L.VTs;
3347 NumValues = L.NumVTs;
3348
3349 // Clear the operands list, updating used nodes to remove this from their
3350 // use list.
3351 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levensteindc1adac2008-04-07 10:06:32 +00003352 I->Val->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003353
3354 // If NumOps is larger than the # of operands we currently have, reallocate
3355 // the operand list.
3356 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003357 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003358 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003359 }
Chris Lattner63e3f142007-02-04 07:28:00 +00003360 OperandList = new SDOperand[NumOps];
3361 OperandsNeedDelete = true;
3362 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003363
3364 // Assign the new operands.
3365 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003366
3367 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3368 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003369 OperandList[i].setUser(this);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003370 SDNode *N = OperandList[i].Val;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003371 N->addUser(i, this);
3372 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003373 }
3374}
Chris Lattner149c58c2005-08-16 18:17:10 +00003375
3376/// SelectNodeTo - These are used for target selectors to *mutate* the
3377/// specified node to have the specified return type, Target opcode, and
3378/// operands. Note that target opcodes are stored as
3379/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003380///
3381/// Note that SelectNodeTo returns the resultant node. If there is already a
3382/// node of the specified opcode and operands, it returns that node instead of
3383/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003384SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3385 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003386 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003387 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003388 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003389 void *IP = 0;
3390 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003391 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003392
Chris Lattner7651fa42005-08-24 23:00:29 +00003393 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003394
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003395 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003396
Chris Lattner4a283e92006-08-11 18:38:11 +00003397 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003398 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003399}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003400
Evan Cheng95514ba2006-08-26 08:00:10 +00003401SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3402 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003403 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003404 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003405 SDOperand Ops[] = { Op1 };
3406
Jim Laskey583bd472006-10-27 23:46:08 +00003407 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003408 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003409 void *IP = 0;
3410 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003411 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003412
Chris Lattner149c58c2005-08-16 18:17:10 +00003413 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003414 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003415 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003416 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003417}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003418
Evan Cheng95514ba2006-08-26 08:00:10 +00003419SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3420 MVT::ValueType VT, SDOperand Op1,
3421 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003422 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003423 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003424 SDOperand Ops[] = { Op1, Op2 };
3425
Jim Laskey583bd472006-10-27 23:46:08 +00003426 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003427 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003428 void *IP = 0;
3429 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003430 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003431
Chris Lattner149c58c2005-08-16 18:17:10 +00003432 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003433
Chris Lattner63e3f142007-02-04 07:28:00 +00003434 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003435
Chris Lattnera5682852006-08-07 23:03:03 +00003436 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003437 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003438}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003439
Evan Cheng95514ba2006-08-26 08:00:10 +00003440SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3441 MVT::ValueType VT, SDOperand Op1,
3442 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003443 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003444 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003445 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003446 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003447 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003448 void *IP = 0;
3449 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003450 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003451
Chris Lattner149c58c2005-08-16 18:17:10 +00003452 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003453
Chris Lattner63e3f142007-02-04 07:28:00 +00003454 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003455
Chris Lattnera5682852006-08-07 23:03:03 +00003456 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003457 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003458}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003459
Evan Cheng95514ba2006-08-26 08:00:10 +00003460SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003461 MVT::ValueType VT, const SDOperand *Ops,
3462 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003463 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003464 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003465 FoldingSetNodeID ID;
3466 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003467 void *IP = 0;
3468 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003469 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003470
Chris Lattner6b09a292005-08-21 18:49:33 +00003471 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003472 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003473
Chris Lattnera5682852006-08-07 23:03:03 +00003474 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003475 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003476}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003477
Evan Cheng95514ba2006-08-26 08:00:10 +00003478SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3479 MVT::ValueType VT1, MVT::ValueType VT2,
3480 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003481 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003482 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003483 SDOperand Ops[] = { Op1, Op2 };
3484 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003485 void *IP = 0;
3486 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003487 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003488
Chris Lattner0fb094f2005-11-19 01:44:53 +00003489 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003490 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003491 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003492 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003493}
3494
Evan Cheng95514ba2006-08-26 08:00:10 +00003495SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3496 MVT::ValueType VT1, MVT::ValueType VT2,
3497 SDOperand Op1, SDOperand Op2,
3498 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003499 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003500 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003501 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003502 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003503 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003504 void *IP = 0;
3505 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003506 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003507
Chris Lattner0fb094f2005-11-19 01:44:53 +00003508 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003509
Chris Lattner63e3f142007-02-04 07:28:00 +00003510 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003511 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003512 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003513}
3514
Chris Lattner0fb094f2005-11-19 01:44:53 +00003515
Evan Cheng6ae46c42006-02-09 07:15:23 +00003516/// getTargetNode - These are used for target selectors to create a new node
3517/// with specified return type(s), target opcode, and operands.
3518///
3519/// Note that getTargetNode returns the resultant node. If there is already a
3520/// node of the specified opcode and operands, it returns that node instead of
3521/// the current one.
3522SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3523 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3524}
3525SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3526 SDOperand Op1) {
3527 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3528}
3529SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3530 SDOperand Op1, SDOperand Op2) {
3531 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3532}
3533SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003534 SDOperand Op1, SDOperand Op2,
3535 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003536 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3537}
3538SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003539 const SDOperand *Ops, unsigned NumOps) {
3540 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003541}
3542SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003543 MVT::ValueType VT2) {
3544 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3545 SDOperand Op;
3546 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3547}
3548SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003549 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003550 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003551 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003552}
3553SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003554 MVT::ValueType VT2, SDOperand Op1,
3555 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003556 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003557 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003558 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003559}
3560SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003561 MVT::ValueType VT2, SDOperand Op1,
3562 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003563 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003564 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003565 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003566}
Evan Cheng694481e2006-08-27 08:08:54 +00003567SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3568 MVT::ValueType VT2,
3569 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003570 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003571 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003572}
3573SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3574 MVT::ValueType VT2, MVT::ValueType VT3,
3575 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003576 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003577 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003578 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003579}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003580SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3581 MVT::ValueType VT2, MVT::ValueType VT3,
3582 SDOperand Op1, SDOperand Op2,
3583 SDOperand Op3) {
3584 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3585 SDOperand Ops[] = { Op1, Op2, Op3 };
3586 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3587}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003588SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003589 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003590 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003591 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3592 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003593}
Evan Cheng05e69c12007-09-12 23:39:49 +00003594SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3595 MVT::ValueType VT2, MVT::ValueType VT3,
3596 MVT::ValueType VT4,
3597 const SDOperand *Ops, unsigned NumOps) {
3598 std::vector<MVT::ValueType> VTList;
3599 VTList.push_back(VT1);
3600 VTList.push_back(VT2);
3601 VTList.push_back(VT3);
3602 VTList.push_back(VT4);
3603 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3604 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3605}
Evan Cheng39305cf2007-10-05 01:10:49 +00003606SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3607 std::vector<MVT::ValueType> &ResultTys,
3608 const SDOperand *Ops, unsigned NumOps) {
3609 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3610 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3611 Ops, NumOps).Val;
3612}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003613
Evan Cheng08b11732008-03-22 01:55:50 +00003614/// getNodeIfExists - Get the specified node if it's already available, or
3615/// else return NULL.
3616SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
3617 const SDOperand *Ops, unsigned NumOps) {
3618 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3619 FoldingSetNodeID ID;
3620 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3621 void *IP = 0;
3622 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3623 return E;
3624 }
3625 return NULL;
3626}
3627
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003628
Evan Cheng99157a02006-08-07 22:13:29 +00003629/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003630/// This can cause recursive merging of nodes in the DAG.
3631///
Chris Lattner11d049c2008-02-03 03:35:22 +00003632/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003633///
Chris Lattner11d049c2008-02-03 03:35:22 +00003634void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003635 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003636 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003637 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003638 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003639 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003640
Chris Lattner8b8749f2005-08-17 19:00:20 +00003641 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003642 SDNode::use_iterator UI = From->use_begin();
3643 SDNode *U = UI->getUser();
3644
Chris Lattner8b8749f2005-08-17 19:00:20 +00003645 // This node is about to morph, remove its old self from the CSE maps.
3646 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003647 int operandNum = 0;
3648 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3649 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003650 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003651 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003652 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003653 I->setUser(U);
3654 To.Val->addUser(operandNum, U);
3655 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003656
3657 // Now that we have modified U, add it back to the CSE maps. If it already
3658 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003659 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003660 ReplaceAllUsesWith(U, Existing, UpdateListener);
3661 // U is now dead. Inform the listener if it exists and delete it.
3662 if (UpdateListener)
3663 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003664 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003665 } else {
3666 // If the node doesn't already exist, we updated it. Inform a listener if
3667 // it exists.
3668 if (UpdateListener)
3669 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003670 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003671 }
3672}
3673
3674/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3675/// This can cause recursive merging of nodes in the DAG.
3676///
3677/// This version assumes From/To have matching types and numbers of result
3678/// values.
3679///
Chris Lattner1e111c72005-09-07 05:37:01 +00003680void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003681 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003682 assert(From != To && "Cannot replace uses of with self");
3683 assert(From->getNumValues() == To->getNumValues() &&
3684 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003685 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003686 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3687 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003688
3689 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003690 SDNode::use_iterator UI = From->use_begin();
3691 SDNode *U = UI->getUser();
3692
Chris Lattner8b52f212005-08-26 18:36:28 +00003693 // This node is about to morph, remove its old self from the CSE maps.
3694 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003695 int operandNum = 0;
3696 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3697 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003698 if (I->Val == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003699 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003700 I->Val = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003701 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003702 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003703
Chris Lattner8b8749f2005-08-17 19:00:20 +00003704 // Now that we have modified U, add it back to the CSE maps. If it already
3705 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003706 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003707 ReplaceAllUsesWith(U, Existing, UpdateListener);
3708 // U is now dead. Inform the listener if it exists and delete it.
3709 if (UpdateListener)
3710 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003711 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003712 } else {
3713 // If the node doesn't already exist, we updated it. Inform a listener if
3714 // it exists.
3715 if (UpdateListener)
3716 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003717 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003718 }
3719}
3720
Chris Lattner8b52f212005-08-26 18:36:28 +00003721/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3722/// This can cause recursive merging of nodes in the DAG.
3723///
3724/// This version can replace From with any result values. To must match the
3725/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003726void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003727 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003728 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003729 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003730 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003731
3732 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003733 SDNode::use_iterator UI = From->use_begin();
3734 SDNode *U = UI->getUser();
3735
Chris Lattner7b2880c2005-08-24 22:44:39 +00003736 // This node is about to morph, remove its old self from the CSE maps.
3737 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003738 int operandNum = 0;
3739 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3740 I != E; ++I, ++operandNum)
Chris Lattner65113b22005-11-08 22:07:03 +00003741 if (I->Val == From) {
3742 const SDOperand &ToOp = To[I->ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003743 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003744 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003745 I->setUser(U);
3746 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003747 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003748
Chris Lattner7b2880c2005-08-24 22:44:39 +00003749 // Now that we have modified U, add it back to the CSE maps. If it already
3750 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003751 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003752 ReplaceAllUsesWith(U, Existing, UpdateListener);
3753 // U is now dead. Inform the listener if it exists and delete it.
3754 if (UpdateListener)
3755 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003756 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003757 } else {
3758 // If the node doesn't already exist, we updated it. Inform a listener if
3759 // it exists.
3760 if (UpdateListener)
3761 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003762 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003763 }
3764}
3765
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003766namespace {
3767 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3768 /// any deleted nodes from the set passed into its constructor and recursively
3769 /// notifies another update listener if specified.
3770 class ChainedSetUpdaterListener :
3771 public SelectionDAG::DAGUpdateListener {
3772 SmallSetVector<SDNode*, 16> &Set;
3773 SelectionDAG::DAGUpdateListener *Chain;
3774 public:
3775 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3776 SelectionDAG::DAGUpdateListener *chain)
3777 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00003778
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003779 virtual void NodeDeleted(SDNode *N) {
3780 Set.remove(N);
3781 if (Chain) Chain->NodeDeleted(N);
3782 }
3783 virtual void NodeUpdated(SDNode *N) {
3784 if (Chain) Chain->NodeUpdated(N);
3785 }
3786 };
3787}
3788
Chris Lattner012f2412006-02-17 21:58:01 +00003789/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3790/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003791/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003792void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003793 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003794 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003795
Chris Lattner012f2412006-02-17 21:58:01 +00003796 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003797 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003798 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003799 return;
3800 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003801
3802 if (From.use_empty()) return;
3803
Chris Lattnercf5640b2007-02-04 00:14:31 +00003804 // Get all of the users of From.Val. We want these in a nice,
3805 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003806 SmallSetVector<SDNode*, 16> Users;
3807 for (SDNode::use_iterator UI = From.Val->use_begin(),
3808 E = From.Val->use_end(); UI != E; ++UI) {
3809 SDNode *User = UI->getUser();
3810 if (!Users.count(User))
3811 Users.insert(User);
3812 }
Chris Lattner012f2412006-02-17 21:58:01 +00003813
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003814 // When one of the recursive merges deletes nodes from the graph, we need to
3815 // make sure that UpdateListener is notified *and* that the node is removed
3816 // from Users if present. CSUL does this.
3817 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003818
Chris Lattner012f2412006-02-17 21:58:01 +00003819 while (!Users.empty()) {
3820 // We know that this user uses some value of From. If it is the right
3821 // value, update it.
3822 SDNode *User = Users.back();
3823 Users.pop_back();
3824
Chris Lattner01d029b2007-10-15 06:10:22 +00003825 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003826 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00003827 for (; Op != E; ++Op)
3828 if (*Op == From) break;
3829
3830 // If there are no matches, the user must use some other result of From.
3831 if (Op == E) continue;
3832
3833 // Okay, we know this user needs to be updated. Remove its old self
3834 // from the CSE maps.
3835 RemoveNodeFromCSEMaps(User);
3836
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003837 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003838 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003839 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003840 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00003841 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003842 Op->setUser(User);
3843 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00003844 }
3845 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003846
3847 // Now that we have modified User, add it back to the CSE maps. If it
3848 // already exists there, recursively merge the results together.
3849 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003850 if (!Existing) {
3851 if (UpdateListener) UpdateListener->NodeUpdated(User);
3852 continue; // Continue on to next user.
3853 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003854
3855 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003856 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003857 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003858 // can cause deletion of nodes that used the old value. To handle this, we
3859 // use CSUL to remove them from the Users set.
3860 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003861
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003862 // User is now dead. Notify a listener if present.
3863 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003864 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003865 }
3866}
3867
Chris Lattner7b2880c2005-08-24 22:44:39 +00003868
Evan Chenge6f35d82006-08-01 08:20:41 +00003869/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3870/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003871unsigned SelectionDAG::AssignNodeIds() {
3872 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003873 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3874 SDNode *N = I;
3875 N->setNodeId(Id++);
3876 }
3877 return Id;
3878}
3879
Evan Chenge6f35d82006-08-01 08:20:41 +00003880/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003881/// based on their topological order. It returns the maximum id and a vector
3882/// of the SDNodes* in assigned order by reference.
3883unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003884 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003885 std::vector<unsigned> InDegree(DAGSize);
3886 std::vector<SDNode*> Sources;
3887
3888 // Use a two pass approach to avoid using a std::map which is slow.
3889 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003890 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3891 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003892 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003893 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003894 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003895 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003896 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003897 }
3898
Evan Cheng99157a02006-08-07 22:13:29 +00003899 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003900 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003901 SDNode *N = Sources.back();
3902 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003903 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003904 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3905 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003906 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003907 if (Degree == 0)
3908 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003909 }
3910 }
3911
Evan Chengc384d6c2006-08-02 22:00:34 +00003912 // Second pass, assign the actual topological order as node ids.
3913 Id = 0;
3914 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3915 TI != TE; ++TI)
3916 (*TI)->setNodeId(Id++);
3917
3918 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003919}
3920
3921
Evan Cheng091cba12006-07-27 06:39:06 +00003922
Jim Laskey58b968b2005-08-17 20:08:02 +00003923//===----------------------------------------------------------------------===//
3924// SDNode Class
3925//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003926
Chris Lattner917d2c92006-07-19 00:00:37 +00003927// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003928void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003929void UnarySDNode::ANCHOR() {}
3930void BinarySDNode::ANCHOR() {}
3931void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003932void HandleSDNode::ANCHOR() {}
3933void StringSDNode::ANCHOR() {}
3934void ConstantSDNode::ANCHOR() {}
3935void ConstantFPSDNode::ANCHOR() {}
3936void GlobalAddressSDNode::ANCHOR() {}
3937void FrameIndexSDNode::ANCHOR() {}
3938void JumpTableSDNode::ANCHOR() {}
3939void ConstantPoolSDNode::ANCHOR() {}
3940void BasicBlockSDNode::ANCHOR() {}
3941void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003942void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003943void RegisterSDNode::ANCHOR() {}
3944void ExternalSymbolSDNode::ANCHOR() {}
3945void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00003946void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003947void VTSDNode::ANCHOR() {}
3948void LoadSDNode::ANCHOR() {}
3949void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003950void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003951
Chris Lattner48b85922007-02-04 02:41:42 +00003952HandleSDNode::~HandleSDNode() {
3953 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003954 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003955}
3956
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003957GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3958 MVT::ValueType VT, int o)
3959 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003960 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003961 // Thread Local
3962 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3963 // Non Thread Local
3964 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3965 getSDVTList(VT)), Offset(o) {
3966 TheGlobal = const_cast<GlobalValue*>(GA);
3967}
Chris Lattner48b85922007-02-04 02:41:42 +00003968
Dan Gohman36b5c132008-04-07 19:35:22 +00003969/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00003970/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00003971MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman69de1932008-02-06 22:27:42 +00003972 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3973 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00003974 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
3975 MachineMemOperand::MOStore;
3976 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00003977
3978 // Check if the load references a frame index, and does not have
3979 // an SV attached.
3980 const FrameIndexSDNode *FI =
3981 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3982 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00003983 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
3984 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003985 else
Dan Gohman36b5c132008-04-07 19:35:22 +00003986 return MachineMemOperand(getSrcValue(), Flags,
3987 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003988}
3989
Jim Laskey583bd472006-10-27 23:46:08 +00003990/// Profile - Gather unique data for the node.
3991///
3992void SDNode::Profile(FoldingSetNodeID &ID) {
3993 AddNodeIDNode(ID, this);
3994}
3995
Chris Lattnera3255112005-11-08 23:30:28 +00003996/// getValueTypeList - Return a pointer to the specified value type.
3997///
Dan Gohman547ca532008-02-08 03:26:46 +00003998const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003999 if (MVT::isExtendedVT(VT)) {
4000 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004001 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004002 } else {
4003 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
4004 VTs[VT] = VT;
4005 return &VTs[VT];
4006 }
Chris Lattnera3255112005-11-08 23:30:28 +00004007}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004008
Chris Lattner5c884562005-01-12 18:37:47 +00004009/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4010/// indicated value. This method ignores uses of other values defined by this
4011/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004012bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004013 assert(Value < getNumValues() && "Bad value!");
4014
4015 // If there is only one value, this is easy.
4016 if (getNumValues() == 1)
4017 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004018 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004019
Evan Cheng4ee62112006-02-05 06:29:23 +00004020 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004021
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004022 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004023
Roman Levensteindc1adac2008-04-07 10:06:32 +00004024 // TODO: Only iterate over uses of a given value of the node
4025 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4026 if (*UI == TheValue) {
4027 if (NUses == 0)
4028 return false;
4029 --NUses;
4030 }
Chris Lattner5c884562005-01-12 18:37:47 +00004031 }
4032
4033 // Found exactly the right number of uses?
4034 return NUses == 0;
4035}
4036
4037
Evan Cheng33d55952007-08-02 05:29:38 +00004038/// hasAnyUseOfValue - Return true if there are any use of the indicated
4039/// value. This method ignores uses of other values defined by this operation.
4040bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4041 assert(Value < getNumValues() && "Bad value!");
4042
Dan Gohman30359592008-01-29 13:02:09 +00004043 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004044
4045 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4046
4047 SmallPtrSet<SDNode*, 32> UsersHandled;
4048
Roman Levensteindc1adac2008-04-07 10:06:32 +00004049 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4050 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004051 if (User->getNumOperands() == 1 ||
4052 UsersHandled.insert(User)) // First time we've seen this?
4053 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4054 if (User->getOperand(i) == TheValue) {
4055 return true;
4056 }
4057 }
4058
4059 return false;
4060}
4061
4062
Evan Cheng917be682008-03-04 00:41:45 +00004063/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004064///
Evan Cheng917be682008-03-04 00:41:45 +00004065bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004066 bool Seen = false;
4067 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004068 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004069 if (User == this)
4070 Seen = true;
4071 else
4072 return false;
4073 }
4074
4075 return Seen;
4076}
4077
Evan Chenge6e97e62006-11-03 07:31:32 +00004078/// isOperand - Return true if this node is an operand of N.
4079///
Roman Levensteindc1adac2008-04-07 10:06:32 +00004080bool SDOperandImpl::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004081 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4082 if (*this == N->getOperand(i))
4083 return true;
4084 return false;
4085}
4086
Evan Cheng917be682008-03-04 00:41:45 +00004087bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004088 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
4089 if (this == N->OperandList[i].Val)
4090 return true;
4091 return false;
4092}
Evan Cheng4ee62112006-02-05 06:29:23 +00004093
Chris Lattner572dee72008-01-16 05:49:24 +00004094/// reachesChainWithoutSideEffects - Return true if this operand (which must
4095/// be a chain) reaches the specified operand without crossing any
4096/// side-effecting instructions. In practice, this looks through token
4097/// factors and non-volatile loads. In order to remain efficient, this only
4098/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004099bool SDOperandImpl::reachesChainWithoutSideEffects(SDOperandImpl Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004100 unsigned Depth) const {
4101 if (*this == Dest) return true;
4102
4103 // Don't search too deeply, we just want to be able to see through
4104 // TokenFactor's etc.
4105 if (Depth == 0) return false;
4106
4107 // If this is a token factor, all inputs to the TF happen in parallel. If any
4108 // of the operands of the TF reach dest, then we can do the xform.
4109 if (getOpcode() == ISD::TokenFactor) {
4110 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4111 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4112 return true;
4113 return false;
4114 }
4115
4116 // Loads don't have side effects, look through them.
4117 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4118 if (!Ld->isVolatile())
4119 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4120 }
4121 return false;
4122}
4123
4124
Evan Chengc5fc57d2006-11-03 03:05:24 +00004125static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004126 SmallPtrSet<SDNode *, 32> &Visited) {
4127 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004128 return;
4129
4130 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4131 SDNode *Op = N->getOperand(i).Val;
4132 if (Op == P) {
4133 found = true;
4134 return;
4135 }
4136 findPredecessor(Op, P, found, Visited);
4137 }
4138}
4139
Evan Cheng917be682008-03-04 00:41:45 +00004140/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004141/// is either an operand of N or it can be reached by recursively traversing
4142/// up the operands.
4143/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004144bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004145 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004146 bool found = false;
4147 findPredecessor(N, this, found, Visited);
4148 return found;
4149}
4150
Evan Chengc5484282006-10-04 00:56:09 +00004151uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4152 assert(Num < NumOperands && "Invalid child # of SDNode!");
4153 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4154}
4155
Reid Spencer577cc322007-04-01 07:32:19 +00004156std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004157 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004158 default:
4159 if (getOpcode() < ISD::BUILTIN_OP_END)
4160 return "<<Unknown DAG Node>>";
4161 else {
Evan Cheng72261582005-12-20 06:22:03 +00004162 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004163 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004164 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004165 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004166
Evan Cheng72261582005-12-20 06:22:03 +00004167 TargetLowering &TLI = G->getTargetLoweringInfo();
4168 const char *Name =
4169 TLI.getTargetNodeName(getOpcode());
4170 if (Name) return Name;
4171 }
4172
4173 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004174 }
4175
Evan Cheng27b7db52008-03-08 00:58:38 +00004176 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004177 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004178 case ISD::ATOMIC_LCS: return "AtomicLCS";
4179 case ISD::ATOMIC_LAS: return "AtomicLAS";
4180 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004181 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004182 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004183 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004184 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004185 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004186 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004187 case ISD::AssertSext: return "AssertSext";
4188 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004189
4190 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004191 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004192 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004193 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004194 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004195
4196 case ISD::Constant: return "Constant";
4197 case ISD::ConstantFP: return "ConstantFP";
4198 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004199 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004200 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004201 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004202 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004203 case ISD::RETURNADDR: return "RETURNADDR";
4204 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004205 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004206 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4207 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004208 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004209 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004210 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004211 case ISD::INTRINSIC_WO_CHAIN: {
4212 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4213 return Intrinsic::getName((Intrinsic::ID)IID);
4214 }
4215 case ISD::INTRINSIC_VOID:
4216 case ISD::INTRINSIC_W_CHAIN: {
4217 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004218 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004219 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004220
Chris Lattnerb2827b02006-03-19 00:52:58 +00004221 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004222 case ISD::TargetConstant: return "TargetConstant";
4223 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004224 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004225 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004226 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004227 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004228 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004229 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004230
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004231 case ISD::CopyToReg: return "CopyToReg";
4232 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004233 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004234 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004235 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004236 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004237 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004238 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004239 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004240 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004241
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004242 // Unary operators
4243 case ISD::FABS: return "fabs";
4244 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004245 case ISD::FSQRT: return "fsqrt";
4246 case ISD::FSIN: return "fsin";
4247 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004248 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004249 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004250
4251 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004252 case ISD::ADD: return "add";
4253 case ISD::SUB: return "sub";
4254 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004255 case ISD::MULHU: return "mulhu";
4256 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004257 case ISD::SDIV: return "sdiv";
4258 case ISD::UDIV: return "udiv";
4259 case ISD::SREM: return "srem";
4260 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004261 case ISD::SMUL_LOHI: return "smul_lohi";
4262 case ISD::UMUL_LOHI: return "umul_lohi";
4263 case ISD::SDIVREM: return "sdivrem";
4264 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004265 case ISD::AND: return "and";
4266 case ISD::OR: return "or";
4267 case ISD::XOR: return "xor";
4268 case ISD::SHL: return "shl";
4269 case ISD::SRA: return "sra";
4270 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004271 case ISD::ROTL: return "rotl";
4272 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004273 case ISD::FADD: return "fadd";
4274 case ISD::FSUB: return "fsub";
4275 case ISD::FMUL: return "fmul";
4276 case ISD::FDIV: return "fdiv";
4277 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004278 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004279 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004280
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004281 case ISD::SETCC: return "setcc";
4282 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004283 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004284 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004285 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004286 case ISD::CONCAT_VECTORS: return "concat_vectors";
4287 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004288 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004289 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004290 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004291 case ISD::ADDC: return "addc";
4292 case ISD::ADDE: return "adde";
4293 case ISD::SUBC: return "subc";
4294 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004295 case ISD::SHL_PARTS: return "shl_parts";
4296 case ISD::SRA_PARTS: return "sra_parts";
4297 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004298
4299 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4300 case ISD::INSERT_SUBREG: return "insert_subreg";
4301
Chris Lattner7f644642005-04-28 21:44:03 +00004302 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004303 case ISD::SIGN_EXTEND: return "sign_extend";
4304 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004305 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004306 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004307 case ISD::TRUNCATE: return "truncate";
4308 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004309 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004310 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004311 case ISD::FP_EXTEND: return "fp_extend";
4312
4313 case ISD::SINT_TO_FP: return "sint_to_fp";
4314 case ISD::UINT_TO_FP: return "uint_to_fp";
4315 case ISD::FP_TO_SINT: return "fp_to_sint";
4316 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004317 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004318
4319 // Control flow instructions
4320 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004321 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004322 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004323 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004324 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004325 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004326 case ISD::CALLSEQ_START: return "callseq_start";
4327 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004328
4329 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004330 case ISD::LOAD: return "load";
4331 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004332 case ISD::VAARG: return "vaarg";
4333 case ISD::VACOPY: return "vacopy";
4334 case ISD::VAEND: return "vaend";
4335 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004336 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004337 case ISD::EXTRACT_ELEMENT: return "extract_element";
4338 case ISD::BUILD_PAIR: return "build_pair";
4339 case ISD::STACKSAVE: return "stacksave";
4340 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004341 case ISD::TRAP: return "trap";
4342
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004343 // Bit manipulation
4344 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004345 case ISD::CTPOP: return "ctpop";
4346 case ISD::CTTZ: return "cttz";
4347 case ISD::CTLZ: return "ctlz";
4348
Chris Lattner36ce6912005-11-29 06:21:05 +00004349 // Debug info
4350 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004351 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004352
Duncan Sands36397f52007-07-27 12:58:54 +00004353 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004354 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004355
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004356 case ISD::CONDCODE:
4357 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004358 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004359 case ISD::SETOEQ: return "setoeq";
4360 case ISD::SETOGT: return "setogt";
4361 case ISD::SETOGE: return "setoge";
4362 case ISD::SETOLT: return "setolt";
4363 case ISD::SETOLE: return "setole";
4364 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004365
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004366 case ISD::SETO: return "seto";
4367 case ISD::SETUO: return "setuo";
4368 case ISD::SETUEQ: return "setue";
4369 case ISD::SETUGT: return "setugt";
4370 case ISD::SETUGE: return "setuge";
4371 case ISD::SETULT: return "setult";
4372 case ISD::SETULE: return "setule";
4373 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004374
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004375 case ISD::SETEQ: return "seteq";
4376 case ISD::SETGT: return "setgt";
4377 case ISD::SETGE: return "setge";
4378 case ISD::SETLT: return "setlt";
4379 case ISD::SETLE: return "setle";
4380 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004381 }
4382 }
4383}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004384
Evan Cheng144d8f02006-11-09 17:55:04 +00004385const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004386 switch (AM) {
4387 default:
4388 return "";
4389 case ISD::PRE_INC:
4390 return "<pre-inc>";
4391 case ISD::PRE_DEC:
4392 return "<pre-dec>";
4393 case ISD::POST_INC:
4394 return "<post-inc>";
4395 case ISD::POST_DEC:
4396 return "<post-dec>";
4397 }
4398}
4399
Duncan Sands276dcbd2008-03-21 09:14:45 +00004400std::string ISD::ArgFlagsTy::getArgFlagsString() {
4401 std::string S = "< ";
4402
4403 if (isZExt())
4404 S += "zext ";
4405 if (isSExt())
4406 S += "sext ";
4407 if (isInReg())
4408 S += "inreg ";
4409 if (isSRet())
4410 S += "sret ";
4411 if (isByVal())
4412 S += "byval ";
4413 if (isNest())
4414 S += "nest ";
4415 if (getByValAlign())
4416 S += "byval-align:" + utostr(getByValAlign()) + " ";
4417 if (getOrigAlign())
4418 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4419 if (getByValSize())
4420 S += "byval-size:" + utostr(getByValSize()) + " ";
4421 return S + ">";
4422}
4423
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004424void SDNode::dump() const { dump(0); }
4425void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004426 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004427
4428 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004429 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004430 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004431 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004432 else
Bill Wendling832171c2006-12-07 20:04:42 +00004433 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004434 }
Bill Wendling832171c2006-12-07 20:04:42 +00004435 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004436
Bill Wendling832171c2006-12-07 20:04:42 +00004437 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004438 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004439 if (i) cerr << ", ";
4440 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004441 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004442 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004443 }
4444
Evan Chengce254432007-12-11 02:08:35 +00004445 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4446 SDNode *Mask = getOperand(2).Val;
4447 cerr << "<";
4448 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4449 if (i) cerr << ",";
4450 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4451 cerr << "u";
4452 else
4453 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4454 }
4455 cerr << ">";
4456 }
4457
Chris Lattnerc3aae252005-01-07 07:46:32 +00004458 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004459 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004460 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004461 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4462 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4463 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4464 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4465 else {
4466 cerr << "<APFloat(";
4467 CSDN->getValueAPF().convertToAPInt().dump();
4468 cerr << ")>";
4469 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004470 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004471 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004472 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004473 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004474 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004475 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004476 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004477 else
Bill Wendling832171c2006-12-07 20:04:42 +00004478 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004479 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004480 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004481 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004482 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004483 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004484 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004485 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004486 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004487 else
Bill Wendling832171c2006-12-07 20:04:42 +00004488 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004489 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004490 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004491 else
Bill Wendling832171c2006-12-07 20:04:42 +00004492 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004493 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004494 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004495 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4496 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004497 cerr << LBB->getName() << " ";
4498 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004499 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004500 if (G && R->getReg() &&
4501 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004502 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004503 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004504 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004505 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004506 } else if (const ExternalSymbolSDNode *ES =
4507 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004508 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004509 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4510 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004511 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004512 else
Dan Gohman69de1932008-02-06 22:27:42 +00004513 cerr << "<null>";
4514 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4515 if (M->MO.getValue())
4516 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4517 else
4518 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004519 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4520 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004521 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004522 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004523 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004524 const Value *SrcValue = LD->getSrcValue();
4525 int SrcOffset = LD->getSrcValueOffset();
4526 cerr << " <";
4527 if (SrcValue)
4528 cerr << SrcValue;
4529 else
4530 cerr << "null";
4531 cerr << ":" << SrcOffset << ">";
4532
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004533 bool doExt = true;
4534 switch (LD->getExtensionType()) {
4535 default: doExt = false; break;
4536 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004537 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004538 break;
4539 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004540 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004541 break;
4542 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004543 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004544 break;
4545 }
4546 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004547 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004548
Evan Cheng144d8f02006-11-09 17:55:04 +00004549 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004550 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004551 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004552 if (LD->isVolatile())
4553 cerr << " <volatile>";
4554 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004555 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004556 const Value *SrcValue = ST->getSrcValue();
4557 int SrcOffset = ST->getSrcValueOffset();
4558 cerr << " <";
4559 if (SrcValue)
4560 cerr << SrcValue;
4561 else
4562 cerr << "null";
4563 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004564
4565 if (ST->isTruncatingStore())
4566 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004567 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004568
4569 const char *AM = getIndexedModeName(ST->getAddressingMode());
4570 if (*AM)
4571 cerr << " " << AM;
4572 if (ST->isVolatile())
4573 cerr << " <volatile>";
4574 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004575 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004576}
4577
Chris Lattnerde202b32005-11-09 23:47:37 +00004578static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004579 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4580 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004581 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004582 else
Bill Wendling832171c2006-12-07 20:04:42 +00004583 cerr << "\n" << std::string(indent+2, ' ')
4584 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004585
Chris Lattnerea946cd2005-01-09 20:38:33 +00004586
Bill Wendling832171c2006-12-07 20:04:42 +00004587 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004588 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004589}
4590
Chris Lattnerc3aae252005-01-07 07:46:32 +00004591void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004592 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004593 std::vector<const SDNode*> Nodes;
4594 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4595 I != E; ++I)
4596 Nodes.push_back(I);
4597
Chris Lattner49d24712005-01-09 20:26:36 +00004598 std::sort(Nodes.begin(), Nodes.end());
4599
4600 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004601 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004602 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004603 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004604
Jim Laskey26f7fa72006-10-17 19:33:52 +00004605 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004606
Bill Wendling832171c2006-12-07 20:04:42 +00004607 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004608}
4609
Evan Chengd6594ae2006-09-12 21:00:35 +00004610const Type *ConstantPoolSDNode::getType() const {
4611 if (isMachineConstantPoolEntry())
4612 return Val.MachineCPVal->getType();
4613 return Val.ConstVal->getType();
4614}