blob: 14ae80171cea3f5a3662acace248e6da7d3d8c06 [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 Gohman44066042008-07-01 00:05:16 +0000398 case ISD::DBG_LABEL:
399 case ISD::EH_LABEL:
400 ID.AddInteger(cast<LabelSDNode>(N)->getLabelID());
401 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000402 case ISD::SRCVALUE:
403 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
404 break;
405 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000406 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000407 ID.AddPointer(MO.getValue());
408 ID.AddInteger(MO.getFlags());
409 ID.AddInteger(MO.getOffset());
410 ID.AddInteger(MO.getSize());
411 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000412 break;
413 }
414 case ISD::FrameIndex:
415 case ISD::TargetFrameIndex:
416 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
417 break;
418 case ISD::JumpTable:
419 case ISD::TargetJumpTable:
420 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
421 break;
422 case ISD::ConstantPool:
423 case ISD::TargetConstantPool: {
424 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
425 ID.AddInteger(CP->getAlignment());
426 ID.AddInteger(CP->getOffset());
427 if (CP->isMachineConstantPoolEntry())
428 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
429 else
430 ID.AddPointer(CP->getConstVal());
431 break;
432 }
433 case ISD::LOAD: {
434 LoadSDNode *LD = cast<LoadSDNode>(N);
435 ID.AddInteger(LD->getAddressingMode());
436 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000437 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000438 ID.AddInteger(LD->getAlignment());
439 ID.AddInteger(LD->isVolatile());
440 break;
441 }
442 case ISD::STORE: {
443 StoreSDNode *ST = cast<StoreSDNode>(N);
444 ID.AddInteger(ST->getAddressingMode());
445 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000446 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000447 ID.AddInteger(ST->getAlignment());
448 ID.AddInteger(ST->isVolatile());
449 break;
450 }
Mon P Wang28873102008-06-25 08:15:39 +0000451 case ISD::ATOMIC_CMP_SWAP:
452 case ISD::ATOMIC_LOAD_ADD:
453 case ISD::ATOMIC_SWAP:
454 case ISD::ATOMIC_LOAD_SUB:
455 case ISD::ATOMIC_LOAD_AND:
456 case ISD::ATOMIC_LOAD_OR:
457 case ISD::ATOMIC_LOAD_XOR:
458 case ISD::ATOMIC_LOAD_NAND:
459 case ISD::ATOMIC_LOAD_MIN:
460 case ISD::ATOMIC_LOAD_MAX:
461 case ISD::ATOMIC_LOAD_UMIN:
462 case ISD::ATOMIC_LOAD_UMAX: {
463 AtomicSDNode *AT = cast<AtomicSDNode>(N);
464 ID.AddInteger(AT->getAlignment());
465 ID.AddInteger(AT->isVolatile());
466 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000467 }
Mon P Wang28873102008-06-25 08:15:39 +0000468 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000469}
470
471//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000472// SelectionDAG Class
473//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000474
Dan Gohman0e5f1302008-07-07 23:02:41 +0000475inline alist_traits<SDNode, LargestSDNode>::AllocatorType &
476SelectionDAG::getAllocator() {
477 return AllNodes.getTraits().Allocator;
478}
479
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000480/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000481/// SelectionDAG.
482void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000483 // Create a dummy node (which is not added to allnodes), that adds a reference
484 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000485 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000486
Chris Lattner190a4182006-08-04 17:45:20 +0000487 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000488
Chris Lattner190a4182006-08-04 17:45:20 +0000489 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000490 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000491 if (I->use_empty())
492 DeadNodes.push_back(I);
493
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000494 RemoveDeadNodes(DeadNodes);
495
496 // If the root changed (e.g. it was a dead load, update the root).
497 setRoot(Dummy.getValue());
498}
499
500/// RemoveDeadNodes - This method deletes the unreachable nodes in the
501/// given list, and any nodes that become unreachable as a result.
502void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
503 DAGUpdateListener *UpdateListener) {
504
Chris Lattner190a4182006-08-04 17:45:20 +0000505 // Process the worklist, deleting the nodes and adding their uses to the
506 // worklist.
507 while (!DeadNodes.empty()) {
508 SDNode *N = DeadNodes.back();
509 DeadNodes.pop_back();
510
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000511 if (UpdateListener)
512 UpdateListener->NodeDeleted(N, 0);
513
Chris Lattner190a4182006-08-04 17:45:20 +0000514 // Take the node out of the appropriate CSE map.
515 RemoveNodeFromCSEMaps(N);
516
517 // Next, brutally remove the operand list. This is safe to do, as there are
518 // no cycles in the graph.
519 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000520 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000521 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000522
523 // Now that we removed this operand, see if there are no uses of it left.
524 if (Operand->use_empty())
525 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000526 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000527 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000528 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000529 }
Chris Lattner190a4182006-08-04 17:45:20 +0000530 N->OperandList = 0;
531 N->NumOperands = 0;
532
533 // Finally, remove N itself.
534 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000535 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536}
537
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000538void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000539 SmallVector<SDNode*, 16> DeadNodes;
540 DeadNodes.push_back(N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000541 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000542}
543
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000544void SelectionDAG::DeleteNode(SDNode *N) {
545 assert(N->use_empty() && "Cannot delete a node that is not dead!");
546
547 // First take this out of the appropriate CSE map.
548 RemoveNodeFromCSEMaps(N);
549
Chris Lattner1e111c72005-09-07 05:37:01 +0000550 // Finally, remove uses due to operands of this node, remove from the
551 // AllNodes list, and delete the node.
552 DeleteNodeNotInCSEMaps(N);
553}
554
555void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
556
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000557 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000558 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000559 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000560 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000561 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000562 }
Chris Lattner65113b22005-11-08 22:07:03 +0000563 N->OperandList = 0;
564 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000565
Dan Gohman0e5f1302008-07-07 23:02:41 +0000566 AllNodes.erase(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000567}
568
Chris Lattner149c58c2005-08-16 18:17:10 +0000569/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
570/// correspond to it. This is useful when we're about to delete or repurpose
571/// the node. We don't want future request for structurally identical nodes
572/// to return N anymore.
573void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000574 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000576 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000577 case ISD::CONDCODE:
578 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
579 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000580 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000581 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
582 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000583 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000584 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000585 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000586 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000587 Erased =
588 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000589 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000590 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000591 MVT VT = cast<VTSDNode>(N)->getVT();
592 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000593 Erased = ExtendedValueTypeNodes.erase(VT);
594 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000595 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
596 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000597 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000598 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000599 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000600 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000601 // Remove it from the CSE Map.
602 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000603 break;
604 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000605#ifndef NDEBUG
606 // Verify that the node was actually in one of the CSE maps, unless it has a
607 // flag result (which cannot be CSE'd) or is one of the special cases that are
608 // not subject to CSE.
609 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000610 !N->isTargetOpcode()) {
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!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000626 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000627
Chris Lattner9f8cc692005-12-19 22:21:21 +0000628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
632
Chris Lattnera5682852006-08-07 23:03:03 +0000633 SDNode *New = CSEMap.GetOrInsertNode(N);
634 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000635 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000636}
637
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000638/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
639/// were replaced with those specified. If this node is never memoized,
640/// return null, otherwise return a pointer to the slot it would take. If a
641/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000642SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
643 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000644 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000645 return 0; // Never add these nodes.
646
647 // Check that remaining values produced are not flags.
648 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
649 if (N->getValueType(i) == MVT::Flag)
650 return 0; // Never CSE anything that produces a flag.
651
Chris Lattner63e3f142007-02-04 07:28:00 +0000652 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000653 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000654 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000655 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000656}
657
658/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
659/// were replaced with those specified. If this node is never memoized,
660/// return null, otherwise return a pointer to the slot it would take. If a
661/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000662SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
663 SDOperand Op1, SDOperand Op2,
664 void *&InsertPos) {
665 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
666 return 0; // Never add these nodes.
667
668 // Check that remaining values produced are not flags.
669 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
670 if (N->getValueType(i) == MVT::Flag)
671 return 0; // Never CSE anything that produces a flag.
672
Chris Lattner63e3f142007-02-04 07:28:00 +0000673 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000674 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000675 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000676 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
677}
678
679
680/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
681/// were replaced with those specified. If this node is never memoized,
682/// return null, otherwise return a pointer to the slot it would take. If a
683/// node already exists with these operands, the slot will be non-null.
684SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000685 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000686 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000687 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000688 return 0; // Never add these nodes.
689
690 // Check that remaining values produced are not flags.
691 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
692 if (N->getValueType(i) == MVT::Flag)
693 return 0; // Never CSE anything that produces a flag.
694
Jim Laskey583bd472006-10-27 23:46:08 +0000695 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000696 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000697
Evan Cheng9629aba2006-10-11 01:47:58 +0000698 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
699 ID.AddInteger(LD->getAddressingMode());
700 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000701 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000702 ID.AddInteger(LD->getAlignment());
703 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000704 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
705 ID.AddInteger(ST->getAddressingMode());
706 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000707 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000708 ID.AddInteger(ST->getAlignment());
709 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000710 }
Jim Laskey583bd472006-10-27 23:46:08 +0000711
Chris Lattnera5682852006-08-07 23:03:03 +0000712 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000713}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000714
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000715
Chris Lattner78ec3112003-08-11 14:57:33 +0000716SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000717 while (!AllNodes.empty()) {
718 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000719 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000720 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000721 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000722 }
Chris Lattner65113b22005-11-08 22:07:03 +0000723 N->OperandList = 0;
724 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000725 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000726 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000727}
728
Duncan Sands83ec4b62008-06-06 12:08:01 +0000729SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000730 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000731 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000732 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000733 return getNode(ISD::AND, Op.getValueType(), Op,
734 getConstant(Imm, Op.getValueType()));
735}
736
Duncan Sands83ec4b62008-06-06 12:08:01 +0000737SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000738 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000739 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000740}
741
Duncan Sands83ec4b62008-06-06 12:08:01 +0000742SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
743 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000744
Evan Cheng0ff39b32008-06-30 07:31:25 +0000745 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000746 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000747 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000748
749 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000750 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000751 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000752 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000753 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000754 SDNode *N = NULL;
755 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000756 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000757 return SDOperand(N, 0);
758 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000759 N = getAllocator().Allocate<ConstantSDNode>();
760 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000761 CSEMap.InsertNode(N, IP);
762 AllNodes.push_back(N);
763 }
764
765 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000767 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000769 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
770 }
771 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000772}
773
Chris Lattner0bd48932008-01-17 07:00:52 +0000774SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
775 return getConstant(Val, TLI.getPointerTy(), isTarget);
776}
777
778
Duncan Sands83ec4b62008-06-06 12:08:01 +0000779SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
780 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000781
Duncan Sands83ec4b62008-06-06 12:08:01 +0000782 MVT EltVT =
783 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784
Chris Lattnerd8658612005-02-17 20:17:32 +0000785 // Do the map lookup using the actual bit pattern for the floating point
786 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
787 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000788 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000789 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000790 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000791 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000792 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000793 SDNode *N = NULL;
794 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000795 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000796 return SDOperand(N, 0);
797 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000798 N = getAllocator().Allocate<ConstantFPSDNode>();
799 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000800 CSEMap.InsertNode(N, IP);
801 AllNodes.push_back(N);
802 }
803
Dan Gohman7f321562007-06-25 16:23:39 +0000804 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000806 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000807 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000808 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
809 }
810 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000811}
812
Duncan Sands83ec4b62008-06-06 12:08:01 +0000813SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
814 MVT EltVT =
815 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000816 if (EltVT==MVT::f32)
817 return getConstantFP(APFloat((float)Val), VT, isTarget);
818 else
819 return getConstantFP(APFloat(Val), VT, isTarget);
820}
821
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000823 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000824 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000825 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000826
827 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
828 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000829 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000830 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
831 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
832 }
833
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 if (GVar && GVar->isThreadLocal())
835 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
836 else
837 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000838
Jim Laskey583bd472006-10-27 23:46:08 +0000839 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000840 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000841 ID.AddPointer(GV);
842 ID.AddInteger(Offset);
843 void *IP = 0;
844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
845 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000846 SDNode *N = getAllocator().Allocate<GlobalAddressSDNode>();
847 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000848 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 AllNodes.push_back(N);
850 return SDOperand(N, 0);
851}
852
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000854 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000855 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000856 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000857 ID.AddInteger(FI);
858 void *IP = 0;
859 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
860 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000861 SDNode *N = getAllocator().Allocate<FrameIndexSDNode>();
862 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000863 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
866}
867
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000869 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000870 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000871 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000872 ID.AddInteger(JTI);
873 void *IP = 0;
874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
875 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000876 SDNode *N = getAllocator().Allocate<JumpTableSDNode>();
877 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000878 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000879 AllNodes.push_back(N);
880 return SDOperand(N, 0);
881}
882
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000884 unsigned Alignment, int Offset,
885 bool isTarget) {
886 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000887 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000888 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000889 ID.AddInteger(Alignment);
890 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000891 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000892 void *IP = 0;
893 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
894 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000895 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
896 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000897 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000898 AllNodes.push_back(N);
899 return SDOperand(N, 0);
900}
901
Chris Lattnerc3aae252005-01-07 07:46:32 +0000902
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000904 unsigned Alignment, int Offset,
905 bool isTarget) {
906 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000907 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000908 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000909 ID.AddInteger(Alignment);
910 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000911 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000912 void *IP = 0;
913 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
914 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000915 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
916 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000917 CSEMap.InsertNode(N, IP);
918 AllNodes.push_back(N);
919 return SDOperand(N, 0);
920}
921
922
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000924 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000925 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000926 ID.AddPointer(MBB);
927 void *IP = 0;
928 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
929 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000930 SDNode *N = getAllocator().Allocate<BasicBlockSDNode>();
931 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000932 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 AllNodes.push_back(N);
934 return SDOperand(N, 0);
935}
936
Duncan Sands276dcbd2008-03-21 09:14:45 +0000937SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
938 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000939 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000940 ID.AddInteger(Flags.getRawBits());
941 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<ARG_FLAGSSDNode>();
945 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000946 CSEMap.InsertNode(N, IP);
947 AllNodes.push_back(N);
948 return SDOperand(N, 0);
949}
950
Duncan Sands83ec4b62008-06-06 12:08:01 +0000951SDOperand SelectionDAG::getValueType(MVT VT) {
952 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
953 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000954
Duncan Sands83ec4b62008-06-06 12:08:01 +0000955 SDNode *&N = VT.isExtended() ?
956 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000957
958 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000959 N = getAllocator().Allocate<VTSDNode>();
960 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +0000961 AllNodes.push_back(N);
962 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000963}
964
Duncan Sands83ec4b62008-06-06 12:08:01 +0000965SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966 SDNode *&N = ExternalSymbols[Sym];
967 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000968 N = getAllocator().Allocate<ExternalSymbolSDNode>();
969 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000970 AllNodes.push_back(N);
971 return SDOperand(N, 0);
972}
973
Duncan Sands83ec4b62008-06-06 12:08:01 +0000974SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000975 SDNode *&N = TargetExternalSymbols[Sym];
976 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000977 N = getAllocator().Allocate<ExternalSymbolSDNode>();
978 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 AllNodes.push_back(N);
980 return SDOperand(N, 0);
981}
982
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000983SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
984 if ((unsigned)Cond >= CondCodeNodes.size())
985 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000986
Chris Lattner079a27a2005-08-09 20:40:02 +0000987 if (CondCodeNodes[Cond] == 0) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000988 CondCodeSDNode *N = getAllocator().Allocate<CondCodeSDNode>();
989 new (N) CondCodeSDNode(Cond);
990 CondCodeNodes[Cond] = N;
991 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +0000992 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000993 return SDOperand(CondCodeNodes[Cond], 0);
994}
995
Duncan Sands83ec4b62008-06-06 12:08:01 +0000996SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000997 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000998 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000999 ID.AddInteger(RegNo);
1000 void *IP = 0;
1001 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1002 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001003 SDNode *N = getAllocator().Allocate<RegisterSDNode>();
1004 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001005 CSEMap.InsertNode(N, IP);
1006 AllNodes.push_back(N);
1007 return SDOperand(N, 0);
1008}
1009
Dan Gohman7f460202008-06-30 20:59:49 +00001010SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1011 unsigned Line, unsigned Col,
1012 const CompileUnitDesc *CU) {
1013 FoldingSetNodeID ID;
1014 SDOperand Ops[] = { Root };
1015 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
1016 ID.AddInteger(Line);
1017 ID.AddInteger(Col);
1018 ID.AddPointer(CU);
1019 void *IP = 0;
1020 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1021 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001022 SDNode *N = getAllocator().Allocate<DbgStopPointSDNode>();
1023 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001024 CSEMap.InsertNode(N, IP);
1025 AllNodes.push_back(N);
1026 return SDOperand(N, 0);
1027}
1028
Dan Gohman44066042008-07-01 00:05:16 +00001029SDOperand SelectionDAG::getLabel(unsigned Opcode,
1030 SDOperand Root,
1031 unsigned LabelID) {
1032 FoldingSetNodeID ID;
1033 SDOperand Ops[] = { Root };
1034 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1035 ID.AddInteger(LabelID);
1036 void *IP = 0;
1037 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1038 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001039 SDNode *N = getAllocator().Allocate<LabelSDNode>();
1040 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001041 CSEMap.InsertNode(N, IP);
1042 AllNodes.push_back(N);
1043 return SDOperand(N, 0);
1044}
1045
Dan Gohman69de1932008-02-06 22:27:42 +00001046SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001047 assert((!V || isa<PointerType>(V->getType())) &&
1048 "SrcValue is not a pointer?");
1049
Jim Laskey583bd472006-10-27 23:46:08 +00001050 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001051 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001052 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001053
Chris Lattner61b09412006-08-11 21:01:22 +00001054 void *IP = 0;
1055 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1056 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001057
Dan Gohman0e5f1302008-07-07 23:02:41 +00001058 SDNode *N = getAllocator().Allocate<SrcValueSDNode>();
1059 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001060 CSEMap.InsertNode(N, IP);
1061 AllNodes.push_back(N);
1062 return SDOperand(N, 0);
1063}
1064
Dan Gohman36b5c132008-04-07 19:35:22 +00001065SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001066 const Value *v = MO.getValue();
1067 assert((!v || isa<PointerType>(v->getType())) &&
1068 "SrcValue is not a pointer?");
1069
1070 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001071 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001072 ID.AddPointer(v);
1073 ID.AddInteger(MO.getFlags());
1074 ID.AddInteger(MO.getOffset());
1075 ID.AddInteger(MO.getSize());
1076 ID.AddInteger(MO.getAlignment());
1077
1078 void *IP = 0;
1079 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1080 return SDOperand(E, 0);
1081
Dan Gohman0e5f1302008-07-07 23:02:41 +00001082 SDNode *N = getAllocator().Allocate<MemOperandSDNode>();
1083 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001084 CSEMap.InsertNode(N, IP);
1085 AllNodes.push_back(N);
1086 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001087}
1088
Chris Lattner37ce9df2007-10-15 17:47:20 +00001089/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1090/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001091SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001092 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001093 unsigned ByteSize = VT.getSizeInBits()/8;
1094 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001095 unsigned StackAlign =
1096 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1097
Chris Lattner37ce9df2007-10-15 17:47:20 +00001098 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1099 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1100}
1101
Duncan Sands83ec4b62008-06-06 12:08:01 +00001102SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001103 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104 // These setcc operations always fold.
1105 switch (Cond) {
1106 default: break;
1107 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001108 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001109 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001110 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001111
1112 case ISD::SETOEQ:
1113 case ISD::SETOGT:
1114 case ISD::SETOGE:
1115 case ISD::SETOLT:
1116 case ISD::SETOLE:
1117 case ISD::SETONE:
1118 case ISD::SETO:
1119 case ISD::SETUO:
1120 case ISD::SETUEQ:
1121 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001122 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001123 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001125
Chris Lattner67255a12005-04-07 18:14:58 +00001126 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001127 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001128 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001129 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001130
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 switch (Cond) {
1132 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001133 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1134 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001135 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1136 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1137 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1138 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1139 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1140 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1141 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1142 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001143 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 }
Chris Lattner67255a12005-04-07 18:14:58 +00001145 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001146 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001148 // No compile time operations on this type yet.
1149 if (N1C->getValueType(0) == MVT::ppcf128)
1150 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001151
1152 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001154 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001155 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1156 return getNode(ISD::UNDEF, VT);
1157 // fall through
1158 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1159 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1160 return getNode(ISD::UNDEF, VT);
1161 // fall through
1162 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001163 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001164 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1165 return getNode(ISD::UNDEF, VT);
1166 // fall through
1167 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1168 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1169 return getNode(ISD::UNDEF, VT);
1170 // fall through
1171 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1172 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1173 return getNode(ISD::UNDEF, VT);
1174 // fall through
1175 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001176 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001177 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1178 return getNode(ISD::UNDEF, VT);
1179 // fall through
1180 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001181 R==APFloat::cmpEqual, VT);
1182 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1183 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1184 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1185 R==APFloat::cmpEqual, VT);
1186 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1187 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1188 R==APFloat::cmpLessThan, VT);
1189 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1190 R==APFloat::cmpUnordered, VT);
1191 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1192 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 }
1194 } else {
1195 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001196 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001198 }
1199
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001200 // Could not fold it.
1201 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001202}
1203
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001204/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1205/// use this predicate to simplify operations downstream.
1206bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1207 unsigned BitWidth = Op.getValueSizeInBits();
1208 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1209}
1210
Dan Gohmanea859be2007-06-22 14:59:07 +00001211/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1212/// this predicate to simplify operations downstream. Mask is known to be zero
1213/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001214bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001215 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001216 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001217 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1218 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1219 return (KnownZero & Mask) == Mask;
1220}
1221
1222/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1223/// known to be either zero or one and return them in the KnownZero/KnownOne
1224/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1225/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001226void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001227 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001228 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001229 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001230 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001231 "Mask size mismatches value type size!");
1232
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001233 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001234 if (Depth == 6 || Mask == 0)
1235 return; // Limit search depth.
1236
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001237 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001238
1239 switch (Op.getOpcode()) {
1240 case ISD::Constant:
1241 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001242 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001243 KnownZero = ~KnownOne & Mask;
1244 return;
1245 case ISD::AND:
1246 // If either the LHS or the RHS are Zero, the result is zero.
1247 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001248 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1249 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001250 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1251 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1252
1253 // Output known-1 bits are only known if set in both the LHS & RHS.
1254 KnownOne &= KnownOne2;
1255 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1256 KnownZero |= KnownZero2;
1257 return;
1258 case ISD::OR:
1259 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001260 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1261 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001262 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1263 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1264
1265 // Output known-0 bits are only known if clear in both the LHS & RHS.
1266 KnownZero &= KnownZero2;
1267 // Output known-1 are known to be set if set in either the LHS | RHS.
1268 KnownOne |= KnownOne2;
1269 return;
1270 case ISD::XOR: {
1271 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1272 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1273 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1274 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1275
1276 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001277 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001278 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1279 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1280 KnownZero = KnownZeroOut;
1281 return;
1282 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001283 case ISD::MUL: {
1284 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1285 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1286 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1287 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1288 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1289
1290 // If low bits are zero in either operand, output low known-0 bits.
1291 // Also compute a conserative estimate for high known-0 bits.
1292 // More trickiness is possible, but this is sufficient for the
1293 // interesting case of alignment computation.
1294 KnownOne.clear();
1295 unsigned TrailZ = KnownZero.countTrailingOnes() +
1296 KnownZero2.countTrailingOnes();
1297 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001298 KnownZero2.countLeadingOnes(),
1299 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001300
1301 TrailZ = std::min(TrailZ, BitWidth);
1302 LeadZ = std::min(LeadZ, BitWidth);
1303 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1304 APInt::getHighBitsSet(BitWidth, LeadZ);
1305 KnownZero &= Mask;
1306 return;
1307 }
1308 case ISD::UDIV: {
1309 // For the purposes of computing leading zeros we can conservatively
1310 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001311 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001312 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1313 ComputeMaskedBits(Op.getOperand(0),
1314 AllOnes, KnownZero2, KnownOne2, Depth+1);
1315 unsigned LeadZ = KnownZero2.countLeadingOnes();
1316
1317 KnownOne2.clear();
1318 KnownZero2.clear();
1319 ComputeMaskedBits(Op.getOperand(1),
1320 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001321 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1322 if (RHSUnknownLeadingOnes != BitWidth)
1323 LeadZ = std::min(BitWidth,
1324 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001325
1326 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1327 return;
1328 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001329 case ISD::SELECT:
1330 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1331 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1332 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1333 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1334
1335 // Only known if known in both the LHS and RHS.
1336 KnownOne &= KnownOne2;
1337 KnownZero &= KnownZero2;
1338 return;
1339 case ISD::SELECT_CC:
1340 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1341 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1342 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1343 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1344
1345 // Only known if known in both the LHS and RHS.
1346 KnownOne &= KnownOne2;
1347 KnownZero &= KnownZero2;
1348 return;
1349 case ISD::SETCC:
1350 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001351 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1352 BitWidth > 1)
1353 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001354 return;
1355 case ISD::SHL:
1356 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1357 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001358 unsigned ShAmt = SA->getValue();
1359
1360 // If the shift count is an invalid immediate, don't do anything.
1361 if (ShAmt >= BitWidth)
1362 return;
1363
1364 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001365 KnownZero, KnownOne, Depth+1);
1366 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001367 KnownZero <<= ShAmt;
1368 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001369 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001370 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001371 }
1372 return;
1373 case ISD::SRL:
1374 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1375 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001376 unsigned ShAmt = SA->getValue();
1377
Dan Gohmand4cf9922008-02-26 18:50:50 +00001378 // If the shift count is an invalid immediate, don't do anything.
1379 if (ShAmt >= BitWidth)
1380 return;
1381
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001382 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001383 KnownZero, KnownOne, Depth+1);
1384 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001385 KnownZero = KnownZero.lshr(ShAmt);
1386 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001387
Dan Gohman72d2fd52008-02-13 22:43:25 +00001388 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001389 KnownZero |= HighBits; // High bits known zero.
1390 }
1391 return;
1392 case ISD::SRA:
1393 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001394 unsigned ShAmt = SA->getValue();
1395
Dan Gohmand4cf9922008-02-26 18:50:50 +00001396 // If the shift count is an invalid immediate, don't do anything.
1397 if (ShAmt >= BitWidth)
1398 return;
1399
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001400 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001401 // If any of the demanded bits are produced by the sign extension, we also
1402 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001403 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1404 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001405 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001406
1407 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1408 Depth+1);
1409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001410 KnownZero = KnownZero.lshr(ShAmt);
1411 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001412
1413 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001414 APInt SignBit = APInt::getSignBit(BitWidth);
1415 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001416
Dan Gohmanca93a432008-02-20 16:30:17 +00001417 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001419 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001420 KnownOne |= HighBits; // New bits are known one.
1421 }
1422 }
1423 return;
1424 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001425 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1426 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001427
1428 // Sign extension. Compute the demanded bits in the result that are not
1429 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001430 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001431
Dan Gohman977a76f2008-02-13 22:28:48 +00001432 APInt InSignBit = APInt::getSignBit(EBits);
1433 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001434
1435 // If the sign extended bits are demanded, we know that the sign
1436 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001437 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001438 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 InputDemandedBits |= InSignBit;
1440
1441 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1442 KnownZero, KnownOne, Depth+1);
1443 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1444
1445 // If the sign bit of the input is known set or clear, then we know the
1446 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001447 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001448 KnownZero |= NewBits;
1449 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001450 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001451 KnownOne |= NewBits;
1452 KnownZero &= ~NewBits;
1453 } else { // Input sign bit unknown
1454 KnownZero &= ~NewBits;
1455 KnownOne &= ~NewBits;
1456 }
1457 return;
1458 }
1459 case ISD::CTTZ:
1460 case ISD::CTLZ:
1461 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001462 unsigned LowBits = Log2_32(BitWidth)+1;
1463 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001464 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 return;
1466 }
1467 case ISD::LOAD: {
1468 if (ISD::isZEXTLoad(Op.Val)) {
1469 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001470 MVT VT = LD->getMemoryVT();
1471 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001472 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001473 }
1474 return;
1475 }
1476 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001477 MVT InVT = Op.getOperand(0).getValueType();
1478 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001479 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1480 APInt InMask = Mask;
1481 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001482 KnownZero.trunc(InBits);
1483 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001484 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001485 KnownZero.zext(BitWidth);
1486 KnownOne.zext(BitWidth);
1487 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001488 return;
1489 }
1490 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001491 MVT InVT = Op.getOperand(0).getValueType();
1492 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001493 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001494 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1495 APInt InMask = Mask;
1496 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001497
1498 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001499 // bit is demanded. Temporarily set this bit in the mask for our callee.
1500 if (NewBits.getBoolValue())
1501 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001502
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001503 KnownZero.trunc(InBits);
1504 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001505 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1506
1507 // Note if the sign bit is known to be zero or one.
1508 bool SignBitKnownZero = KnownZero.isNegative();
1509 bool SignBitKnownOne = KnownOne.isNegative();
1510 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1511 "Sign bit can't be known to be both zero and one!");
1512
1513 // If the sign bit wasn't actually demanded by our caller, we don't
1514 // want it set in the KnownZero and KnownOne result values. Reset the
1515 // mask and reapply it to the result values.
1516 InMask = Mask;
1517 InMask.trunc(InBits);
1518 KnownZero &= InMask;
1519 KnownOne &= InMask;
1520
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001521 KnownZero.zext(BitWidth);
1522 KnownOne.zext(BitWidth);
1523
Dan Gohman977a76f2008-02-13 22:28:48 +00001524 // If the sign bit is known zero or one, the top bits match.
1525 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001526 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001527 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001528 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001529 return;
1530 }
1531 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 MVT InVT = Op.getOperand(0).getValueType();
1533 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001534 APInt InMask = Mask;
1535 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001536 KnownZero.trunc(InBits);
1537 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001538 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001539 KnownZero.zext(BitWidth);
1540 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001541 return;
1542 }
1543 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001544 MVT InVT = Op.getOperand(0).getValueType();
1545 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001546 APInt InMask = Mask;
1547 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001548 KnownZero.zext(InBits);
1549 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001550 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001551 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001552 KnownZero.trunc(BitWidth);
1553 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001554 break;
1555 }
1556 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001557 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1558 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001559 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1560 KnownOne, Depth+1);
1561 KnownZero |= (~InMask) & Mask;
1562 return;
1563 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001564 case ISD::FGETSIGN:
1565 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001566 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001567 return;
1568
Dan Gohman23e8b712008-04-28 17:02:21 +00001569 case ISD::SUB: {
1570 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1571 // We know that the top bits of C-X are clear if X contains less bits
1572 // than C (i.e. no wrap-around can happen). For example, 20-X is
1573 // positive if we can prove that X is >= 0 and < 16.
1574 if (CLHS->getAPIntValue().isNonNegative()) {
1575 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1576 // NLZ can't be BitWidth with no sign bit
1577 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1578 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1579 Depth+1);
1580
1581 // If all of the MaskV bits are known to be zero, then we know the
1582 // output top bits are zero, because we now know that the output is
1583 // from [0-C].
1584 if ((KnownZero2 & MaskV) == MaskV) {
1585 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1586 // Top bits known zero.
1587 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1588 }
1589 }
1590 }
1591 }
1592 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001593 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001594 // Output known-0 bits are known if clear or set in both the low clear bits
1595 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1596 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001597 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1598 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1599 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1600 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1601
1602 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1603 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1604 KnownZeroOut = std::min(KnownZeroOut,
1605 KnownZero2.countTrailingOnes());
1606
1607 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 return;
1609 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001610 case ISD::SREM:
1611 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001612 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001613 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001614 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001615 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1616 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001617
Dan Gohman23e8b712008-04-28 17:02:21 +00001618 // The sign of a remainder is equal to the sign of the first
1619 // operand (zero being positive).
1620 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1621 KnownZero2 |= ~LowBits;
1622 else if (KnownOne2[BitWidth-1])
1623 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001624
Dan Gohman23e8b712008-04-28 17:02:21 +00001625 KnownZero |= KnownZero2 & Mask;
1626 KnownOne |= KnownOne2 & Mask;
1627
1628 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001629 }
1630 }
1631 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001632 case ISD::UREM: {
1633 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001634 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001635 if (RA.isPowerOf2()) {
1636 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001637 APInt Mask2 = LowBits & Mask;
1638 KnownZero |= ~LowBits & Mask;
1639 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1640 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1641 break;
1642 }
1643 }
1644
1645 // Since the result is less than or equal to either operand, any leading
1646 // zero bits in either operand must also exist in the result.
1647 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1648 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1649 Depth+1);
1650 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1651 Depth+1);
1652
1653 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1654 KnownZero2.countLeadingOnes());
1655 KnownOne.clear();
1656 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1657 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001658 }
1659 default:
1660 // Allow the target to implement this method for its nodes.
1661 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1662 case ISD::INTRINSIC_WO_CHAIN:
1663 case ISD::INTRINSIC_W_CHAIN:
1664 case ISD::INTRINSIC_VOID:
1665 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1666 }
1667 return;
1668 }
1669}
1670
1671/// ComputeNumSignBits - Return the number of times the sign bit of the
1672/// register is replicated into the other bits. We know that at least 1 bit
1673/// is always equal to the sign bit (itself), but other cases can give us
1674/// information. For example, immediately after an "SRA X, 2", we know that
1675/// the top 3 bits are all equal to each other, so we return 3.
1676unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001677 MVT VT = Op.getValueType();
1678 assert(VT.isInteger() && "Invalid VT!");
1679 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001680 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001681 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001682
1683 if (Depth == 6)
1684 return 1; // Limit search depth.
1685
1686 switch (Op.getOpcode()) {
1687 default: break;
1688 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001689 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001690 return VTBits-Tmp+1;
1691 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001692 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001693 return VTBits-Tmp;
1694
1695 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001696 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1697 // If negative, return # leading ones.
1698 if (Val.isNegative())
1699 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001700
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001701 // Return # leading zeros.
1702 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001703 }
1704
1705 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001706 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001707 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1708
1709 case ISD::SIGN_EXTEND_INREG:
1710 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001711 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001712 Tmp = VTBits-Tmp+1;
1713
1714 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1715 return std::max(Tmp, Tmp2);
1716
1717 case ISD::SRA:
1718 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1719 // SRA X, C -> adds C sign bits.
1720 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1721 Tmp += C->getValue();
1722 if (Tmp > VTBits) Tmp = VTBits;
1723 }
1724 return Tmp;
1725 case ISD::SHL:
1726 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1727 // shl destroys sign bits.
1728 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1729 if (C->getValue() >= VTBits || // Bad shift.
1730 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1731 return Tmp - C->getValue();
1732 }
1733 break;
1734 case ISD::AND:
1735 case ISD::OR:
1736 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001737 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001738 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001739 if (Tmp != 1) {
1740 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1741 FirstAnswer = std::min(Tmp, Tmp2);
1742 // We computed what we know about the sign bits as our first
1743 // answer. Now proceed to the generic code that uses
1744 // ComputeMaskedBits, and pick whichever answer is better.
1745 }
1746 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001747
1748 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001749 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001750 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001751 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 return std::min(Tmp, Tmp2);
1753
1754 case ISD::SETCC:
1755 // If setcc returns 0/-1, all bits are sign bits.
1756 if (TLI.getSetCCResultContents() ==
1757 TargetLowering::ZeroOrNegativeOneSetCCResult)
1758 return VTBits;
1759 break;
1760 case ISD::ROTL:
1761 case ISD::ROTR:
1762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1763 unsigned RotAmt = C->getValue() & (VTBits-1);
1764
1765 // Handle rotate right by N like a rotate left by 32-N.
1766 if (Op.getOpcode() == ISD::ROTR)
1767 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1768
1769 // If we aren't rotating out all of the known-in sign bits, return the
1770 // number that are left. This handles rotl(sext(x), 1) for example.
1771 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1772 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1773 }
1774 break;
1775 case ISD::ADD:
1776 // Add can have at most one carry bit. Thus we know that the output
1777 // is, at worst, one more bit than the inputs.
1778 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1779 if (Tmp == 1) return 1; // Early out.
1780
1781 // Special case decrementing a value (ADD X, -1):
1782 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1783 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001784 APInt KnownZero, KnownOne;
1785 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001786 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1787
1788 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1789 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001790 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001791 return VTBits;
1792
1793 // If we are subtracting one from a positive number, there is no carry
1794 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001795 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001796 return Tmp;
1797 }
1798
1799 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1800 if (Tmp2 == 1) return 1;
1801 return std::min(Tmp, Tmp2)-1;
1802 break;
1803
1804 case ISD::SUB:
1805 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1806 if (Tmp2 == 1) return 1;
1807
1808 // Handle NEG.
1809 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001810 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001811 APInt KnownZero, KnownOne;
1812 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1814 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1815 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001816 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 return VTBits;
1818
1819 // If the input is known to be positive (the sign bit is known clear),
1820 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001821 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001822 return Tmp2;
1823
1824 // Otherwise, we treat this like a SUB.
1825 }
1826
1827 // Sub can have at most one carry bit. Thus we know that the output
1828 // is, at worst, one more bit than the inputs.
1829 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1830 if (Tmp == 1) return 1; // Early out.
1831 return std::min(Tmp, Tmp2)-1;
1832 break;
1833 case ISD::TRUNCATE:
1834 // FIXME: it's tricky to do anything useful for this, but it is an important
1835 // case for targets like X86.
1836 break;
1837 }
1838
1839 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1840 if (Op.getOpcode() == ISD::LOAD) {
1841 LoadSDNode *LD = cast<LoadSDNode>(Op);
1842 unsigned ExtType = LD->getExtensionType();
1843 switch (ExtType) {
1844 default: break;
1845 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001846 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001847 return VTBits-Tmp+1;
1848 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001849 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001850 return VTBits-Tmp;
1851 }
1852 }
1853
1854 // Allow the target to implement this method for its nodes.
1855 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1856 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1857 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1858 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1859 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001860 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 }
1862
1863 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1864 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001865 APInt KnownZero, KnownOne;
1866 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001867 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1868
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001869 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001871 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001872 Mask = KnownOne;
1873 } else {
1874 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001875 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001876 }
1877
1878 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1879 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001880 Mask = ~Mask;
1881 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 // Return # leading zeros. We use 'min' here in case Val was zero before
1883 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001884 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001885}
1886
Chris Lattner51dabfb2006-10-14 00:41:01 +00001887
Evan Chenga844bde2008-02-02 04:07:54 +00001888bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1889 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1890 if (!GA) return false;
1891 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1892 if (!GV) return false;
1893 MachineModuleInfo *MMI = getMachineModuleInfo();
1894 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1895}
1896
1897
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001898/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1899/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001900SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001902 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001903 SDOperand Idx = PermMask.getOperand(i);
1904 if (Idx.getOpcode() == ISD::UNDEF)
1905 return getNode(ISD::UNDEF, VT.getVectorElementType());
1906 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001907 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001908 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1909 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001910
1911 if (V.getOpcode() == ISD::BIT_CONVERT) {
1912 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001913 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001914 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001915 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001916 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001917 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001918 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001919 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001920 return V.getOperand(Index);
1921 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1922 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001923 return SDOperand();
1924}
1925
1926
Chris Lattnerc3aae252005-01-07 07:46:32 +00001927/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001928///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001929SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001930 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001931 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001932 void *IP = 0;
1933 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1934 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001935 SDNode *N = getAllocator().Allocate<SDNode>();
1936 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001937 CSEMap.InsertNode(N, IP);
1938
1939 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001940 return SDOperand(N, 0);
1941}
1942
Duncan Sands83ec4b62008-06-06 12:08:01 +00001943SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001944 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001945 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001946 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001947 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001948 switch (Opcode) {
1949 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001950 case ISD::SIGN_EXTEND:
1951 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001952 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001953 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001954 case ISD::TRUNCATE:
1955 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001956 case ISD::UINT_TO_FP:
1957 case ISD::SINT_TO_FP: {
1958 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001959 // No compile time operations on this type.
1960 if (VT==MVT::ppcf128)
1961 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001962 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1963 (void)apf.convertFromAPInt(Val,
1964 Opcode==ISD::SINT_TO_FP,
1965 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001966 return getConstantFP(apf, VT);
1967 }
Chris Lattner94683772005-12-23 05:30:37 +00001968 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001969 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001970 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001971 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001972 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001973 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001974 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001975 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001976 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001977 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001978 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001979 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001980 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001981 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001982 }
1983 }
1984
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001985 // Constant fold unary operations with a floating point constant operand.
1986 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1987 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001988 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001989 switch (Opcode) {
1990 case ISD::FNEG:
1991 V.changeSign();
1992 return getConstantFP(V, VT);
1993 case ISD::FABS:
1994 V.clearSign();
1995 return getConstantFP(V, VT);
1996 case ISD::FP_ROUND:
1997 case ISD::FP_EXTEND:
1998 // This can return overflow, underflow, or inexact; we don't care.
1999 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002000 (void)V.convert(*MVTToAPFloatSemantics(VT),
2001 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002002 return getConstantFP(V, VT);
2003 case ISD::FP_TO_SINT:
2004 case ISD::FP_TO_UINT: {
2005 integerPart x;
2006 assert(integerPartWidth >= 64);
2007 // FIXME need to be more flexible about rounding mode.
2008 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2009 Opcode==ISD::FP_TO_SINT,
2010 APFloat::rmTowardZero);
2011 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2012 break;
2013 return getConstant(x, VT);
2014 }
2015 case ISD::BIT_CONVERT:
2016 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2017 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2018 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2019 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002020 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002021 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002022 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002023 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002024
2025 unsigned OpOpcode = Operand.Val->getOpcode();
2026 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002027 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002028 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002029 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002030 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 assert(VT.isFloatingPoint() &&
2032 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002033 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002034 if (Operand.getOpcode() == ISD::UNDEF)
2035 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002036 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002037 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002038 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002039 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002040 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002041 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002042 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2044 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2045 break;
2046 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002047 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002048 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002049 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002050 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002051 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002052 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002053 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002054 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002055 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002056 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002057 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002058 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002059 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002060 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002061 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2062 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2063 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2064 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002065 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002066 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002067 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002068 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002069 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002070 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002071 if (OpOpcode == ISD::TRUNCATE)
2072 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002073 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2074 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002075 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002076 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002077 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002078 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002079 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2080 else
2081 return Operand.Val->getOperand(0);
2082 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002083 break;
Chris Lattner35481892005-12-23 00:16:34 +00002084 case ISD::BIT_CONVERT:
2085 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002086 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002087 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002088 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002089 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2090 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002091 if (OpOpcode == ISD::UNDEF)
2092 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002093 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002094 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002095 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2096 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002097 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002098 if (OpOpcode == ISD::UNDEF)
2099 return getNode(ISD::UNDEF, VT);
2100 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2101 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2102 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2103 Operand.getConstantOperandVal(1) == 0 &&
2104 Operand.getOperand(0).getValueType() == VT)
2105 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002106 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002107 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002108 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2109 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002110 Operand.Val->getOperand(0));
2111 if (OpOpcode == ISD::FNEG) // --X -> X
2112 return Operand.Val->getOperand(0);
2113 break;
2114 case ISD::FABS:
2115 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2116 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2117 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002118 }
2119
Chris Lattner43247a12005-08-25 19:12:10 +00002120 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002121 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002122 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002123 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002124 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002125 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002126 void *IP = 0;
2127 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2128 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002129 N = getAllocator().Allocate<UnarySDNode>();
2130 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002131 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002132 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002133 N = getAllocator().Allocate<UnarySDNode>();
2134 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002135 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002136 AllNodes.push_back(N);
2137 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002138}
2139
Chris Lattner36019aa2005-04-18 03:48:41 +00002140
2141
Duncan Sands83ec4b62008-06-06 12:08:01 +00002142SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002144 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2145 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002146 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002147 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002148 case ISD::TokenFactor:
2149 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2150 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002151 // Fold trivial token factors.
2152 if (N1.getOpcode() == ISD::EntryToken) return N2;
2153 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002154 break;
Chris Lattner76365122005-01-16 02:23:22 +00002155 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002157 N1.getValueType() == VT && "Binary operator types must match!");
2158 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2159 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002160 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002161 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002162 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2163 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002164 break;
Chris Lattner76365122005-01-16 02:23:22 +00002165 case ISD::OR:
2166 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002167 case ISD::ADD:
2168 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002169 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002170 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002171 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2172 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002173 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002174 return N1;
2175 break;
Chris Lattner76365122005-01-16 02:23:22 +00002176 case ISD::UDIV:
2177 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002178 case ISD::MULHU:
2179 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002180 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002181 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002182 case ISD::MUL:
2183 case ISD::SDIV:
2184 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002185 case ISD::FADD:
2186 case ISD::FSUB:
2187 case ISD::FMUL:
2188 case ISD::FDIV:
2189 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002190 assert(N1.getValueType() == N2.getValueType() &&
2191 N1.getValueType() == VT && "Binary operator types must match!");
2192 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002193 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2194 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002195 N1.getValueType().isFloatingPoint() &&
2196 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002197 "Invalid FCOPYSIGN!");
2198 break;
Chris Lattner76365122005-01-16 02:23:22 +00002199 case ISD::SHL:
2200 case ISD::SRA:
2201 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002202 case ISD::ROTL:
2203 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002204 assert(VT == N1.getValueType() &&
2205 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002207 "Shifts only work on integers");
2208
2209 // Always fold shifts of i1 values so the code generator doesn't need to
2210 // handle them. Since we know the size of the shift has to be less than the
2211 // size of the value, the shift/rotate count is guaranteed to be zero.
2212 if (VT == MVT::i1)
2213 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002214 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002215 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002217 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002218 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002219 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002220 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002221 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002222 break;
2223 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002224 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002225 assert(VT.isFloatingPoint() &&
2226 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002227 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002228 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002229 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002230 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002231 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002232 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002233 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002234 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002236 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002237 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002238 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002239 break;
2240 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002241 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002242 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002243 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002245 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002246 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002247 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002248
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002249 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002250 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002251 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002252 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002253 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002254 return getConstant(Val, VT);
2255 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002256 break;
2257 }
2258 case ISD::EXTRACT_VECTOR_ELT:
2259 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2260
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002261 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2262 if (N1.getOpcode() == ISD::UNDEF)
2263 return getNode(ISD::UNDEF, VT);
2264
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002265 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2266 // expanding copies of large vectors from registers.
2267 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2268 N1.getNumOperands() > 0) {
2269 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002270 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002271 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2272 N1.getOperand(N2C->getValue() / Factor),
2273 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2274 }
2275
2276 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2277 // expanding large vector constants.
2278 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2279 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002280
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002281 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2282 // operations are lowered to scalars.
2283 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2284 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2285 if (IEC == N2C)
2286 return N1.getOperand(1);
2287 else
2288 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2289 }
2290 break;
2291 case ISD::EXTRACT_ELEMENT:
2292 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002293 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2294 (N1.getValueType().isInteger() == VT.isInteger()) &&
2295 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002296
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002297 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2298 // 64-bit integers into 32-bit parts. Instead of building the extract of
2299 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2300 if (N1.getOpcode() == ISD::BUILD_PAIR)
2301 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002302
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002303 // EXTRACT_ELEMENT of a constant int is also very common.
2304 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002305 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002306 unsigned Shift = ElementSize * N2C->getValue();
2307 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2308 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002309 }
2310 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002311 case ISD::EXTRACT_SUBVECTOR:
2312 if (N1.getValueType() == VT) // Trivial extraction.
2313 return N1;
2314 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002315 }
2316
2317 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002318 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002319 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 switch (Opcode) {
2321 case ISD::ADD: return getConstant(C1 + C2, VT);
2322 case ISD::SUB: return getConstant(C1 - C2, VT);
2323 case ISD::MUL: return getConstant(C1 * C2, VT);
2324 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002325 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002326 break;
2327 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002328 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002329 break;
2330 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002331 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002332 break;
2333 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002334 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002335 break;
2336 case ISD::AND : return getConstant(C1 & C2, VT);
2337 case ISD::OR : return getConstant(C1 | C2, VT);
2338 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002339 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002340 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2341 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2342 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2343 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 default: break;
2345 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002346 } else { // Cannonicalize constant to RHS if commutative
2347 if (isCommutativeBinOp(Opcode)) {
2348 std::swap(N1C, N2C);
2349 std::swap(N1, N2);
2350 }
2351 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002352 }
2353
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002354 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002355 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2356 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002357 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002358 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2359 // Cannonicalize constant to RHS if commutative
2360 std::swap(N1CFP, N2CFP);
2361 std::swap(N1, N2);
2362 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002363 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2364 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002365 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002366 case ISD::FADD:
2367 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002369 return getConstantFP(V1, VT);
2370 break;
2371 case ISD::FSUB:
2372 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2373 if (s!=APFloat::opInvalidOp)
2374 return getConstantFP(V1, VT);
2375 break;
2376 case ISD::FMUL:
2377 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2378 if (s!=APFloat::opInvalidOp)
2379 return getConstantFP(V1, VT);
2380 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002381 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002382 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2383 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2384 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002386 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002387 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2388 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2389 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002390 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002391 case ISD::FCOPYSIGN:
2392 V1.copySign(V2);
2393 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002394 default: break;
2395 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002396 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002397 }
Chris Lattner62b57722006-04-20 05:39:12 +00002398
2399 // Canonicalize an UNDEF to the RHS, even over a constant.
2400 if (N1.getOpcode() == ISD::UNDEF) {
2401 if (isCommutativeBinOp(Opcode)) {
2402 std::swap(N1, N2);
2403 } else {
2404 switch (Opcode) {
2405 case ISD::FP_ROUND_INREG:
2406 case ISD::SIGN_EXTEND_INREG:
2407 case ISD::SUB:
2408 case ISD::FSUB:
2409 case ISD::FDIV:
2410 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002411 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002412 return N1; // fold op(undef, arg2) -> undef
2413 case ISD::UDIV:
2414 case ISD::SDIV:
2415 case ISD::UREM:
2416 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002417 case ISD::SRL:
2418 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002419 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002420 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2421 // For vectors, we can't easily build an all zero vector, just return
2422 // the LHS.
2423 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002424 }
2425 }
2426 }
2427
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002428 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002429 if (N2.getOpcode() == ISD::UNDEF) {
2430 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002431 case ISD::XOR:
2432 if (N1.getOpcode() == ISD::UNDEF)
2433 // Handle undef ^ undef -> 0 special case. This is a common
2434 // idiom (misuse).
2435 return getConstant(0, VT);
2436 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002437 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002438 case ISD::ADDC:
2439 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002440 case ISD::SUB:
2441 case ISD::FADD:
2442 case ISD::FSUB:
2443 case ISD::FMUL:
2444 case ISD::FDIV:
2445 case ISD::FREM:
2446 case ISD::UDIV:
2447 case ISD::SDIV:
2448 case ISD::UREM:
2449 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002450 return N2; // fold op(arg1, undef) -> undef
2451 case ISD::MUL:
2452 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002453 case ISD::SRL:
2454 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002455 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002456 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2457 // For vectors, we can't easily build an all zero vector, just return
2458 // the LHS.
2459 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002460 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002461 if (!VT.isVector())
2462 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002463 // For vectors, we can't easily build an all one vector, just return
2464 // the LHS.
2465 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002466 case ISD::SRA:
2467 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002468 }
2469 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002470
Chris Lattner27e9b412005-05-11 18:57:39 +00002471 // Memoize this node if possible.
2472 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002473 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002474 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002475 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002476 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002477 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002478 void *IP = 0;
2479 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2480 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002481 N = getAllocator().Allocate<BinarySDNode>();
2482 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002483 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002484 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002485 N = getAllocator().Allocate<BinarySDNode>();
2486 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002487 }
2488
Chris Lattnerc3aae252005-01-07 07:46:32 +00002489 AllNodes.push_back(N);
2490 return SDOperand(N, 0);
2491}
2492
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002494 SDOperand N1, SDOperand N2, SDOperand N3) {
2495 // Perform various simplifications.
2496 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2497 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002498 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002499 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002500 // Use FoldSetCC to simplify SETCC's.
2501 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002502 if (Simp.Val) return Simp;
2503 break;
2504 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002505 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002506 if (N1C) {
2507 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002508 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002509 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002510 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002511 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002512
2513 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002514 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002515 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002516 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002517 if (N2C->getValue()) // Unconditional branch
2518 return getNode(ISD::BR, MVT::Other, N1, N3);
2519 else
2520 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002521 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002522 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002523 case ISD::VECTOR_SHUFFLE:
2524 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002525 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002526 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002527 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002528 "Illegal VECTOR_SHUFFLE node!");
2529 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002530 case ISD::BIT_CONVERT:
2531 // Fold bit_convert nodes from a type to themselves.
2532 if (N1.getValueType() == VT)
2533 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002534 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002535 }
2536
Chris Lattner43247a12005-08-25 19:12:10 +00002537 // Memoize node if it doesn't produce a flag.
2538 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002539 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002540 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002541 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002542 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002543 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002544 void *IP = 0;
2545 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2546 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002547 N = getAllocator().Allocate<TernarySDNode>();
2548 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002549 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002550 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002551 N = getAllocator().Allocate<TernarySDNode>();
2552 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002553 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002554 AllNodes.push_back(N);
2555 return SDOperand(N, 0);
2556}
2557
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002559 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002560 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002561 SDOperand Ops[] = { N1, N2, N3, N4 };
2562 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002563}
2564
Duncan Sands83ec4b62008-06-06 12:08:01 +00002565SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002566 SDOperand N1, SDOperand N2, SDOperand N3,
2567 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002568 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2569 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002570}
2571
Dan Gohman707e0182008-04-12 04:36:06 +00002572/// getMemsetValue - Vectorized representation of the memset value
2573/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002574static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2575 unsigned NumBits = VT.isVector() ?
2576 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002577 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002578 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002579 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002580 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002581 Val = (Val << Shift) | Val;
2582 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002583 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002584 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002585 return DAG.getConstant(Val, VT);
2586 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002587 }
Evan Chengf0df0312008-05-15 08:39:06 +00002588
2589 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2590 unsigned Shift = 8;
2591 for (unsigned i = NumBits; i > 8; i >>= 1) {
2592 Value = DAG.getNode(ISD::OR, VT,
2593 DAG.getNode(ISD::SHL, VT, Value,
2594 DAG.getConstant(Shift, MVT::i8)), Value);
2595 Shift <<= 1;
2596 }
2597
2598 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002599}
2600
Dan Gohman707e0182008-04-12 04:36:06 +00002601/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2602/// used when a memcpy is turned into a memset when the source is a constant
2603/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002604static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002605 const TargetLowering &TLI,
2606 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002607 // Handle vector with all elements zero.
2608 if (Str.empty()) {
2609 if (VT.isInteger())
2610 return DAG.getConstant(0, VT);
2611 unsigned NumElts = VT.getVectorNumElements();
2612 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2613 return DAG.getNode(ISD::BIT_CONVERT, VT,
2614 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2615 }
2616
Duncan Sands83ec4b62008-06-06 12:08:01 +00002617 assert(!VT.isVector() && "Can't handle vector type here!");
2618 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002619 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002620 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002621 if (TLI.isLittleEndian())
2622 Offset = Offset + MSB - 1;
2623 for (unsigned i = 0; i != MSB; ++i) {
2624 Val = (Val << 8) | (unsigned char)Str[Offset];
2625 Offset += TLI.isLittleEndian() ? -1 : 1;
2626 }
2627 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002628}
2629
Dan Gohman707e0182008-04-12 04:36:06 +00002630/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002631///
Dan Gohman707e0182008-04-12 04:36:06 +00002632static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2633 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002634 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002635 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2636}
2637
Evan Chengf0df0312008-05-15 08:39:06 +00002638/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2639///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002640static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002641 unsigned SrcDelta = 0;
2642 GlobalAddressSDNode *G = NULL;
2643 if (Src.getOpcode() == ISD::GlobalAddress)
2644 G = cast<GlobalAddressSDNode>(Src);
2645 else if (Src.getOpcode() == ISD::ADD &&
2646 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2647 Src.getOperand(1).getOpcode() == ISD::Constant) {
2648 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2649 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2650 }
2651 if (!G)
2652 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002653
Evan Chengf0df0312008-05-15 08:39:06 +00002654 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002655 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2656 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002657
Evan Chengf0df0312008-05-15 08:39:06 +00002658 return false;
2659}
Dan Gohman707e0182008-04-12 04:36:06 +00002660
Evan Chengf0df0312008-05-15 08:39:06 +00002661/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2662/// to replace the memset / memcpy is below the threshold. It also returns the
2663/// types of the sequence of memory ops to perform memset / memcpy.
2664static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002665bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002666 SDOperand Dst, SDOperand Src,
2667 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002668 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002669 SelectionDAG &DAG,
2670 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002671 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002672 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002673 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002674 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002675 if (VT != MVT::iAny) {
2676 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002678 // If source is a string constant, this will require an unaligned load.
2679 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2680 if (Dst.getOpcode() != ISD::FrameIndex) {
2681 // Can't change destination alignment. It requires a unaligned store.
2682 if (AllowUnalign)
2683 VT = MVT::iAny;
2684 } else {
2685 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2686 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2687 if (MFI->isFixedObjectIndex(FI)) {
2688 // Can't change destination alignment. It requires a unaligned store.
2689 if (AllowUnalign)
2690 VT = MVT::iAny;
2691 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002692 // Give the stack frame object a larger alignment if needed.
2693 if (MFI->getObjectAlignment(FI) < NewAlign)
2694 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002695 Align = NewAlign;
2696 }
2697 }
2698 }
2699 }
2700
2701 if (VT == MVT::iAny) {
2702 if (AllowUnalign) {
2703 VT = MVT::i64;
2704 } else {
2705 switch (Align & 7) {
2706 case 0: VT = MVT::i64; break;
2707 case 4: VT = MVT::i32; break;
2708 case 2: VT = MVT::i16; break;
2709 default: VT = MVT::i8; break;
2710 }
2711 }
2712
Duncan Sands83ec4b62008-06-06 12:08:01 +00002713 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002714 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002715 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2716 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002717
Duncan Sands8e4eb092008-06-08 20:54:56 +00002718 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002719 VT = LVT;
2720 }
Dan Gohman707e0182008-04-12 04:36:06 +00002721
2722 unsigned NumMemOps = 0;
2723 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002724 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002725 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002726 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002727 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002728 VT = MVT::i64;
2729 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002730 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2731 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002732 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002733 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002734 VTSize >>= 1;
2735 }
Dan Gohman707e0182008-04-12 04:36:06 +00002736 }
Dan Gohman707e0182008-04-12 04:36:06 +00002737
2738 if (++NumMemOps > Limit)
2739 return false;
2740 MemOps.push_back(VT);
2741 Size -= VTSize;
2742 }
2743
2744 return true;
2745}
2746
2747static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2748 SDOperand Chain, SDOperand Dst,
2749 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002750 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002751 const Value *DstSV, uint64_t DstSVOff,
2752 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002753 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2754
Dan Gohman21323f32008-05-29 19:42:22 +00002755 // Expand memcpy to a series of load and store ops if the size operand falls
2756 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002757 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002758 uint64_t Limit = -1;
2759 if (!AlwaysInline)
2760 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002761 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002762 std::string Str;
2763 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002764 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002765 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002766 return SDOperand();
2767
Dan Gohman707e0182008-04-12 04:36:06 +00002768
Evan Cheng0ff39b32008-06-30 07:31:25 +00002769 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002770 SmallVector<SDOperand, 8> OutChains;
2771 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002772 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002773 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 MVT VT = MemOps[i];
2775 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002776 SDOperand Value, Store;
2777
Evan Cheng0ff39b32008-06-30 07:31:25 +00002778 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002779 // It's unlikely a store of a vector immediate can be done in a single
2780 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002781 // We also handle store a vector with all zero's.
2782 // FIXME: Handle other cases where store of vector immediate is done in
2783 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002784 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002785 Store = DAG.getStore(Chain, Value,
2786 getMemBasePlusOffset(Dst, DstOff, DAG),
2787 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002788 } else {
2789 Value = DAG.getLoad(VT, Chain,
2790 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002791 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002792 Store = DAG.getStore(Chain, Value,
2793 getMemBasePlusOffset(Dst, DstOff, DAG),
2794 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002795 }
2796 OutChains.push_back(Store);
2797 SrcOff += VTSize;
2798 DstOff += VTSize;
2799 }
2800
2801 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2802 &OutChains[0], OutChains.size());
2803}
2804
Dan Gohman21323f32008-05-29 19:42:22 +00002805static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2806 SDOperand Chain, SDOperand Dst,
2807 SDOperand Src, uint64_t Size,
2808 unsigned Align, bool AlwaysInline,
2809 const Value *DstSV, uint64_t DstSVOff,
2810 const Value *SrcSV, uint64_t SrcSVOff){
2811 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2812
2813 // Expand memmove to a series of load and store ops if the size operand falls
2814 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002815 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002816 uint64_t Limit = -1;
2817 if (!AlwaysInline)
2818 Limit = TLI.getMaxStoresPerMemmove();
2819 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002820 std::string Str;
2821 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002822 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002823 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002824 return SDOperand();
2825
Dan Gohman21323f32008-05-29 19:42:22 +00002826 uint64_t SrcOff = 0, DstOff = 0;
2827
2828 SmallVector<SDOperand, 8> LoadValues;
2829 SmallVector<SDOperand, 8> LoadChains;
2830 SmallVector<SDOperand, 8> OutChains;
2831 unsigned NumMemOps = MemOps.size();
2832 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002833 MVT VT = MemOps[i];
2834 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002835 SDOperand Value, Store;
2836
2837 Value = DAG.getLoad(VT, Chain,
2838 getMemBasePlusOffset(Src, SrcOff, DAG),
2839 SrcSV, SrcSVOff + SrcOff, false, Align);
2840 LoadValues.push_back(Value);
2841 LoadChains.push_back(Value.getValue(1));
2842 SrcOff += VTSize;
2843 }
2844 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2845 &LoadChains[0], LoadChains.size());
2846 OutChains.clear();
2847 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002848 MVT VT = MemOps[i];
2849 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002850 SDOperand Value, Store;
2851
2852 Store = DAG.getStore(Chain, LoadValues[i],
2853 getMemBasePlusOffset(Dst, DstOff, DAG),
2854 DstSV, DstSVOff + DstOff, false, DstAlign);
2855 OutChains.push_back(Store);
2856 DstOff += VTSize;
2857 }
2858
2859 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2860 &OutChains[0], OutChains.size());
2861}
2862
Dan Gohman707e0182008-04-12 04:36:06 +00002863static SDOperand getMemsetStores(SelectionDAG &DAG,
2864 SDOperand Chain, SDOperand Dst,
2865 SDOperand Src, uint64_t Size,
2866 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002867 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002868 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2869
2870 // Expand memset to a series of load/store ops if the size operand
2871 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002872 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002873 std::string Str;
2874 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002875 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002876 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002877 return SDOperand();
2878
2879 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002880 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002881
2882 unsigned NumMemOps = MemOps.size();
2883 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 MVT VT = MemOps[i];
2885 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002886 SDOperand Value = getMemsetValue(Src, VT, DAG);
2887 SDOperand Store = DAG.getStore(Chain, Value,
2888 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002889 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002890 OutChains.push_back(Store);
2891 DstOff += VTSize;
2892 }
2893
2894 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2895 &OutChains[0], OutChains.size());
2896}
2897
2898SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002899 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002900 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002901 const Value *DstSV, uint64_t DstSVOff,
2902 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002903
2904 // Check to see if we should lower the memcpy to loads and stores first.
2905 // For cases within the target-specified limits, this is the best choice.
2906 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2907 if (ConstantSize) {
2908 // Memcpy with size zero? Just return the original chain.
2909 if (ConstantSize->isNullValue())
2910 return Chain;
2911
2912 SDOperand Result =
2913 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002914 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002915 if (Result.Val)
2916 return Result;
2917 }
2918
2919 // Then check to see if we should lower the memcpy with target-specific
2920 // code. If the target chooses to do this, this is the next best.
2921 SDOperand Result =
2922 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2923 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002924 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002925 if (Result.Val)
2926 return Result;
2927
2928 // If we really need inline code and the target declined to provide it,
2929 // use a (potentially long) sequence of loads and stores.
2930 if (AlwaysInline) {
2931 assert(ConstantSize && "AlwaysInline requires a constant size!");
2932 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2933 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002934 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002935 }
2936
2937 // Emit a library call.
2938 TargetLowering::ArgListTy Args;
2939 TargetLowering::ArgListEntry Entry;
2940 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2941 Entry.Node = Dst; Args.push_back(Entry);
2942 Entry.Node = Src; Args.push_back(Entry);
2943 Entry.Node = Size; Args.push_back(Entry);
2944 std::pair<SDOperand,SDOperand> CallResult =
2945 TLI.LowerCallTo(Chain, Type::VoidTy,
2946 false, false, false, CallingConv::C, false,
2947 getExternalSymbol("memcpy", TLI.getPointerTy()),
2948 Args, *this);
2949 return CallResult.second;
2950}
2951
2952SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2953 SDOperand Src, SDOperand Size,
2954 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002955 const Value *DstSV, uint64_t DstSVOff,
2956 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002957
Dan Gohman21323f32008-05-29 19:42:22 +00002958 // Check to see if we should lower the memmove to loads and stores first.
2959 // For cases within the target-specified limits, this is the best choice.
2960 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2961 if (ConstantSize) {
2962 // Memmove with size zero? Just return the original chain.
2963 if (ConstantSize->isNullValue())
2964 return Chain;
2965
2966 SDOperand Result =
2967 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2968 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2969 if (Result.Val)
2970 return Result;
2971 }
Dan Gohman707e0182008-04-12 04:36:06 +00002972
2973 // Then check to see if we should lower the memmove with target-specific
2974 // code. If the target chooses to do this, this is the next best.
2975 SDOperand Result =
2976 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002977 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002978 if (Result.Val)
2979 return Result;
2980
2981 // Emit a library call.
2982 TargetLowering::ArgListTy Args;
2983 TargetLowering::ArgListEntry Entry;
2984 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2985 Entry.Node = Dst; Args.push_back(Entry);
2986 Entry.Node = Src; Args.push_back(Entry);
2987 Entry.Node = Size; Args.push_back(Entry);
2988 std::pair<SDOperand,SDOperand> CallResult =
2989 TLI.LowerCallTo(Chain, Type::VoidTy,
2990 false, false, false, CallingConv::C, false,
2991 getExternalSymbol("memmove", TLI.getPointerTy()),
2992 Args, *this);
2993 return CallResult.second;
2994}
2995
2996SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2997 SDOperand Src, SDOperand Size,
2998 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002999 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003000
3001 // Check to see if we should lower the memset to stores first.
3002 // For cases within the target-specified limits, this is the best choice.
3003 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3004 if (ConstantSize) {
3005 // Memset with size zero? Just return the original chain.
3006 if (ConstantSize->isNullValue())
3007 return Chain;
3008
3009 SDOperand Result =
3010 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003011 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003012 if (Result.Val)
3013 return Result;
3014 }
3015
3016 // Then check to see if we should lower the memset with target-specific
3017 // code. If the target chooses to do this, this is the next best.
3018 SDOperand Result =
3019 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003020 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003021 if (Result.Val)
3022 return Result;
3023
3024 // Emit a library call.
3025 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3026 TargetLowering::ArgListTy Args;
3027 TargetLowering::ArgListEntry Entry;
3028 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3029 Args.push_back(Entry);
3030 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003031 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003032 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3033 else
3034 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3035 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3036 Args.push_back(Entry);
3037 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3038 Args.push_back(Entry);
3039 std::pair<SDOperand,SDOperand> CallResult =
3040 TLI.LowerCallTo(Chain, Type::VoidTy,
3041 false, false, false, CallingConv::C, false,
3042 getExternalSymbol("memset", TLI.getPointerTy()),
3043 Args, *this);
3044 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003045}
3046
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003047SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003048 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003049 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003050 unsigned Alignment) {
3051 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003052 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3053 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003054 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003055 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003056 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003057 void* IP = 0;
3058 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3059 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003060 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3061 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003062 CSEMap.InsertNode(N, IP);
3063 AllNodes.push_back(N);
3064 return SDOperand(N, 0);
3065}
3066
3067SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003068 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003069 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003070 unsigned Alignment) {
3071 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003072 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3073 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003074 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003075 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3076 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003077 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003078 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003079 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003080 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003081 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003082 void* IP = 0;
3083 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3084 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003085 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3086 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003087 CSEMap.InsertNode(N, IP);
3088 AllNodes.push_back(N);
3089 return SDOperand(N, 0);
3090}
3091
Duncan Sands4bdcb612008-07-02 17:40:58 +00003092/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3093/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003094SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003095 bool Simplify) {
3096 if (Simplify && NumOps == 1)
3097 return Ops[0];
3098
3099 SmallVector<MVT, 4> VTs;
3100 VTs.reserve(NumOps);
3101 for (unsigned i = 0; i < NumOps; ++i)
3102 VTs.push_back(Ops[i].getValueType());
3103 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3104}
3105
Duncan Sands14ea39c2008-03-27 20:23:40 +00003106SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003107SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003108 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003109 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003111 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003112 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3113 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003114 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003115 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003116 } else if (SV) {
3117 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3118 assert(PT && "Value for load must be a pointer");
3119 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003120 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003121 assert(Ty && "Could not get type information for load");
3122 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3123 }
Evan Cheng466685d2006-10-09 20:57:25 +00003124
Duncan Sands14ea39c2008-03-27 20:23:40 +00003125 if (VT == EVT) {
3126 ExtType = ISD::NON_EXTLOAD;
3127 } else if (ExtType == ISD::NON_EXTLOAD) {
3128 assert(VT == EVT && "Non-extending load from different memory type!");
3129 } else {
3130 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003131 if (VT.isVector())
3132 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003133 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003134 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003135 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003136 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003137 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003138 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003139 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003140 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003141
3142 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003143 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003144 "Unindexed load with an offset!");
3145
3146 SDVTList VTs = Indexed ?
3147 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3148 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003149 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003150 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003151 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003152 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003153 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003154 ID.AddInteger(Alignment);
3155 ID.AddInteger(isVolatile);
3156 void *IP = 0;
3157 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3158 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003159 SDNode *N = getAllocator().Allocate<LoadSDNode>();
3160 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3161 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003162 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003163 AllNodes.push_back(N);
3164 return SDOperand(N, 0);
3165}
3166
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003168 SDOperand Chain, SDOperand Ptr,
3169 const Value *SV, int SVOffset,
3170 bool isVolatile, unsigned Alignment) {
3171 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003172 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3173 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003174}
3175
Duncan Sands83ec4b62008-06-06 12:08:01 +00003176SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003177 SDOperand Chain, SDOperand Ptr,
3178 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003179 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003180 bool isVolatile, unsigned Alignment) {
3181 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003182 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3183 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003184}
3185
Evan Cheng144d8f02006-11-09 17:55:04 +00003186SDOperand
3187SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3188 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003189 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003190 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3191 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003192 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3193 LD->getChain(), Base, Offset, LD->getSrcValue(),
3194 LD->getSrcValueOffset(), LD->getMemoryVT(),
3195 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003196}
3197
Jeff Cohend41b30d2006-11-05 19:31:28 +00003198SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003199 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003200 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003201 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003202
Dan Gohman575e2f42007-06-04 15:49:41 +00003203 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3204 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003205 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003207 } else if (SV) {
3208 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3209 assert(PT && "Value for store must be a pointer");
3210 Ty = PT->getElementType();
3211 }
3212 assert(Ty && "Could not get type information for store");
3213 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3214 }
Evan Chengad071e12006-10-05 22:57:11 +00003215 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003216 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003217 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003218 FoldingSetNodeID ID;
3219 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003220 ID.AddInteger(ISD::UNINDEXED);
3221 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003222 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003223 ID.AddInteger(Alignment);
3224 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003225 void *IP = 0;
3226 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3227 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003228 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3229 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3230 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003231 CSEMap.InsertNode(N, IP);
3232 AllNodes.push_back(N);
3233 return SDOperand(N, 0);
3234}
3235
Jeff Cohend41b30d2006-11-05 19:31:28 +00003236SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003237 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003239 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003240 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003241
3242 if (VT == SVT)
3243 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003244
Duncan Sands8e4eb092008-06-08 20:54:56 +00003245 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003246 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003247 "Can't do FP-INT conversion!");
3248
Dan Gohman575e2f42007-06-04 15:49:41 +00003249 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3250 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003251 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003252 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003253 } else if (SV) {
3254 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3255 assert(PT && "Value for store must be a pointer");
3256 Ty = PT->getElementType();
3257 }
3258 assert(Ty && "Could not get type information for store");
3259 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3260 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003261 SDVTList VTs = getVTList(MVT::Other);
3262 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003263 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003264 FoldingSetNodeID ID;
3265 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003266 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003267 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003268 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003269 ID.AddInteger(Alignment);
3270 ID.AddInteger(isVolatile);
3271 void *IP = 0;
3272 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3273 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003274 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3275 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3276 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003277 CSEMap.InsertNode(N, IP);
3278 AllNodes.push_back(N);
3279 return SDOperand(N, 0);
3280}
3281
Evan Cheng144d8f02006-11-09 17:55:04 +00003282SDOperand
3283SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3284 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003285 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3286 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3287 "Store is already a indexed store!");
3288 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3289 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3290 FoldingSetNodeID ID;
3291 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3292 ID.AddInteger(AM);
3293 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003294 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003295 ID.AddInteger(ST->getAlignment());
3296 ID.AddInteger(ST->isVolatile());
3297 void *IP = 0;
3298 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3299 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003300 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3301 new (N) StoreSDNode(Ops, VTs, AM,
3302 ST->isTruncatingStore(), ST->getMemoryVT(),
3303 ST->getSrcValue(), ST->getSrcValueOffset(),
3304 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003305 CSEMap.InsertNode(N, IP);
3306 AllNodes.push_back(N);
3307 return SDOperand(N, 0);
3308}
3309
Duncan Sands83ec4b62008-06-06 12:08:01 +00003310SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003311 SDOperand Chain, SDOperand Ptr,
3312 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003313 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003314 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003315}
3316
Duncan Sands83ec4b62008-06-06 12:08:01 +00003317SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003318 const SDUse *Ops, unsigned NumOps) {
3319 switch (NumOps) {
3320 case 0: return getNode(Opcode, VT);
3321 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3322 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3323 Ops[1].getSDOperand());
3324 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3325 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3326 default: break;
3327 }
3328
3329 // Copy from an SDUse array into an SDOperand array for use with
3330 // the regular getNode logic.
3331 SmallVector<SDOperand, 8> NewOps;
3332 NewOps.reserve(NumOps);
3333 for (unsigned i = 0; i != NumOps; ++i)
3334 NewOps.push_back(Ops[i].getSDOperand());
3335 return getNode(Opcode, VT, Ops, NumOps);
3336}
3337
3338SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3339 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003340 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003341 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003342 case 1: return getNode(Opcode, VT, Ops[0]);
3343 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3344 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003345 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003346 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003347
Chris Lattneref847df2005-04-09 03:27:28 +00003348 switch (Opcode) {
3349 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003350 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003351 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003352 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3353 "LHS and RHS of condition must have same type!");
3354 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3355 "True and False arms of SelectCC must have same type!");
3356 assert(Ops[2].getValueType() == VT &&
3357 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003358 break;
3359 }
3360 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003361 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003362 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3363 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003364 break;
3365 }
Chris Lattneref847df2005-04-09 03:27:28 +00003366 }
3367
Chris Lattner385328c2005-05-14 07:42:29 +00003368 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003369 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003370 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003371 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003372 FoldingSetNodeID ID;
3373 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003374 void *IP = 0;
3375 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3376 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003377 N = getAllocator().Allocate<SDNode>();
3378 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003379 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003380 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003381 N = getAllocator().Allocate<SDNode>();
3382 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003383 }
Chris Lattneref847df2005-04-09 03:27:28 +00003384 AllNodes.push_back(N);
3385 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003386}
3387
Chris Lattner89c34632005-05-14 06:20:26 +00003388SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003390 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003391 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3392 Ops, NumOps);
3393}
3394
3395SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003397 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003398 if (NumVTs == 1)
3399 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003400 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3401}
3402
3403SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003404 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003405 if (VTList.NumVTs == 1)
3406 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003407
Chris Lattner5f056bf2005-07-10 01:55:33 +00003408 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003409 // FIXME: figure out how to safely handle things like
3410 // int foo(int x) { return 1 << (x & 255); }
3411 // int bar() { return foo(256); }
3412#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003413 case ISD::SRA_PARTS:
3414 case ISD::SRL_PARTS:
3415 case ISD::SHL_PARTS:
3416 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003417 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003418 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3419 else if (N3.getOpcode() == ISD::AND)
3420 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3421 // If the and is only masking out bits that cannot effect the shift,
3422 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003423 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003424 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3425 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3426 }
3427 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003428#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003429 }
Chris Lattner89c34632005-05-14 06:20:26 +00003430
Chris Lattner43247a12005-08-25 19:12:10 +00003431 // Memoize the node unless it returns a flag.
3432 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003433 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003434 FoldingSetNodeID ID;
3435 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003436 void *IP = 0;
3437 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3438 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003439 if (NumOps == 1) {
3440 N = getAllocator().Allocate<UnarySDNode>();
3441 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3442 } else if (NumOps == 2) {
3443 N = getAllocator().Allocate<BinarySDNode>();
3444 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3445 } else if (NumOps == 3) {
3446 N = getAllocator().Allocate<TernarySDNode>();
3447 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3448 } else {
3449 N = getAllocator().Allocate<SDNode>();
3450 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3451 }
Chris Lattnera5682852006-08-07 23:03:03 +00003452 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003453 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003454 if (NumOps == 1) {
3455 N = getAllocator().Allocate<UnarySDNode>();
3456 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3457 } else if (NumOps == 2) {
3458 N = getAllocator().Allocate<BinarySDNode>();
3459 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3460 } else if (NumOps == 3) {
3461 N = getAllocator().Allocate<TernarySDNode>();
3462 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3463 } else {
3464 N = getAllocator().Allocate<SDNode>();
3465 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3466 }
Chris Lattner43247a12005-08-25 19:12:10 +00003467 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003468 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003469 return SDOperand(N, 0);
3470}
3471
Dan Gohman08ce9762007-10-08 15:49:58 +00003472SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003473 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003474}
3475
3476SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3477 SDOperand N1) {
3478 SDOperand Ops[] = { N1 };
3479 return getNode(Opcode, VTList, Ops, 1);
3480}
3481
3482SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3483 SDOperand N1, SDOperand N2) {
3484 SDOperand Ops[] = { N1, N2 };
3485 return getNode(Opcode, VTList, Ops, 2);
3486}
3487
3488SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3489 SDOperand N1, SDOperand N2, SDOperand N3) {
3490 SDOperand Ops[] = { N1, N2, N3 };
3491 return getNode(Opcode, VTList, Ops, 3);
3492}
3493
3494SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3495 SDOperand N1, SDOperand N2, SDOperand N3,
3496 SDOperand N4) {
3497 SDOperand Ops[] = { N1, N2, N3, N4 };
3498 return getNode(Opcode, VTList, Ops, 4);
3499}
3500
3501SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3502 SDOperand N1, SDOperand N2, SDOperand N3,
3503 SDOperand N4, SDOperand N5) {
3504 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3505 return getNode(Opcode, VTList, Ops, 5);
3506}
3507
Duncan Sands83ec4b62008-06-06 12:08:01 +00003508SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003509 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003510}
3511
Duncan Sands83ec4b62008-06-06 12:08:01 +00003512SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3513 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003514 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003515 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003516 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003517 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003518 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003519 V.push_back(VT1);
3520 V.push_back(VT2);
3521 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003522 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003523}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3525 MVT VT3) {
3526 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003527 E = VTList.end(); I != E; ++I) {
3528 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3529 (*I)[2] == VT3)
3530 return makeVTList(&(*I)[0], 3);
3531 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003532 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003533 V.push_back(VT1);
3534 V.push_back(VT2);
3535 V.push_back(VT3);
3536 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003537 return makeVTList(&(*VTList.begin())[0], 3);
3538}
3539
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003541 switch (NumVTs) {
3542 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003543 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003544 case 2: return getVTList(VTs[0], VTs[1]);
3545 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3546 default: break;
3547 }
3548
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003550 E = VTList.end(); I != E; ++I) {
3551 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3552
3553 bool NoMatch = false;
3554 for (unsigned i = 2; i != NumVTs; ++i)
3555 if (VTs[i] != (*I)[i]) {
3556 NoMatch = true;
3557 break;
3558 }
3559 if (!NoMatch)
3560 return makeVTList(&*I->begin(), NumVTs);
3561 }
3562
Duncan Sands83ec4b62008-06-06 12:08:01 +00003563 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003564 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003565}
3566
3567
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003568/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3569/// specified operands. If the resultant node already exists in the DAG,
3570/// this does not modify the specified node, instead it returns the node that
3571/// already exists. If the resultant node does not exist in the DAG, the
3572/// input node is returned. As a degenerate case, if you specify the same
3573/// input operands as the node already has, the input node is returned.
3574SDOperand SelectionDAG::
3575UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3576 SDNode *N = InN.Val;
3577 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3578
3579 // Check to see if there is no change.
3580 if (Op == N->getOperand(0)) return InN;
3581
3582 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003583 void *InsertPos = 0;
3584 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3585 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003586
3587 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003588 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003589 RemoveNodeFromCSEMaps(N);
3590
3591 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003592 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003593 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003594 N->OperandList[0].setUser(N);
3595 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003596
3597 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003598 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003599 return InN;
3600}
3601
3602SDOperand SelectionDAG::
3603UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3604 SDNode *N = InN.Val;
3605 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3606
3607 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003608 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3609 return InN; // No operands changed, just return the input node.
3610
3611 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003612 void *InsertPos = 0;
3613 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3614 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003615
3616 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003617 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003618 RemoveNodeFromCSEMaps(N);
3619
3620 // Now we update the operands.
3621 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003622 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003623 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003624 N->OperandList[0].setUser(N);
3625 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003626 }
3627 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003628 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003629 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003630 N->OperandList[1].setUser(N);
3631 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003632 }
3633
3634 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003635 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003636 return InN;
3637}
3638
3639SDOperand SelectionDAG::
3640UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003641 SDOperand Ops[] = { Op1, Op2, Op3 };
3642 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003643}
3644
3645SDOperand SelectionDAG::
3646UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3647 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003648 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3649 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003650}
3651
3652SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003653UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3654 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003655 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3656 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003657}
3658
Chris Lattner809ec112006-01-28 10:09:25 +00003659SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003660UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003661 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003662 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003663 "Update with wrong number of operands");
3664
3665 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003666 bool AnyChange = false;
3667 for (unsigned i = 0; i != NumOps; ++i) {
3668 if (Ops[i] != N->getOperand(i)) {
3669 AnyChange = true;
3670 break;
3671 }
3672 }
3673
3674 // No operands changed, just return the input node.
3675 if (!AnyChange) return InN;
3676
3677 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003678 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003679 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003680 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003681
Dan Gohman7ceda162008-05-02 00:05:03 +00003682 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003683 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003684 RemoveNodeFromCSEMaps(N);
3685
3686 // Now we update the operands.
3687 for (unsigned i = 0; i != NumOps; ++i) {
3688 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003689 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003690 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003691 N->OperandList[i].setUser(N);
3692 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003693 }
3694 }
3695
3696 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003697 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003698 return InN;
3699}
3700
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003701/// MorphNodeTo - This frees the operands of the current node, resets the
3702/// opcode, types, and operands to the specified value. This should only be
3703/// used by the SelectionDAG class.
3704void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003705 const SDOperand *Ops, unsigned NumOps,
3706 SmallVectorImpl<SDNode *> &DeadNodes) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003707 NodeType = Opc;
3708 ValueList = L.VTs;
3709 NumValues = L.NumVTs;
3710
3711 // Clear the operands list, updating used nodes to remove this from their
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003712 // use list. Keep track of any operands that become dead as a result.
3713 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3714 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) {
3715 SDNode *N = I->getVal();
3716 N->removeUser(std::distance(op_begin(), I), this);
3717 if (N->use_empty())
3718 DeadNodeSet.insert(N);
3719 }
3720
Chris Lattner63e3f142007-02-04 07:28:00 +00003721 // If NumOps is larger than the # of operands we currently have, reallocate
3722 // the operand list.
3723 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003724 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003725 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003726 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003727 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003728 OperandsNeedDelete = true;
3729 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003730
3731 // Assign the new operands.
3732 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003733
3734 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3735 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003736 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003737 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003738 N->addUser(i, this);
3739 ++N->UsesSize;
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003740 DeadNodeSet.erase(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003741 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003742
3743 // Clean up any nodes that are still dead after adding the uses for the
3744 // new operands.
3745 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
3746 E = DeadNodeSet.end(); I != E; ++I)
3747 DeadNodes.push_back(*I);
3748}
3749
3750/// DropOperands - Release the operands and set this node to have
3751/// zero operands. This should only be used by HandleSDNode to clear
3752/// its operand list.
3753void SDNode::DropOperands() {
3754 assert(NodeType == ISD::HANDLENODE &&
3755 "DropOperands is for HANDLENODE only!");
3756
3757 // Unlike the code in MorphNodeTo that does this, we don't need to
3758 // watch for dead nodes here.
3759 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3760 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3761
3762 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003763}
Chris Lattner149c58c2005-08-16 18:17:10 +00003764
3765/// SelectNodeTo - These are used for target selectors to *mutate* the
3766/// specified node to have the specified return type, Target opcode, and
3767/// operands. Note that target opcodes are stored as
3768/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003769///
3770/// Note that SelectNodeTo returns the resultant node. If there is already a
3771/// node of the specified opcode and operands, it returns that node instead of
3772/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003773SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003774 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003775 SDVTList VTs = getVTList(VT);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003776 return SelectNodeTo(N, TargetOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003777}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003778
Evan Cheng95514ba2006-08-26 08:00:10 +00003779SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003780 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003781 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003782 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003783 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003784}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003785
Evan Cheng95514ba2006-08-26 08:00:10 +00003786SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003787 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003788 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003789 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003790 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003791 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003792}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003793
Evan Cheng95514ba2006-08-26 08:00:10 +00003794SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003795 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003796 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003797 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003798 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003799 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003800}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003801
Evan Cheng95514ba2006-08-26 08:00:10 +00003802SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003803 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003804 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003805 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003806 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3807}
3808
3809SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003810 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003811 unsigned NumOps) {
3812 SDVTList VTs = getVTList(VT1, VT2);
3813 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3814}
3815
3816SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3817 MVT VT1, MVT VT2) {
3818 SDVTList VTs = getVTList(VT1, VT2);
3819 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3820}
3821
3822SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003823 MVT VT1, MVT VT2, MVT VT3,
3824 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003825 SDVTList VTs = getVTList(VT1, VT2, VT3);
3826 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3827}
3828
3829SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3830 MVT VT1, MVT VT2,
3831 SDOperand Op1) {
3832 SDVTList VTs = getVTList(VT1, VT2);
3833 SDOperand Ops[] = { Op1 };
3834 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003835}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003836
Evan Cheng95514ba2006-08-26 08:00:10 +00003837SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003838 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003839 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003840 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003841 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003842 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003843}
3844
Evan Cheng95514ba2006-08-26 08:00:10 +00003845SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003847 SDOperand Op1, SDOperand Op2,
3848 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003849 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003850 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003851 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3852}
3853
3854SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003855 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003856 unsigned NumOps) {
3857 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003858 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003859 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003860 void *IP = 0;
3861 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003862 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003863
Chris Lattner0fb094f2005-11-19 01:44:53 +00003864 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003865
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003866 SmallVector<SDNode *, 16> DeadNodes;
3867 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps, DeadNodes);
3868 RemoveDeadNodes(DeadNodes);
3869
Chris Lattnera5682852006-08-07 23:03:03 +00003870 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003871 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003872}
3873
Chris Lattner0fb094f2005-11-19 01:44:53 +00003874
Evan Cheng6ae46c42006-02-09 07:15:23 +00003875/// getTargetNode - These are used for target selectors to create a new node
3876/// with specified return type(s), target opcode, and operands.
3877///
3878/// Note that getTargetNode returns the resultant node. If there is already a
3879/// node of the specified opcode and operands, it returns that node instead of
3880/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003881SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003882 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3883}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003884SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003885 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3886}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003887SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003888 SDOperand Op1, SDOperand Op2) {
3889 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3890}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003892 SDOperand Op1, SDOperand Op2,
3893 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003894 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3895}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003896SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003897 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003898 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003899}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003900SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3901 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003902 SDOperand Op;
3903 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3904}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003905SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3906 MVT VT2, SDOperand Op1) {
3907 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003908 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003909}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003910SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3911 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003912 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003913 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003914 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003915 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003916}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003917SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3918 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003919 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003920 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003921 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003922 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003923}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003924SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003925 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003926 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003927 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003928}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003929SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003930 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003931 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003932 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003933 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003934}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003935SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003936 SDOperand Op1, SDOperand Op2,
3937 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003938 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003939 SDOperand Ops[] = { Op1, Op2, Op3 };
3940 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3941}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003942SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003943 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003944 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003945 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003946}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3948 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003949 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003950 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003951 VTList.push_back(VT1);
3952 VTList.push_back(VT2);
3953 VTList.push_back(VT3);
3954 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003955 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003956 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3957}
Evan Cheng39305cf2007-10-05 01:10:49 +00003958SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003959 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003960 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003962 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3963 Ops, NumOps).Val;
3964}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003965
Evan Cheng08b11732008-03-22 01:55:50 +00003966/// getNodeIfExists - Get the specified node if it's already available, or
3967/// else return NULL.
3968SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003969 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003970 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3971 FoldingSetNodeID ID;
3972 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3973 void *IP = 0;
3974 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3975 return E;
3976 }
3977 return NULL;
3978}
3979
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003980
Evan Cheng99157a02006-08-07 22:13:29 +00003981/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003982/// This can cause recursive merging of nodes in the DAG.
3983///
Chris Lattner11d049c2008-02-03 03:35:22 +00003984/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003985///
Chris Lattner11d049c2008-02-03 03:35:22 +00003986void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003987 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003988 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003989 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003990 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003991 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003992
Chris Lattner8b8749f2005-08-17 19:00:20 +00003993 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003994 SDNode::use_iterator UI = From->use_begin();
3995 SDNode *U = UI->getUser();
3996
Chris Lattner8b8749f2005-08-17 19:00:20 +00003997 // This node is about to morph, remove its old self from the CSE maps.
3998 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003999 int operandNum = 0;
4000 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4001 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004002 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004003 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004004 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004005 I->setUser(U);
4006 To.Val->addUser(operandNum, U);
4007 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004008
4009 // Now that we have modified U, add it back to the CSE maps. If it already
4010 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004011 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004012 ReplaceAllUsesWith(U, Existing, UpdateListener);
4013 // U is now dead. Inform the listener if it exists and delete it.
4014 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004015 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004016 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004017 } else {
4018 // If the node doesn't already exist, we updated it. Inform a listener if
4019 // it exists.
4020 if (UpdateListener)
4021 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004022 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004023 }
4024}
4025
4026/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4027/// This can cause recursive merging of nodes in the DAG.
4028///
4029/// This version assumes From/To have matching types and numbers of result
4030/// values.
4031///
Chris Lattner1e111c72005-09-07 05:37:01 +00004032void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004033 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00004034 assert(From != To && "Cannot replace uses of with self");
4035 assert(From->getNumValues() == To->getNumValues() &&
4036 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004037 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004038 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
4039 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004040
4041 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004042 SDNode::use_iterator UI = From->use_begin();
4043 SDNode *U = UI->getUser();
4044
Chris Lattner8b52f212005-08-26 18:36:28 +00004045 // This node is about to morph, remove its old self from the CSE maps.
4046 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004047 int operandNum = 0;
4048 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4049 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004050 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004051 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004052 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004053 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004054 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004055
Chris Lattner8b8749f2005-08-17 19:00:20 +00004056 // Now that we have modified U, add it back to the CSE maps. If it already
4057 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004058 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004059 ReplaceAllUsesWith(U, Existing, UpdateListener);
4060 // U is now dead. Inform the listener if it exists and delete it.
4061 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004062 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004063 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004064 } else {
4065 // If the node doesn't already exist, we updated it. Inform a listener if
4066 // it exists.
4067 if (UpdateListener)
4068 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004069 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004070 }
4071}
4072
Chris Lattner8b52f212005-08-26 18:36:28 +00004073/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4074/// This can cause recursive merging of nodes in the DAG.
4075///
4076/// This version can replace From with any result values. To must match the
4077/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004078void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004079 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004080 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004081 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004083
4084 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004085 SDNode::use_iterator UI = From->use_begin();
4086 SDNode *U = UI->getUser();
4087
Chris Lattner7b2880c2005-08-24 22:44:39 +00004088 // This node is about to morph, remove its old self from the CSE maps.
4089 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004090 int operandNum = 0;
4091 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4092 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004093 if (I->getVal() == From) {
4094 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004095 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004096 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004097 I->setUser(U);
4098 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004099 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004100
Chris Lattner7b2880c2005-08-24 22:44:39 +00004101 // Now that we have modified U, add it back to the CSE maps. If it already
4102 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004103 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004104 ReplaceAllUsesWith(U, Existing, UpdateListener);
4105 // U is now dead. Inform the listener if it exists and delete it.
4106 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004107 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004108 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004109 } else {
4110 // If the node doesn't already exist, we updated it. Inform a listener if
4111 // it exists.
4112 if (UpdateListener)
4113 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004114 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004115 }
4116}
4117
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004118namespace {
4119 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4120 /// any deleted nodes from the set passed into its constructor and recursively
4121 /// notifies another update listener if specified.
4122 class ChainedSetUpdaterListener :
4123 public SelectionDAG::DAGUpdateListener {
4124 SmallSetVector<SDNode*, 16> &Set;
4125 SelectionDAG::DAGUpdateListener *Chain;
4126 public:
4127 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4128 SelectionDAG::DAGUpdateListener *chain)
4129 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004130
Duncan Sandsedfcf592008-06-11 11:42:12 +00004131 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004132 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004133 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004134 }
4135 virtual void NodeUpdated(SDNode *N) {
4136 if (Chain) Chain->NodeUpdated(N);
4137 }
4138 };
4139}
4140
Chris Lattner012f2412006-02-17 21:58:01 +00004141/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4142/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004143/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004144void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004145 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004146 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004147
Chris Lattner012f2412006-02-17 21:58:01 +00004148 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004149 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004150 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004151 return;
4152 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004153
4154 if (From.use_empty()) return;
4155
Chris Lattnercf5640b2007-02-04 00:14:31 +00004156 // Get all of the users of From.Val. We want these in a nice,
4157 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004158 SmallSetVector<SDNode*, 16> Users;
4159 for (SDNode::use_iterator UI = From.Val->use_begin(),
4160 E = From.Val->use_end(); UI != E; ++UI) {
4161 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004162 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004163 }
Chris Lattner012f2412006-02-17 21:58:01 +00004164
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004165 // When one of the recursive merges deletes nodes from the graph, we need to
4166 // make sure that UpdateListener is notified *and* that the node is removed
4167 // from Users if present. CSUL does this.
4168 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004169
Chris Lattner012f2412006-02-17 21:58:01 +00004170 while (!Users.empty()) {
4171 // We know that this user uses some value of From. If it is the right
4172 // value, update it.
4173 SDNode *User = Users.back();
4174 Users.pop_back();
4175
Chris Lattner01d029b2007-10-15 06:10:22 +00004176 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004177 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004178 for (; Op != E; ++Op)
4179 if (*Op == From) break;
4180
4181 // If there are no matches, the user must use some other result of From.
4182 if (Op == E) continue;
4183
4184 // Okay, we know this user needs to be updated. Remove its old self
4185 // from the CSE maps.
4186 RemoveNodeFromCSEMaps(User);
4187
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004188 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004189 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004190 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004191 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004192 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004193 Op->setUser(User);
4194 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004195 }
4196 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004197
4198 // Now that we have modified User, add it back to the CSE maps. If it
4199 // already exists there, recursively merge the results together.
4200 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004201 if (!Existing) {
4202 if (UpdateListener) UpdateListener->NodeUpdated(User);
4203 continue; // Continue on to next user.
4204 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004205
4206 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004207 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004208 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004209 // can cause deletion of nodes that used the old value. To handle this, we
4210 // use CSUL to remove them from the Users set.
4211 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004212
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004213 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004214 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004215 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004216 }
4217}
4218
Evan Chenge6f35d82006-08-01 08:20:41 +00004219/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4220/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004221unsigned SelectionDAG::AssignNodeIds() {
4222 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004223 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4224 SDNode *N = I;
4225 N->setNodeId(Id++);
4226 }
4227 return Id;
4228}
4229
Evan Chenge6f35d82006-08-01 08:20:41 +00004230/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004231/// based on their topological order. It returns the maximum id and a vector
4232/// of the SDNodes* in assigned order by reference.
4233unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004234 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004235 std::vector<unsigned> InDegree(DAGSize);
4236 std::vector<SDNode*> Sources;
4237
4238 // Use a two pass approach to avoid using a std::map which is slow.
4239 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004240 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4241 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004242 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004243 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004244 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004245 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004246 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004247 }
4248
Evan Cheng99157a02006-08-07 22:13:29 +00004249 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004250 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004251 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004252 SDNode *N = Sources.back();
4253 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004254 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004255 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004256 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004257 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004258 if (Degree == 0)
4259 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004260 }
4261 }
4262
Evan Chengc384d6c2006-08-02 22:00:34 +00004263 // Second pass, assign the actual topological order as node ids.
4264 Id = 0;
4265 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4266 TI != TE; ++TI)
4267 (*TI)->setNodeId(Id++);
4268
4269 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004270}
4271
4272
Evan Cheng091cba12006-07-27 06:39:06 +00004273
Jim Laskey58b968b2005-08-17 20:08:02 +00004274//===----------------------------------------------------------------------===//
4275// SDNode Class
4276//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004277
Chris Lattner917d2c92006-07-19 00:00:37 +00004278// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004279void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004280void UnarySDNode::ANCHOR() {}
4281void BinarySDNode::ANCHOR() {}
4282void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004283void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004284void ConstantSDNode::ANCHOR() {}
4285void ConstantFPSDNode::ANCHOR() {}
4286void GlobalAddressSDNode::ANCHOR() {}
4287void FrameIndexSDNode::ANCHOR() {}
4288void JumpTableSDNode::ANCHOR() {}
4289void ConstantPoolSDNode::ANCHOR() {}
4290void BasicBlockSDNode::ANCHOR() {}
4291void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004292void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004293void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004294void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004295void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004296void ExternalSymbolSDNode::ANCHOR() {}
4297void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004298void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004299void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004300void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004301void LoadSDNode::ANCHOR() {}
4302void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004303void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004304
Chris Lattner48b85922007-02-04 02:41:42 +00004305HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004306 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004307}
4308
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004309GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004310 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004311 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004312 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004313 // Thread Local
4314 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4315 // Non Thread Local
4316 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4317 getSDVTList(VT)), Offset(o) {
4318 TheGlobal = const_cast<GlobalValue*>(GA);
4319}
Chris Lattner48b85922007-02-04 02:41:42 +00004320
Dan Gohman36b5c132008-04-07 19:35:22 +00004321/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004322/// reference performed by this atomic.
4323MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004324 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004325 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4326 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4327
4328 // Check if the atomic references a frame index
4329 const FrameIndexSDNode *FI =
4330 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4331 if (!getSrcValue() && FI)
4332 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4333 FI->getIndex(), Size, getAlignment());
4334 else
4335 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4336 Size, getAlignment());
4337}
4338
4339/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004340/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004341MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004342 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004343 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004344 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4345 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004346 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004347
4348 // Check if the load references a frame index, and does not have
4349 // an SV attached.
4350 const FrameIndexSDNode *FI =
4351 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4352 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004353 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004354 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004355 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004356 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004357 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004358}
4359
Jim Laskey583bd472006-10-27 23:46:08 +00004360/// Profile - Gather unique data for the node.
4361///
4362void SDNode::Profile(FoldingSetNodeID &ID) {
4363 AddNodeIDNode(ID, this);
4364}
4365
Chris Lattnera3255112005-11-08 23:30:28 +00004366/// getValueTypeList - Return a pointer to the specified value type.
4367///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004368const MVT *SDNode::getValueTypeList(MVT VT) {
4369 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004370 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004371 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004372 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004373 static MVT VTs[MVT::LAST_VALUETYPE];
4374 VTs[VT.getSimpleVT()] = VT;
4375 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004376 }
Chris Lattnera3255112005-11-08 23:30:28 +00004377}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004378
Chris Lattner5c884562005-01-12 18:37:47 +00004379/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4380/// indicated value. This method ignores uses of other values defined by this
4381/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004382bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004383 assert(Value < getNumValues() && "Bad value!");
4384
4385 // If there is only one value, this is easy.
4386 if (getNumValues() == 1)
4387 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004388 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004389
Evan Cheng4ee62112006-02-05 06:29:23 +00004390 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004391
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004392 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004393
Roman Levensteindc1adac2008-04-07 10:06:32 +00004394 // TODO: Only iterate over uses of a given value of the node
4395 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4396 if (*UI == TheValue) {
4397 if (NUses == 0)
4398 return false;
4399 --NUses;
4400 }
Chris Lattner5c884562005-01-12 18:37:47 +00004401 }
4402
4403 // Found exactly the right number of uses?
4404 return NUses == 0;
4405}
4406
4407
Evan Cheng33d55952007-08-02 05:29:38 +00004408/// hasAnyUseOfValue - Return true if there are any use of the indicated
4409/// value. This method ignores uses of other values defined by this operation.
4410bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4411 assert(Value < getNumValues() && "Bad value!");
4412
Dan Gohman30359592008-01-29 13:02:09 +00004413 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004414
4415 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4416
4417 SmallPtrSet<SDNode*, 32> UsersHandled;
4418
Roman Levensteindc1adac2008-04-07 10:06:32 +00004419 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4420 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004421 if (User->getNumOperands() == 1 ||
4422 UsersHandled.insert(User)) // First time we've seen this?
4423 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4424 if (User->getOperand(i) == TheValue) {
4425 return true;
4426 }
4427 }
4428
4429 return false;
4430}
4431
4432
Evan Cheng917be682008-03-04 00:41:45 +00004433/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004434///
Evan Cheng917be682008-03-04 00:41:45 +00004435bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004436 bool Seen = false;
4437 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004438 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004439 if (User == this)
4440 Seen = true;
4441 else
4442 return false;
4443 }
4444
4445 return Seen;
4446}
4447
Evan Chenge6e97e62006-11-03 07:31:32 +00004448/// isOperand - Return true if this node is an operand of N.
4449///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004450bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004451 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4452 if (*this == N->getOperand(i))
4453 return true;
4454 return false;
4455}
4456
Evan Cheng917be682008-03-04 00:41:45 +00004457bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004458 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004459 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004460 return true;
4461 return false;
4462}
Evan Cheng4ee62112006-02-05 06:29:23 +00004463
Chris Lattner572dee72008-01-16 05:49:24 +00004464/// reachesChainWithoutSideEffects - Return true if this operand (which must
4465/// be a chain) reaches the specified operand without crossing any
4466/// side-effecting instructions. In practice, this looks through token
4467/// factors and non-volatile loads. In order to remain efficient, this only
4468/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004469bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004470 unsigned Depth) const {
4471 if (*this == Dest) return true;
4472
4473 // Don't search too deeply, we just want to be able to see through
4474 // TokenFactor's etc.
4475 if (Depth == 0) return false;
4476
4477 // If this is a token factor, all inputs to the TF happen in parallel. If any
4478 // of the operands of the TF reach dest, then we can do the xform.
4479 if (getOpcode() == ISD::TokenFactor) {
4480 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4481 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4482 return true;
4483 return false;
4484 }
4485
4486 // Loads don't have side effects, look through them.
4487 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4488 if (!Ld->isVolatile())
4489 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4490 }
4491 return false;
4492}
4493
4494
Evan Chengc5fc57d2006-11-03 03:05:24 +00004495static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004496 SmallPtrSet<SDNode *, 32> &Visited) {
4497 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004498 return;
4499
4500 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4501 SDNode *Op = N->getOperand(i).Val;
4502 if (Op == P) {
4503 found = true;
4504 return;
4505 }
4506 findPredecessor(Op, P, found, Visited);
4507 }
4508}
4509
Evan Cheng917be682008-03-04 00:41:45 +00004510/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004511/// is either an operand of N or it can be reached by recursively traversing
4512/// up the operands.
4513/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004514bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004515 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004516 bool found = false;
4517 findPredecessor(N, this, found, Visited);
4518 return found;
4519}
4520
Evan Chengc5484282006-10-04 00:56:09 +00004521uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4522 assert(Num < NumOperands && "Invalid child # of SDNode!");
4523 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4524}
4525
Reid Spencer577cc322007-04-01 07:32:19 +00004526std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004527 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004528 default:
4529 if (getOpcode() < ISD::BUILTIN_OP_END)
4530 return "<<Unknown DAG Node>>";
4531 else {
Evan Cheng72261582005-12-20 06:22:03 +00004532 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004533 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004534 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004535 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004536
Evan Cheng72261582005-12-20 06:22:03 +00004537 TargetLowering &TLI = G->getTargetLoweringInfo();
4538 const char *Name =
4539 TLI.getTargetNodeName(getOpcode());
4540 if (Name) return Name;
4541 }
4542
4543 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004544 }
4545
Evan Cheng27b7db52008-03-08 00:58:38 +00004546 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004547 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004548 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4549 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4550 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004551 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4552 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4553 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004554 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004555 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4556 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4557 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4558 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4559 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004560 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004561 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004562 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004563 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004564 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004565 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004566 case ISD::AssertSext: return "AssertSext";
4567 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004568
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004569 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004570 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004571 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004572 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004573
4574 case ISD::Constant: return "Constant";
4575 case ISD::ConstantFP: return "ConstantFP";
4576 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004577 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004578 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004579 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004580 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004581 case ISD::RETURNADDR: return "RETURNADDR";
4582 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004583 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004584 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4585 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004586 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004587 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004588 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004589 case ISD::INTRINSIC_WO_CHAIN: {
4590 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4591 return Intrinsic::getName((Intrinsic::ID)IID);
4592 }
4593 case ISD::INTRINSIC_VOID:
4594 case ISD::INTRINSIC_W_CHAIN: {
4595 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004596 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004597 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004598
Chris Lattnerb2827b02006-03-19 00:52:58 +00004599 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004600 case ISD::TargetConstant: return "TargetConstant";
4601 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004602 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004603 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004604 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004605 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004606 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004607 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004608
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004609 case ISD::CopyToReg: return "CopyToReg";
4610 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004611 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004612 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004613 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004614 case ISD::DBG_LABEL: return "dbg_label";
4615 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004616 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004617 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004618 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004619 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004620
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004621 // Unary operators
4622 case ISD::FABS: return "fabs";
4623 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004624 case ISD::FSQRT: return "fsqrt";
4625 case ISD::FSIN: return "fsin";
4626 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004627 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004628 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004629
4630 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004631 case ISD::ADD: return "add";
4632 case ISD::SUB: return "sub";
4633 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004634 case ISD::MULHU: return "mulhu";
4635 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004636 case ISD::SDIV: return "sdiv";
4637 case ISD::UDIV: return "udiv";
4638 case ISD::SREM: return "srem";
4639 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004640 case ISD::SMUL_LOHI: return "smul_lohi";
4641 case ISD::UMUL_LOHI: return "umul_lohi";
4642 case ISD::SDIVREM: return "sdivrem";
4643 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004644 case ISD::AND: return "and";
4645 case ISD::OR: return "or";
4646 case ISD::XOR: return "xor";
4647 case ISD::SHL: return "shl";
4648 case ISD::SRA: return "sra";
4649 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004650 case ISD::ROTL: return "rotl";
4651 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004652 case ISD::FADD: return "fadd";
4653 case ISD::FSUB: return "fsub";
4654 case ISD::FMUL: return "fmul";
4655 case ISD::FDIV: return "fdiv";
4656 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004657 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004658 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004659
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004660 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004661 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004662 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004663 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004664 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004665 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004666 case ISD::CONCAT_VECTORS: return "concat_vectors";
4667 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004668 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004669 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004670 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004671 case ISD::ADDC: return "addc";
4672 case ISD::ADDE: return "adde";
4673 case ISD::SUBC: return "subc";
4674 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004675 case ISD::SHL_PARTS: return "shl_parts";
4676 case ISD::SRA_PARTS: return "sra_parts";
4677 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004678
4679 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4680 case ISD::INSERT_SUBREG: return "insert_subreg";
4681
Chris Lattner7f644642005-04-28 21:44:03 +00004682 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004683 case ISD::SIGN_EXTEND: return "sign_extend";
4684 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004685 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004686 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004687 case ISD::TRUNCATE: return "truncate";
4688 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004689 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004690 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004691 case ISD::FP_EXTEND: return "fp_extend";
4692
4693 case ISD::SINT_TO_FP: return "sint_to_fp";
4694 case ISD::UINT_TO_FP: return "uint_to_fp";
4695 case ISD::FP_TO_SINT: return "fp_to_sint";
4696 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004697 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004698
4699 // Control flow instructions
4700 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004701 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004702 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004703 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004704 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004705 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004706 case ISD::CALLSEQ_START: return "callseq_start";
4707 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004708
4709 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004710 case ISD::LOAD: return "load";
4711 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004712 case ISD::VAARG: return "vaarg";
4713 case ISD::VACOPY: return "vacopy";
4714 case ISD::VAEND: return "vaend";
4715 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004716 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004717 case ISD::EXTRACT_ELEMENT: return "extract_element";
4718 case ISD::BUILD_PAIR: return "build_pair";
4719 case ISD::STACKSAVE: return "stacksave";
4720 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004721 case ISD::TRAP: return "trap";
4722
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004723 // Bit manipulation
4724 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004725 case ISD::CTPOP: return "ctpop";
4726 case ISD::CTTZ: return "cttz";
4727 case ISD::CTLZ: return "ctlz";
4728
Chris Lattner36ce6912005-11-29 06:21:05 +00004729 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004730 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004731 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004732
Duncan Sands36397f52007-07-27 12:58:54 +00004733 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004734 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004735
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004736 case ISD::CONDCODE:
4737 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004738 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004739 case ISD::SETOEQ: return "setoeq";
4740 case ISD::SETOGT: return "setogt";
4741 case ISD::SETOGE: return "setoge";
4742 case ISD::SETOLT: return "setolt";
4743 case ISD::SETOLE: return "setole";
4744 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004745
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004746 case ISD::SETO: return "seto";
4747 case ISD::SETUO: return "setuo";
4748 case ISD::SETUEQ: return "setue";
4749 case ISD::SETUGT: return "setugt";
4750 case ISD::SETUGE: return "setuge";
4751 case ISD::SETULT: return "setult";
4752 case ISD::SETULE: return "setule";
4753 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004754
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004755 case ISD::SETEQ: return "seteq";
4756 case ISD::SETGT: return "setgt";
4757 case ISD::SETGE: return "setge";
4758 case ISD::SETLT: return "setlt";
4759 case ISD::SETLE: return "setle";
4760 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004761 }
4762 }
4763}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004764
Evan Cheng144d8f02006-11-09 17:55:04 +00004765const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004766 switch (AM) {
4767 default:
4768 return "";
4769 case ISD::PRE_INC:
4770 return "<pre-inc>";
4771 case ISD::PRE_DEC:
4772 return "<pre-dec>";
4773 case ISD::POST_INC:
4774 return "<post-inc>";
4775 case ISD::POST_DEC:
4776 return "<post-dec>";
4777 }
4778}
4779
Duncan Sands276dcbd2008-03-21 09:14:45 +00004780std::string ISD::ArgFlagsTy::getArgFlagsString() {
4781 std::string S = "< ";
4782
4783 if (isZExt())
4784 S += "zext ";
4785 if (isSExt())
4786 S += "sext ";
4787 if (isInReg())
4788 S += "inreg ";
4789 if (isSRet())
4790 S += "sret ";
4791 if (isByVal())
4792 S += "byval ";
4793 if (isNest())
4794 S += "nest ";
4795 if (getByValAlign())
4796 S += "byval-align:" + utostr(getByValAlign()) + " ";
4797 if (getOrigAlign())
4798 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4799 if (getByValSize())
4800 S += "byval-size:" + utostr(getByValSize()) + " ";
4801 return S + ">";
4802}
4803
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004804void SDNode::dump() const { dump(0); }
4805void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004806 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004807
4808 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004809 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004810 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004811 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004812 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004813 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004814 }
Bill Wendling832171c2006-12-07 20:04:42 +00004815 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004816
Bill Wendling832171c2006-12-07 20:04:42 +00004817 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004818 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004819 if (i) cerr << ", ";
4820 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004821 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004822 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004823 }
4824
Evan Chengce254432007-12-11 02:08:35 +00004825 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4826 SDNode *Mask = getOperand(2).Val;
4827 cerr << "<";
4828 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4829 if (i) cerr << ",";
4830 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4831 cerr << "u";
4832 else
4833 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4834 }
4835 cerr << ">";
4836 }
4837
Chris Lattnerc3aae252005-01-07 07:46:32 +00004838 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004839 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004840 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004841 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4842 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4843 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4844 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4845 else {
4846 cerr << "<APFloat(";
4847 CSDN->getValueAPF().convertToAPInt().dump();
4848 cerr << ")>";
4849 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004850 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004851 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004852 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004853 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004854 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004855 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004856 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004857 else
Bill Wendling832171c2006-12-07 20:04:42 +00004858 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004859 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004860 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004861 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004862 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004863 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004864 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004865 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004866 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004867 else
Bill Wendling832171c2006-12-07 20:04:42 +00004868 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004869 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004870 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004871 else
Bill Wendling832171c2006-12-07 20:04:42 +00004872 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004873 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004874 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004875 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4876 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004877 cerr << LBB->getName() << " ";
4878 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004879 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004880 if (G && R->getReg() &&
4881 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004882 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004883 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004884 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004885 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004886 } else if (const ExternalSymbolSDNode *ES =
4887 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004888 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004889 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4890 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004891 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004892 else
Dan Gohman69de1932008-02-06 22:27:42 +00004893 cerr << "<null>";
4894 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4895 if (M->MO.getValue())
4896 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4897 else
4898 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004899 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4900 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004901 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004902 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004903 }
4904 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004905 const Value *SrcValue = LD->getSrcValue();
4906 int SrcOffset = LD->getSrcValueOffset();
4907 cerr << " <";
4908 if (SrcValue)
4909 cerr << SrcValue;
4910 else
4911 cerr << "null";
4912 cerr << ":" << SrcOffset << ">";
4913
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004914 bool doExt = true;
4915 switch (LD->getExtensionType()) {
4916 default: doExt = false; break;
4917 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004918 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004919 break;
4920 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004921 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004922 break;
4923 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004924 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004925 break;
4926 }
4927 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004928 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004929
Evan Cheng144d8f02006-11-09 17:55:04 +00004930 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004931 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004932 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004933 if (LD->isVolatile())
4934 cerr << " <volatile>";
4935 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004936 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004937 const Value *SrcValue = ST->getSrcValue();
4938 int SrcOffset = ST->getSrcValueOffset();
4939 cerr << " <";
4940 if (SrcValue)
4941 cerr << SrcValue;
4942 else
4943 cerr << "null";
4944 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004945
4946 if (ST->isTruncatingStore())
4947 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004948 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004949
4950 const char *AM = getIndexedModeName(ST->getAddressingMode());
4951 if (*AM)
4952 cerr << " " << AM;
4953 if (ST->isVolatile())
4954 cerr << " <volatile>";
4955 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004956 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4957 const Value *SrcValue = AT->getSrcValue();
4958 int SrcOffset = AT->getSrcValueOffset();
4959 cerr << " <";
4960 if (SrcValue)
4961 cerr << SrcValue;
4962 else
4963 cerr << "null";
4964 cerr << ":" << SrcOffset << ">";
4965 if (AT->isVolatile())
4966 cerr << " <volatile>";
4967 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004968 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004969}
4970
Chris Lattnerde202b32005-11-09 23:47:37 +00004971static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004972 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4973 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004974 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004975 else
Bill Wendling832171c2006-12-07 20:04:42 +00004976 cerr << "\n" << std::string(indent+2, ' ')
4977 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004978
Chris Lattnerea946cd2005-01-09 20:38:33 +00004979
Bill Wendling832171c2006-12-07 20:04:42 +00004980 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004981 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004982}
4983
Chris Lattnerc3aae252005-01-07 07:46:32 +00004984void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004985 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004986 std::vector<const SDNode*> Nodes;
4987 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4988 I != E; ++I)
4989 Nodes.push_back(I);
4990
Chris Lattner49d24712005-01-09 20:26:36 +00004991 std::sort(Nodes.begin(), Nodes.end());
4992
4993 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004994 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004995 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004996 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004997
Jim Laskey26f7fa72006-10-17 19:33:52 +00004998 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004999
Bill Wendling832171c2006-12-07 20:04:42 +00005000 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005001}
5002
Evan Chengd6594ae2006-09-12 21:00:35 +00005003const Type *ConstantPoolSDNode::getType() const {
5004 if (isMachineConstantPoolEntry())
5005 return Val.MachineCPVal->getType();
5006 return Val.ConstVal->getType();
5007}