blob: d3c16189105d2ae640175fbdd5ae5685fd4f8686 [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"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000033#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000034#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000036#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000037#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000038#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000039#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner0b3e5252006-08-15 19:11:05 +000042/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands83ec4b62008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattnerf8dc0612008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Jim Laskey58b968b2005-08-17 20:08:02 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000072}
73
Duncan Sands83ec4b62008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000077
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000113 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Evan Chengefec7512008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Chengbb81d972008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000195/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
200 if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::DBG_LABEL)
202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000326 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
338 ID.AddPointer(Ops->getSDOperand().Val);
339 ID.AddInteger(Ops->getSDOperand().ResNo);
340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345 const SDOperand *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Roman Levenstein9cac5252008-04-16 16:15:27 +0000351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
353/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
355 AddNodeIDOpcode(ID, N->getOpcode());
356 // Add the return value info.
357 AddNodeIDValueTypes(ID, N->getVTList());
358 // Add the operand info.
359 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
360
361 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 switch (N->getOpcode()) {
363 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000364 case ISD::ARG_FLAGS:
365 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
366 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetConstant:
368 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000369 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 break;
371 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000372 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000373 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000377 case ISD::GlobalAddress:
378 case ISD::TargetGlobalTLSAddress:
379 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
381 ID.AddPointer(GA->getGlobal());
382 ID.AddInteger(GA->getOffset());
383 break;
384 }
385 case ISD::BasicBlock:
386 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
387 break;
388 case ISD::Register:
389 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
390 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000391 case ISD::DBG_STOPPOINT: {
392 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
393 ID.AddInteger(DSP->getLine());
394 ID.AddInteger(DSP->getColumn());
395 ID.AddPointer(DSP->getCompileUnit());
396 break;
397 }
Dan Gohman69de1932008-02-06 22:27:42 +0000398 case ISD::SRCVALUE:
399 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
400 break;
401 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000402 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000403 ID.AddPointer(MO.getValue());
404 ID.AddInteger(MO.getFlags());
405 ID.AddInteger(MO.getOffset());
406 ID.AddInteger(MO.getSize());
407 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000408 break;
409 }
410 case ISD::FrameIndex:
411 case ISD::TargetFrameIndex:
412 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
413 break;
414 case ISD::JumpTable:
415 case ISD::TargetJumpTable:
416 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
417 break;
418 case ISD::ConstantPool:
419 case ISD::TargetConstantPool: {
420 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
421 ID.AddInteger(CP->getAlignment());
422 ID.AddInteger(CP->getOffset());
423 if (CP->isMachineConstantPoolEntry())
424 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
425 else
426 ID.AddPointer(CP->getConstVal());
427 break;
428 }
429 case ISD::LOAD: {
430 LoadSDNode *LD = cast<LoadSDNode>(N);
431 ID.AddInteger(LD->getAddressingMode());
432 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 ID.AddInteger(LD->getAlignment());
435 ID.AddInteger(LD->isVolatile());
436 break;
437 }
438 case ISD::STORE: {
439 StoreSDNode *ST = cast<StoreSDNode>(N);
440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 ID.AddInteger(ST->getAlignment());
444 ID.AddInteger(ST->isVolatile());
445 break;
446 }
Mon P Wang28873102008-06-25 08:15:39 +0000447 case ISD::ATOMIC_CMP_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_SWAP:
450 case ISD::ATOMIC_LOAD_SUB:
451 case ISD::ATOMIC_LOAD_AND:
452 case ISD::ATOMIC_LOAD_OR:
453 case ISD::ATOMIC_LOAD_XOR:
454 case ISD::ATOMIC_LOAD_NAND:
455 case ISD::ATOMIC_LOAD_MIN:
456 case ISD::ATOMIC_LOAD_MAX:
457 case ISD::ATOMIC_LOAD_UMIN:
458 case ISD::ATOMIC_LOAD_UMAX: {
459 AtomicSDNode *AT = cast<AtomicSDNode>(N);
460 ID.AddInteger(AT->getAlignment());
461 ID.AddInteger(AT->isVolatile());
462 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000463 }
Mon P Wang28873102008-06-25 08:15:39 +0000464 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000465}
466
467//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000468// SelectionDAG Class
469//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000470
Dan Gohman0e5f1302008-07-07 23:02:41 +0000471inline alist_traits<SDNode, LargestSDNode>::AllocatorType &
472SelectionDAG::getAllocator() {
473 return AllNodes.getTraits().Allocator;
474}
475
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000476/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000477/// SelectionDAG.
478void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000479 // Create a dummy node (which is not added to allnodes), that adds a reference
480 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000481 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000482
Chris Lattner190a4182006-08-04 17:45:20 +0000483 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000484
Chris Lattner190a4182006-08-04 17:45:20 +0000485 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000486 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000487 if (I->use_empty())
488 DeadNodes.push_back(I);
489
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000490 RemoveDeadNodes(DeadNodes);
491
492 // If the root changed (e.g. it was a dead load, update the root).
493 setRoot(Dummy.getValue());
494}
495
496/// RemoveDeadNodes - This method deletes the unreachable nodes in the
497/// given list, and any nodes that become unreachable as a result.
498void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
499 DAGUpdateListener *UpdateListener) {
500
Chris Lattner190a4182006-08-04 17:45:20 +0000501 // Process the worklist, deleting the nodes and adding their uses to the
502 // worklist.
503 while (!DeadNodes.empty()) {
504 SDNode *N = DeadNodes.back();
505 DeadNodes.pop_back();
506
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000507 if (UpdateListener)
508 UpdateListener->NodeDeleted(N, 0);
509
Chris Lattner190a4182006-08-04 17:45:20 +0000510 // Take the node out of the appropriate CSE map.
511 RemoveNodeFromCSEMaps(N);
512
513 // Next, brutally remove the operand list. This is safe to do, as there are
514 // no cycles in the graph.
515 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000516 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000517 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000518
519 // Now that we removed this operand, see if there are no uses of it left.
520 if (Operand->use_empty())
521 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000522 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000523 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000524 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000525 }
Chris Lattner190a4182006-08-04 17:45:20 +0000526 N->OperandList = 0;
527 N->NumOperands = 0;
528
529 // Finally, remove N itself.
530 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000531 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532}
533
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000535 SmallVector<SDNode*, 16> DeadNodes;
536 DeadNodes.push_back(N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000537 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000538}
539
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000540void SelectionDAG::DeleteNode(SDNode *N) {
541 assert(N->use_empty() && "Cannot delete a node that is not dead!");
542
543 // First take this out of the appropriate CSE map.
544 RemoveNodeFromCSEMaps(N);
545
Chris Lattner1e111c72005-09-07 05:37:01 +0000546 // Finally, remove uses due to operands of this node, remove from the
547 // AllNodes list, and delete the node.
548 DeleteNodeNotInCSEMaps(N);
549}
550
551void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
552
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000553 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000554 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000555 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000556 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000557 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000558 }
Chris Lattner65113b22005-11-08 22:07:03 +0000559 N->OperandList = 0;
560 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000561
Dan Gohman0e5f1302008-07-07 23:02:41 +0000562 AllNodes.erase(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000563}
564
Chris Lattner149c58c2005-08-16 18:17:10 +0000565/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
566/// correspond to it. This is useful when we're about to delete or repurpose
567/// the node. We don't want future request for structurally identical nodes
568/// to return N anymore.
569void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000571 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000572 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000573 case ISD::CONDCODE:
574 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
575 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000576 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000577 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
578 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000579 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000580 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000582 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000583 Erased =
584 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000585 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000586 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 MVT VT = cast<VTSDNode>(N)->getVT();
588 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000589 Erased = ExtendedValueTypeNodes.erase(VT);
590 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000591 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
592 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000593 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000594 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000595 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000597 // Remove it from the CSE Map.
598 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000599 break;
600 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000601#ifndef NDEBUG
602 // Verify that the node was actually in one of the CSE maps, unless it has a
603 // flag result (which cannot be CSE'd) or is one of the special cases that are
604 // not subject to CSE.
605 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000606 !N->isTargetOpcode() &&
607 N->getOpcode() != ISD::DBG_LABEL &&
608 N->getOpcode() != ISD::DBG_STOPPOINT &&
609 N->getOpcode() != ISD::EH_LABEL &&
610 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000611 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000612 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000613 assert(0 && "Node is not in map!");
614 }
615#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000616}
617
Chris Lattner8b8749f2005-08-17 19:00:20 +0000618/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
619/// has been taken out and modified in some way. If the specified node already
620/// exists in the CSE maps, do not modify the maps, but return the existing node
621/// instead. If it doesn't exist, add it and return null.
622///
623SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
624 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000625
626 if (N->getValueType(0) == MVT::Flag)
627 return 0; // Never CSE anything that produces a flag.
628
629 switch (N->getOpcode()) {
630 default: break;
631 case ISD::HANDLENODE:
632 case ISD::DBG_LABEL:
633 case ISD::DBG_STOPPOINT:
634 case ISD::EH_LABEL:
635 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000636 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000637 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000638
Chris Lattner9f8cc692005-12-19 22:21:21 +0000639 // Check that remaining values produced are not flags.
640 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
641 if (N->getValueType(i) == MVT::Flag)
642 return 0; // Never CSE anything that produces a flag.
643
Chris Lattnera5682852006-08-07 23:03:03 +0000644 SDNode *New = CSEMap.GetOrInsertNode(N);
645 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000646 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000647}
648
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
650/// were replaced with those specified. If this node is never memoized,
651/// return null, otherwise return a pointer to the slot it would take. If a
652/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000653SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
654 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000655 if (N->getValueType(0) == MVT::Flag)
656 return 0; // Never CSE anything that produces a flag.
657
658 switch (N->getOpcode()) {
659 default: break;
660 case ISD::HANDLENODE:
661 case ISD::DBG_LABEL:
662 case ISD::DBG_STOPPOINT:
663 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000664 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000665 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000666
667 // Check that remaining values produced are not flags.
668 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
669 if (N->getValueType(i) == MVT::Flag)
670 return 0; // Never CSE anything that produces a flag.
671
Chris Lattner63e3f142007-02-04 07:28:00 +0000672 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000673 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000674 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000675 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000676}
677
678/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
679/// were replaced with those specified. If this node is never memoized,
680/// return null, otherwise return a pointer to the slot it would take. If a
681/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000682SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
683 SDOperand Op1, SDOperand Op2,
684 void *&InsertPos) {
685 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000686
687 // Check that remaining values produced are not flags.
688 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
689 if (N->getValueType(i) == MVT::Flag)
690 return 0; // Never CSE anything that produces a flag.
691
Chris Lattner63e3f142007-02-04 07:28:00 +0000692 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000693 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000694 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000695 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
696}
697
698
699/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
700/// were replaced with those specified. If this node is never memoized,
701/// return null, otherwise return a pointer to the slot it would take. If a
702/// node already exists with these operands, the slot will be non-null.
703SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000704 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000705 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000706 if (N->getValueType(0) == MVT::Flag)
707 return 0; // Never CSE anything that produces a flag.
708
709 switch (N->getOpcode()) {
710 default: break;
711 case ISD::HANDLENODE:
712 case ISD::DBG_LABEL:
713 case ISD::DBG_STOPPOINT:
714 case ISD::EH_LABEL:
715 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000716 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000717 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000718
719 // Check that remaining values produced are not flags.
720 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
721 if (N->getValueType(i) == MVT::Flag)
722 return 0; // Never CSE anything that produces a flag.
723
Jim Laskey583bd472006-10-27 23:46:08 +0000724 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000725 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000726
Evan Cheng9629aba2006-10-11 01:47:58 +0000727 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
728 ID.AddInteger(LD->getAddressingMode());
729 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000730 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000731 ID.AddInteger(LD->getAlignment());
732 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000733 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
734 ID.AddInteger(ST->getAddressingMode());
735 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000736 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000737 ID.AddInteger(ST->getAlignment());
738 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000739 }
Jim Laskey583bd472006-10-27 23:46:08 +0000740
Chris Lattnera5682852006-08-07 23:03:03 +0000741 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000742}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000743
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000744
Chris Lattner78ec3112003-08-11 14:57:33 +0000745SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000746 while (!AllNodes.empty()) {
747 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000748 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000749 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000750 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000751 }
Chris Lattner65113b22005-11-08 22:07:03 +0000752 N->OperandList = 0;
753 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000754 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000755 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000756}
757
Duncan Sands83ec4b62008-06-06 12:08:01 +0000758SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000759 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000760 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000761 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000762 return getNode(ISD::AND, Op.getValueType(), Op,
763 getConstant(Imm, Op.getValueType()));
764}
765
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000767 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000769}
770
Duncan Sands83ec4b62008-06-06 12:08:01 +0000771SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
772 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000773
Evan Cheng0ff39b32008-06-30 07:31:25 +0000774 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000775 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000776 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000777
778 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000779 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000780 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000781 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000782 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000783 SDNode *N = NULL;
784 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000785 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000786 return SDOperand(N, 0);
787 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000788 N = getAllocator().Allocate<ConstantSDNode>();
789 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000790 CSEMap.InsertNode(N, IP);
791 AllNodes.push_back(N);
792 }
793
794 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000795 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000796 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000797 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000798 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
799 }
800 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000801}
802
Chris Lattner0bd48932008-01-17 07:00:52 +0000803SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
804 return getConstant(Val, TLI.getPointerTy(), isTarget);
805}
806
807
Duncan Sands83ec4b62008-06-06 12:08:01 +0000808SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
809 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000810
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 MVT EltVT =
812 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000813
Chris Lattnerd8658612005-02-17 20:17:32 +0000814 // Do the map lookup using the actual bit pattern for the floating point
815 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
816 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000817 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000818 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000819 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000820 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000821 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000822 SDNode *N = NULL;
823 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000825 return SDOperand(N, 0);
826 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000827 N = getAllocator().Allocate<ConstantFPSDNode>();
828 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000829 CSEMap.InsertNode(N, IP);
830 AllNodes.push_back(N);
831 }
832
Dan Gohman7f321562007-06-25 16:23:39 +0000833 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000834 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000835 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000836 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000837 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
838 }
839 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000840}
841
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
843 MVT EltVT =
844 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000845 if (EltVT==MVT::f32)
846 return getConstantFP(APFloat((float)Val), VT, isTarget);
847 else
848 return getConstantFP(APFloat(Val), VT, isTarget);
849}
850
Chris Lattnerc3aae252005-01-07 07:46:32 +0000851SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000852 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000853 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000854 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000855
856 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
857 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000858 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000859 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
860 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
861 }
862
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000863 if (GVar && GVar->isThreadLocal())
864 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
865 else
866 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000867
Jim Laskey583bd472006-10-27 23:46:08 +0000868 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000869 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000870 ID.AddPointer(GV);
871 ID.AddInteger(Offset);
872 void *IP = 0;
873 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
874 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000875 SDNode *N = getAllocator().Allocate<GlobalAddressSDNode>();
876 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000877 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
880}
881
Duncan Sands83ec4b62008-06-06 12:08:01 +0000882SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000883 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000884 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000885 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 ID.AddInteger(FI);
887 void *IP = 0;
888 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
889 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000890 SDNode *N = getAllocator().Allocate<FrameIndexSDNode>();
891 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000892 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000893 AllNodes.push_back(N);
894 return SDOperand(N, 0);
895}
896
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000898 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000899 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000900 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000901 ID.AddInteger(JTI);
902 void *IP = 0;
903 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
904 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000905 SDNode *N = getAllocator().Allocate<JumpTableSDNode>();
906 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000907 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
910}
911
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000913 unsigned Alignment, int Offset,
914 bool isTarget) {
915 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000916 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000917 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000918 ID.AddInteger(Alignment);
919 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000920 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000921 void *IP = 0;
922 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
923 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000924 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
925 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000926 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000927 AllNodes.push_back(N);
928 return SDOperand(N, 0);
929}
930
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931
Duncan Sands83ec4b62008-06-06 12:08:01 +0000932SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000933 unsigned Alignment, int Offset,
934 bool isTarget) {
935 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000936 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000937 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000938 ID.AddInteger(Alignment);
939 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000940 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000941 void *IP = 0;
942 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
943 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000944 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
945 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000946 CSEMap.InsertNode(N, IP);
947 AllNodes.push_back(N);
948 return SDOperand(N, 0);
949}
950
951
Chris Lattnerc3aae252005-01-07 07:46:32 +0000952SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000953 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000954 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000955 ID.AddPointer(MBB);
956 void *IP = 0;
957 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
958 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000959 SDNode *N = getAllocator().Allocate<BasicBlockSDNode>();
960 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000961 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
964}
965
Duncan Sands276dcbd2008-03-21 09:14:45 +0000966SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
967 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000968 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000969 ID.AddInteger(Flags.getRawBits());
970 void *IP = 0;
971 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
972 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000973 SDNode *N = getAllocator().Allocate<ARG_FLAGSSDNode>();
974 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000975 CSEMap.InsertNode(N, IP);
976 AllNodes.push_back(N);
977 return SDOperand(N, 0);
978}
979
Duncan Sands83ec4b62008-06-06 12:08:01 +0000980SDOperand SelectionDAG::getValueType(MVT VT) {
981 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
982 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000983
Duncan Sands83ec4b62008-06-06 12:08:01 +0000984 SDNode *&N = VT.isExtended() ?
985 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000986
987 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000988 N = getAllocator().Allocate<VTSDNode>();
989 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +0000990 AllNodes.push_back(N);
991 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000992}
993
Duncan Sands83ec4b62008-06-06 12:08:01 +0000994SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 SDNode *&N = ExternalSymbols[Sym];
996 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000997 N = getAllocator().Allocate<ExternalSymbolSDNode>();
998 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000999 AllNodes.push_back(N);
1000 return SDOperand(N, 0);
1001}
1002
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001004 SDNode *&N = TargetExternalSymbols[Sym];
1005 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001006 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1007 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 AllNodes.push_back(N);
1009 return SDOperand(N, 0);
1010}
1011
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001012SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
1013 if ((unsigned)Cond >= CondCodeNodes.size())
1014 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001015
Chris Lattner079a27a2005-08-09 20:40:02 +00001016 if (CondCodeNodes[Cond] == 0) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001017 CondCodeSDNode *N = getAllocator().Allocate<CondCodeSDNode>();
1018 new (N) CondCodeSDNode(Cond);
1019 CondCodeNodes[Cond] = N;
1020 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001021 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001022 return SDOperand(CondCodeNodes[Cond], 0);
1023}
1024
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001026 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001027 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001028 ID.AddInteger(RegNo);
1029 void *IP = 0;
1030 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1031 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001032 SDNode *N = getAllocator().Allocate<RegisterSDNode>();
1033 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001034 CSEMap.InsertNode(N, IP);
1035 AllNodes.push_back(N);
1036 return SDOperand(N, 0);
1037}
1038
Dan Gohman7f460202008-06-30 20:59:49 +00001039SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1040 unsigned Line, unsigned Col,
1041 const CompileUnitDesc *CU) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001042 SDNode *N = getAllocator().Allocate<DbgStopPointSDNode>();
1043 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001044 AllNodes.push_back(N);
1045 return SDOperand(N, 0);
1046}
1047
Dan Gohman44066042008-07-01 00:05:16 +00001048SDOperand SelectionDAG::getLabel(unsigned Opcode,
1049 SDOperand Root,
1050 unsigned LabelID) {
1051 FoldingSetNodeID ID;
1052 SDOperand Ops[] = { Root };
1053 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1054 ID.AddInteger(LabelID);
1055 void *IP = 0;
1056 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1057 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001058 SDNode *N = getAllocator().Allocate<LabelSDNode>();
1059 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001060 CSEMap.InsertNode(N, IP);
1061 AllNodes.push_back(N);
1062 return SDOperand(N, 0);
1063}
1064
Dan Gohman69de1932008-02-06 22:27:42 +00001065SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001066 assert((!V || isa<PointerType>(V->getType())) &&
1067 "SrcValue is not a pointer?");
1068
Jim Laskey583bd472006-10-27 23:46:08 +00001069 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001070 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001071 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001072
Chris Lattner61b09412006-08-11 21:01:22 +00001073 void *IP = 0;
1074 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1075 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001076
Dan Gohman0e5f1302008-07-07 23:02:41 +00001077 SDNode *N = getAllocator().Allocate<SrcValueSDNode>();
1078 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001079 CSEMap.InsertNode(N, IP);
1080 AllNodes.push_back(N);
1081 return SDOperand(N, 0);
1082}
1083
Dan Gohman36b5c132008-04-07 19:35:22 +00001084SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001085 const Value *v = MO.getValue();
1086 assert((!v || isa<PointerType>(v->getType())) &&
1087 "SrcValue is not a pointer?");
1088
1089 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001090 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001091 ID.AddPointer(v);
1092 ID.AddInteger(MO.getFlags());
1093 ID.AddInteger(MO.getOffset());
1094 ID.AddInteger(MO.getSize());
1095 ID.AddInteger(MO.getAlignment());
1096
1097 void *IP = 0;
1098 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1099 return SDOperand(E, 0);
1100
Dan Gohman0e5f1302008-07-07 23:02:41 +00001101 SDNode *N = getAllocator().Allocate<MemOperandSDNode>();
1102 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001103 CSEMap.InsertNode(N, IP);
1104 AllNodes.push_back(N);
1105 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001106}
1107
Chris Lattner37ce9df2007-10-15 17:47:20 +00001108/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1109/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001110SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001111 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001112 unsigned ByteSize = VT.getSizeInBits()/8;
1113 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001114 unsigned StackAlign =
1115 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1116
Chris Lattner37ce9df2007-10-15 17:47:20 +00001117 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1118 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1119}
1120
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001122 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123 // These setcc operations always fold.
1124 switch (Cond) {
1125 default: break;
1126 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001127 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001129 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001130
1131 case ISD::SETOEQ:
1132 case ISD::SETOGT:
1133 case ISD::SETOGE:
1134 case ISD::SETOLT:
1135 case ISD::SETOLE:
1136 case ISD::SETONE:
1137 case ISD::SETO:
1138 case ISD::SETUO:
1139 case ISD::SETUEQ:
1140 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001141 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001142 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001143 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001144
Chris Lattner67255a12005-04-07 18:14:58 +00001145 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001146 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001147 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001148 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001149
Chris Lattnerc3aae252005-01-07 07:46:32 +00001150 switch (Cond) {
1151 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001152 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1153 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001154 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1155 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1156 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1157 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1158 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1159 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1160 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1161 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 }
Chris Lattner67255a12005-04-07 18:14:58 +00001164 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001165 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001166 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001167 // No compile time operations on this type yet.
1168 if (N1C->getValueType(0) == MVT::ppcf128)
1169 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001170
1171 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001173 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001174 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1175 return getNode(ISD::UNDEF, VT);
1176 // fall through
1177 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1178 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1179 return getNode(ISD::UNDEF, VT);
1180 // fall through
1181 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001182 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001183 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1184 return getNode(ISD::UNDEF, VT);
1185 // fall through
1186 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1187 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1188 return getNode(ISD::UNDEF, VT);
1189 // fall through
1190 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1191 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1192 return getNode(ISD::UNDEF, VT);
1193 // fall through
1194 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001195 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001196 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1197 return getNode(ISD::UNDEF, VT);
1198 // fall through
1199 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001200 R==APFloat::cmpEqual, VT);
1201 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1202 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1203 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1204 R==APFloat::cmpEqual, VT);
1205 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1206 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1207 R==APFloat::cmpLessThan, VT);
1208 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1209 R==APFloat::cmpUnordered, VT);
1210 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1211 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001212 }
1213 } else {
1214 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001215 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001216 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001217 }
1218
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001219 // Could not fold it.
1220 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001221}
1222
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001223/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1224/// use this predicate to simplify operations downstream.
1225bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1226 unsigned BitWidth = Op.getValueSizeInBits();
1227 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1228}
1229
Dan Gohmanea859be2007-06-22 14:59:07 +00001230/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1231/// this predicate to simplify operations downstream. Mask is known to be zero
1232/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001233bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001234 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001235 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001236 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1237 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1238 return (KnownZero & Mask) == Mask;
1239}
1240
1241/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1242/// known to be either zero or one and return them in the KnownZero/KnownOne
1243/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1244/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001245void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001246 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001247 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001248 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001249 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001250 "Mask size mismatches value type size!");
1251
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001252 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001253 if (Depth == 6 || Mask == 0)
1254 return; // Limit search depth.
1255
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001256 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001257
1258 switch (Op.getOpcode()) {
1259 case ISD::Constant:
1260 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001261 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001262 KnownZero = ~KnownOne & Mask;
1263 return;
1264 case ISD::AND:
1265 // If either the LHS or the RHS are Zero, the result is zero.
1266 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001267 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1268 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1270 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1271
1272 // Output known-1 bits are only known if set in both the LHS & RHS.
1273 KnownOne &= KnownOne2;
1274 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1275 KnownZero |= KnownZero2;
1276 return;
1277 case ISD::OR:
1278 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001279 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1280 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001281 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1282 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1283
1284 // Output known-0 bits are only known if clear in both the LHS & RHS.
1285 KnownZero &= KnownZero2;
1286 // Output known-1 are known to be set if set in either the LHS | RHS.
1287 KnownOne |= KnownOne2;
1288 return;
1289 case ISD::XOR: {
1290 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1291 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1294
1295 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001296 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001297 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1298 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1299 KnownZero = KnownZeroOut;
1300 return;
1301 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001302 case ISD::MUL: {
1303 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1304 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1305 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1306 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1307 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1308
1309 // If low bits are zero in either operand, output low known-0 bits.
1310 // Also compute a conserative estimate for high known-0 bits.
1311 // More trickiness is possible, but this is sufficient for the
1312 // interesting case of alignment computation.
1313 KnownOne.clear();
1314 unsigned TrailZ = KnownZero.countTrailingOnes() +
1315 KnownZero2.countTrailingOnes();
1316 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001317 KnownZero2.countLeadingOnes(),
1318 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001319
1320 TrailZ = std::min(TrailZ, BitWidth);
1321 LeadZ = std::min(LeadZ, BitWidth);
1322 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1323 APInt::getHighBitsSet(BitWidth, LeadZ);
1324 KnownZero &= Mask;
1325 return;
1326 }
1327 case ISD::UDIV: {
1328 // For the purposes of computing leading zeros we can conservatively
1329 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001330 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001331 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1332 ComputeMaskedBits(Op.getOperand(0),
1333 AllOnes, KnownZero2, KnownOne2, Depth+1);
1334 unsigned LeadZ = KnownZero2.countLeadingOnes();
1335
1336 KnownOne2.clear();
1337 KnownZero2.clear();
1338 ComputeMaskedBits(Op.getOperand(1),
1339 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001340 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1341 if (RHSUnknownLeadingOnes != BitWidth)
1342 LeadZ = std::min(BitWidth,
1343 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001344
1345 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1346 return;
1347 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001348 case ISD::SELECT:
1349 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1350 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1351 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1352 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1353
1354 // Only known if known in both the LHS and RHS.
1355 KnownOne &= KnownOne2;
1356 KnownZero &= KnownZero2;
1357 return;
1358 case ISD::SELECT_CC:
1359 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1360 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1361 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1362 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1363
1364 // Only known if known in both the LHS and RHS.
1365 KnownOne &= KnownOne2;
1366 KnownZero &= KnownZero2;
1367 return;
1368 case ISD::SETCC:
1369 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001370 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1371 BitWidth > 1)
1372 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001373 return;
1374 case ISD::SHL:
1375 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1376 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001377 unsigned ShAmt = SA->getValue();
1378
1379 // If the shift count is an invalid immediate, don't do anything.
1380 if (ShAmt >= BitWidth)
1381 return;
1382
1383 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001384 KnownZero, KnownOne, Depth+1);
1385 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001386 KnownZero <<= ShAmt;
1387 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001388 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001389 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001390 }
1391 return;
1392 case ISD::SRL:
1393 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1394 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001395 unsigned ShAmt = SA->getValue();
1396
Dan Gohmand4cf9922008-02-26 18:50:50 +00001397 // If the shift count is an invalid immediate, don't do anything.
1398 if (ShAmt >= BitWidth)
1399 return;
1400
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001401 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001402 KnownZero, KnownOne, Depth+1);
1403 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001404 KnownZero = KnownZero.lshr(ShAmt);
1405 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001406
Dan Gohman72d2fd52008-02-13 22:43:25 +00001407 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001408 KnownZero |= HighBits; // High bits known zero.
1409 }
1410 return;
1411 case ISD::SRA:
1412 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001413 unsigned ShAmt = SA->getValue();
1414
Dan Gohmand4cf9922008-02-26 18:50:50 +00001415 // If the shift count is an invalid immediate, don't do anything.
1416 if (ShAmt >= BitWidth)
1417 return;
1418
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001419 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001420 // If any of the demanded bits are produced by the sign extension, we also
1421 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001422 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1423 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001424 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001425
1426 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1427 Depth+1);
1428 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001429 KnownZero = KnownZero.lshr(ShAmt);
1430 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001431
1432 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001433 APInt SignBit = APInt::getSignBit(BitWidth);
1434 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001435
Dan Gohmanca93a432008-02-20 16:30:17 +00001436 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001437 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001438 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 KnownOne |= HighBits; // New bits are known one.
1440 }
1441 }
1442 return;
1443 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001444 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1445 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001446
1447 // Sign extension. Compute the demanded bits in the result that are not
1448 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001449 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001450
Dan Gohman977a76f2008-02-13 22:28:48 +00001451 APInt InSignBit = APInt::getSignBit(EBits);
1452 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001453
1454 // If the sign extended bits are demanded, we know that the sign
1455 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001456 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001457 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001458 InputDemandedBits |= InSignBit;
1459
1460 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1461 KnownZero, KnownOne, Depth+1);
1462 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1463
1464 // If the sign bit of the input is known set or clear, then we know the
1465 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001466 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 KnownZero |= NewBits;
1468 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001469 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001470 KnownOne |= NewBits;
1471 KnownZero &= ~NewBits;
1472 } else { // Input sign bit unknown
1473 KnownZero &= ~NewBits;
1474 KnownOne &= ~NewBits;
1475 }
1476 return;
1477 }
1478 case ISD::CTTZ:
1479 case ISD::CTLZ:
1480 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481 unsigned LowBits = Log2_32(BitWidth)+1;
1482 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001483 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001484 return;
1485 }
1486 case ISD::LOAD: {
1487 if (ISD::isZEXTLoad(Op.Val)) {
1488 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001489 MVT VT = LD->getMemoryVT();
1490 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001491 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001492 }
1493 return;
1494 }
1495 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001496 MVT InVT = Op.getOperand(0).getValueType();
1497 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001498 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1499 APInt InMask = Mask;
1500 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001501 KnownZero.trunc(InBits);
1502 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001503 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504 KnownZero.zext(BitWidth);
1505 KnownOne.zext(BitWidth);
1506 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001507 return;
1508 }
1509 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001510 MVT InVT = Op.getOperand(0).getValueType();
1511 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001512 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001513 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1514 APInt InMask = Mask;
1515 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001516
1517 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001518 // bit is demanded. Temporarily set this bit in the mask for our callee.
1519 if (NewBits.getBoolValue())
1520 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001521
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001522 KnownZero.trunc(InBits);
1523 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001524 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1525
1526 // Note if the sign bit is known to be zero or one.
1527 bool SignBitKnownZero = KnownZero.isNegative();
1528 bool SignBitKnownOne = KnownOne.isNegative();
1529 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1530 "Sign bit can't be known to be both zero and one!");
1531
1532 // If the sign bit wasn't actually demanded by our caller, we don't
1533 // want it set in the KnownZero and KnownOne result values. Reset the
1534 // mask and reapply it to the result values.
1535 InMask = Mask;
1536 InMask.trunc(InBits);
1537 KnownZero &= InMask;
1538 KnownOne &= InMask;
1539
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001540 KnownZero.zext(BitWidth);
1541 KnownOne.zext(BitWidth);
1542
Dan Gohman977a76f2008-02-13 22:28:48 +00001543 // If the sign bit is known zero or one, the top bits match.
1544 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001545 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001546 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001547 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001548 return;
1549 }
1550 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001551 MVT InVT = Op.getOperand(0).getValueType();
1552 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001553 APInt InMask = Mask;
1554 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001555 KnownZero.trunc(InBits);
1556 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001557 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001558 KnownZero.zext(BitWidth);
1559 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001560 return;
1561 }
1562 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001563 MVT InVT = Op.getOperand(0).getValueType();
1564 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001565 APInt InMask = Mask;
1566 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001567 KnownZero.zext(InBits);
1568 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001569 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001571 KnownZero.trunc(BitWidth);
1572 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 break;
1574 }
1575 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001576 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1577 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001578 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1579 KnownOne, Depth+1);
1580 KnownZero |= (~InMask) & Mask;
1581 return;
1582 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001583 case ISD::FGETSIGN:
1584 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001585 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001586 return;
1587
Dan Gohman23e8b712008-04-28 17:02:21 +00001588 case ISD::SUB: {
1589 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1590 // We know that the top bits of C-X are clear if X contains less bits
1591 // than C (i.e. no wrap-around can happen). For example, 20-X is
1592 // positive if we can prove that X is >= 0 and < 16.
1593 if (CLHS->getAPIntValue().isNonNegative()) {
1594 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1595 // NLZ can't be BitWidth with no sign bit
1596 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1597 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1598 Depth+1);
1599
1600 // If all of the MaskV bits are known to be zero, then we know the
1601 // output top bits are zero, because we now know that the output is
1602 // from [0-C].
1603 if ((KnownZero2 & MaskV) == MaskV) {
1604 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1605 // Top bits known zero.
1606 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1607 }
1608 }
1609 }
1610 }
1611 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001612 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001613 // Output known-0 bits are known if clear or set in both the low clear bits
1614 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1615 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001616 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1617 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1618 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1619 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1620
1621 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1622 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1623 KnownZeroOut = std::min(KnownZeroOut,
1624 KnownZero2.countTrailingOnes());
1625
1626 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001627 return;
1628 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001629 case ISD::SREM:
1630 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001631 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001632 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001633 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001634 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1635 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001636
Dan Gohman23e8b712008-04-28 17:02:21 +00001637 // The sign of a remainder is equal to the sign of the first
1638 // operand (zero being positive).
1639 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1640 KnownZero2 |= ~LowBits;
1641 else if (KnownOne2[BitWidth-1])
1642 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001643
Dan Gohman23e8b712008-04-28 17:02:21 +00001644 KnownZero |= KnownZero2 & Mask;
1645 KnownOne |= KnownOne2 & Mask;
1646
1647 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001648 }
1649 }
1650 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001651 case ISD::UREM: {
1652 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001653 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001654 if (RA.isPowerOf2()) {
1655 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001656 APInt Mask2 = LowBits & Mask;
1657 KnownZero |= ~LowBits & Mask;
1658 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1659 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1660 break;
1661 }
1662 }
1663
1664 // Since the result is less than or equal to either operand, any leading
1665 // zero bits in either operand must also exist in the result.
1666 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1667 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1668 Depth+1);
1669 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1670 Depth+1);
1671
1672 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1673 KnownZero2.countLeadingOnes());
1674 KnownOne.clear();
1675 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1676 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001677 }
1678 default:
1679 // Allow the target to implement this method for its nodes.
1680 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1681 case ISD::INTRINSIC_WO_CHAIN:
1682 case ISD::INTRINSIC_W_CHAIN:
1683 case ISD::INTRINSIC_VOID:
1684 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1685 }
1686 return;
1687 }
1688}
1689
1690/// ComputeNumSignBits - Return the number of times the sign bit of the
1691/// register is replicated into the other bits. We know that at least 1 bit
1692/// is always equal to the sign bit (itself), but other cases can give us
1693/// information. For example, immediately after an "SRA X, 2", we know that
1694/// the top 3 bits are all equal to each other, so we return 3.
1695unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001696 MVT VT = Op.getValueType();
1697 assert(VT.isInteger() && "Invalid VT!");
1698 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001700 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001701
1702 if (Depth == 6)
1703 return 1; // Limit search depth.
1704
1705 switch (Op.getOpcode()) {
1706 default: break;
1707 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001708 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001709 return VTBits-Tmp+1;
1710 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001711 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001712 return VTBits-Tmp;
1713
1714 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001715 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1716 // If negative, return # leading ones.
1717 if (Val.isNegative())
1718 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001719
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001720 // Return # leading zeros.
1721 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001722 }
1723
1724 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1727
1728 case ISD::SIGN_EXTEND_INREG:
1729 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001730 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 Tmp = VTBits-Tmp+1;
1732
1733 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1734 return std::max(Tmp, Tmp2);
1735
1736 case ISD::SRA:
1737 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1738 // SRA X, C -> adds C sign bits.
1739 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1740 Tmp += C->getValue();
1741 if (Tmp > VTBits) Tmp = VTBits;
1742 }
1743 return Tmp;
1744 case ISD::SHL:
1745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1746 // shl destroys sign bits.
1747 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1748 if (C->getValue() >= VTBits || // Bad shift.
1749 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1750 return Tmp - C->getValue();
1751 }
1752 break;
1753 case ISD::AND:
1754 case ISD::OR:
1755 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001756 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001757 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001758 if (Tmp != 1) {
1759 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1760 FirstAnswer = std::min(Tmp, Tmp2);
1761 // We computed what we know about the sign bits as our first
1762 // answer. Now proceed to the generic code that uses
1763 // ComputeMaskedBits, and pick whichever answer is better.
1764 }
1765 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001766
1767 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001768 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001769 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001770 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 return std::min(Tmp, Tmp2);
1772
1773 case ISD::SETCC:
1774 // If setcc returns 0/-1, all bits are sign bits.
1775 if (TLI.getSetCCResultContents() ==
1776 TargetLowering::ZeroOrNegativeOneSetCCResult)
1777 return VTBits;
1778 break;
1779 case ISD::ROTL:
1780 case ISD::ROTR:
1781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1782 unsigned RotAmt = C->getValue() & (VTBits-1);
1783
1784 // Handle rotate right by N like a rotate left by 32-N.
1785 if (Op.getOpcode() == ISD::ROTR)
1786 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1787
1788 // If we aren't rotating out all of the known-in sign bits, return the
1789 // number that are left. This handles rotl(sext(x), 1) for example.
1790 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1791 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1792 }
1793 break;
1794 case ISD::ADD:
1795 // Add can have at most one carry bit. Thus we know that the output
1796 // is, at worst, one more bit than the inputs.
1797 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1798 if (Tmp == 1) return 1; // Early out.
1799
1800 // Special case decrementing a value (ADD X, -1):
1801 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1802 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001803 APInt KnownZero, KnownOne;
1804 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1806
1807 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1808 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001809 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001810 return VTBits;
1811
1812 // If we are subtracting one from a positive number, there is no carry
1813 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001814 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 return Tmp;
1816 }
1817
1818 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1819 if (Tmp2 == 1) return 1;
1820 return std::min(Tmp, Tmp2)-1;
1821 break;
1822
1823 case ISD::SUB:
1824 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1825 if (Tmp2 == 1) return 1;
1826
1827 // Handle NEG.
1828 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001829 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001830 APInt KnownZero, KnownOne;
1831 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1833 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1834 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001835 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001836 return VTBits;
1837
1838 // If the input is known to be positive (the sign bit is known clear),
1839 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001840 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 return Tmp2;
1842
1843 // Otherwise, we treat this like a SUB.
1844 }
1845
1846 // Sub can have at most one carry bit. Thus we know that the output
1847 // is, at worst, one more bit than the inputs.
1848 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1849 if (Tmp == 1) return 1; // Early out.
1850 return std::min(Tmp, Tmp2)-1;
1851 break;
1852 case ISD::TRUNCATE:
1853 // FIXME: it's tricky to do anything useful for this, but it is an important
1854 // case for targets like X86.
1855 break;
1856 }
1857
1858 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1859 if (Op.getOpcode() == ISD::LOAD) {
1860 LoadSDNode *LD = cast<LoadSDNode>(Op);
1861 unsigned ExtType = LD->getExtensionType();
1862 switch (ExtType) {
1863 default: break;
1864 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001865 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 return VTBits-Tmp+1;
1867 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001868 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 return VTBits-Tmp;
1870 }
1871 }
1872
1873 // Allow the target to implement this method for its nodes.
1874 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1875 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1876 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1877 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1878 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001879 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001880 }
1881
1882 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1883 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001884 APInt KnownZero, KnownOne;
1885 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001886 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1887
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001888 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001890 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001891 Mask = KnownOne;
1892 } else {
1893 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001894 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001895 }
1896
1897 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1898 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001899 Mask = ~Mask;
1900 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 // Return # leading zeros. We use 'min' here in case Val was zero before
1902 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001903 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001904}
1905
Chris Lattner51dabfb2006-10-14 00:41:01 +00001906
Evan Chenga844bde2008-02-02 04:07:54 +00001907bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1908 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1909 if (!GA) return false;
1910 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1911 if (!GV) return false;
1912 MachineModuleInfo *MMI = getMachineModuleInfo();
1913 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1914}
1915
1916
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001917/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1918/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001919SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001920 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001921 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001922 SDOperand Idx = PermMask.getOperand(i);
1923 if (Idx.getOpcode() == ISD::UNDEF)
1924 return getNode(ISD::UNDEF, VT.getVectorElementType());
1925 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001926 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001927 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1928 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001929
1930 if (V.getOpcode() == ISD::BIT_CONVERT) {
1931 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001932 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001933 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001934 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001935 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001936 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001937 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001938 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001939 return V.getOperand(Index);
1940 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1941 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001942 return SDOperand();
1943}
1944
1945
Chris Lattnerc3aae252005-01-07 07:46:32 +00001946/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001947///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001949 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001950 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001951 void *IP = 0;
1952 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1953 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001954 SDNode *N = getAllocator().Allocate<SDNode>();
1955 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001956 CSEMap.InsertNode(N, IP);
1957
1958 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001959 return SDOperand(N, 0);
1960}
1961
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001963 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001964 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001965 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001966 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001967 switch (Opcode) {
1968 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001969 case ISD::SIGN_EXTEND:
1970 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001971 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001972 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001973 case ISD::TRUNCATE:
1974 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001975 case ISD::UINT_TO_FP:
1976 case ISD::SINT_TO_FP: {
1977 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001978 // No compile time operations on this type.
1979 if (VT==MVT::ppcf128)
1980 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001981 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1982 (void)apf.convertFromAPInt(Val,
1983 Opcode==ISD::SINT_TO_FP,
1984 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001985 return getConstantFP(apf, VT);
1986 }
Chris Lattner94683772005-12-23 05:30:37 +00001987 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001988 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001989 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001990 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001991 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001992 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001993 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001994 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001995 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001996 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001997 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001998 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001999 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002000 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002001 }
2002 }
2003
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002004 // Constant fold unary operations with a floating point constant operand.
2005 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2006 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002007 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002008 switch (Opcode) {
2009 case ISD::FNEG:
2010 V.changeSign();
2011 return getConstantFP(V, VT);
2012 case ISD::FABS:
2013 V.clearSign();
2014 return getConstantFP(V, VT);
2015 case ISD::FP_ROUND:
2016 case ISD::FP_EXTEND:
2017 // This can return overflow, underflow, or inexact; we don't care.
2018 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002019 (void)V.convert(*MVTToAPFloatSemantics(VT),
2020 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002021 return getConstantFP(V, VT);
2022 case ISD::FP_TO_SINT:
2023 case ISD::FP_TO_UINT: {
2024 integerPart x;
2025 assert(integerPartWidth >= 64);
2026 // FIXME need to be more flexible about rounding mode.
2027 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2028 Opcode==ISD::FP_TO_SINT,
2029 APFloat::rmTowardZero);
2030 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2031 break;
2032 return getConstant(x, VT);
2033 }
2034 case ISD::BIT_CONVERT:
2035 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2036 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2037 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2038 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002039 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002040 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002041 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002042 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043
2044 unsigned OpOpcode = Operand.Val->getOpcode();
2045 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002046 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002047 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002048 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002049 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002050 assert(VT.isFloatingPoint() &&
2051 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002052 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002053 if (Operand.getOpcode() == ISD::UNDEF)
2054 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002055 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002056 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002058 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002059 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002060 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002061 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002062 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2063 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2064 break;
2065 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002066 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002067 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002068 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002069 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002070 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002071 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002072 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002073 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002074 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002076 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002077 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002078 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002079 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002080 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2081 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2082 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2083 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002084 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002086 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002087 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002088 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002089 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090 if (OpOpcode == ISD::TRUNCATE)
2091 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002092 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2093 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002094 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002095 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002096 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002097 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002098 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2099 else
2100 return Operand.Val->getOperand(0);
2101 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002102 break;
Chris Lattner35481892005-12-23 00:16:34 +00002103 case ISD::BIT_CONVERT:
2104 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002106 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002107 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002108 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2109 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002110 if (OpOpcode == ISD::UNDEF)
2111 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002112 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002113 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002114 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2115 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002116 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002117 if (OpOpcode == ISD::UNDEF)
2118 return getNode(ISD::UNDEF, VT);
2119 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2120 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2121 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2122 Operand.getConstantOperandVal(1) == 0 &&
2123 Operand.getOperand(0).getValueType() == VT)
2124 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002125 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002126 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002127 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2128 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002129 Operand.Val->getOperand(0));
2130 if (OpOpcode == ISD::FNEG) // --X -> X
2131 return Operand.Val->getOperand(0);
2132 break;
2133 case ISD::FABS:
2134 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2135 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2136 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002137 }
2138
Chris Lattner43247a12005-08-25 19:12:10 +00002139 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002140 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002141 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002142 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002143 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002144 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002145 void *IP = 0;
2146 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2147 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002148 N = getAllocator().Allocate<UnarySDNode>();
2149 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002150 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002151 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002152 N = getAllocator().Allocate<UnarySDNode>();
2153 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002154 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002155 AllNodes.push_back(N);
2156 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002157}
2158
Chris Lattner36019aa2005-04-18 03:48:41 +00002159
2160
Duncan Sands83ec4b62008-06-06 12:08:01 +00002161SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002163 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2164 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002165 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002166 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002167 case ISD::TokenFactor:
2168 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2169 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002170 // Fold trivial token factors.
2171 if (N1.getOpcode() == ISD::EntryToken) return N2;
2172 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002173 break;
Chris Lattner76365122005-01-16 02:23:22 +00002174 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002175 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002176 N1.getValueType() == VT && "Binary operator types must match!");
2177 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2178 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002179 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002180 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002181 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2182 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002183 break;
Chris Lattner76365122005-01-16 02:23:22 +00002184 case ISD::OR:
2185 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002186 case ISD::ADD:
2187 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002189 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002190 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2191 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002192 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002193 return N1;
2194 break;
Chris Lattner76365122005-01-16 02:23:22 +00002195 case ISD::UDIV:
2196 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002197 case ISD::MULHU:
2198 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002199 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002200 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002201 case ISD::MUL:
2202 case ISD::SDIV:
2203 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002204 case ISD::FADD:
2205 case ISD::FSUB:
2206 case ISD::FMUL:
2207 case ISD::FDIV:
2208 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002209 assert(N1.getValueType() == N2.getValueType() &&
2210 N1.getValueType() == VT && "Binary operator types must match!");
2211 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002212 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2213 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002214 N1.getValueType().isFloatingPoint() &&
2215 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002216 "Invalid FCOPYSIGN!");
2217 break;
Chris Lattner76365122005-01-16 02:23:22 +00002218 case ISD::SHL:
2219 case ISD::SRA:
2220 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002221 case ISD::ROTL:
2222 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002223 assert(VT == N1.getValueType() &&
2224 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002225 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002226 "Shifts only work on integers");
2227
2228 // Always fold shifts of i1 values so the code generator doesn't need to
2229 // handle them. Since we know the size of the shift has to be less than the
2230 // size of the value, the shift/rotate count is guaranteed to be zero.
2231 if (VT == MVT::i1)
2232 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002233 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002234 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002236 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002237 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002238 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002239 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002240 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002241 break;
2242 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002243 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 assert(VT.isFloatingPoint() &&
2245 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002246 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002247 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002248 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002249 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002250 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002251 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002252 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002253 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002254 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002255 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002256 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002257 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002258 break;
2259 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002260 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002262 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002264 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002265 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002266 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002267
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002268 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002269 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002270 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002271 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002272 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002273 return getConstant(Val, VT);
2274 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002275 break;
2276 }
2277 case ISD::EXTRACT_VECTOR_ELT:
2278 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2279
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002280 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2281 if (N1.getOpcode() == ISD::UNDEF)
2282 return getNode(ISD::UNDEF, VT);
2283
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002284 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2285 // expanding copies of large vectors from registers.
2286 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2287 N1.getNumOperands() > 0) {
2288 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002289 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002290 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2291 N1.getOperand(N2C->getValue() / Factor),
2292 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2293 }
2294
2295 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2296 // expanding large vector constants.
2297 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2298 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002299
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002300 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2301 // operations are lowered to scalars.
2302 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2303 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2304 if (IEC == N2C)
2305 return N1.getOperand(1);
2306 else
2307 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2308 }
2309 break;
2310 case ISD::EXTRACT_ELEMENT:
2311 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002312 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2313 (N1.getValueType().isInteger() == VT.isInteger()) &&
2314 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002315
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002316 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2317 // 64-bit integers into 32-bit parts. Instead of building the extract of
2318 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2319 if (N1.getOpcode() == ISD::BUILD_PAIR)
2320 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002321
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002322 // EXTRACT_ELEMENT of a constant int is also very common.
2323 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002324 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002325 unsigned Shift = ElementSize * N2C->getValue();
2326 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2327 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002328 }
2329 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002330 case ISD::EXTRACT_SUBVECTOR:
2331 if (N1.getValueType() == VT) // Trivial extraction.
2332 return N1;
2333 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002334 }
2335
2336 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002337 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002338 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002339 switch (Opcode) {
2340 case ISD::ADD: return getConstant(C1 + C2, VT);
2341 case ISD::SUB: return getConstant(C1 - C2, VT);
2342 case ISD::MUL: return getConstant(C1 * C2, VT);
2343 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002344 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002345 break;
2346 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002347 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002348 break;
2349 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002350 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002351 break;
2352 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002353 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354 break;
2355 case ISD::AND : return getConstant(C1 & C2, VT);
2356 case ISD::OR : return getConstant(C1 | C2, VT);
2357 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002358 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002359 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2360 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2361 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2362 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002363 default: break;
2364 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002365 } else { // Cannonicalize constant to RHS if commutative
2366 if (isCommutativeBinOp(Opcode)) {
2367 std::swap(N1C, N2C);
2368 std::swap(N1, N2);
2369 }
2370 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002371 }
2372
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002373 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2375 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002376 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002377 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2378 // Cannonicalize constant to RHS if commutative
2379 std::swap(N1CFP, N2CFP);
2380 std::swap(N1, N2);
2381 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002382 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2383 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002385 case ISD::FADD:
2386 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002387 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002388 return getConstantFP(V1, VT);
2389 break;
2390 case ISD::FSUB:
2391 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2392 if (s!=APFloat::opInvalidOp)
2393 return getConstantFP(V1, VT);
2394 break;
2395 case ISD::FMUL:
2396 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2397 if (s!=APFloat::opInvalidOp)
2398 return getConstantFP(V1, VT);
2399 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002400 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002401 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2402 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2403 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002405 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002406 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2407 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2408 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002409 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002410 case ISD::FCOPYSIGN:
2411 V1.copySign(V2);
2412 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002413 default: break;
2414 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002415 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002416 }
Chris Lattner62b57722006-04-20 05:39:12 +00002417
2418 // Canonicalize an UNDEF to the RHS, even over a constant.
2419 if (N1.getOpcode() == ISD::UNDEF) {
2420 if (isCommutativeBinOp(Opcode)) {
2421 std::swap(N1, N2);
2422 } else {
2423 switch (Opcode) {
2424 case ISD::FP_ROUND_INREG:
2425 case ISD::SIGN_EXTEND_INREG:
2426 case ISD::SUB:
2427 case ISD::FSUB:
2428 case ISD::FDIV:
2429 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002430 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002431 return N1; // fold op(undef, arg2) -> undef
2432 case ISD::UDIV:
2433 case ISD::SDIV:
2434 case ISD::UREM:
2435 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002436 case ISD::SRL:
2437 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002439 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2440 // For vectors, we can't easily build an all zero vector, just return
2441 // the LHS.
2442 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002443 }
2444 }
2445 }
2446
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002447 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002448 if (N2.getOpcode() == ISD::UNDEF) {
2449 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002450 case ISD::XOR:
2451 if (N1.getOpcode() == ISD::UNDEF)
2452 // Handle undef ^ undef -> 0 special case. This is a common
2453 // idiom (misuse).
2454 return getConstant(0, VT);
2455 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002456 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002457 case ISD::ADDC:
2458 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002459 case ISD::SUB:
2460 case ISD::FADD:
2461 case ISD::FSUB:
2462 case ISD::FMUL:
2463 case ISD::FDIV:
2464 case ISD::FREM:
2465 case ISD::UDIV:
2466 case ISD::SDIV:
2467 case ISD::UREM:
2468 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002469 return N2; // fold op(arg1, undef) -> undef
2470 case ISD::MUL:
2471 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002472 case ISD::SRL:
2473 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002474 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002475 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2476 // For vectors, we can't easily build an all zero vector, just return
2477 // the LHS.
2478 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002479 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002480 if (!VT.isVector())
2481 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002482 // For vectors, we can't easily build an all one vector, just return
2483 // the LHS.
2484 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002485 case ISD::SRA:
2486 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002487 }
2488 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002489
Chris Lattner27e9b412005-05-11 18:57:39 +00002490 // Memoize this node if possible.
2491 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002492 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002493 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002494 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002495 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002496 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002497 void *IP = 0;
2498 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2499 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002500 N = getAllocator().Allocate<BinarySDNode>();
2501 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002502 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002503 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002504 N = getAllocator().Allocate<BinarySDNode>();
2505 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002506 }
2507
Chris Lattnerc3aae252005-01-07 07:46:32 +00002508 AllNodes.push_back(N);
2509 return SDOperand(N, 0);
2510}
2511
Duncan Sands83ec4b62008-06-06 12:08:01 +00002512SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002513 SDOperand N1, SDOperand N2, SDOperand N3) {
2514 // Perform various simplifications.
2515 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2516 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002517 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002518 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002519 // Use FoldSetCC to simplify SETCC's.
2520 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002521 if (Simp.Val) return Simp;
2522 break;
2523 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002524 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002525 if (N1C) {
2526 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002527 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002528 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002529 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002530 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002531
2532 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002533 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002534 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002535 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002536 if (N2C->getValue()) // Unconditional branch
2537 return getNode(ISD::BR, MVT::Other, N1, N3);
2538 else
2539 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002540 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002541 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002542 case ISD::VECTOR_SHUFFLE:
2543 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002545 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002546 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002547 "Illegal VECTOR_SHUFFLE node!");
2548 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002549 case ISD::BIT_CONVERT:
2550 // Fold bit_convert nodes from a type to themselves.
2551 if (N1.getValueType() == VT)
2552 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002553 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002554 }
2555
Chris Lattner43247a12005-08-25 19:12:10 +00002556 // Memoize node if it doesn't produce a flag.
2557 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002558 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002559 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002560 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002561 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002562 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002563 void *IP = 0;
2564 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2565 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002566 N = getAllocator().Allocate<TernarySDNode>();
2567 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002568 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002569 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002570 N = getAllocator().Allocate<TernarySDNode>();
2571 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002572 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573 AllNodes.push_back(N);
2574 return SDOperand(N, 0);
2575}
2576
Duncan Sands83ec4b62008-06-06 12:08:01 +00002577SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002578 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002579 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002580 SDOperand Ops[] = { N1, N2, N3, N4 };
2581 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002582}
2583
Duncan Sands83ec4b62008-06-06 12:08:01 +00002584SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002585 SDOperand N1, SDOperand N2, SDOperand N3,
2586 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002587 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2588 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002589}
2590
Dan Gohman707e0182008-04-12 04:36:06 +00002591/// getMemsetValue - Vectorized representation of the memset value
2592/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002593static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2594 unsigned NumBits = VT.isVector() ?
2595 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002597 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002598 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002599 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002600 Val = (Val << Shift) | Val;
2601 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002602 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002604 return DAG.getConstant(Val, VT);
2605 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002606 }
Evan Chengf0df0312008-05-15 08:39:06 +00002607
2608 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2609 unsigned Shift = 8;
2610 for (unsigned i = NumBits; i > 8; i >>= 1) {
2611 Value = DAG.getNode(ISD::OR, VT,
2612 DAG.getNode(ISD::SHL, VT, Value,
2613 DAG.getConstant(Shift, MVT::i8)), Value);
2614 Shift <<= 1;
2615 }
2616
2617 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002618}
2619
Dan Gohman707e0182008-04-12 04:36:06 +00002620/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2621/// used when a memcpy is turned into a memset when the source is a constant
2622/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002624 const TargetLowering &TLI,
2625 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002626 // Handle vector with all elements zero.
2627 if (Str.empty()) {
2628 if (VT.isInteger())
2629 return DAG.getConstant(0, VT);
2630 unsigned NumElts = VT.getVectorNumElements();
2631 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2632 return DAG.getNode(ISD::BIT_CONVERT, VT,
2633 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2634 }
2635
Duncan Sands83ec4b62008-06-06 12:08:01 +00002636 assert(!VT.isVector() && "Can't handle vector type here!");
2637 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002638 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002639 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002640 if (TLI.isLittleEndian())
2641 Offset = Offset + MSB - 1;
2642 for (unsigned i = 0; i != MSB; ++i) {
2643 Val = (Val << 8) | (unsigned char)Str[Offset];
2644 Offset += TLI.isLittleEndian() ? -1 : 1;
2645 }
2646 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002647}
2648
Dan Gohman707e0182008-04-12 04:36:06 +00002649/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002650///
Dan Gohman707e0182008-04-12 04:36:06 +00002651static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2652 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002653 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002654 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2655}
2656
Evan Chengf0df0312008-05-15 08:39:06 +00002657/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2658///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002659static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002660 unsigned SrcDelta = 0;
2661 GlobalAddressSDNode *G = NULL;
2662 if (Src.getOpcode() == ISD::GlobalAddress)
2663 G = cast<GlobalAddressSDNode>(Src);
2664 else if (Src.getOpcode() == ISD::ADD &&
2665 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2666 Src.getOperand(1).getOpcode() == ISD::Constant) {
2667 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2668 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2669 }
2670 if (!G)
2671 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002672
Evan Chengf0df0312008-05-15 08:39:06 +00002673 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002674 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2675 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002676
Evan Chengf0df0312008-05-15 08:39:06 +00002677 return false;
2678}
Dan Gohman707e0182008-04-12 04:36:06 +00002679
Evan Chengf0df0312008-05-15 08:39:06 +00002680/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2681/// to replace the memset / memcpy is below the threshold. It also returns the
2682/// types of the sequence of memory ops to perform memset / memcpy.
2683static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002685 SDOperand Dst, SDOperand Src,
2686 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002687 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002688 SelectionDAG &DAG,
2689 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002690 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002691 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002692 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002693 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002694 if (VT != MVT::iAny) {
2695 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002696 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002697 // If source is a string constant, this will require an unaligned load.
2698 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2699 if (Dst.getOpcode() != ISD::FrameIndex) {
2700 // Can't change destination alignment. It requires a unaligned store.
2701 if (AllowUnalign)
2702 VT = MVT::iAny;
2703 } else {
2704 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2705 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2706 if (MFI->isFixedObjectIndex(FI)) {
2707 // Can't change destination alignment. It requires a unaligned store.
2708 if (AllowUnalign)
2709 VT = MVT::iAny;
2710 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002711 // Give the stack frame object a larger alignment if needed.
2712 if (MFI->getObjectAlignment(FI) < NewAlign)
2713 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002714 Align = NewAlign;
2715 }
2716 }
2717 }
2718 }
2719
2720 if (VT == MVT::iAny) {
2721 if (AllowUnalign) {
2722 VT = MVT::i64;
2723 } else {
2724 switch (Align & 7) {
2725 case 0: VT = MVT::i64; break;
2726 case 4: VT = MVT::i32; break;
2727 case 2: VT = MVT::i16; break;
2728 default: VT = MVT::i8; break;
2729 }
2730 }
2731
Duncan Sands83ec4b62008-06-06 12:08:01 +00002732 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002733 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002734 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2735 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002736
Duncan Sands8e4eb092008-06-08 20:54:56 +00002737 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002738 VT = LVT;
2739 }
Dan Gohman707e0182008-04-12 04:36:06 +00002740
2741 unsigned NumMemOps = 0;
2742 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002743 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002744 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002745 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002747 VT = MVT::i64;
2748 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2750 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002751 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002753 VTSize >>= 1;
2754 }
Dan Gohman707e0182008-04-12 04:36:06 +00002755 }
Dan Gohman707e0182008-04-12 04:36:06 +00002756
2757 if (++NumMemOps > Limit)
2758 return false;
2759 MemOps.push_back(VT);
2760 Size -= VTSize;
2761 }
2762
2763 return true;
2764}
2765
2766static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2767 SDOperand Chain, SDOperand Dst,
2768 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002769 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002770 const Value *DstSV, uint64_t DstSVOff,
2771 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002772 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2773
Dan Gohman21323f32008-05-29 19:42:22 +00002774 // Expand memcpy to a series of load and store ops if the size operand falls
2775 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002776 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002777 uint64_t Limit = -1;
2778 if (!AlwaysInline)
2779 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002780 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002781 std::string Str;
2782 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002783 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002784 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002785 return SDOperand();
2786
Dan Gohman707e0182008-04-12 04:36:06 +00002787
Evan Cheng0ff39b32008-06-30 07:31:25 +00002788 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002789 SmallVector<SDOperand, 8> OutChains;
2790 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002791 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002792 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002793 MVT VT = MemOps[i];
2794 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002795 SDOperand Value, Store;
2796
Evan Cheng0ff39b32008-06-30 07:31:25 +00002797 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002798 // It's unlikely a store of a vector immediate can be done in a single
2799 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002800 // We also handle store a vector with all zero's.
2801 // FIXME: Handle other cases where store of vector immediate is done in
2802 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002803 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002804 Store = DAG.getStore(Chain, Value,
2805 getMemBasePlusOffset(Dst, DstOff, DAG),
2806 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002807 } else {
2808 Value = DAG.getLoad(VT, Chain,
2809 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002810 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002811 Store = DAG.getStore(Chain, Value,
2812 getMemBasePlusOffset(Dst, DstOff, DAG),
2813 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002814 }
2815 OutChains.push_back(Store);
2816 SrcOff += VTSize;
2817 DstOff += VTSize;
2818 }
2819
2820 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2821 &OutChains[0], OutChains.size());
2822}
2823
Dan Gohman21323f32008-05-29 19:42:22 +00002824static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2825 SDOperand Chain, SDOperand Dst,
2826 SDOperand Src, uint64_t Size,
2827 unsigned Align, bool AlwaysInline,
2828 const Value *DstSV, uint64_t DstSVOff,
2829 const Value *SrcSV, uint64_t SrcSVOff){
2830 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2831
2832 // Expand memmove to a series of load and store ops if the size operand falls
2833 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002834 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002835 uint64_t Limit = -1;
2836 if (!AlwaysInline)
2837 Limit = TLI.getMaxStoresPerMemmove();
2838 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002839 std::string Str;
2840 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002841 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002842 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002843 return SDOperand();
2844
Dan Gohman21323f32008-05-29 19:42:22 +00002845 uint64_t SrcOff = 0, DstOff = 0;
2846
2847 SmallVector<SDOperand, 8> LoadValues;
2848 SmallVector<SDOperand, 8> LoadChains;
2849 SmallVector<SDOperand, 8> OutChains;
2850 unsigned NumMemOps = MemOps.size();
2851 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 MVT VT = MemOps[i];
2853 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002854 SDOperand Value, Store;
2855
2856 Value = DAG.getLoad(VT, Chain,
2857 getMemBasePlusOffset(Src, SrcOff, DAG),
2858 SrcSV, SrcSVOff + SrcOff, false, Align);
2859 LoadValues.push_back(Value);
2860 LoadChains.push_back(Value.getValue(1));
2861 SrcOff += VTSize;
2862 }
2863 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2864 &LoadChains[0], LoadChains.size());
2865 OutChains.clear();
2866 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 MVT VT = MemOps[i];
2868 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002869 SDOperand Value, Store;
2870
2871 Store = DAG.getStore(Chain, LoadValues[i],
2872 getMemBasePlusOffset(Dst, DstOff, DAG),
2873 DstSV, DstSVOff + DstOff, false, DstAlign);
2874 OutChains.push_back(Store);
2875 DstOff += VTSize;
2876 }
2877
2878 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2879 &OutChains[0], OutChains.size());
2880}
2881
Dan Gohman707e0182008-04-12 04:36:06 +00002882static SDOperand getMemsetStores(SelectionDAG &DAG,
2883 SDOperand Chain, SDOperand Dst,
2884 SDOperand Src, uint64_t Size,
2885 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002886 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002887 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2888
2889 // Expand memset to a series of load/store ops if the size operand
2890 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002891 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002892 std::string Str;
2893 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002894 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002895 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002896 return SDOperand();
2897
2898 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002899 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002900
2901 unsigned NumMemOps = MemOps.size();
2902 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002903 MVT VT = MemOps[i];
2904 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002905 SDOperand Value = getMemsetValue(Src, VT, DAG);
2906 SDOperand Store = DAG.getStore(Chain, Value,
2907 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002908 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002909 OutChains.push_back(Store);
2910 DstOff += VTSize;
2911 }
2912
2913 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2914 &OutChains[0], OutChains.size());
2915}
2916
2917SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002918 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002919 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002920 const Value *DstSV, uint64_t DstSVOff,
2921 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002922
2923 // Check to see if we should lower the memcpy to loads and stores first.
2924 // For cases within the target-specified limits, this is the best choice.
2925 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2926 if (ConstantSize) {
2927 // Memcpy with size zero? Just return the original chain.
2928 if (ConstantSize->isNullValue())
2929 return Chain;
2930
2931 SDOperand Result =
2932 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002933 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002934 if (Result.Val)
2935 return Result;
2936 }
2937
2938 // Then check to see if we should lower the memcpy with target-specific
2939 // code. If the target chooses to do this, this is the next best.
2940 SDOperand Result =
2941 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2942 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002943 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002944 if (Result.Val)
2945 return Result;
2946
2947 // If we really need inline code and the target declined to provide it,
2948 // use a (potentially long) sequence of loads and stores.
2949 if (AlwaysInline) {
2950 assert(ConstantSize && "AlwaysInline requires a constant size!");
2951 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2952 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002953 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002954 }
2955
2956 // Emit a library call.
2957 TargetLowering::ArgListTy Args;
2958 TargetLowering::ArgListEntry Entry;
2959 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2960 Entry.Node = Dst; Args.push_back(Entry);
2961 Entry.Node = Src; Args.push_back(Entry);
2962 Entry.Node = Size; Args.push_back(Entry);
2963 std::pair<SDOperand,SDOperand> CallResult =
2964 TLI.LowerCallTo(Chain, Type::VoidTy,
2965 false, false, false, CallingConv::C, false,
2966 getExternalSymbol("memcpy", TLI.getPointerTy()),
2967 Args, *this);
2968 return CallResult.second;
2969}
2970
2971SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2972 SDOperand Src, SDOperand Size,
2973 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002974 const Value *DstSV, uint64_t DstSVOff,
2975 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002976
Dan Gohman21323f32008-05-29 19:42:22 +00002977 // Check to see if we should lower the memmove to loads and stores first.
2978 // For cases within the target-specified limits, this is the best choice.
2979 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2980 if (ConstantSize) {
2981 // Memmove with size zero? Just return the original chain.
2982 if (ConstantSize->isNullValue())
2983 return Chain;
2984
2985 SDOperand Result =
2986 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2987 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2988 if (Result.Val)
2989 return Result;
2990 }
Dan Gohman707e0182008-04-12 04:36:06 +00002991
2992 // Then check to see if we should lower the memmove with target-specific
2993 // code. If the target chooses to do this, this is the next best.
2994 SDOperand Result =
2995 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002996 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002997 if (Result.Val)
2998 return Result;
2999
3000 // Emit a library call.
3001 TargetLowering::ArgListTy Args;
3002 TargetLowering::ArgListEntry Entry;
3003 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3004 Entry.Node = Dst; Args.push_back(Entry);
3005 Entry.Node = Src; Args.push_back(Entry);
3006 Entry.Node = Size; Args.push_back(Entry);
3007 std::pair<SDOperand,SDOperand> CallResult =
3008 TLI.LowerCallTo(Chain, Type::VoidTy,
3009 false, false, false, CallingConv::C, false,
3010 getExternalSymbol("memmove", TLI.getPointerTy()),
3011 Args, *this);
3012 return CallResult.second;
3013}
3014
3015SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
3016 SDOperand Src, SDOperand Size,
3017 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003018 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003019
3020 // Check to see if we should lower the memset to stores first.
3021 // For cases within the target-specified limits, this is the best choice.
3022 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3023 if (ConstantSize) {
3024 // Memset with size zero? Just return the original chain.
3025 if (ConstantSize->isNullValue())
3026 return Chain;
3027
3028 SDOperand Result =
3029 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003030 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003031 if (Result.Val)
3032 return Result;
3033 }
3034
3035 // Then check to see if we should lower the memset with target-specific
3036 // code. If the target chooses to do this, this is the next best.
3037 SDOperand Result =
3038 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003039 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003040 if (Result.Val)
3041 return Result;
3042
3043 // Emit a library call.
3044 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3045 TargetLowering::ArgListTy Args;
3046 TargetLowering::ArgListEntry Entry;
3047 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3048 Args.push_back(Entry);
3049 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003050 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003051 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3052 else
3053 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3054 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3055 Args.push_back(Entry);
3056 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3057 Args.push_back(Entry);
3058 std::pair<SDOperand,SDOperand> CallResult =
3059 TLI.LowerCallTo(Chain, Type::VoidTy,
3060 false, false, false, CallingConv::C, false,
3061 getExternalSymbol("memset", TLI.getPointerTy()),
3062 Args, *this);
3063 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003064}
3065
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003066SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003067 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003068 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003069 unsigned Alignment) {
3070 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003071 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3072 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003073 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003074 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003075 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003076 void* IP = 0;
3077 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3078 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003079 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3080 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003081 CSEMap.InsertNode(N, IP);
3082 AllNodes.push_back(N);
3083 return SDOperand(N, 0);
3084}
3085
3086SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003087 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003088 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003089 unsigned Alignment) {
3090 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003091 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3092 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003093 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003094 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3095 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003096 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003097 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003098 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003099 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003100 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003101 void* IP = 0;
3102 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3103 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003104 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3105 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003106 CSEMap.InsertNode(N, IP);
3107 AllNodes.push_back(N);
3108 return SDOperand(N, 0);
3109}
3110
Duncan Sands4bdcb612008-07-02 17:40:58 +00003111/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3112/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003113SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003114 bool Simplify) {
3115 if (Simplify && NumOps == 1)
3116 return Ops[0];
3117
3118 SmallVector<MVT, 4> VTs;
3119 VTs.reserve(NumOps);
3120 for (unsigned i = 0; i < NumOps; ++i)
3121 VTs.push_back(Ops[i].getValueType());
3122 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3123}
3124
Duncan Sands14ea39c2008-03-27 20:23:40 +00003125SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003126SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003127 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003128 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003129 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003130 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003131 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3132 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003133 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003134 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003135 } else if (SV) {
3136 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3137 assert(PT && "Value for load must be a pointer");
3138 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003139 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003140 assert(Ty && "Could not get type information for load");
3141 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3142 }
Evan Cheng466685d2006-10-09 20:57:25 +00003143
Duncan Sands14ea39c2008-03-27 20:23:40 +00003144 if (VT == EVT) {
3145 ExtType = ISD::NON_EXTLOAD;
3146 } else if (ExtType == ISD::NON_EXTLOAD) {
3147 assert(VT == EVT && "Non-extending load from different memory type!");
3148 } else {
3149 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003150 if (VT.isVector())
3151 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003152 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003153 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003154 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003155 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003156 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003157 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003158 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003159 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003160
3161 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003162 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003163 "Unindexed load with an offset!");
3164
3165 SDVTList VTs = Indexed ?
3166 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3167 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003168 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003169 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003170 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003171 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003172 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003173 ID.AddInteger(Alignment);
3174 ID.AddInteger(isVolatile);
3175 void *IP = 0;
3176 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3177 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003178 SDNode *N = getAllocator().Allocate<LoadSDNode>();
3179 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3180 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003181 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003182 AllNodes.push_back(N);
3183 return SDOperand(N, 0);
3184}
3185
Duncan Sands83ec4b62008-06-06 12:08:01 +00003186SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003187 SDOperand Chain, SDOperand Ptr,
3188 const Value *SV, int SVOffset,
3189 bool isVolatile, unsigned Alignment) {
3190 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003191 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3192 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003193}
3194
Duncan Sands83ec4b62008-06-06 12:08:01 +00003195SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003196 SDOperand Chain, SDOperand Ptr,
3197 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003199 bool isVolatile, unsigned Alignment) {
3200 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003201 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3202 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003203}
3204
Evan Cheng144d8f02006-11-09 17:55:04 +00003205SDOperand
3206SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3207 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003208 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003209 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3210 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003211 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3212 LD->getChain(), Base, Offset, LD->getSrcValue(),
3213 LD->getSrcValueOffset(), LD->getMemoryVT(),
3214 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003215}
3216
Jeff Cohend41b30d2006-11-05 19:31:28 +00003217SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003218 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003219 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003220 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003221
Dan Gohman575e2f42007-06-04 15:49:41 +00003222 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3223 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003224 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003226 } else if (SV) {
3227 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3228 assert(PT && "Value for store must be a pointer");
3229 Ty = PT->getElementType();
3230 }
3231 assert(Ty && "Could not get type information for store");
3232 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3233 }
Evan Chengad071e12006-10-05 22:57:11 +00003234 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003235 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003236 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003237 FoldingSetNodeID ID;
3238 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003239 ID.AddInteger(ISD::UNINDEXED);
3240 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003241 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003242 ID.AddInteger(Alignment);
3243 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003244 void *IP = 0;
3245 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3246 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003247 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3248 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3249 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003250 CSEMap.InsertNode(N, IP);
3251 AllNodes.push_back(N);
3252 return SDOperand(N, 0);
3253}
3254
Jeff Cohend41b30d2006-11-05 19:31:28 +00003255SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003256 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003257 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003258 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003259 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003260
3261 if (VT == SVT)
3262 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003263
Duncan Sands8e4eb092008-06-08 20:54:56 +00003264 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003265 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003266 "Can't do FP-INT conversion!");
3267
Dan Gohman575e2f42007-06-04 15:49:41 +00003268 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3269 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003270 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003271 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003272 } else if (SV) {
3273 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3274 assert(PT && "Value for store must be a pointer");
3275 Ty = PT->getElementType();
3276 }
3277 assert(Ty && "Could not get type information for store");
3278 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3279 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003280 SDVTList VTs = getVTList(MVT::Other);
3281 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003282 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003283 FoldingSetNodeID ID;
3284 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003285 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003286 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003287 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003288 ID.AddInteger(Alignment);
3289 ID.AddInteger(isVolatile);
3290 void *IP = 0;
3291 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3292 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003293 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3294 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3295 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003296 CSEMap.InsertNode(N, IP);
3297 AllNodes.push_back(N);
3298 return SDOperand(N, 0);
3299}
3300
Evan Cheng144d8f02006-11-09 17:55:04 +00003301SDOperand
3302SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3303 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003304 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3305 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3306 "Store is already a indexed store!");
3307 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3308 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3309 FoldingSetNodeID ID;
3310 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3311 ID.AddInteger(AM);
3312 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003313 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003314 ID.AddInteger(ST->getAlignment());
3315 ID.AddInteger(ST->isVolatile());
3316 void *IP = 0;
3317 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3318 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003319 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3320 new (N) StoreSDNode(Ops, VTs, AM,
3321 ST->isTruncatingStore(), ST->getMemoryVT(),
3322 ST->getSrcValue(), ST->getSrcValueOffset(),
3323 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003324 CSEMap.InsertNode(N, IP);
3325 AllNodes.push_back(N);
3326 return SDOperand(N, 0);
3327}
3328
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003330 SDOperand Chain, SDOperand Ptr,
3331 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003332 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003333 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003334}
3335
Duncan Sands83ec4b62008-06-06 12:08:01 +00003336SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003337 const SDUse *Ops, unsigned NumOps) {
3338 switch (NumOps) {
3339 case 0: return getNode(Opcode, VT);
3340 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3341 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3342 Ops[1].getSDOperand());
3343 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3344 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3345 default: break;
3346 }
3347
3348 // Copy from an SDUse array into an SDOperand array for use with
3349 // the regular getNode logic.
3350 SmallVector<SDOperand, 8> NewOps;
3351 NewOps.reserve(NumOps);
3352 for (unsigned i = 0; i != NumOps; ++i)
3353 NewOps.push_back(Ops[i].getSDOperand());
3354 return getNode(Opcode, VT, Ops, NumOps);
3355}
3356
3357SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3358 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003359 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003360 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003361 case 1: return getNode(Opcode, VT, Ops[0]);
3362 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3363 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003364 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003365 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003366
Chris Lattneref847df2005-04-09 03:27:28 +00003367 switch (Opcode) {
3368 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003369 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003370 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003371 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3372 "LHS and RHS of condition must have same type!");
3373 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3374 "True and False arms of SelectCC must have same type!");
3375 assert(Ops[2].getValueType() == VT &&
3376 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003377 break;
3378 }
3379 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003380 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003381 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3382 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003383 break;
3384 }
Chris Lattneref847df2005-04-09 03:27:28 +00003385 }
3386
Chris Lattner385328c2005-05-14 07:42:29 +00003387 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003388 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003389 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003390 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003391 FoldingSetNodeID ID;
3392 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003393 void *IP = 0;
3394 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3395 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003396 N = getAllocator().Allocate<SDNode>();
3397 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003398 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003399 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003400 N = getAllocator().Allocate<SDNode>();
3401 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003402 }
Chris Lattneref847df2005-04-09 03:27:28 +00003403 AllNodes.push_back(N);
3404 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003405}
3406
Chris Lattner89c34632005-05-14 06:20:26 +00003407SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003408 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003409 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003410 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3411 Ops, NumOps);
3412}
3413
3414SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003415 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003416 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003417 if (NumVTs == 1)
3418 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003419 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3420}
3421
3422SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003423 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003424 if (VTList.NumVTs == 1)
3425 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003426
Chris Lattner5f056bf2005-07-10 01:55:33 +00003427 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003428 // FIXME: figure out how to safely handle things like
3429 // int foo(int x) { return 1 << (x & 255); }
3430 // int bar() { return foo(256); }
3431#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003432 case ISD::SRA_PARTS:
3433 case ISD::SRL_PARTS:
3434 case ISD::SHL_PARTS:
3435 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003436 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003437 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3438 else if (N3.getOpcode() == ISD::AND)
3439 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3440 // If the and is only masking out bits that cannot effect the shift,
3441 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003442 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003443 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3444 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3445 }
3446 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003447#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003448 }
Chris Lattner89c34632005-05-14 06:20:26 +00003449
Chris Lattner43247a12005-08-25 19:12:10 +00003450 // Memoize the node unless it returns a flag.
3451 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003452 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003453 FoldingSetNodeID ID;
3454 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003455 void *IP = 0;
3456 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3457 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003458 if (NumOps == 1) {
3459 N = getAllocator().Allocate<UnarySDNode>();
3460 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3461 } else if (NumOps == 2) {
3462 N = getAllocator().Allocate<BinarySDNode>();
3463 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3464 } else if (NumOps == 3) {
3465 N = getAllocator().Allocate<TernarySDNode>();
3466 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3467 } else {
3468 N = getAllocator().Allocate<SDNode>();
3469 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3470 }
Chris Lattnera5682852006-08-07 23:03:03 +00003471 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003472 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003473 if (NumOps == 1) {
3474 N = getAllocator().Allocate<UnarySDNode>();
3475 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3476 } else if (NumOps == 2) {
3477 N = getAllocator().Allocate<BinarySDNode>();
3478 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3479 } else if (NumOps == 3) {
3480 N = getAllocator().Allocate<TernarySDNode>();
3481 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3482 } else {
3483 N = getAllocator().Allocate<SDNode>();
3484 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3485 }
Chris Lattner43247a12005-08-25 19:12:10 +00003486 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003487 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003488 return SDOperand(N, 0);
3489}
3490
Dan Gohman08ce9762007-10-08 15:49:58 +00003491SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003492 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003493}
3494
3495SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3496 SDOperand N1) {
3497 SDOperand Ops[] = { N1 };
3498 return getNode(Opcode, VTList, Ops, 1);
3499}
3500
3501SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3502 SDOperand N1, SDOperand N2) {
3503 SDOperand Ops[] = { N1, N2 };
3504 return getNode(Opcode, VTList, Ops, 2);
3505}
3506
3507SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3508 SDOperand N1, SDOperand N2, SDOperand N3) {
3509 SDOperand Ops[] = { N1, N2, N3 };
3510 return getNode(Opcode, VTList, Ops, 3);
3511}
3512
3513SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3514 SDOperand N1, SDOperand N2, SDOperand N3,
3515 SDOperand N4) {
3516 SDOperand Ops[] = { N1, N2, N3, N4 };
3517 return getNode(Opcode, VTList, Ops, 4);
3518}
3519
3520SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3521 SDOperand N1, SDOperand N2, SDOperand N3,
3522 SDOperand N4, SDOperand N5) {
3523 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3524 return getNode(Opcode, VTList, Ops, 5);
3525}
3526
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003528 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003529}
3530
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3532 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003533 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003534 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003535 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003536 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003538 V.push_back(VT1);
3539 V.push_back(VT2);
3540 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003541 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003542}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003543SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3544 MVT VT3) {
3545 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003546 E = VTList.end(); I != E; ++I) {
3547 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3548 (*I)[2] == VT3)
3549 return makeVTList(&(*I)[0], 3);
3550 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003551 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003552 V.push_back(VT1);
3553 V.push_back(VT2);
3554 V.push_back(VT3);
3555 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003556 return makeVTList(&(*VTList.begin())[0], 3);
3557}
3558
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003560 switch (NumVTs) {
3561 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003562 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003563 case 2: return getVTList(VTs[0], VTs[1]);
3564 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3565 default: break;
3566 }
3567
Duncan Sands83ec4b62008-06-06 12:08:01 +00003568 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003569 E = VTList.end(); I != E; ++I) {
3570 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3571
3572 bool NoMatch = false;
3573 for (unsigned i = 2; i != NumVTs; ++i)
3574 if (VTs[i] != (*I)[i]) {
3575 NoMatch = true;
3576 break;
3577 }
3578 if (!NoMatch)
3579 return makeVTList(&*I->begin(), NumVTs);
3580 }
3581
Duncan Sands83ec4b62008-06-06 12:08:01 +00003582 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003583 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003584}
3585
3586
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003587/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3588/// specified operands. If the resultant node already exists in the DAG,
3589/// this does not modify the specified node, instead it returns the node that
3590/// already exists. If the resultant node does not exist in the DAG, the
3591/// input node is returned. As a degenerate case, if you specify the same
3592/// input operands as the node already has, the input node is returned.
3593SDOperand SelectionDAG::
3594UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3595 SDNode *N = InN.Val;
3596 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3597
3598 // Check to see if there is no change.
3599 if (Op == N->getOperand(0)) return InN;
3600
3601 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003602 void *InsertPos = 0;
3603 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3604 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003605
3606 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003607 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003608 RemoveNodeFromCSEMaps(N);
3609
3610 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003611 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003612 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003613 N->OperandList[0].setUser(N);
3614 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003615
3616 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003617 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003618 return InN;
3619}
3620
3621SDOperand SelectionDAG::
3622UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3623 SDNode *N = InN.Val;
3624 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3625
3626 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003627 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3628 return InN; // No operands changed, just return the input node.
3629
3630 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003631 void *InsertPos = 0;
3632 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3633 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003634
3635 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003636 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003637 RemoveNodeFromCSEMaps(N);
3638
3639 // Now we update the operands.
3640 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003641 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003642 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003643 N->OperandList[0].setUser(N);
3644 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003645 }
3646 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003647 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003648 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003649 N->OperandList[1].setUser(N);
3650 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003651 }
3652
3653 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003654 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003655 return InN;
3656}
3657
3658SDOperand SelectionDAG::
3659UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003660 SDOperand Ops[] = { Op1, Op2, Op3 };
3661 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003662}
3663
3664SDOperand SelectionDAG::
3665UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3666 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003667 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3668 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003669}
3670
3671SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003672UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3673 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003674 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3675 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003676}
3677
Chris Lattner809ec112006-01-28 10:09:25 +00003678SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003679UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003680 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003681 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003682 "Update with wrong number of operands");
3683
3684 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003685 bool AnyChange = false;
3686 for (unsigned i = 0; i != NumOps; ++i) {
3687 if (Ops[i] != N->getOperand(i)) {
3688 AnyChange = true;
3689 break;
3690 }
3691 }
3692
3693 // No operands changed, just return the input node.
3694 if (!AnyChange) return InN;
3695
3696 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003697 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003698 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003699 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003700
Dan Gohman7ceda162008-05-02 00:05:03 +00003701 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003702 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003703 RemoveNodeFromCSEMaps(N);
3704
3705 // Now we update the operands.
3706 for (unsigned i = 0; i != NumOps; ++i) {
3707 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003708 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003709 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003710 N->OperandList[i].setUser(N);
3711 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003712 }
3713 }
3714
3715 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003716 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003717 return InN;
3718}
3719
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003720/// MorphNodeTo - This frees the operands of the current node, resets the
3721/// opcode, types, and operands to the specified value. This should only be
3722/// used by the SelectionDAG class.
3723void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003724 const SDOperand *Ops, unsigned NumOps,
3725 SmallVectorImpl<SDNode *> &DeadNodes) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003726 NodeType = Opc;
3727 ValueList = L.VTs;
3728 NumValues = L.NumVTs;
3729
3730 // Clear the operands list, updating used nodes to remove this from their
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003731 // use list. Keep track of any operands that become dead as a result.
3732 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3733 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) {
3734 SDNode *N = I->getVal();
3735 N->removeUser(std::distance(op_begin(), I), this);
3736 if (N->use_empty())
3737 DeadNodeSet.insert(N);
3738 }
3739
Chris Lattner63e3f142007-02-04 07:28:00 +00003740 // If NumOps is larger than the # of operands we currently have, reallocate
3741 // the operand list.
3742 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003743 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003744 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003745 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003746 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003747 OperandsNeedDelete = true;
3748 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003749
3750 // Assign the new operands.
3751 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003752
3753 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3754 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003755 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003756 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003757 N->addUser(i, this);
3758 ++N->UsesSize;
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003759 DeadNodeSet.erase(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003760 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003761
3762 // Clean up any nodes that are still dead after adding the uses for the
3763 // new operands.
3764 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
3765 E = DeadNodeSet.end(); I != E; ++I)
3766 DeadNodes.push_back(*I);
3767}
3768
3769/// DropOperands - Release the operands and set this node to have
3770/// zero operands. This should only be used by HandleSDNode to clear
3771/// its operand list.
3772void SDNode::DropOperands() {
3773 assert(NodeType == ISD::HANDLENODE &&
3774 "DropOperands is for HANDLENODE only!");
3775
3776 // Unlike the code in MorphNodeTo that does this, we don't need to
3777 // watch for dead nodes here.
3778 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3779 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3780
3781 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003782}
Chris Lattner149c58c2005-08-16 18:17:10 +00003783
3784/// SelectNodeTo - These are used for target selectors to *mutate* the
3785/// specified node to have the specified return type, Target opcode, and
3786/// operands. Note that target opcodes are stored as
3787/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003788///
3789/// Note that SelectNodeTo returns the resultant node. If there is already a
3790/// node of the specified opcode and operands, it returns that node instead of
3791/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003792SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003793 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003794 SDVTList VTs = getVTList(VT);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003795 return SelectNodeTo(N, TargetOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003796}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003797
Evan Cheng95514ba2006-08-26 08:00:10 +00003798SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003799 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003800 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003801 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003802 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003803}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003804
Evan Cheng95514ba2006-08-26 08:00:10 +00003805SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003806 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003807 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003808 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003809 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003810 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003811}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003812
Evan Cheng95514ba2006-08-26 08:00:10 +00003813SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003815 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003816 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003817 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003818 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003819}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003820
Evan Cheng95514ba2006-08-26 08:00:10 +00003821SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003822 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003823 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003824 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003825 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3826}
3827
3828SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003829 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003830 unsigned NumOps) {
3831 SDVTList VTs = getVTList(VT1, VT2);
3832 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3833}
3834
3835SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3836 MVT VT1, MVT VT2) {
3837 SDVTList VTs = getVTList(VT1, VT2);
3838 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3839}
3840
3841SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003842 MVT VT1, MVT VT2, MVT VT3,
3843 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003844 SDVTList VTs = getVTList(VT1, VT2, VT3);
3845 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3846}
3847
3848SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3849 MVT VT1, MVT VT2,
3850 SDOperand Op1) {
3851 SDVTList VTs = getVTList(VT1, VT2);
3852 SDOperand Ops[] = { Op1 };
3853 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003854}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003855
Evan Cheng95514ba2006-08-26 08:00:10 +00003856SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003857 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003858 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003859 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003860 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003861 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003862}
3863
Evan Cheng95514ba2006-08-26 08:00:10 +00003864SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003866 SDOperand Op1, SDOperand Op2,
3867 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003868 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003869 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003870 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3871}
3872
3873SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003874 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003875 unsigned NumOps) {
3876 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003877 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003878 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003879 void *IP = 0;
3880 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003881 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003882
Chris Lattner0fb094f2005-11-19 01:44:53 +00003883 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003884
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003885 SmallVector<SDNode *, 16> DeadNodes;
3886 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps, DeadNodes);
3887 RemoveDeadNodes(DeadNodes);
3888
Chris Lattnera5682852006-08-07 23:03:03 +00003889 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003890 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003891}
3892
Chris Lattner0fb094f2005-11-19 01:44:53 +00003893
Evan Cheng6ae46c42006-02-09 07:15:23 +00003894/// getTargetNode - These are used for target selectors to create a new node
3895/// with specified return type(s), target opcode, and operands.
3896///
3897/// Note that getTargetNode returns the resultant node. If there is already a
3898/// node of the specified opcode and operands, it returns that node instead of
3899/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003900SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003901 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3902}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003904 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3905}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003906SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003907 SDOperand Op1, SDOperand Op2) {
3908 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3909}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003910SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003911 SDOperand Op1, SDOperand Op2,
3912 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003913 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3914}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003915SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003916 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003917 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003918}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003919SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3920 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003921 SDOperand Op;
3922 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3923}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003924SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3925 MVT VT2, SDOperand Op1) {
3926 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003927 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003928}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003929SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3930 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003931 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003933 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003934 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003935}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003936SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3937 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003938 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003939 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003940 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003941 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003942}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003943SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003944 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003945 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003946 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003947}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003948SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003949 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003950 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003951 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003952 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003953}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003954SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003955 SDOperand Op1, SDOperand Op2,
3956 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003957 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003958 SDOperand Ops[] = { Op1, Op2, Op3 };
3959 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3960}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003962 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003963 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003964 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003965}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003966SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3967 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003968 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003969 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003970 VTList.push_back(VT1);
3971 VTList.push_back(VT2);
3972 VTList.push_back(VT3);
3973 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003974 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003975 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3976}
Evan Cheng39305cf2007-10-05 01:10:49 +00003977SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003978 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003979 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003980 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003981 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3982 Ops, NumOps).Val;
3983}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003984
Evan Cheng08b11732008-03-22 01:55:50 +00003985/// getNodeIfExists - Get the specified node if it's already available, or
3986/// else return NULL.
3987SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003988 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003989 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3990 FoldingSetNodeID ID;
3991 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3992 void *IP = 0;
3993 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3994 return E;
3995 }
3996 return NULL;
3997}
3998
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003999
Evan Cheng99157a02006-08-07 22:13:29 +00004000/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004001/// This can cause recursive merging of nodes in the DAG.
4002///
Chris Lattner11d049c2008-02-03 03:35:22 +00004003/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004004///
Chris Lattner11d049c2008-02-03 03:35:22 +00004005void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004006 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004007 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004008 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004009 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004010 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004011
Chris Lattner8b8749f2005-08-17 19:00:20 +00004012 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004013 SDNode::use_iterator UI = From->use_begin();
4014 SDNode *U = UI->getUser();
4015
Chris Lattner8b8749f2005-08-17 19:00:20 +00004016 // This node is about to morph, remove its old self from the CSE maps.
4017 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004018 int operandNum = 0;
4019 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4020 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004021 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004022 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004023 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004024 I->setUser(U);
4025 To.Val->addUser(operandNum, U);
4026 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004027
4028 // Now that we have modified U, add it back to the CSE maps. If it already
4029 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004030 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004031 ReplaceAllUsesWith(U, Existing, UpdateListener);
4032 // U is now dead. Inform the listener if it exists and delete it.
4033 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004034 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004035 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004036 } else {
4037 // If the node doesn't already exist, we updated it. Inform a listener if
4038 // it exists.
4039 if (UpdateListener)
4040 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004041 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004042 }
4043}
4044
4045/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4046/// This can cause recursive merging of nodes in the DAG.
4047///
4048/// This version assumes From/To have matching types and numbers of result
4049/// values.
4050///
Chris Lattner1e111c72005-09-07 05:37:01 +00004051void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004052 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00004053 assert(From != To && "Cannot replace uses of with self");
4054 assert(From->getNumValues() == To->getNumValues() &&
4055 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004056 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004057 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
4058 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004059
4060 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004061 SDNode::use_iterator UI = From->use_begin();
4062 SDNode *U = UI->getUser();
4063
Chris Lattner8b52f212005-08-26 18:36:28 +00004064 // This node is about to morph, remove its old self from the CSE maps.
4065 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004066 int operandNum = 0;
4067 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4068 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004069 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004070 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004071 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004072 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004073 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004074
Chris Lattner8b8749f2005-08-17 19:00:20 +00004075 // Now that we have modified U, add it back to the CSE maps. If it already
4076 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004077 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004078 ReplaceAllUsesWith(U, Existing, UpdateListener);
4079 // U is now dead. Inform the listener if it exists and delete it.
4080 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004081 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004082 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004083 } else {
4084 // If the node doesn't already exist, we updated it. Inform a listener if
4085 // it exists.
4086 if (UpdateListener)
4087 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004088 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004089 }
4090}
4091
Chris Lattner8b52f212005-08-26 18:36:28 +00004092/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4093/// This can cause recursive merging of nodes in the DAG.
4094///
4095/// This version can replace From with any result values. To must match the
4096/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004097void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004098 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004099 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004100 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004101 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004102
4103 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004104 SDNode::use_iterator UI = From->use_begin();
4105 SDNode *U = UI->getUser();
4106
Chris Lattner7b2880c2005-08-24 22:44:39 +00004107 // This node is about to morph, remove its old self from the CSE maps.
4108 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004109 int operandNum = 0;
4110 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4111 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004112 if (I->getVal() == From) {
4113 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004114 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004115 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004116 I->setUser(U);
4117 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004118 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004119
Chris Lattner7b2880c2005-08-24 22:44:39 +00004120 // Now that we have modified U, add it back to the CSE maps. If it already
4121 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004122 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004123 ReplaceAllUsesWith(U, Existing, UpdateListener);
4124 // U is now dead. Inform the listener if it exists and delete it.
4125 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004126 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004127 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004128 } else {
4129 // If the node doesn't already exist, we updated it. Inform a listener if
4130 // it exists.
4131 if (UpdateListener)
4132 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004133 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004134 }
4135}
4136
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004137namespace {
4138 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4139 /// any deleted nodes from the set passed into its constructor and recursively
4140 /// notifies another update listener if specified.
4141 class ChainedSetUpdaterListener :
4142 public SelectionDAG::DAGUpdateListener {
4143 SmallSetVector<SDNode*, 16> &Set;
4144 SelectionDAG::DAGUpdateListener *Chain;
4145 public:
4146 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4147 SelectionDAG::DAGUpdateListener *chain)
4148 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004149
Duncan Sandsedfcf592008-06-11 11:42:12 +00004150 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004151 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004152 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004153 }
4154 virtual void NodeUpdated(SDNode *N) {
4155 if (Chain) Chain->NodeUpdated(N);
4156 }
4157 };
4158}
4159
Chris Lattner012f2412006-02-17 21:58:01 +00004160/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4161/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004162/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004163void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004164 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004165 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004166
Chris Lattner012f2412006-02-17 21:58:01 +00004167 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004168 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004169 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004170 return;
4171 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004172
4173 if (From.use_empty()) return;
4174
Chris Lattnercf5640b2007-02-04 00:14:31 +00004175 // Get all of the users of From.Val. We want these in a nice,
4176 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004177 SmallSetVector<SDNode*, 16> Users;
4178 for (SDNode::use_iterator UI = From.Val->use_begin(),
4179 E = From.Val->use_end(); UI != E; ++UI) {
4180 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004181 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004182 }
Chris Lattner012f2412006-02-17 21:58:01 +00004183
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004184 // When one of the recursive merges deletes nodes from the graph, we need to
4185 // make sure that UpdateListener is notified *and* that the node is removed
4186 // from Users if present. CSUL does this.
4187 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004188
Chris Lattner012f2412006-02-17 21:58:01 +00004189 while (!Users.empty()) {
4190 // We know that this user uses some value of From. If it is the right
4191 // value, update it.
4192 SDNode *User = Users.back();
4193 Users.pop_back();
4194
Chris Lattner01d029b2007-10-15 06:10:22 +00004195 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004196 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004197 for (; Op != E; ++Op)
4198 if (*Op == From) break;
4199
4200 // If there are no matches, the user must use some other result of From.
4201 if (Op == E) continue;
4202
4203 // Okay, we know this user needs to be updated. Remove its old self
4204 // from the CSE maps.
4205 RemoveNodeFromCSEMaps(User);
4206
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004207 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004208 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004209 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004210 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004211 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004212 Op->setUser(User);
4213 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004214 }
4215 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004216
4217 // Now that we have modified User, add it back to the CSE maps. If it
4218 // already exists there, recursively merge the results together.
4219 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004220 if (!Existing) {
4221 if (UpdateListener) UpdateListener->NodeUpdated(User);
4222 continue; // Continue on to next user.
4223 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004224
4225 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004226 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004227 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004228 // can cause deletion of nodes that used the old value. To handle this, we
4229 // use CSUL to remove them from the Users set.
4230 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004231
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004232 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004233 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004234 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004235 }
4236}
4237
Evan Chenge6f35d82006-08-01 08:20:41 +00004238/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4239/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004240unsigned SelectionDAG::AssignNodeIds() {
4241 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004242 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4243 SDNode *N = I;
4244 N->setNodeId(Id++);
4245 }
4246 return Id;
4247}
4248
Evan Chenge6f35d82006-08-01 08:20:41 +00004249/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004250/// based on their topological order. It returns the maximum id and a vector
4251/// of the SDNodes* in assigned order by reference.
4252unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004253 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004254 std::vector<unsigned> InDegree(DAGSize);
4255 std::vector<SDNode*> Sources;
4256
4257 // Use a two pass approach to avoid using a std::map which is slow.
4258 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004259 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4260 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004261 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004262 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004263 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004264 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004265 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004266 }
4267
Evan Cheng99157a02006-08-07 22:13:29 +00004268 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004269 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004270 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004271 SDNode *N = Sources.back();
4272 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004273 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004274 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004275 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004276 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004277 if (Degree == 0)
4278 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004279 }
4280 }
4281
Evan Chengc384d6c2006-08-02 22:00:34 +00004282 // Second pass, assign the actual topological order as node ids.
4283 Id = 0;
4284 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4285 TI != TE; ++TI)
4286 (*TI)->setNodeId(Id++);
4287
4288 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004289}
4290
4291
Evan Cheng091cba12006-07-27 06:39:06 +00004292
Jim Laskey58b968b2005-08-17 20:08:02 +00004293//===----------------------------------------------------------------------===//
4294// SDNode Class
4295//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004296
Chris Lattner917d2c92006-07-19 00:00:37 +00004297// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004298void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004299void UnarySDNode::ANCHOR() {}
4300void BinarySDNode::ANCHOR() {}
4301void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004302void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004303void ConstantSDNode::ANCHOR() {}
4304void ConstantFPSDNode::ANCHOR() {}
4305void GlobalAddressSDNode::ANCHOR() {}
4306void FrameIndexSDNode::ANCHOR() {}
4307void JumpTableSDNode::ANCHOR() {}
4308void ConstantPoolSDNode::ANCHOR() {}
4309void BasicBlockSDNode::ANCHOR() {}
4310void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004311void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004312void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004313void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004314void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004315void ExternalSymbolSDNode::ANCHOR() {}
4316void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004317void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004318void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004319void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004320void LoadSDNode::ANCHOR() {}
4321void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004322void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004323
Chris Lattner48b85922007-02-04 02:41:42 +00004324HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004325 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004326}
4327
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004328GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004329 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004330 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004331 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004332 // Thread Local
4333 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4334 // Non Thread Local
4335 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4336 getSDVTList(VT)), Offset(o) {
4337 TheGlobal = const_cast<GlobalValue*>(GA);
4338}
Chris Lattner48b85922007-02-04 02:41:42 +00004339
Dan Gohman36b5c132008-04-07 19:35:22 +00004340/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004341/// reference performed by this atomic.
4342MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004343 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004344 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4345 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4346
4347 // Check if the atomic references a frame index
4348 const FrameIndexSDNode *FI =
4349 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4350 if (!getSrcValue() && FI)
4351 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4352 FI->getIndex(), Size, getAlignment());
4353 else
4354 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4355 Size, getAlignment());
4356}
4357
4358/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004359/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004360MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004361 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004362 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004363 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4364 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004365 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004366
4367 // Check if the load references a frame index, and does not have
4368 // an SV attached.
4369 const FrameIndexSDNode *FI =
4370 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4371 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004372 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004373 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004374 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004375 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004376 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004377}
4378
Jim Laskey583bd472006-10-27 23:46:08 +00004379/// Profile - Gather unique data for the node.
4380///
4381void SDNode::Profile(FoldingSetNodeID &ID) {
4382 AddNodeIDNode(ID, this);
4383}
4384
Chris Lattnera3255112005-11-08 23:30:28 +00004385/// getValueTypeList - Return a pointer to the specified value type.
4386///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004387const MVT *SDNode::getValueTypeList(MVT VT) {
4388 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004389 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004390 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004391 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004392 static MVT VTs[MVT::LAST_VALUETYPE];
4393 VTs[VT.getSimpleVT()] = VT;
4394 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004395 }
Chris Lattnera3255112005-11-08 23:30:28 +00004396}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004397
Chris Lattner5c884562005-01-12 18:37:47 +00004398/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4399/// indicated value. This method ignores uses of other values defined by this
4400/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004401bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004402 assert(Value < getNumValues() && "Bad value!");
4403
4404 // If there is only one value, this is easy.
4405 if (getNumValues() == 1)
4406 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004407 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004408
Evan Cheng4ee62112006-02-05 06:29:23 +00004409 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004410
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004411 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004412
Roman Levensteindc1adac2008-04-07 10:06:32 +00004413 // TODO: Only iterate over uses of a given value of the node
4414 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4415 if (*UI == TheValue) {
4416 if (NUses == 0)
4417 return false;
4418 --NUses;
4419 }
Chris Lattner5c884562005-01-12 18:37:47 +00004420 }
4421
4422 // Found exactly the right number of uses?
4423 return NUses == 0;
4424}
4425
4426
Evan Cheng33d55952007-08-02 05:29:38 +00004427/// hasAnyUseOfValue - Return true if there are any use of the indicated
4428/// value. This method ignores uses of other values defined by this operation.
4429bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4430 assert(Value < getNumValues() && "Bad value!");
4431
Dan Gohman30359592008-01-29 13:02:09 +00004432 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004433
4434 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4435
4436 SmallPtrSet<SDNode*, 32> UsersHandled;
4437
Roman Levensteindc1adac2008-04-07 10:06:32 +00004438 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4439 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004440 if (User->getNumOperands() == 1 ||
4441 UsersHandled.insert(User)) // First time we've seen this?
4442 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4443 if (User->getOperand(i) == TheValue) {
4444 return true;
4445 }
4446 }
4447
4448 return false;
4449}
4450
4451
Evan Cheng917be682008-03-04 00:41:45 +00004452/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004453///
Evan Cheng917be682008-03-04 00:41:45 +00004454bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004455 bool Seen = false;
4456 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004457 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004458 if (User == this)
4459 Seen = true;
4460 else
4461 return false;
4462 }
4463
4464 return Seen;
4465}
4466
Evan Chenge6e97e62006-11-03 07:31:32 +00004467/// isOperand - Return true if this node is an operand of N.
4468///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004469bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4471 if (*this == N->getOperand(i))
4472 return true;
4473 return false;
4474}
4475
Evan Cheng917be682008-03-04 00:41:45 +00004476bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004477 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004478 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004479 return true;
4480 return false;
4481}
Evan Cheng4ee62112006-02-05 06:29:23 +00004482
Chris Lattner572dee72008-01-16 05:49:24 +00004483/// reachesChainWithoutSideEffects - Return true if this operand (which must
4484/// be a chain) reaches the specified operand without crossing any
4485/// side-effecting instructions. In practice, this looks through token
4486/// factors and non-volatile loads. In order to remain efficient, this only
4487/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004488bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004489 unsigned Depth) const {
4490 if (*this == Dest) return true;
4491
4492 // Don't search too deeply, we just want to be able to see through
4493 // TokenFactor's etc.
4494 if (Depth == 0) return false;
4495
4496 // If this is a token factor, all inputs to the TF happen in parallel. If any
4497 // of the operands of the TF reach dest, then we can do the xform.
4498 if (getOpcode() == ISD::TokenFactor) {
4499 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4500 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4501 return true;
4502 return false;
4503 }
4504
4505 // Loads don't have side effects, look through them.
4506 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4507 if (!Ld->isVolatile())
4508 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4509 }
4510 return false;
4511}
4512
4513
Evan Chengc5fc57d2006-11-03 03:05:24 +00004514static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004515 SmallPtrSet<SDNode *, 32> &Visited) {
4516 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004517 return;
4518
4519 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4520 SDNode *Op = N->getOperand(i).Val;
4521 if (Op == P) {
4522 found = true;
4523 return;
4524 }
4525 findPredecessor(Op, P, found, Visited);
4526 }
4527}
4528
Evan Cheng917be682008-03-04 00:41:45 +00004529/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004530/// is either an operand of N or it can be reached by recursively traversing
4531/// up the operands.
4532/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004533bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004534 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004535 bool found = false;
4536 findPredecessor(N, this, found, Visited);
4537 return found;
4538}
4539
Evan Chengc5484282006-10-04 00:56:09 +00004540uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4541 assert(Num < NumOperands && "Invalid child # of SDNode!");
4542 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4543}
4544
Reid Spencer577cc322007-04-01 07:32:19 +00004545std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004546 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004547 default:
4548 if (getOpcode() < ISD::BUILTIN_OP_END)
4549 return "<<Unknown DAG Node>>";
4550 else {
Evan Cheng72261582005-12-20 06:22:03 +00004551 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004552 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004553 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004554 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004555
Evan Cheng72261582005-12-20 06:22:03 +00004556 TargetLowering &TLI = G->getTargetLoweringInfo();
4557 const char *Name =
4558 TLI.getTargetNodeName(getOpcode());
4559 if (Name) return Name;
4560 }
4561
4562 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004563 }
4564
Evan Cheng27b7db52008-03-08 00:58:38 +00004565 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004566 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004567 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4568 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4569 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004570 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4571 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4572 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004573 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004574 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4575 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4576 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4577 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4578 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004579 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004580 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004581 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004582 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004583 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004584 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004585 case ISD::AssertSext: return "AssertSext";
4586 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004587
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004588 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004589 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004590 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004591 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004592
4593 case ISD::Constant: return "Constant";
4594 case ISD::ConstantFP: return "ConstantFP";
4595 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004596 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004597 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004598 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004599 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004600 case ISD::RETURNADDR: return "RETURNADDR";
4601 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004602 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004603 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4604 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004605 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004606 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004607 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004608 case ISD::INTRINSIC_WO_CHAIN: {
4609 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4610 return Intrinsic::getName((Intrinsic::ID)IID);
4611 }
4612 case ISD::INTRINSIC_VOID:
4613 case ISD::INTRINSIC_W_CHAIN: {
4614 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004615 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004616 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004617
Chris Lattnerb2827b02006-03-19 00:52:58 +00004618 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004619 case ISD::TargetConstant: return "TargetConstant";
4620 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004621 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004622 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004623 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004624 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004625 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004626 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004627
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004628 case ISD::CopyToReg: return "CopyToReg";
4629 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004630 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004631 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004632 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004633 case ISD::DBG_LABEL: return "dbg_label";
4634 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004635 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004636 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004637 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004638 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004639
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004640 // Unary operators
4641 case ISD::FABS: return "fabs";
4642 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004643 case ISD::FSQRT: return "fsqrt";
4644 case ISD::FSIN: return "fsin";
4645 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004646 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004647 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004648
4649 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004650 case ISD::ADD: return "add";
4651 case ISD::SUB: return "sub";
4652 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004653 case ISD::MULHU: return "mulhu";
4654 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004655 case ISD::SDIV: return "sdiv";
4656 case ISD::UDIV: return "udiv";
4657 case ISD::SREM: return "srem";
4658 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004659 case ISD::SMUL_LOHI: return "smul_lohi";
4660 case ISD::UMUL_LOHI: return "umul_lohi";
4661 case ISD::SDIVREM: return "sdivrem";
4662 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004663 case ISD::AND: return "and";
4664 case ISD::OR: return "or";
4665 case ISD::XOR: return "xor";
4666 case ISD::SHL: return "shl";
4667 case ISD::SRA: return "sra";
4668 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004669 case ISD::ROTL: return "rotl";
4670 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004671 case ISD::FADD: return "fadd";
4672 case ISD::FSUB: return "fsub";
4673 case ISD::FMUL: return "fmul";
4674 case ISD::FDIV: return "fdiv";
4675 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004676 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004677 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004678
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004679 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004680 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004681 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004682 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004683 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004684 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004685 case ISD::CONCAT_VECTORS: return "concat_vectors";
4686 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004687 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004688 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004689 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004690 case ISD::ADDC: return "addc";
4691 case ISD::ADDE: return "adde";
4692 case ISD::SUBC: return "subc";
4693 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004694 case ISD::SHL_PARTS: return "shl_parts";
4695 case ISD::SRA_PARTS: return "sra_parts";
4696 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004697
4698 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4699 case ISD::INSERT_SUBREG: return "insert_subreg";
4700
Chris Lattner7f644642005-04-28 21:44:03 +00004701 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004702 case ISD::SIGN_EXTEND: return "sign_extend";
4703 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004704 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004705 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004706 case ISD::TRUNCATE: return "truncate";
4707 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004708 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004709 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004710 case ISD::FP_EXTEND: return "fp_extend";
4711
4712 case ISD::SINT_TO_FP: return "sint_to_fp";
4713 case ISD::UINT_TO_FP: return "uint_to_fp";
4714 case ISD::FP_TO_SINT: return "fp_to_sint";
4715 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004716 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004717
4718 // Control flow instructions
4719 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004720 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004721 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004722 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004723 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004724 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004725 case ISD::CALLSEQ_START: return "callseq_start";
4726 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004727
4728 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004729 case ISD::LOAD: return "load";
4730 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004731 case ISD::VAARG: return "vaarg";
4732 case ISD::VACOPY: return "vacopy";
4733 case ISD::VAEND: return "vaend";
4734 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004735 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004736 case ISD::EXTRACT_ELEMENT: return "extract_element";
4737 case ISD::BUILD_PAIR: return "build_pair";
4738 case ISD::STACKSAVE: return "stacksave";
4739 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004740 case ISD::TRAP: return "trap";
4741
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004742 // Bit manipulation
4743 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004744 case ISD::CTPOP: return "ctpop";
4745 case ISD::CTTZ: return "cttz";
4746 case ISD::CTLZ: return "ctlz";
4747
Chris Lattner36ce6912005-11-29 06:21:05 +00004748 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004749 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004750 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004751
Duncan Sands36397f52007-07-27 12:58:54 +00004752 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004753 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004754
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004755 case ISD::CONDCODE:
4756 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004757 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004758 case ISD::SETOEQ: return "setoeq";
4759 case ISD::SETOGT: return "setogt";
4760 case ISD::SETOGE: return "setoge";
4761 case ISD::SETOLT: return "setolt";
4762 case ISD::SETOLE: return "setole";
4763 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004764
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004765 case ISD::SETO: return "seto";
4766 case ISD::SETUO: return "setuo";
4767 case ISD::SETUEQ: return "setue";
4768 case ISD::SETUGT: return "setugt";
4769 case ISD::SETUGE: return "setuge";
4770 case ISD::SETULT: return "setult";
4771 case ISD::SETULE: return "setule";
4772 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004773
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004774 case ISD::SETEQ: return "seteq";
4775 case ISD::SETGT: return "setgt";
4776 case ISD::SETGE: return "setge";
4777 case ISD::SETLT: return "setlt";
4778 case ISD::SETLE: return "setle";
4779 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004780 }
4781 }
4782}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004783
Evan Cheng144d8f02006-11-09 17:55:04 +00004784const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004785 switch (AM) {
4786 default:
4787 return "";
4788 case ISD::PRE_INC:
4789 return "<pre-inc>";
4790 case ISD::PRE_DEC:
4791 return "<pre-dec>";
4792 case ISD::POST_INC:
4793 return "<post-inc>";
4794 case ISD::POST_DEC:
4795 return "<post-dec>";
4796 }
4797}
4798
Duncan Sands276dcbd2008-03-21 09:14:45 +00004799std::string ISD::ArgFlagsTy::getArgFlagsString() {
4800 std::string S = "< ";
4801
4802 if (isZExt())
4803 S += "zext ";
4804 if (isSExt())
4805 S += "sext ";
4806 if (isInReg())
4807 S += "inreg ";
4808 if (isSRet())
4809 S += "sret ";
4810 if (isByVal())
4811 S += "byval ";
4812 if (isNest())
4813 S += "nest ";
4814 if (getByValAlign())
4815 S += "byval-align:" + utostr(getByValAlign()) + " ";
4816 if (getOrigAlign())
4817 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4818 if (getByValSize())
4819 S += "byval-size:" + utostr(getByValSize()) + " ";
4820 return S + ">";
4821}
4822
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004823void SDNode::dump() const { dump(0); }
4824void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004825 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004826
4827 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004828 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004829 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004830 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004831 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004832 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004833 }
Bill Wendling832171c2006-12-07 20:04:42 +00004834 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004835
Bill Wendling832171c2006-12-07 20:04:42 +00004836 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004837 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004838 if (i) cerr << ", ";
4839 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004840 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004841 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004842 }
4843
Evan Chengce254432007-12-11 02:08:35 +00004844 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4845 SDNode *Mask = getOperand(2).Val;
4846 cerr << "<";
4847 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4848 if (i) cerr << ",";
4849 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4850 cerr << "u";
4851 else
4852 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4853 }
4854 cerr << ">";
4855 }
4856
Chris Lattnerc3aae252005-01-07 07:46:32 +00004857 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004858 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004859 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004860 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4861 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4862 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4863 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4864 else {
4865 cerr << "<APFloat(";
4866 CSDN->getValueAPF().convertToAPInt().dump();
4867 cerr << ")>";
4868 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004869 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004870 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004871 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004872 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004873 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004874 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004875 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004876 else
Bill Wendling832171c2006-12-07 20:04:42 +00004877 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004878 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004879 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004880 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004881 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004882 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004883 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004884 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004885 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004886 else
Bill Wendling832171c2006-12-07 20:04:42 +00004887 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004888 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004889 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004890 else
Bill Wendling832171c2006-12-07 20:04:42 +00004891 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004892 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004893 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004894 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4895 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004896 cerr << LBB->getName() << " ";
4897 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004898 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004899 if (G && R->getReg() &&
4900 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004901 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004902 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004903 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004905 } else if (const ExternalSymbolSDNode *ES =
4906 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004907 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004908 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4909 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004910 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004911 else
Dan Gohman69de1932008-02-06 22:27:42 +00004912 cerr << "<null>";
4913 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4914 if (M->MO.getValue())
4915 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4916 else
4917 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004918 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4919 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004920 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004921 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004922 }
4923 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004924 const Value *SrcValue = LD->getSrcValue();
4925 int SrcOffset = LD->getSrcValueOffset();
4926 cerr << " <";
4927 if (SrcValue)
4928 cerr << SrcValue;
4929 else
4930 cerr << "null";
4931 cerr << ":" << SrcOffset << ">";
4932
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004933 bool doExt = true;
4934 switch (LD->getExtensionType()) {
4935 default: doExt = false; break;
4936 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004937 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004938 break;
4939 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004940 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004941 break;
4942 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004943 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004944 break;
4945 }
4946 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004947 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004948
Evan Cheng144d8f02006-11-09 17:55:04 +00004949 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004950 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004951 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004952 if (LD->isVolatile())
4953 cerr << " <volatile>";
4954 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004955 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004956 const Value *SrcValue = ST->getSrcValue();
4957 int SrcOffset = ST->getSrcValueOffset();
4958 cerr << " <";
4959 if (SrcValue)
4960 cerr << SrcValue;
4961 else
4962 cerr << "null";
4963 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004964
4965 if (ST->isTruncatingStore())
4966 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004967 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004968
4969 const char *AM = getIndexedModeName(ST->getAddressingMode());
4970 if (*AM)
4971 cerr << " " << AM;
4972 if (ST->isVolatile())
4973 cerr << " <volatile>";
4974 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004975 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4976 const Value *SrcValue = AT->getSrcValue();
4977 int SrcOffset = AT->getSrcValueOffset();
4978 cerr << " <";
4979 if (SrcValue)
4980 cerr << SrcValue;
4981 else
4982 cerr << "null";
4983 cerr << ":" << SrcOffset << ">";
4984 if (AT->isVolatile())
4985 cerr << " <volatile>";
4986 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004987 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004988}
4989
Chris Lattnerde202b32005-11-09 23:47:37 +00004990static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004991 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4992 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004993 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004994 else
Bill Wendling832171c2006-12-07 20:04:42 +00004995 cerr << "\n" << std::string(indent+2, ' ')
4996 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004997
Chris Lattnerea946cd2005-01-09 20:38:33 +00004998
Bill Wendling832171c2006-12-07 20:04:42 +00004999 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005000 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005001}
5002
Chris Lattnerc3aae252005-01-07 07:46:32 +00005003void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005004 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005005 std::vector<const SDNode*> Nodes;
5006 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5007 I != E; ++I)
5008 Nodes.push_back(I);
5009
Chris Lattner49d24712005-01-09 20:26:36 +00005010 std::sort(Nodes.begin(), Nodes.end());
5011
5012 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005013 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005014 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005015 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005016
Jim Laskey26f7fa72006-10-17 19:33:52 +00005017 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005018
Bill Wendling832171c2006-12-07 20:04:42 +00005019 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005020}
5021
Evan Chengd6594ae2006-09-12 21:00:35 +00005022const Type *ConstantPoolSDNode::getType() const {
5023 if (isMachineConstantPoolEntry())
5024 return Val.MachineCPVal->getType();
5025 return Val.ConstVal->getType();
5026}