blob: c37fa38a0587adfb5b07fc80e6ee971cd950577a [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;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000200 if (N->isMachineOpcode() &&
201 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000326 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
338 ID.AddPointer(Ops->getSDOperand().Val);
339 ID.AddInteger(Ops->getSDOperand().ResNo);
340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345 const SDOperand *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Roman Levenstein9cac5252008-04-16 16:15:27 +0000351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
353/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
355 AddNodeIDOpcode(ID, N->getOpcode());
356 // Add the return value info.
357 AddNodeIDValueTypes(ID, N->getVTList());
358 // Add the operand info.
359 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
360
361 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 switch (N->getOpcode()) {
363 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000364 case ISD::ARG_FLAGS:
365 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
366 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetConstant:
368 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000369 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 break;
371 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000372 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000373 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000377 case ISD::GlobalAddress:
378 case ISD::TargetGlobalTLSAddress:
379 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
381 ID.AddPointer(GA->getGlobal());
382 ID.AddInteger(GA->getOffset());
383 break;
384 }
385 case ISD::BasicBlock:
386 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
387 break;
388 case ISD::Register:
389 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
390 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000391 case ISD::DBG_STOPPOINT: {
392 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
393 ID.AddInteger(DSP->getLine());
394 ID.AddInteger(DSP->getColumn());
395 ID.AddPointer(DSP->getCompileUnit());
396 break;
397 }
Dan Gohman69de1932008-02-06 22:27:42 +0000398 case ISD::SRCVALUE:
399 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
400 break;
401 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000402 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000403 ID.AddPointer(MO.getValue());
404 ID.AddInteger(MO.getFlags());
405 ID.AddInteger(MO.getOffset());
406 ID.AddInteger(MO.getSize());
407 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000408 break;
409 }
410 case ISD::FrameIndex:
411 case ISD::TargetFrameIndex:
412 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
413 break;
414 case ISD::JumpTable:
415 case ISD::TargetJumpTable:
416 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
417 break;
418 case ISD::ConstantPool:
419 case ISD::TargetConstantPool: {
420 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
421 ID.AddInteger(CP->getAlignment());
422 ID.AddInteger(CP->getOffset());
423 if (CP->isMachineConstantPoolEntry())
424 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
425 else
426 ID.AddPointer(CP->getConstVal());
427 break;
428 }
429 case ISD::LOAD: {
430 LoadSDNode *LD = cast<LoadSDNode>(N);
431 ID.AddInteger(LD->getAddressingMode());
432 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 ID.AddInteger(LD->getAlignment());
435 ID.AddInteger(LD->isVolatile());
436 break;
437 }
438 case ISD::STORE: {
439 StoreSDNode *ST = cast<StoreSDNode>(N);
440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 ID.AddInteger(ST->getAlignment());
444 ID.AddInteger(ST->isVolatile());
445 break;
446 }
Mon P Wang28873102008-06-25 08:15:39 +0000447 case ISD::ATOMIC_CMP_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_SWAP:
450 case ISD::ATOMIC_LOAD_SUB:
451 case ISD::ATOMIC_LOAD_AND:
452 case ISD::ATOMIC_LOAD_OR:
453 case ISD::ATOMIC_LOAD_XOR:
454 case ISD::ATOMIC_LOAD_NAND:
455 case ISD::ATOMIC_LOAD_MIN:
456 case ISD::ATOMIC_LOAD_MAX:
457 case ISD::ATOMIC_LOAD_UMIN:
458 case ISD::ATOMIC_LOAD_UMAX: {
459 AtomicSDNode *AT = cast<AtomicSDNode>(N);
460 ID.AddInteger(AT->getAlignment());
461 ID.AddInteger(AT->isVolatile());
462 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000463 }
Mon P Wang28873102008-06-25 08:15:39 +0000464 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000465}
466
467//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000468// SelectionDAG Class
469//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000470
Dan Gohman0e5f1302008-07-07 23:02:41 +0000471inline alist_traits<SDNode, LargestSDNode>::AllocatorType &
472SelectionDAG::getAllocator() {
473 return AllNodes.getTraits().Allocator;
474}
475
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000476/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000477/// SelectionDAG.
478void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000479 // Create a dummy node (which is not added to allnodes), that adds a reference
480 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000481 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000482
Chris Lattner190a4182006-08-04 17:45:20 +0000483 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000484
Chris Lattner190a4182006-08-04 17:45:20 +0000485 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000486 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000487 if (I->use_empty())
488 DeadNodes.push_back(I);
489
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000490 RemoveDeadNodes(DeadNodes);
491
492 // If the root changed (e.g. it was a dead load, update the root).
493 setRoot(Dummy.getValue());
494}
495
496/// RemoveDeadNodes - This method deletes the unreachable nodes in the
497/// given list, and any nodes that become unreachable as a result.
498void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
499 DAGUpdateListener *UpdateListener) {
500
Chris Lattner190a4182006-08-04 17:45:20 +0000501 // Process the worklist, deleting the nodes and adding their uses to the
502 // worklist.
503 while (!DeadNodes.empty()) {
504 SDNode *N = DeadNodes.back();
505 DeadNodes.pop_back();
506
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000507 if (UpdateListener)
508 UpdateListener->NodeDeleted(N, 0);
509
Chris Lattner190a4182006-08-04 17:45:20 +0000510 // Take the node out of the appropriate CSE map.
511 RemoveNodeFromCSEMaps(N);
512
513 // Next, brutally remove the operand list. This is safe to do, as there are
514 // no cycles in the graph.
515 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000516 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000517 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000518
519 // Now that we removed this operand, see if there are no uses of it left.
520 if (Operand->use_empty())
521 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000522 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000523 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000524 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000525 }
Chris Lattner190a4182006-08-04 17:45:20 +0000526 N->OperandList = 0;
527 N->NumOperands = 0;
528
529 // Finally, remove N itself.
530 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000531 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532}
533
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000535 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000536 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000537}
538
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000539void SelectionDAG::DeleteNode(SDNode *N) {
540 assert(N->use_empty() && "Cannot delete a node that is not dead!");
541
542 // First take this out of the appropriate CSE map.
543 RemoveNodeFromCSEMaps(N);
544
Chris Lattner1e111c72005-09-07 05:37:01 +0000545 // Finally, remove uses due to operands of this node, remove from the
546 // AllNodes list, and delete the node.
547 DeleteNodeNotInCSEMaps(N);
548}
549
550void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
551
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000552 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000553 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000554 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000555 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000556 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000557 }
Chris Lattner65113b22005-11-08 22:07:03 +0000558 N->OperandList = 0;
559 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000560
Dan Gohman0e5f1302008-07-07 23:02:41 +0000561 AllNodes.erase(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000562}
563
Chris Lattner149c58c2005-08-16 18:17:10 +0000564/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
565/// correspond to it. This is useful when we're about to delete or repurpose
566/// the node. We don't want future request for structurally identical nodes
567/// to return N anymore.
568void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000569 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000570 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000571 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000572 case ISD::CONDCODE:
573 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
574 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000575 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000576 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
577 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000578 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000579 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000580 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000581 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000582 Erased =
583 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000584 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000585 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000586 MVT VT = cast<VTSDNode>(N)->getVT();
587 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000588 Erased = ExtendedValueTypeNodes.erase(VT);
589 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000590 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
591 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000592 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000593 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000594 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000595 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000596 // Remove it from the CSE Map.
597 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000598 break;
599 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000600#ifndef NDEBUG
601 // Verify that the node was actually in one of the CSE maps, unless it has a
602 // flag result (which cannot be CSE'd) or is one of the special cases that are
603 // not subject to CSE.
604 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000605 !N->isTargetOpcode() &&
606 N->getOpcode() != ISD::DBG_LABEL &&
607 N->getOpcode() != ISD::DBG_STOPPOINT &&
608 N->getOpcode() != ISD::EH_LABEL &&
609 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000610 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000611 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000612 assert(0 && "Node is not in map!");
613 }
614#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000615}
616
Chris Lattner8b8749f2005-08-17 19:00:20 +0000617/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
618/// has been taken out and modified in some way. If the specified node already
619/// exists in the CSE maps, do not modify the maps, but return the existing node
620/// instead. If it doesn't exist, add it and return null.
621///
622SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
623 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000624
625 if (N->getValueType(0) == MVT::Flag)
626 return 0; // Never CSE anything that produces a flag.
627
628 switch (N->getOpcode()) {
629 default: break;
630 case ISD::HANDLENODE:
631 case ISD::DBG_LABEL:
632 case ISD::DBG_STOPPOINT:
633 case ISD::EH_LABEL:
634 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000635 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000636 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000637
Chris Lattner9f8cc692005-12-19 22:21:21 +0000638 // Check that remaining values produced are not flags.
639 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
640 if (N->getValueType(i) == MVT::Flag)
641 return 0; // Never CSE anything that produces a flag.
642
Chris Lattnera5682852006-08-07 23:03:03 +0000643 SDNode *New = CSEMap.GetOrInsertNode(N);
644 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000645 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000646}
647
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000648/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
649/// were replaced with those specified. If this node is never memoized,
650/// return null, otherwise return a pointer to the slot it would take. If a
651/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000652SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
653 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000654 if (N->getValueType(0) == MVT::Flag)
655 return 0; // Never CSE anything that produces a flag.
656
657 switch (N->getOpcode()) {
658 default: break;
659 case ISD::HANDLENODE:
660 case ISD::DBG_LABEL:
661 case ISD::DBG_STOPPOINT:
662 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000663 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000664 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000665
666 // Check that remaining values produced are not flags.
667 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
668 if (N->getValueType(i) == MVT::Flag)
669 return 0; // Never CSE anything that produces a flag.
670
Chris Lattner63e3f142007-02-04 07:28:00 +0000671 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000672 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000673 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000674 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000675}
676
677/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
678/// were replaced with those specified. If this node is never memoized,
679/// return null, otherwise return a pointer to the slot it would take. If a
680/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000681SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
682 SDOperand Op1, SDOperand Op2,
683 void *&InsertPos) {
684 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000685
686 // Check that remaining values produced are not flags.
687 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
688 if (N->getValueType(i) == MVT::Flag)
689 return 0; // Never CSE anything that produces a flag.
690
Chris Lattner63e3f142007-02-04 07:28:00 +0000691 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000692 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000693 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000694 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
695}
696
697
698/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
699/// were replaced with those specified. If this node is never memoized,
700/// return null, otherwise return a pointer to the slot it would take. If a
701/// node already exists with these operands, the slot will be non-null.
702SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000703 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000704 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000705 if (N->getValueType(0) == MVT::Flag)
706 return 0; // Never CSE anything that produces a flag.
707
708 switch (N->getOpcode()) {
709 default: break;
710 case ISD::HANDLENODE:
711 case ISD::DBG_LABEL:
712 case ISD::DBG_STOPPOINT:
713 case ISD::EH_LABEL:
714 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000715 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000716 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000717
718 // Check that remaining values produced are not flags.
719 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
720 if (N->getValueType(i) == MVT::Flag)
721 return 0; // Never CSE anything that produces a flag.
722
Jim Laskey583bd472006-10-27 23:46:08 +0000723 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000724 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000725
Evan Cheng9629aba2006-10-11 01:47:58 +0000726 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
727 ID.AddInteger(LD->getAddressingMode());
728 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000729 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000730 ID.AddInteger(LD->getAlignment());
731 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000732 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
733 ID.AddInteger(ST->getAddressingMode());
734 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000735 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000736 ID.AddInteger(ST->getAlignment());
737 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000738 }
Jim Laskey583bd472006-10-27 23:46:08 +0000739
Chris Lattnera5682852006-08-07 23:03:03 +0000740 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000741}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000742
Duncan Sandsd038e042008-07-21 10:20:31 +0000743/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
744void SelectionDAG::VerifyNode(SDNode *N) {
745 switch (N->getOpcode()) {
746 default:
747 break;
748 case ISD::BUILD_VECTOR: {
749 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
750 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
751 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
752 "Wrong number of BUILD_VECTOR operands!");
753 MVT EltVT = N->getValueType(0).getVectorElementType();
754 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
755 assert(I->getSDOperand().getValueType() == EltVT &&
756 "Wrong BUILD_VECTOR operand type!");
757 break;
758 }
759 }
760}
761
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000762/// getMVTAlignment - Compute the default alignment value for the
763/// given type.
764///
765unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
766 const Type *Ty = VT == MVT::iPTR ?
767 PointerType::get(Type::Int8Ty, 0) :
768 VT.getTypeForMVT();
769
770 return TLI.getTargetData()->getABITypeAlignment(Ty);
771}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000772
Chris Lattner78ec3112003-08-11 14:57:33 +0000773SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000774 while (!AllNodes.empty()) {
775 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000776 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000777 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000778 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000779 }
Chris Lattner65113b22005-11-08 22:07:03 +0000780 N->OperandList = 0;
781 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000782 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000783 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000784}
785
Duncan Sands83ec4b62008-06-06 12:08:01 +0000786SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000787 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000788 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000789 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000790 return getNode(ISD::AND, Op.getValueType(), Op,
791 getConstant(Imm, Op.getValueType()));
792}
793
Duncan Sands83ec4b62008-06-06 12:08:01 +0000794SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000795 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000796 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000797}
798
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
800 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000801
Evan Cheng0ff39b32008-06-30 07:31:25 +0000802 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000803 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000804 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000805
806 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000807 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000808 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000809 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000810 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000811 SDNode *N = NULL;
812 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000813 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000814 return SDOperand(N, 0);
815 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000816 N = getAllocator().Allocate<ConstantSDNode>();
817 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000818 CSEMap.InsertNode(N, IP);
819 AllNodes.push_back(N);
820 }
821
822 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000823 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000824 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000825 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000826 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
827 }
828 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000829}
830
Chris Lattner0bd48932008-01-17 07:00:52 +0000831SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
832 return getConstant(Val, TLI.getPointerTy(), isTarget);
833}
834
835
Duncan Sands83ec4b62008-06-06 12:08:01 +0000836SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
837 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000838
Duncan Sands83ec4b62008-06-06 12:08:01 +0000839 MVT EltVT =
840 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841
Chris Lattnerd8658612005-02-17 20:17:32 +0000842 // Do the map lookup using the actual bit pattern for the floating point
843 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
844 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000845 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000846 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000847 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000848 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000849 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000850 SDNode *N = NULL;
851 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000852 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000853 return SDOperand(N, 0);
854 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000855 N = getAllocator().Allocate<ConstantFPSDNode>();
856 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000857 CSEMap.InsertNode(N, IP);
858 AllNodes.push_back(N);
859 }
860
Dan Gohman7f321562007-06-25 16:23:39 +0000861 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000862 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000863 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000865 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
866 }
867 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868}
869
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
871 MVT EltVT =
872 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000873 if (EltVT==MVT::f32)
874 return getConstantFP(APFloat((float)Val), VT, isTarget);
875 else
876 return getConstantFP(APFloat(Val), VT, isTarget);
877}
878
Chris Lattnerc3aae252005-01-07 07:46:32 +0000879SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000881 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000882 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000883
884 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
885 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000886 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000887 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
888 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
889 }
890
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000891 if (GVar && GVar->isThreadLocal())
892 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
893 else
894 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000895
Jim Laskey583bd472006-10-27 23:46:08 +0000896 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000897 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000898 ID.AddPointer(GV);
899 ID.AddInteger(Offset);
900 void *IP = 0;
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000903 SDNode *N = getAllocator().Allocate<GlobalAddressSDNode>();
904 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000905 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000906 AllNodes.push_back(N);
907 return SDOperand(N, 0);
908}
909
Duncan Sands83ec4b62008-06-06 12:08:01 +0000910SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000911 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000912 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000913 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000914 ID.AddInteger(FI);
915 void *IP = 0;
916 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
917 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000918 SDNode *N = getAllocator().Allocate<FrameIndexSDNode>();
919 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000920 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000921 AllNodes.push_back(N);
922 return SDOperand(N, 0);
923}
924
Duncan Sands83ec4b62008-06-06 12:08:01 +0000925SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000926 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000927 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000928 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000929 ID.AddInteger(JTI);
930 void *IP = 0;
931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
932 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000933 SDNode *N = getAllocator().Allocate<JumpTableSDNode>();
934 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000935 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Duncan Sands83ec4b62008-06-06 12:08:01 +0000940SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000941 unsigned Alignment, int Offset,
942 bool isTarget) {
943 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000944 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000945 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000946 ID.AddInteger(Alignment);
947 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000948 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000949 void *IP = 0;
950 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
951 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000952 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
953 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000954 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000955 AllNodes.push_back(N);
956 return SDOperand(N, 0);
957}
958
Chris Lattnerc3aae252005-01-07 07:46:32 +0000959
Duncan Sands83ec4b62008-06-06 12:08:01 +0000960SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000961 unsigned Alignment, int Offset,
962 bool isTarget) {
963 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000964 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000965 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000966 ID.AddInteger(Alignment);
967 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000968 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000969 void *IP = 0;
970 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
971 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000972 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
973 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000974 CSEMap.InsertNode(N, IP);
975 AllNodes.push_back(N);
976 return SDOperand(N, 0);
977}
978
979
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000981 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000982 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000983 ID.AddPointer(MBB);
984 void *IP = 0;
985 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
986 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000987 SDNode *N = getAllocator().Allocate<BasicBlockSDNode>();
988 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000989 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000990 AllNodes.push_back(N);
991 return SDOperand(N, 0);
992}
993
Duncan Sands276dcbd2008-03-21 09:14:45 +0000994SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
995 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000996 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000997 ID.AddInteger(Flags.getRawBits());
998 void *IP = 0;
999 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1000 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001001 SDNode *N = getAllocator().Allocate<ARG_FLAGSSDNode>();
1002 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001003 CSEMap.InsertNode(N, IP);
1004 AllNodes.push_back(N);
1005 return SDOperand(N, 0);
1006}
1007
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008SDOperand SelectionDAG::getValueType(MVT VT) {
1009 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1010 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001011
Duncan Sands83ec4b62008-06-06 12:08:01 +00001012 SDNode *&N = VT.isExtended() ?
1013 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001014
1015 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001016 N = getAllocator().Allocate<VTSDNode>();
1017 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001018 AllNodes.push_back(N);
1019 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001020}
1021
Duncan Sands83ec4b62008-06-06 12:08:01 +00001022SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 SDNode *&N = ExternalSymbols[Sym];
1024 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001025 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1026 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001027 AllNodes.push_back(N);
1028 return SDOperand(N, 0);
1029}
1030
Duncan Sands83ec4b62008-06-06 12:08:01 +00001031SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001032 SDNode *&N = TargetExternalSymbols[Sym];
1033 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001034 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1035 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
1038}
1039
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001040SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
1041 if ((unsigned)Cond >= CondCodeNodes.size())
1042 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001043
Chris Lattner079a27a2005-08-09 20:40:02 +00001044 if (CondCodeNodes[Cond] == 0) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001045 CondCodeSDNode *N = getAllocator().Allocate<CondCodeSDNode>();
1046 new (N) CondCodeSDNode(Cond);
1047 CondCodeNodes[Cond] = N;
1048 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001049 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001050 return SDOperand(CondCodeNodes[Cond], 0);
1051}
1052
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001054 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001055 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001056 ID.AddInteger(RegNo);
1057 void *IP = 0;
1058 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1059 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001060 SDNode *N = getAllocator().Allocate<RegisterSDNode>();
1061 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001062 CSEMap.InsertNode(N, IP);
1063 AllNodes.push_back(N);
1064 return SDOperand(N, 0);
1065}
1066
Dan Gohman7f460202008-06-30 20:59:49 +00001067SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1068 unsigned Line, unsigned Col,
1069 const CompileUnitDesc *CU) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001070 SDNode *N = getAllocator().Allocate<DbgStopPointSDNode>();
1071 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001072 AllNodes.push_back(N);
1073 return SDOperand(N, 0);
1074}
1075
Dan Gohman44066042008-07-01 00:05:16 +00001076SDOperand SelectionDAG::getLabel(unsigned Opcode,
1077 SDOperand Root,
1078 unsigned LabelID) {
1079 FoldingSetNodeID ID;
1080 SDOperand Ops[] = { Root };
1081 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1082 ID.AddInteger(LabelID);
1083 void *IP = 0;
1084 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1085 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001086 SDNode *N = getAllocator().Allocate<LabelSDNode>();
1087 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001088 CSEMap.InsertNode(N, IP);
1089 AllNodes.push_back(N);
1090 return SDOperand(N, 0);
1091}
1092
Dan Gohman69de1932008-02-06 22:27:42 +00001093SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001094 assert((!V || isa<PointerType>(V->getType())) &&
1095 "SrcValue is not a pointer?");
1096
Jim Laskey583bd472006-10-27 23:46:08 +00001097 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001098 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001099 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001100
Chris Lattner61b09412006-08-11 21:01:22 +00001101 void *IP = 0;
1102 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1103 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001104
Dan Gohman0e5f1302008-07-07 23:02:41 +00001105 SDNode *N = getAllocator().Allocate<SrcValueSDNode>();
1106 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001107 CSEMap.InsertNode(N, IP);
1108 AllNodes.push_back(N);
1109 return SDOperand(N, 0);
1110}
1111
Dan Gohman36b5c132008-04-07 19:35:22 +00001112SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001113 const Value *v = MO.getValue();
1114 assert((!v || isa<PointerType>(v->getType())) &&
1115 "SrcValue is not a pointer?");
1116
1117 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001118 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001119 ID.AddPointer(v);
1120 ID.AddInteger(MO.getFlags());
1121 ID.AddInteger(MO.getOffset());
1122 ID.AddInteger(MO.getSize());
1123 ID.AddInteger(MO.getAlignment());
1124
1125 void *IP = 0;
1126 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1127 return SDOperand(E, 0);
1128
Dan Gohman0e5f1302008-07-07 23:02:41 +00001129 SDNode *N = getAllocator().Allocate<MemOperandSDNode>();
1130 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001131 CSEMap.InsertNode(N, IP);
1132 AllNodes.push_back(N);
1133 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001134}
1135
Chris Lattner37ce9df2007-10-15 17:47:20 +00001136/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1137/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001138SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001139 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001140 unsigned ByteSize = VT.getSizeInBits()/8;
1141 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001142 unsigned StackAlign =
1143 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1144
Chris Lattner37ce9df2007-10-15 17:47:20 +00001145 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1146 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1147}
1148
Duncan Sands83ec4b62008-06-06 12:08:01 +00001149SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001150 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001151 // These setcc operations always fold.
1152 switch (Cond) {
1153 default: break;
1154 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001155 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001156 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001157 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001158
1159 case ISD::SETOEQ:
1160 case ISD::SETOGT:
1161 case ISD::SETOGE:
1162 case ISD::SETOLT:
1163 case ISD::SETOLE:
1164 case ISD::SETONE:
1165 case ISD::SETO:
1166 case ISD::SETUO:
1167 case ISD::SETUEQ:
1168 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001169 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001170 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001172
Chris Lattner67255a12005-04-07 18:14:58 +00001173 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001174 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001175 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001176 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001177
Chris Lattnerc3aae252005-01-07 07:46:32 +00001178 switch (Cond) {
1179 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001180 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1181 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001182 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1183 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1184 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1185 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1186 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1187 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1188 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1189 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001190 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191 }
Chris Lattner67255a12005-04-07 18:14:58 +00001192 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001193 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001195 // No compile time operations on this type yet.
1196 if (N1C->getValueType(0) == MVT::ppcf128)
1197 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001198
1199 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001200 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001201 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001202 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1203 return getNode(ISD::UNDEF, VT);
1204 // fall through
1205 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1206 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1207 return getNode(ISD::UNDEF, VT);
1208 // fall through
1209 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001210 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001211 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1212 return getNode(ISD::UNDEF, VT);
1213 // fall through
1214 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1215 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1216 return getNode(ISD::UNDEF, VT);
1217 // fall through
1218 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1219 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1220 return getNode(ISD::UNDEF, VT);
1221 // fall through
1222 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001223 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001224 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1225 return getNode(ISD::UNDEF, VT);
1226 // fall through
1227 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001228 R==APFloat::cmpEqual, VT);
1229 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1230 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1231 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1232 R==APFloat::cmpEqual, VT);
1233 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1234 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1235 R==APFloat::cmpLessThan, VT);
1236 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1237 R==APFloat::cmpUnordered, VT);
1238 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1239 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001240 }
1241 } else {
1242 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001243 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001245 }
1246
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001247 // Could not fold it.
1248 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249}
1250
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001251/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1252/// use this predicate to simplify operations downstream.
1253bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1254 unsigned BitWidth = Op.getValueSizeInBits();
1255 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1256}
1257
Dan Gohmanea859be2007-06-22 14:59:07 +00001258/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1259/// this predicate to simplify operations downstream. Mask is known to be zero
1260/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001261bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001262 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001263 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001264 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1265 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1266 return (KnownZero & Mask) == Mask;
1267}
1268
1269/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1270/// known to be either zero or one and return them in the KnownZero/KnownOne
1271/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1272/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001273void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001274 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001275 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001276 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001277 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001278 "Mask size mismatches value type size!");
1279
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001280 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001281 if (Depth == 6 || Mask == 0)
1282 return; // Limit search depth.
1283
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001284 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001285
1286 switch (Op.getOpcode()) {
1287 case ISD::Constant:
1288 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001289 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001290 KnownZero = ~KnownOne & Mask;
1291 return;
1292 case ISD::AND:
1293 // If either the LHS or the RHS are Zero, the result is zero.
1294 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001295 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1296 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001297 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1298 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1299
1300 // Output known-1 bits are only known if set in both the LHS & RHS.
1301 KnownOne &= KnownOne2;
1302 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1303 KnownZero |= KnownZero2;
1304 return;
1305 case ISD::OR:
1306 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001307 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1308 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001309 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1310 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1311
1312 // Output known-0 bits are only known if clear in both the LHS & RHS.
1313 KnownZero &= KnownZero2;
1314 // Output known-1 are known to be set if set in either the LHS | RHS.
1315 KnownOne |= KnownOne2;
1316 return;
1317 case ISD::XOR: {
1318 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1319 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1320 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1321 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1322
1323 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001324 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001325 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1326 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1327 KnownZero = KnownZeroOut;
1328 return;
1329 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001330 case ISD::MUL: {
1331 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1332 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1333 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1335 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1336
1337 // If low bits are zero in either operand, output low known-0 bits.
1338 // Also compute a conserative estimate for high known-0 bits.
1339 // More trickiness is possible, but this is sufficient for the
1340 // interesting case of alignment computation.
1341 KnownOne.clear();
1342 unsigned TrailZ = KnownZero.countTrailingOnes() +
1343 KnownZero2.countTrailingOnes();
1344 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001345 KnownZero2.countLeadingOnes(),
1346 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001347
1348 TrailZ = std::min(TrailZ, BitWidth);
1349 LeadZ = std::min(LeadZ, BitWidth);
1350 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1351 APInt::getHighBitsSet(BitWidth, LeadZ);
1352 KnownZero &= Mask;
1353 return;
1354 }
1355 case ISD::UDIV: {
1356 // For the purposes of computing leading zeros we can conservatively
1357 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001358 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001359 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1360 ComputeMaskedBits(Op.getOperand(0),
1361 AllOnes, KnownZero2, KnownOne2, Depth+1);
1362 unsigned LeadZ = KnownZero2.countLeadingOnes();
1363
1364 KnownOne2.clear();
1365 KnownZero2.clear();
1366 ComputeMaskedBits(Op.getOperand(1),
1367 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001368 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1369 if (RHSUnknownLeadingOnes != BitWidth)
1370 LeadZ = std::min(BitWidth,
1371 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001372
1373 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1374 return;
1375 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001376 case ISD::SELECT:
1377 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1378 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1379 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1380 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1381
1382 // Only known if known in both the LHS and RHS.
1383 KnownOne &= KnownOne2;
1384 KnownZero &= KnownZero2;
1385 return;
1386 case ISD::SELECT_CC:
1387 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1388 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1389 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1390 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1391
1392 // Only known if known in both the LHS and RHS.
1393 KnownOne &= KnownOne2;
1394 KnownZero &= KnownZero2;
1395 return;
1396 case ISD::SETCC:
1397 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001398 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1399 BitWidth > 1)
1400 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001401 return;
1402 case ISD::SHL:
1403 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1404 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001405 unsigned ShAmt = SA->getValue();
1406
1407 // If the shift count is an invalid immediate, don't do anything.
1408 if (ShAmt >= BitWidth)
1409 return;
1410
1411 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001412 KnownZero, KnownOne, Depth+1);
1413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001414 KnownZero <<= ShAmt;
1415 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001416 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001417 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 }
1419 return;
1420 case ISD::SRL:
1421 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1422 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001423 unsigned ShAmt = SA->getValue();
1424
Dan Gohmand4cf9922008-02-26 18:50:50 +00001425 // If the shift count is an invalid immediate, don't do anything.
1426 if (ShAmt >= BitWidth)
1427 return;
1428
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001429 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 KnownZero, KnownOne, Depth+1);
1431 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001432 KnownZero = KnownZero.lshr(ShAmt);
1433 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001434
Dan Gohman72d2fd52008-02-13 22:43:25 +00001435 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 KnownZero |= HighBits; // High bits known zero.
1437 }
1438 return;
1439 case ISD::SRA:
1440 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001441 unsigned ShAmt = SA->getValue();
1442
Dan Gohmand4cf9922008-02-26 18:50:50 +00001443 // If the shift count is an invalid immediate, don't do anything.
1444 if (ShAmt >= BitWidth)
1445 return;
1446
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001447 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001448 // If any of the demanded bits are produced by the sign extension, we also
1449 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001450 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1451 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001453
1454 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1455 Depth+1);
1456 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001457 KnownZero = KnownZero.lshr(ShAmt);
1458 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001459
1460 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001461 APInt SignBit = APInt::getSignBit(BitWidth);
1462 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001463
Dan Gohmanca93a432008-02-20 16:30:17 +00001464 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001466 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 KnownOne |= HighBits; // New bits are known one.
1468 }
1469 }
1470 return;
1471 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001472 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1473 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001474
1475 // Sign extension. Compute the demanded bits in the result that are not
1476 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001477 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001478
Dan Gohman977a76f2008-02-13 22:28:48 +00001479 APInt InSignBit = APInt::getSignBit(EBits);
1480 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001481
1482 // If the sign extended bits are demanded, we know that the sign
1483 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001484 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001485 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001486 InputDemandedBits |= InSignBit;
1487
1488 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1489 KnownZero, KnownOne, Depth+1);
1490 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1491
1492 // If the sign bit of the input is known set or clear, then we know the
1493 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001494 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001495 KnownZero |= NewBits;
1496 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001497 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001498 KnownOne |= NewBits;
1499 KnownZero &= ~NewBits;
1500 } else { // Input sign bit unknown
1501 KnownZero &= ~NewBits;
1502 KnownOne &= ~NewBits;
1503 }
1504 return;
1505 }
1506 case ISD::CTTZ:
1507 case ISD::CTLZ:
1508 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001509 unsigned LowBits = Log2_32(BitWidth)+1;
1510 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001511 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 return;
1513 }
1514 case ISD::LOAD: {
1515 if (ISD::isZEXTLoad(Op.Val)) {
1516 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001517 MVT VT = LD->getMemoryVT();
1518 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001519 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 }
1521 return;
1522 }
1523 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001524 MVT InVT = Op.getOperand(0).getValueType();
1525 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001526 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1527 APInt InMask = Mask;
1528 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001529 KnownZero.trunc(InBits);
1530 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001531 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001532 KnownZero.zext(BitWidth);
1533 KnownOne.zext(BitWidth);
1534 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001535 return;
1536 }
1537 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 MVT InVT = Op.getOperand(0).getValueType();
1539 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001540 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001541 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1542 APInt InMask = Mask;
1543 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001544
1545 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001546 // bit is demanded. Temporarily set this bit in the mask for our callee.
1547 if (NewBits.getBoolValue())
1548 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001549
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001550 KnownZero.trunc(InBits);
1551 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001552 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1553
1554 // Note if the sign bit is known to be zero or one.
1555 bool SignBitKnownZero = KnownZero.isNegative();
1556 bool SignBitKnownOne = KnownOne.isNegative();
1557 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1558 "Sign bit can't be known to be both zero and one!");
1559
1560 // If the sign bit wasn't actually demanded by our caller, we don't
1561 // want it set in the KnownZero and KnownOne result values. Reset the
1562 // mask and reapply it to the result values.
1563 InMask = Mask;
1564 InMask.trunc(InBits);
1565 KnownZero &= InMask;
1566 KnownOne &= InMask;
1567
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001568 KnownZero.zext(BitWidth);
1569 KnownOne.zext(BitWidth);
1570
Dan Gohman977a76f2008-02-13 22:28:48 +00001571 // If the sign bit is known zero or one, the top bits match.
1572 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001574 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001575 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001576 return;
1577 }
1578 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001579 MVT InVT = Op.getOperand(0).getValueType();
1580 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001581 APInt InMask = Mask;
1582 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001583 KnownZero.trunc(InBits);
1584 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001585 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001586 KnownZero.zext(BitWidth);
1587 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 return;
1589 }
1590 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001591 MVT InVT = Op.getOperand(0).getValueType();
1592 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001593 APInt InMask = Mask;
1594 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001595 KnownZero.zext(InBits);
1596 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001597 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001598 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001599 KnownZero.trunc(BitWidth);
1600 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001601 break;
1602 }
1603 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001604 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1605 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001606 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1607 KnownOne, Depth+1);
1608 KnownZero |= (~InMask) & Mask;
1609 return;
1610 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001611 case ISD::FGETSIGN:
1612 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001613 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001614 return;
1615
Dan Gohman23e8b712008-04-28 17:02:21 +00001616 case ISD::SUB: {
1617 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1618 // We know that the top bits of C-X are clear if X contains less bits
1619 // than C (i.e. no wrap-around can happen). For example, 20-X is
1620 // positive if we can prove that X is >= 0 and < 16.
1621 if (CLHS->getAPIntValue().isNonNegative()) {
1622 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1623 // NLZ can't be BitWidth with no sign bit
1624 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1625 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1626 Depth+1);
1627
1628 // If all of the MaskV bits are known to be zero, then we know the
1629 // output top bits are zero, because we now know that the output is
1630 // from [0-C].
1631 if ((KnownZero2 & MaskV) == MaskV) {
1632 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1633 // Top bits known zero.
1634 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1635 }
1636 }
1637 }
1638 }
1639 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001640 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001641 // Output known-0 bits are known if clear or set in both the low clear bits
1642 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1643 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001644 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1645 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1646 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1647 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1648
1649 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1650 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1651 KnownZeroOut = std::min(KnownZeroOut,
1652 KnownZero2.countTrailingOnes());
1653
1654 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001655 return;
1656 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001657 case ISD::SREM:
1658 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001659 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001660 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001661 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001662 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1663 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001664
Dan Gohman23e8b712008-04-28 17:02:21 +00001665 // The sign of a remainder is equal to the sign of the first
1666 // operand (zero being positive).
1667 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1668 KnownZero2 |= ~LowBits;
1669 else if (KnownOne2[BitWidth-1])
1670 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001671
Dan Gohman23e8b712008-04-28 17:02:21 +00001672 KnownZero |= KnownZero2 & Mask;
1673 KnownOne |= KnownOne2 & Mask;
1674
1675 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001676 }
1677 }
1678 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001679 case ISD::UREM: {
1680 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001681 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001682 if (RA.isPowerOf2()) {
1683 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001684 APInt Mask2 = LowBits & Mask;
1685 KnownZero |= ~LowBits & Mask;
1686 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1687 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1688 break;
1689 }
1690 }
1691
1692 // Since the result is less than or equal to either operand, any leading
1693 // zero bits in either operand must also exist in the result.
1694 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1695 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1696 Depth+1);
1697 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1698 Depth+1);
1699
1700 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1701 KnownZero2.countLeadingOnes());
1702 KnownOne.clear();
1703 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1704 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001705 }
1706 default:
1707 // Allow the target to implement this method for its nodes.
1708 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1709 case ISD::INTRINSIC_WO_CHAIN:
1710 case ISD::INTRINSIC_W_CHAIN:
1711 case ISD::INTRINSIC_VOID:
1712 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1713 }
1714 return;
1715 }
1716}
1717
1718/// ComputeNumSignBits - Return the number of times the sign bit of the
1719/// register is replicated into the other bits. We know that at least 1 bit
1720/// is always equal to the sign bit (itself), but other cases can give us
1721/// information. For example, immediately after an "SRA X, 2", we know that
1722/// the top 3 bits are all equal to each other, so we return 3.
1723unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001724 MVT VT = Op.getValueType();
1725 assert(VT.isInteger() && "Invalid VT!");
1726 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001727 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001728 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001729
1730 if (Depth == 6)
1731 return 1; // Limit search depth.
1732
1733 switch (Op.getOpcode()) {
1734 default: break;
1735 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001736 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001737 return VTBits-Tmp+1;
1738 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001740 return VTBits-Tmp;
1741
1742 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001743 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1744 // If negative, return # leading ones.
1745 if (Val.isNegative())
1746 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001747
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001748 // Return # leading zeros.
1749 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001750 }
1751
1752 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001753 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001754 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1755
1756 case ISD::SIGN_EXTEND_INREG:
1757 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001759 Tmp = VTBits-Tmp+1;
1760
1761 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1762 return std::max(Tmp, Tmp2);
1763
1764 case ISD::SRA:
1765 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1766 // SRA X, C -> adds C sign bits.
1767 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1768 Tmp += C->getValue();
1769 if (Tmp > VTBits) Tmp = VTBits;
1770 }
1771 return Tmp;
1772 case ISD::SHL:
1773 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1774 // shl destroys sign bits.
1775 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1776 if (C->getValue() >= VTBits || // Bad shift.
1777 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1778 return Tmp - C->getValue();
1779 }
1780 break;
1781 case ISD::AND:
1782 case ISD::OR:
1783 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001784 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001785 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001786 if (Tmp != 1) {
1787 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1788 FirstAnswer = std::min(Tmp, Tmp2);
1789 // We computed what we know about the sign bits as our first
1790 // answer. Now proceed to the generic code that uses
1791 // ComputeMaskedBits, and pick whichever answer is better.
1792 }
1793 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001794
1795 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001796 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001798 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001799 return std::min(Tmp, Tmp2);
1800
1801 case ISD::SETCC:
1802 // If setcc returns 0/-1, all bits are sign bits.
1803 if (TLI.getSetCCResultContents() ==
1804 TargetLowering::ZeroOrNegativeOneSetCCResult)
1805 return VTBits;
1806 break;
1807 case ISD::ROTL:
1808 case ISD::ROTR:
1809 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1810 unsigned RotAmt = C->getValue() & (VTBits-1);
1811
1812 // Handle rotate right by N like a rotate left by 32-N.
1813 if (Op.getOpcode() == ISD::ROTR)
1814 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1815
1816 // If we aren't rotating out all of the known-in sign bits, return the
1817 // number that are left. This handles rotl(sext(x), 1) for example.
1818 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1819 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1820 }
1821 break;
1822 case ISD::ADD:
1823 // Add can have at most one carry bit. Thus we know that the output
1824 // is, at worst, one more bit than the inputs.
1825 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1826 if (Tmp == 1) return 1; // Early out.
1827
1828 // Special case decrementing a value (ADD X, -1):
1829 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1830 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001831 APInt KnownZero, KnownOne;
1832 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001833 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1834
1835 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1836 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001837 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 return VTBits;
1839
1840 // If we are subtracting one from a positive number, there is no carry
1841 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001842 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001843 return Tmp;
1844 }
1845
1846 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1847 if (Tmp2 == 1) return 1;
1848 return std::min(Tmp, Tmp2)-1;
1849 break;
1850
1851 case ISD::SUB:
1852 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1853 if (Tmp2 == 1) return 1;
1854
1855 // Handle NEG.
1856 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001857 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001858 APInt KnownZero, KnownOne;
1859 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001860 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1861 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1862 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001863 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001864 return VTBits;
1865
1866 // If the input is known to be positive (the sign bit is known clear),
1867 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001868 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 return Tmp2;
1870
1871 // Otherwise, we treat this like a SUB.
1872 }
1873
1874 // Sub can have at most one carry bit. Thus we know that the output
1875 // is, at worst, one more bit than the inputs.
1876 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1877 if (Tmp == 1) return 1; // Early out.
1878 return std::min(Tmp, Tmp2)-1;
1879 break;
1880 case ISD::TRUNCATE:
1881 // FIXME: it's tricky to do anything useful for this, but it is an important
1882 // case for targets like X86.
1883 break;
1884 }
1885
1886 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1887 if (Op.getOpcode() == ISD::LOAD) {
1888 LoadSDNode *LD = cast<LoadSDNode>(Op);
1889 unsigned ExtType = LD->getExtensionType();
1890 switch (ExtType) {
1891 default: break;
1892 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 return VTBits-Tmp+1;
1895 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001896 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 return VTBits-Tmp;
1898 }
1899 }
1900
1901 // Allow the target to implement this method for its nodes.
1902 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1903 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1904 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1905 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1906 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001907 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 }
1909
1910 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1911 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001912 APInt KnownZero, KnownOne;
1913 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1915
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001916 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001917 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001918 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001919 Mask = KnownOne;
1920 } else {
1921 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001922 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001923 }
1924
1925 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1926 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001927 Mask = ~Mask;
1928 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001929 // Return # leading zeros. We use 'min' here in case Val was zero before
1930 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001931 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001932}
1933
Chris Lattner51dabfb2006-10-14 00:41:01 +00001934
Evan Chenga844bde2008-02-02 04:07:54 +00001935bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1936 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1937 if (!GA) return false;
1938 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1939 if (!GV) return false;
1940 MachineModuleInfo *MMI = getMachineModuleInfo();
1941 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1942}
1943
1944
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001945/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1946/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001947SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001949 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001950 SDOperand Idx = PermMask.getOperand(i);
1951 if (Idx.getOpcode() == ISD::UNDEF)
1952 return getNode(ISD::UNDEF, VT.getVectorElementType());
1953 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001954 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001955 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1956 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001957
1958 if (V.getOpcode() == ISD::BIT_CONVERT) {
1959 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001960 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001961 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001962 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001963 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001964 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001965 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001966 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001967 return V.getOperand(Index);
1968 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1969 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001970 return SDOperand();
1971}
1972
1973
Chris Lattnerc3aae252005-01-07 07:46:32 +00001974/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001975///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001976SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001977 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001978 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001979 void *IP = 0;
1980 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1981 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001982 SDNode *N = getAllocator().Allocate<SDNode>();
1983 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001984 CSEMap.InsertNode(N, IP);
1985
1986 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00001987#ifndef NDEBUG
1988 VerifyNode(N);
1989#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001990 return SDOperand(N, 0);
1991}
1992
Duncan Sands83ec4b62008-06-06 12:08:01 +00001993SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001994 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001996 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001997 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001998 switch (Opcode) {
1999 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002000 case ISD::SIGN_EXTEND:
2001 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002002 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002003 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002004 case ISD::TRUNCATE:
2005 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002006 case ISD::UINT_TO_FP:
2007 case ISD::SINT_TO_FP: {
2008 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002009 // No compile time operations on this type.
2010 if (VT==MVT::ppcf128)
2011 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002012 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2013 (void)apf.convertFromAPInt(Val,
2014 Opcode==ISD::SINT_TO_FP,
2015 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002016 return getConstantFP(apf, VT);
2017 }
Chris Lattner94683772005-12-23 05:30:37 +00002018 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002019 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002020 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002021 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002022 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002023 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002024 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002025 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002026 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002027 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002028 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002029 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002030 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002031 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002032 }
2033 }
2034
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002035 // Constant fold unary operations with a floating point constant operand.
2036 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2037 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002038 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002039 switch (Opcode) {
2040 case ISD::FNEG:
2041 V.changeSign();
2042 return getConstantFP(V, VT);
2043 case ISD::FABS:
2044 V.clearSign();
2045 return getConstantFP(V, VT);
2046 case ISD::FP_ROUND:
2047 case ISD::FP_EXTEND:
2048 // This can return overflow, underflow, or inexact; we don't care.
2049 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002050 (void)V.convert(*MVTToAPFloatSemantics(VT),
2051 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002052 return getConstantFP(V, VT);
2053 case ISD::FP_TO_SINT:
2054 case ISD::FP_TO_UINT: {
2055 integerPart x;
2056 assert(integerPartWidth >= 64);
2057 // FIXME need to be more flexible about rounding mode.
2058 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2059 Opcode==ISD::FP_TO_SINT,
2060 APFloat::rmTowardZero);
2061 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2062 break;
2063 return getConstant(x, VT);
2064 }
2065 case ISD::BIT_CONVERT:
2066 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2067 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2068 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2069 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002070 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002071 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002072 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002073 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002074
2075 unsigned OpOpcode = Operand.Val->getOpcode();
2076 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002077 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002078 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002079 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002080 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002081 assert(VT.isFloatingPoint() &&
2082 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002083 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002084 if (Operand.getOpcode() == ISD::UNDEF)
2085 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002086 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002087 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002088 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002089 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002091 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002092 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2094 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2095 break;
2096 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002097 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002098 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002100 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002101 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002102 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002103 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002104 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002105 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002106 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002107 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002108 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002109 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002110 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002111 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2112 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2113 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2114 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002115 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002116 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002117 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002118 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002119 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002120 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002121 if (OpOpcode == ISD::TRUNCATE)
2122 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002123 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2124 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002125 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002126 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002127 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002128 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002129 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2130 else
2131 return Operand.Val->getOperand(0);
2132 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002133 break;
Chris Lattner35481892005-12-23 00:16:34 +00002134 case ISD::BIT_CONVERT:
2135 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002136 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002137 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002138 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002139 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2140 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002141 if (OpOpcode == ISD::UNDEF)
2142 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002143 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002144 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002145 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2146 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002147 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002148 if (OpOpcode == ISD::UNDEF)
2149 return getNode(ISD::UNDEF, VT);
2150 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2151 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2152 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2153 Operand.getConstantOperandVal(1) == 0 &&
2154 Operand.getOperand(0).getValueType() == VT)
2155 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002156 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002157 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002158 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2159 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002160 Operand.Val->getOperand(0));
2161 if (OpOpcode == ISD::FNEG) // --X -> X
2162 return Operand.Val->getOperand(0);
2163 break;
2164 case ISD::FABS:
2165 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2166 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2167 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002168 }
2169
Chris Lattner43247a12005-08-25 19:12:10 +00002170 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002171 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002172 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002173 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002174 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002175 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002176 void *IP = 0;
2177 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2178 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002179 N = getAllocator().Allocate<UnarySDNode>();
2180 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002181 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002182 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002183 N = getAllocator().Allocate<UnarySDNode>();
2184 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002185 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002186
Chris Lattnerc3aae252005-01-07 07:46:32 +00002187 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002188#ifndef NDEBUG
2189 VerifyNode(N);
2190#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002191 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002192}
2193
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002196 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2197 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002198 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002199 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002200 case ISD::TokenFactor:
2201 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2202 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002203 // Fold trivial token factors.
2204 if (N1.getOpcode() == ISD::EntryToken) return N2;
2205 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002206 break;
Chris Lattner76365122005-01-16 02:23:22 +00002207 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002208 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002209 N1.getValueType() == VT && "Binary operator types must match!");
2210 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2211 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002212 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002213 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002214 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2215 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002216 break;
Chris Lattner76365122005-01-16 02:23:22 +00002217 case ISD::OR:
2218 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002219 case ISD::ADD:
2220 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002222 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002223 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2224 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002225 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002226 return N1;
2227 break;
Chris Lattner76365122005-01-16 02:23:22 +00002228 case ISD::UDIV:
2229 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002230 case ISD::MULHU:
2231 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002232 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002233 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002234 case ISD::MUL:
2235 case ISD::SDIV:
2236 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002237 case ISD::FADD:
2238 case ISD::FSUB:
2239 case ISD::FMUL:
2240 case ISD::FDIV:
2241 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002242 assert(N1.getValueType() == N2.getValueType() &&
2243 N1.getValueType() == VT && "Binary operator types must match!");
2244 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002245 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2246 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002247 N1.getValueType().isFloatingPoint() &&
2248 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002249 "Invalid FCOPYSIGN!");
2250 break;
Chris Lattner76365122005-01-16 02:23:22 +00002251 case ISD::SHL:
2252 case ISD::SRA:
2253 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002254 case ISD::ROTL:
2255 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002256 assert(VT == N1.getValueType() &&
2257 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002258 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002259 "Shifts only work on integers");
2260
2261 // Always fold shifts of i1 values so the code generator doesn't need to
2262 // handle them. Since we know the size of the shift has to be less than the
2263 // size of the value, the shift/rotate count is guaranteed to be zero.
2264 if (VT == MVT::i1)
2265 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002266 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002267 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002268 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002269 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002270 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002271 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002272 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002273 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002274 break;
2275 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002276 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 assert(VT.isFloatingPoint() &&
2278 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002279 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002280 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002281 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002282 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002283 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002284 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002285 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002286 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002287 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002288 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002289 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002290 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002291 break;
2292 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002293 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002294 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002295 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002296 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002297 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002298 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002299 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002300
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002301 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002302 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002303 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002304 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002305 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002306 return getConstant(Val, VT);
2307 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002308 break;
2309 }
2310 case ISD::EXTRACT_VECTOR_ELT:
2311 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2312
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002313 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2314 if (N1.getOpcode() == ISD::UNDEF)
2315 return getNode(ISD::UNDEF, VT);
2316
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002317 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2318 // expanding copies of large vectors from registers.
2319 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2320 N1.getNumOperands() > 0) {
2321 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002322 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002323 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2324 N1.getOperand(N2C->getValue() / Factor),
2325 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2326 }
2327
2328 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2329 // expanding large vector constants.
2330 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2331 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002332
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002333 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2334 // operations are lowered to scalars.
2335 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2336 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2337 if (IEC == N2C)
2338 return N1.getOperand(1);
2339 else
2340 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2341 }
2342 break;
2343 case ISD::EXTRACT_ELEMENT:
2344 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002345 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2346 (N1.getValueType().isInteger() == VT.isInteger()) &&
2347 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002348
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002349 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2350 // 64-bit integers into 32-bit parts. Instead of building the extract of
2351 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2352 if (N1.getOpcode() == ISD::BUILD_PAIR)
2353 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002354
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002355 // EXTRACT_ELEMENT of a constant int is also very common.
2356 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002358 unsigned Shift = ElementSize * N2C->getValue();
2359 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2360 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002361 }
2362 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002363 case ISD::EXTRACT_SUBVECTOR:
2364 if (N1.getValueType() == VT) // Trivial extraction.
2365 return N1;
2366 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002367 }
2368
2369 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002370 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002371 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 switch (Opcode) {
2373 case ISD::ADD: return getConstant(C1 + C2, VT);
2374 case ISD::SUB: return getConstant(C1 - C2, VT);
2375 case ISD::MUL: return getConstant(C1 * C2, VT);
2376 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002377 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002378 break;
2379 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002380 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002381 break;
2382 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002383 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 break;
2385 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002386 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002387 break;
2388 case ISD::AND : return getConstant(C1 & C2, VT);
2389 case ISD::OR : return getConstant(C1 | C2, VT);
2390 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002391 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002392 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2393 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2394 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2395 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002396 default: break;
2397 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002398 } else { // Cannonicalize constant to RHS if commutative
2399 if (isCommutativeBinOp(Opcode)) {
2400 std::swap(N1C, N2C);
2401 std::swap(N1, N2);
2402 }
2403 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404 }
2405
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002406 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002407 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2408 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002409 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002410 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2411 // Cannonicalize constant to RHS if commutative
2412 std::swap(N1CFP, N2CFP);
2413 std::swap(N1, N2);
2414 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002415 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2416 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002417 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002418 case ISD::FADD:
2419 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002420 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002421 return getConstantFP(V1, VT);
2422 break;
2423 case ISD::FSUB:
2424 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2425 if (s!=APFloat::opInvalidOp)
2426 return getConstantFP(V1, VT);
2427 break;
2428 case ISD::FMUL:
2429 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2430 if (s!=APFloat::opInvalidOp)
2431 return getConstantFP(V1, VT);
2432 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002433 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002434 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2435 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2436 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002437 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002438 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002439 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2440 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2441 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002442 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002443 case ISD::FCOPYSIGN:
2444 V1.copySign(V2);
2445 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002446 default: break;
2447 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002449 }
Chris Lattner62b57722006-04-20 05:39:12 +00002450
2451 // Canonicalize an UNDEF to the RHS, even over a constant.
2452 if (N1.getOpcode() == ISD::UNDEF) {
2453 if (isCommutativeBinOp(Opcode)) {
2454 std::swap(N1, N2);
2455 } else {
2456 switch (Opcode) {
2457 case ISD::FP_ROUND_INREG:
2458 case ISD::SIGN_EXTEND_INREG:
2459 case ISD::SUB:
2460 case ISD::FSUB:
2461 case ISD::FDIV:
2462 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002463 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002464 return N1; // fold op(undef, arg2) -> undef
2465 case ISD::UDIV:
2466 case ISD::SDIV:
2467 case ISD::UREM:
2468 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002469 case ISD::SRL:
2470 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002471 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002472 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2473 // For vectors, we can't easily build an all zero vector, just return
2474 // the LHS.
2475 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002476 }
2477 }
2478 }
2479
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002480 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002481 if (N2.getOpcode() == ISD::UNDEF) {
2482 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002483 case ISD::XOR:
2484 if (N1.getOpcode() == ISD::UNDEF)
2485 // Handle undef ^ undef -> 0 special case. This is a common
2486 // idiom (misuse).
2487 return getConstant(0, VT);
2488 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002489 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002490 case ISD::ADDC:
2491 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002492 case ISD::SUB:
2493 case ISD::FADD:
2494 case ISD::FSUB:
2495 case ISD::FMUL:
2496 case ISD::FDIV:
2497 case ISD::FREM:
2498 case ISD::UDIV:
2499 case ISD::SDIV:
2500 case ISD::UREM:
2501 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002502 return N2; // fold op(arg1, undef) -> undef
2503 case ISD::MUL:
2504 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002505 case ISD::SRL:
2506 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002507 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002508 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2509 // For vectors, we can't easily build an all zero vector, just return
2510 // the LHS.
2511 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002512 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002513 if (!VT.isVector())
2514 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002515 // For vectors, we can't easily build an all one vector, just return
2516 // the LHS.
2517 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002518 case ISD::SRA:
2519 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002520 }
2521 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002522
Chris Lattner27e9b412005-05-11 18:57:39 +00002523 // Memoize this node if possible.
2524 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002525 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002526 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002527 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002528 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002529 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002530 void *IP = 0;
2531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2532 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002533 N = getAllocator().Allocate<BinarySDNode>();
2534 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002535 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002536 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002537 N = getAllocator().Allocate<BinarySDNode>();
2538 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002539 }
2540
Chris Lattnerc3aae252005-01-07 07:46:32 +00002541 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002542#ifndef NDEBUG
2543 VerifyNode(N);
2544#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002545 return SDOperand(N, 0);
2546}
2547
Duncan Sands83ec4b62008-06-06 12:08:01 +00002548SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002549 SDOperand N1, SDOperand N2, SDOperand N3) {
2550 // Perform various simplifications.
2551 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2552 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002553 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002554 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002555 // Use FoldSetCC to simplify SETCC's.
2556 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002557 if (Simp.Val) return Simp;
2558 break;
2559 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002560 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002561 if (N1C) {
2562 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002564 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002565 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002566 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002567
2568 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002569 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002570 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002571 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002572 if (N2C->getValue()) // Unconditional branch
2573 return getNode(ISD::BR, MVT::Other, N1, N3);
2574 else
2575 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002576 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002577 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002578 case ISD::VECTOR_SHUFFLE:
2579 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002581 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002582 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002583 "Illegal VECTOR_SHUFFLE node!");
2584 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002585 case ISD::BIT_CONVERT:
2586 // Fold bit_convert nodes from a type to themselves.
2587 if (N1.getValueType() == VT)
2588 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002589 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002590 }
2591
Chris Lattner43247a12005-08-25 19:12:10 +00002592 // Memoize node if it doesn't produce a flag.
2593 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002594 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002595 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002596 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002597 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002598 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002599 void *IP = 0;
2600 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2601 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002602 N = getAllocator().Allocate<TernarySDNode>();
2603 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002604 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002605 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002606 N = getAllocator().Allocate<TernarySDNode>();
2607 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002608 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002609 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002610#ifndef NDEBUG
2611 VerifyNode(N);
2612#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002613 return SDOperand(N, 0);
2614}
2615
Duncan Sands83ec4b62008-06-06 12:08:01 +00002616SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002617 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002618 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002619 SDOperand Ops[] = { N1, N2, N3, N4 };
2620 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002621}
2622
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002624 SDOperand N1, SDOperand N2, SDOperand N3,
2625 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002626 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2627 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002628}
2629
Dan Gohman707e0182008-04-12 04:36:06 +00002630/// getMemsetValue - Vectorized representation of the memset value
2631/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002632static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2633 unsigned NumBits = VT.isVector() ?
2634 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002635 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002636 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002637 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002638 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002639 Val = (Val << Shift) | Val;
2640 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002641 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002642 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002643 return DAG.getConstant(Val, VT);
2644 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002645 }
Evan Chengf0df0312008-05-15 08:39:06 +00002646
2647 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2648 unsigned Shift = 8;
2649 for (unsigned i = NumBits; i > 8; i >>= 1) {
2650 Value = DAG.getNode(ISD::OR, VT,
2651 DAG.getNode(ISD::SHL, VT, Value,
2652 DAG.getConstant(Shift, MVT::i8)), Value);
2653 Shift <<= 1;
2654 }
2655
2656 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002657}
2658
Dan Gohman707e0182008-04-12 04:36:06 +00002659/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2660/// used when a memcpy is turned into a memset when the source is a constant
2661/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002663 const TargetLowering &TLI,
2664 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002665 // Handle vector with all elements zero.
2666 if (Str.empty()) {
2667 if (VT.isInteger())
2668 return DAG.getConstant(0, VT);
2669 unsigned NumElts = VT.getVectorNumElements();
2670 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2671 return DAG.getNode(ISD::BIT_CONVERT, VT,
2672 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2673 }
2674
Duncan Sands83ec4b62008-06-06 12:08:01 +00002675 assert(!VT.isVector() && "Can't handle vector type here!");
2676 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002677 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002678 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002679 if (TLI.isLittleEndian())
2680 Offset = Offset + MSB - 1;
2681 for (unsigned i = 0; i != MSB; ++i) {
2682 Val = (Val << 8) | (unsigned char)Str[Offset];
2683 Offset += TLI.isLittleEndian() ? -1 : 1;
2684 }
2685 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002686}
2687
Dan Gohman707e0182008-04-12 04:36:06 +00002688/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002689///
Dan Gohman707e0182008-04-12 04:36:06 +00002690static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2691 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002692 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002693 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2694}
2695
Evan Chengf0df0312008-05-15 08:39:06 +00002696/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2697///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002698static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002699 unsigned SrcDelta = 0;
2700 GlobalAddressSDNode *G = NULL;
2701 if (Src.getOpcode() == ISD::GlobalAddress)
2702 G = cast<GlobalAddressSDNode>(Src);
2703 else if (Src.getOpcode() == ISD::ADD &&
2704 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2705 Src.getOperand(1).getOpcode() == ISD::Constant) {
2706 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2707 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2708 }
2709 if (!G)
2710 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002711
Evan Chengf0df0312008-05-15 08:39:06 +00002712 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002713 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2714 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002715
Evan Chengf0df0312008-05-15 08:39:06 +00002716 return false;
2717}
Dan Gohman707e0182008-04-12 04:36:06 +00002718
Evan Chengf0df0312008-05-15 08:39:06 +00002719/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2720/// to replace the memset / memcpy is below the threshold. It also returns the
2721/// types of the sequence of memory ops to perform memset / memcpy.
2722static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002723bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002724 SDOperand Dst, SDOperand Src,
2725 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002726 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002727 SelectionDAG &DAG,
2728 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002729 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002730 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002731 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002732 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002733 if (VT != MVT::iAny) {
2734 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002735 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002736 // If source is a string constant, this will require an unaligned load.
2737 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2738 if (Dst.getOpcode() != ISD::FrameIndex) {
2739 // Can't change destination alignment. It requires a unaligned store.
2740 if (AllowUnalign)
2741 VT = MVT::iAny;
2742 } else {
2743 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2744 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2745 if (MFI->isFixedObjectIndex(FI)) {
2746 // Can't change destination alignment. It requires a unaligned store.
2747 if (AllowUnalign)
2748 VT = MVT::iAny;
2749 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002750 // Give the stack frame object a larger alignment if needed.
2751 if (MFI->getObjectAlignment(FI) < NewAlign)
2752 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002753 Align = NewAlign;
2754 }
2755 }
2756 }
2757 }
2758
2759 if (VT == MVT::iAny) {
2760 if (AllowUnalign) {
2761 VT = MVT::i64;
2762 } else {
2763 switch (Align & 7) {
2764 case 0: VT = MVT::i64; break;
2765 case 4: VT = MVT::i32; break;
2766 case 2: VT = MVT::i16; break;
2767 default: VT = MVT::i8; break;
2768 }
2769 }
2770
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002772 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002773 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2774 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002775
Duncan Sands8e4eb092008-06-08 20:54:56 +00002776 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002777 VT = LVT;
2778 }
Dan Gohman707e0182008-04-12 04:36:06 +00002779
2780 unsigned NumMemOps = 0;
2781 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002782 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002783 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002784 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002786 VT = MVT::i64;
2787 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002788 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2789 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002790 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002791 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002792 VTSize >>= 1;
2793 }
Dan Gohman707e0182008-04-12 04:36:06 +00002794 }
Dan Gohman707e0182008-04-12 04:36:06 +00002795
2796 if (++NumMemOps > Limit)
2797 return false;
2798 MemOps.push_back(VT);
2799 Size -= VTSize;
2800 }
2801
2802 return true;
2803}
2804
2805static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2806 SDOperand Chain, SDOperand Dst,
2807 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002808 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002809 const Value *DstSV, uint64_t DstSVOff,
2810 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002811 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2812
Dan Gohman21323f32008-05-29 19:42:22 +00002813 // Expand memcpy 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 Gohman707e0182008-04-12 04:36:06 +00002816 uint64_t Limit = -1;
2817 if (!AlwaysInline)
2818 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002819 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002820 std::string Str;
2821 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002822 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002823 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002824 return SDOperand();
2825
Dan Gohman707e0182008-04-12 04:36:06 +00002826
Evan Cheng0ff39b32008-06-30 07:31:25 +00002827 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002828 SmallVector<SDOperand, 8> OutChains;
2829 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002830 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002831 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002832 MVT VT = MemOps[i];
2833 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002834 SDOperand Value, Store;
2835
Evan Cheng0ff39b32008-06-30 07:31:25 +00002836 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002837 // It's unlikely a store of a vector immediate can be done in a single
2838 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002839 // We also handle store a vector with all zero's.
2840 // FIXME: Handle other cases where store of vector immediate is done in
2841 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002842 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002843 Store = DAG.getStore(Chain, Value,
2844 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002845 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002846 } else {
2847 Value = DAG.getLoad(VT, Chain,
2848 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002849 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002850 Store = DAG.getStore(Chain, Value,
2851 getMemBasePlusOffset(Dst, DstOff, DAG),
2852 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002853 }
2854 OutChains.push_back(Store);
2855 SrcOff += VTSize;
2856 DstOff += VTSize;
2857 }
2858
2859 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2860 &OutChains[0], OutChains.size());
2861}
2862
Dan Gohman21323f32008-05-29 19:42:22 +00002863static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2864 SDOperand Chain, SDOperand Dst,
2865 SDOperand Src, uint64_t Size,
2866 unsigned Align, bool AlwaysInline,
2867 const Value *DstSV, uint64_t DstSVOff,
2868 const Value *SrcSV, uint64_t SrcSVOff){
2869 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2870
2871 // Expand memmove to a series of load and store ops if the size operand falls
2872 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002874 uint64_t Limit = -1;
2875 if (!AlwaysInline)
2876 Limit = TLI.getMaxStoresPerMemmove();
2877 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002878 std::string Str;
2879 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002880 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002881 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002882 return SDOperand();
2883
Dan Gohman21323f32008-05-29 19:42:22 +00002884 uint64_t SrcOff = 0, DstOff = 0;
2885
2886 SmallVector<SDOperand, 8> LoadValues;
2887 SmallVector<SDOperand, 8> LoadChains;
2888 SmallVector<SDOperand, 8> OutChains;
2889 unsigned NumMemOps = MemOps.size();
2890 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002891 MVT VT = MemOps[i];
2892 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002893 SDOperand Value, Store;
2894
2895 Value = DAG.getLoad(VT, Chain,
2896 getMemBasePlusOffset(Src, SrcOff, DAG),
2897 SrcSV, SrcSVOff + SrcOff, false, Align);
2898 LoadValues.push_back(Value);
2899 LoadChains.push_back(Value.getValue(1));
2900 SrcOff += VTSize;
2901 }
2902 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2903 &LoadChains[0], LoadChains.size());
2904 OutChains.clear();
2905 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002906 MVT VT = MemOps[i];
2907 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002908 SDOperand Value, Store;
2909
2910 Store = DAG.getStore(Chain, LoadValues[i],
2911 getMemBasePlusOffset(Dst, DstOff, DAG),
2912 DstSV, DstSVOff + DstOff, false, DstAlign);
2913 OutChains.push_back(Store);
2914 DstOff += VTSize;
2915 }
2916
2917 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2918 &OutChains[0], OutChains.size());
2919}
2920
Dan Gohman707e0182008-04-12 04:36:06 +00002921static SDOperand getMemsetStores(SelectionDAG &DAG,
2922 SDOperand Chain, SDOperand Dst,
2923 SDOperand Src, uint64_t Size,
2924 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002925 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002926 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2927
2928 // Expand memset to a series of load/store ops if the size operand
2929 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002930 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002931 std::string Str;
2932 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002933 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002934 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002935 return SDOperand();
2936
2937 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002938 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002939
2940 unsigned NumMemOps = MemOps.size();
2941 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002942 MVT VT = MemOps[i];
2943 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002944 SDOperand Value = getMemsetValue(Src, VT, DAG);
2945 SDOperand Store = DAG.getStore(Chain, Value,
2946 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002947 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002948 OutChains.push_back(Store);
2949 DstOff += VTSize;
2950 }
2951
2952 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2953 &OutChains[0], OutChains.size());
2954}
2955
2956SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002957 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002958 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002959 const Value *DstSV, uint64_t DstSVOff,
2960 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002961
2962 // Check to see if we should lower the memcpy to loads and stores first.
2963 // For cases within the target-specified limits, this is the best choice.
2964 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2965 if (ConstantSize) {
2966 // Memcpy with size zero? Just return the original chain.
2967 if (ConstantSize->isNullValue())
2968 return Chain;
2969
2970 SDOperand Result =
2971 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002972 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002973 if (Result.Val)
2974 return Result;
2975 }
2976
2977 // Then check to see if we should lower the memcpy with target-specific
2978 // code. If the target chooses to do this, this is the next best.
2979 SDOperand Result =
2980 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2981 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002982 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002983 if (Result.Val)
2984 return Result;
2985
2986 // If we really need inline code and the target declined to provide it,
2987 // use a (potentially long) sequence of loads and stores.
2988 if (AlwaysInline) {
2989 assert(ConstantSize && "AlwaysInline requires a constant size!");
2990 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2991 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002992 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002993 }
2994
2995 // Emit a library call.
2996 TargetLowering::ArgListTy Args;
2997 TargetLowering::ArgListEntry Entry;
2998 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2999 Entry.Node = Dst; Args.push_back(Entry);
3000 Entry.Node = Src; Args.push_back(Entry);
3001 Entry.Node = Size; Args.push_back(Entry);
3002 std::pair<SDOperand,SDOperand> CallResult =
3003 TLI.LowerCallTo(Chain, Type::VoidTy,
3004 false, false, false, CallingConv::C, false,
3005 getExternalSymbol("memcpy", TLI.getPointerTy()),
3006 Args, *this);
3007 return CallResult.second;
3008}
3009
3010SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
3011 SDOperand Src, SDOperand Size,
3012 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003013 const Value *DstSV, uint64_t DstSVOff,
3014 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003015
Dan Gohman21323f32008-05-29 19:42:22 +00003016 // Check to see if we should lower the memmove to loads and stores first.
3017 // For cases within the target-specified limits, this is the best choice.
3018 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3019 if (ConstantSize) {
3020 // Memmove with size zero? Just return the original chain.
3021 if (ConstantSize->isNullValue())
3022 return Chain;
3023
3024 SDOperand Result =
3025 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3026 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3027 if (Result.Val)
3028 return Result;
3029 }
Dan Gohman707e0182008-04-12 04:36:06 +00003030
3031 // Then check to see if we should lower the memmove with target-specific
3032 // code. If the target chooses to do this, this is the next best.
3033 SDOperand Result =
3034 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003035 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003036 if (Result.Val)
3037 return Result;
3038
3039 // Emit a library call.
3040 TargetLowering::ArgListTy Args;
3041 TargetLowering::ArgListEntry Entry;
3042 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3043 Entry.Node = Dst; Args.push_back(Entry);
3044 Entry.Node = Src; Args.push_back(Entry);
3045 Entry.Node = Size; Args.push_back(Entry);
3046 std::pair<SDOperand,SDOperand> CallResult =
3047 TLI.LowerCallTo(Chain, Type::VoidTy,
3048 false, false, false, CallingConv::C, false,
3049 getExternalSymbol("memmove", TLI.getPointerTy()),
3050 Args, *this);
3051 return CallResult.second;
3052}
3053
3054SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
3055 SDOperand Src, SDOperand Size,
3056 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003057 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003058
3059 // Check to see if we should lower the memset to stores first.
3060 // For cases within the target-specified limits, this is the best choice.
3061 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3062 if (ConstantSize) {
3063 // Memset with size zero? Just return the original chain.
3064 if (ConstantSize->isNullValue())
3065 return Chain;
3066
3067 SDOperand Result =
3068 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003069 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003070 if (Result.Val)
3071 return Result;
3072 }
3073
3074 // Then check to see if we should lower the memset with target-specific
3075 // code. If the target chooses to do this, this is the next best.
3076 SDOperand Result =
3077 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003078 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003079 if (Result.Val)
3080 return Result;
3081
3082 // Emit a library call.
3083 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3084 TargetLowering::ArgListTy Args;
3085 TargetLowering::ArgListEntry Entry;
3086 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3087 Args.push_back(Entry);
3088 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003089 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003090 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3091 else
3092 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3093 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3094 Args.push_back(Entry);
3095 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3096 Args.push_back(Entry);
3097 std::pair<SDOperand,SDOperand> CallResult =
3098 TLI.LowerCallTo(Chain, Type::VoidTy,
3099 false, false, false, CallingConv::C, false,
3100 getExternalSymbol("memset", TLI.getPointerTy()),
3101 Args, *this);
3102 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003103}
3104
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003105SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003106 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003107 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003108 unsigned Alignment) {
3109 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003110 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003111
3112 MVT VT = Cmp.getValueType();
3113
3114 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3115 Alignment = getMVTAlignment(VT);
3116
3117 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003118 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003119 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003120 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003121 void* IP = 0;
3122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3123 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003124 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3125 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003126 CSEMap.InsertNode(N, IP);
3127 AllNodes.push_back(N);
3128 return SDOperand(N, 0);
3129}
3130
3131SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003132 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003133 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003134 unsigned Alignment) {
3135 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003136 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3137 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003138 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003139 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3140 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003141 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003142
3143 MVT VT = Val.getValueType();
3144
3145 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3146 Alignment = getMVTAlignment(VT);
3147
3148 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003149 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003150 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003151 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003152 void* IP = 0;
3153 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3154 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003155 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3156 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003157 CSEMap.InsertNode(N, IP);
3158 AllNodes.push_back(N);
3159 return SDOperand(N, 0);
3160}
3161
Duncan Sands4bdcb612008-07-02 17:40:58 +00003162/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3163/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003164SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003165 bool Simplify) {
3166 if (Simplify && NumOps == 1)
3167 return Ops[0];
3168
3169 SmallVector<MVT, 4> VTs;
3170 VTs.reserve(NumOps);
3171 for (unsigned i = 0; i < NumOps; ++i)
3172 VTs.push_back(Ops[i].getValueType());
3173 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3174}
3175
Duncan Sands14ea39c2008-03-27 20:23:40 +00003176SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003177SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003178 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003179 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003180 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003181 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003182 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3183 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003184
Duncan Sands14ea39c2008-03-27 20:23:40 +00003185 if (VT == EVT) {
3186 ExtType = ISD::NON_EXTLOAD;
3187 } else if (ExtType == ISD::NON_EXTLOAD) {
3188 assert(VT == EVT && "Non-extending load from different memory type!");
3189 } else {
3190 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 if (VT.isVector())
3192 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003193 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003194 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003195 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003196 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003197 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003199 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003200 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003201
3202 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003203 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003204 "Unindexed load with an offset!");
3205
3206 SDVTList VTs = Indexed ?
3207 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3208 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003209 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003210 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003211 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003212 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003213 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003214 ID.AddInteger(Alignment);
3215 ID.AddInteger(isVolatile);
3216 void *IP = 0;
3217 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3218 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003219 SDNode *N = getAllocator().Allocate<LoadSDNode>();
3220 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3221 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003222 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003223 AllNodes.push_back(N);
3224 return SDOperand(N, 0);
3225}
3226
Duncan Sands83ec4b62008-06-06 12:08:01 +00003227SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003228 SDOperand Chain, SDOperand Ptr,
3229 const Value *SV, int SVOffset,
3230 bool isVolatile, unsigned Alignment) {
3231 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003232 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3233 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003234}
3235
Duncan Sands83ec4b62008-06-06 12:08:01 +00003236SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003237 SDOperand Chain, SDOperand Ptr,
3238 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003239 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003240 bool isVolatile, unsigned Alignment) {
3241 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003242 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3243 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003244}
3245
Evan Cheng144d8f02006-11-09 17:55:04 +00003246SDOperand
3247SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3248 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003249 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003250 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3251 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003252 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3253 LD->getChain(), Base, Offset, LD->getSrcValue(),
3254 LD->getSrcValueOffset(), LD->getMemoryVT(),
3255 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003256}
3257
Jeff Cohend41b30d2006-11-05 19:31:28 +00003258SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003259 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003260 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003261 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003262
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003263 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3264 Alignment = getMVTAlignment(VT);
3265
Evan Chengad071e12006-10-05 22:57:11 +00003266 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003267 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003268 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003269 FoldingSetNodeID ID;
3270 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003271 ID.AddInteger(ISD::UNINDEXED);
3272 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003273 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003274 ID.AddInteger(Alignment);
3275 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003276 void *IP = 0;
3277 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3278 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003279 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3280 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3281 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003282 CSEMap.InsertNode(N, IP);
3283 AllNodes.push_back(N);
3284 return SDOperand(N, 0);
3285}
3286
Jeff Cohend41b30d2006-11-05 19:31:28 +00003287SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003288 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003289 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003290 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003292
3293 if (VT == SVT)
3294 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003295
Duncan Sands8e4eb092008-06-08 20:54:56 +00003296 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003297 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003298 "Can't do FP-INT conversion!");
3299
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003300 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3301 Alignment = getMVTAlignment(VT);
3302
Evan Cheng8b2794a2006-10-13 21:14:26 +00003303 SDVTList VTs = getVTList(MVT::Other);
3304 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003305 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003306 FoldingSetNodeID ID;
3307 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003308 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003309 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003310 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003311 ID.AddInteger(Alignment);
3312 ID.AddInteger(isVolatile);
3313 void *IP = 0;
3314 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3315 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003316 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3317 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3318 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003319 CSEMap.InsertNode(N, IP);
3320 AllNodes.push_back(N);
3321 return SDOperand(N, 0);
3322}
3323
Evan Cheng144d8f02006-11-09 17:55:04 +00003324SDOperand
3325SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3326 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003327 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3328 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3329 "Store is already a indexed store!");
3330 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3331 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3332 FoldingSetNodeID ID;
3333 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3334 ID.AddInteger(AM);
3335 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003336 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003337 ID.AddInteger(ST->getAlignment());
3338 ID.AddInteger(ST->isVolatile());
3339 void *IP = 0;
3340 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3341 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003342 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3343 new (N) StoreSDNode(Ops, VTs, AM,
3344 ST->isTruncatingStore(), ST->getMemoryVT(),
3345 ST->getSrcValue(), ST->getSrcValueOffset(),
3346 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003347 CSEMap.InsertNode(N, IP);
3348 AllNodes.push_back(N);
3349 return SDOperand(N, 0);
3350}
3351
Duncan Sands83ec4b62008-06-06 12:08:01 +00003352SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003353 SDOperand Chain, SDOperand Ptr,
3354 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003355 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003356 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003357}
3358
Duncan Sands83ec4b62008-06-06 12:08:01 +00003359SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003360 const SDUse *Ops, unsigned NumOps) {
3361 switch (NumOps) {
3362 case 0: return getNode(Opcode, VT);
3363 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3364 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3365 Ops[1].getSDOperand());
3366 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3367 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3368 default: break;
3369 }
3370
3371 // Copy from an SDUse array into an SDOperand array for use with
3372 // the regular getNode logic.
3373 SmallVector<SDOperand, 8> NewOps;
3374 NewOps.reserve(NumOps);
3375 for (unsigned i = 0; i != NumOps; ++i)
3376 NewOps.push_back(Ops[i].getSDOperand());
3377 return getNode(Opcode, VT, Ops, NumOps);
3378}
3379
3380SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3381 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003382 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003383 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003384 case 1: return getNode(Opcode, VT, Ops[0]);
3385 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3386 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003387 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003388 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003389
Chris Lattneref847df2005-04-09 03:27:28 +00003390 switch (Opcode) {
3391 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003392 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003393 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003394 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3395 "LHS and RHS of condition must have same type!");
3396 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3397 "True and False arms of SelectCC must have same type!");
3398 assert(Ops[2].getValueType() == VT &&
3399 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003400 break;
3401 }
3402 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003403 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003404 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3405 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003406 break;
3407 }
Chris Lattneref847df2005-04-09 03:27:28 +00003408 }
3409
Chris Lattner385328c2005-05-14 07:42:29 +00003410 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003411 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003412 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003413 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003414 FoldingSetNodeID ID;
3415 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003416 void *IP = 0;
3417 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3418 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003419 N = getAllocator().Allocate<SDNode>();
3420 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003421 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003422 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003423 N = getAllocator().Allocate<SDNode>();
3424 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003425 }
Chris Lattneref847df2005-04-09 03:27:28 +00003426 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003427#ifndef NDEBUG
3428 VerifyNode(N);
3429#endif
Chris Lattneref847df2005-04-09 03:27:28 +00003430 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003431}
3432
Chris Lattner89c34632005-05-14 06:20:26 +00003433SDOperand SelectionDAG::getNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00003434 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003435 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003436 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3437 Ops, NumOps);
3438}
3439
3440SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003441 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003442 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003443 if (NumVTs == 1)
3444 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003445 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3446}
3447
3448SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003449 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003450 if (VTList.NumVTs == 1)
3451 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003452
Chris Lattner5f056bf2005-07-10 01:55:33 +00003453 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003454 // FIXME: figure out how to safely handle things like
3455 // int foo(int x) { return 1 << (x & 255); }
3456 // int bar() { return foo(256); }
3457#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003458 case ISD::SRA_PARTS:
3459 case ISD::SRL_PARTS:
3460 case ISD::SHL_PARTS:
3461 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003462 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003463 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3464 else if (N3.getOpcode() == ISD::AND)
3465 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3466 // If the and is only masking out bits that cannot effect the shift,
3467 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003468 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003469 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3470 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3471 }
3472 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003473#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003474 }
Chris Lattner89c34632005-05-14 06:20:26 +00003475
Chris Lattner43247a12005-08-25 19:12:10 +00003476 // Memoize the node unless it returns a flag.
3477 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003478 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003479 FoldingSetNodeID ID;
3480 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003481 void *IP = 0;
3482 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3483 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003484 if (NumOps == 1) {
3485 N = getAllocator().Allocate<UnarySDNode>();
3486 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3487 } else if (NumOps == 2) {
3488 N = getAllocator().Allocate<BinarySDNode>();
3489 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3490 } else if (NumOps == 3) {
3491 N = getAllocator().Allocate<TernarySDNode>();
3492 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3493 } else {
3494 N = getAllocator().Allocate<SDNode>();
3495 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3496 }
Chris Lattnera5682852006-08-07 23:03:03 +00003497 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003498 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003499 if (NumOps == 1) {
3500 N = getAllocator().Allocate<UnarySDNode>();
3501 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3502 } else if (NumOps == 2) {
3503 N = getAllocator().Allocate<BinarySDNode>();
3504 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3505 } else if (NumOps == 3) {
3506 N = getAllocator().Allocate<TernarySDNode>();
3507 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3508 } else {
3509 N = getAllocator().Allocate<SDNode>();
3510 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3511 }
Chris Lattner43247a12005-08-25 19:12:10 +00003512 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003513 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003514#ifndef NDEBUG
3515 VerifyNode(N);
3516#endif
Chris Lattner89c34632005-05-14 06:20:26 +00003517 return SDOperand(N, 0);
3518}
3519
Dan Gohman08ce9762007-10-08 15:49:58 +00003520SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003521 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003522}
3523
3524SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3525 SDOperand N1) {
3526 SDOperand Ops[] = { N1 };
3527 return getNode(Opcode, VTList, Ops, 1);
3528}
3529
3530SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3531 SDOperand N1, SDOperand N2) {
3532 SDOperand Ops[] = { N1, N2 };
3533 return getNode(Opcode, VTList, Ops, 2);
3534}
3535
3536SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3537 SDOperand N1, SDOperand N2, SDOperand N3) {
3538 SDOperand Ops[] = { N1, N2, N3 };
3539 return getNode(Opcode, VTList, Ops, 3);
3540}
3541
3542SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3543 SDOperand N1, SDOperand N2, SDOperand N3,
3544 SDOperand N4) {
3545 SDOperand Ops[] = { N1, N2, N3, N4 };
3546 return getNode(Opcode, VTList, Ops, 4);
3547}
3548
3549SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3550 SDOperand N1, SDOperand N2, SDOperand N3,
3551 SDOperand N4, SDOperand N5) {
3552 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3553 return getNode(Opcode, VTList, Ops, 5);
3554}
3555
Duncan Sands83ec4b62008-06-06 12:08:01 +00003556SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003557 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003558}
3559
Duncan Sands83ec4b62008-06-06 12:08:01 +00003560SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003561 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3562 E = VTList.rend(); I != E; ++I)
3563 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3564 return *I;
3565
3566 MVT *Array = Allocator.Allocate<MVT>(2);
3567 Array[0] = VT1;
3568 Array[1] = VT2;
3569 SDVTList Result = makeVTList(Array, 2);
3570 VTList.push_back(Result);
3571 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003572}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003573
3574SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3575 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3576 E = VTList.rend(); I != E; ++I)
3577 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3578 I->VTs[2] == VT3)
3579 return *I;
3580
3581 MVT *Array = Allocator.Allocate<MVT>(3);
3582 Array[0] = VT1;
3583 Array[1] = VT2;
3584 Array[2] = VT3;
3585 SDVTList Result = makeVTList(Array, 3);
3586 VTList.push_back(Result);
3587 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003588}
3589
Duncan Sands83ec4b62008-06-06 12:08:01 +00003590SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003591 switch (NumVTs) {
3592 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003593 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003594 case 2: return getVTList(VTs[0], VTs[1]);
3595 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3596 default: break;
3597 }
3598
Dan Gohmane8be6c62008-07-17 19:10:17 +00003599 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3600 E = VTList.rend(); I != E; ++I) {
3601 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3602 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003603
3604 bool NoMatch = false;
3605 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003606 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003607 NoMatch = true;
3608 break;
3609 }
3610 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003611 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003612 }
3613
Dan Gohmane8be6c62008-07-17 19:10:17 +00003614 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3615 std::copy(VTs, VTs+NumVTs, Array);
3616 SDVTList Result = makeVTList(Array, NumVTs);
3617 VTList.push_back(Result);
3618 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003619}
3620
3621
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003622/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3623/// specified operands. If the resultant node already exists in the DAG,
3624/// this does not modify the specified node, instead it returns the node that
3625/// already exists. If the resultant node does not exist in the DAG, the
3626/// input node is returned. As a degenerate case, if you specify the same
3627/// input operands as the node already has, the input node is returned.
3628SDOperand SelectionDAG::
3629UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3630 SDNode *N = InN.Val;
3631 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3632
3633 // Check to see if there is no change.
3634 if (Op == N->getOperand(0)) return InN;
3635
3636 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003637 void *InsertPos = 0;
3638 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3639 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003640
Dan Gohman79acd2b2008-07-21 22:38:59 +00003641 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003642 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003643 RemoveNodeFromCSEMaps(N);
3644
3645 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003646 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003647 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003648 N->OperandList[0].setUser(N);
3649 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003650
3651 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003652 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003653 return InN;
3654}
3655
3656SDOperand SelectionDAG::
3657UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3658 SDNode *N = InN.Val;
3659 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3660
3661 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003662 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3663 return InN; // No operands changed, just return the input node.
3664
3665 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003666 void *InsertPos = 0;
3667 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3668 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003669
Dan Gohman79acd2b2008-07-21 22:38:59 +00003670 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003671 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003672 RemoveNodeFromCSEMaps(N);
3673
3674 // Now we update the operands.
3675 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003676 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003678 N->OperandList[0].setUser(N);
3679 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003680 }
3681 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003682 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003683 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003684 N->OperandList[1].setUser(N);
3685 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003686 }
3687
3688 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003689 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003690 return InN;
3691}
3692
3693SDOperand SelectionDAG::
3694UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003695 SDOperand Ops[] = { Op1, Op2, Op3 };
3696 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003697}
3698
3699SDOperand SelectionDAG::
3700UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3701 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003702 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3703 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003704}
3705
3706SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003707UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3708 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003709 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3710 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003711}
3712
Chris Lattner809ec112006-01-28 10:09:25 +00003713SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003714UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003715 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003716 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003717 "Update with wrong number of operands");
3718
3719 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003720 bool AnyChange = false;
3721 for (unsigned i = 0; i != NumOps; ++i) {
3722 if (Ops[i] != N->getOperand(i)) {
3723 AnyChange = true;
3724 break;
3725 }
3726 }
3727
3728 // No operands changed, just return the input node.
3729 if (!AnyChange) return InN;
3730
3731 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003732 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003733 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003734 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003735
Dan Gohman7ceda162008-05-02 00:05:03 +00003736 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003737 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003738 RemoveNodeFromCSEMaps(N);
3739
3740 // Now we update the operands.
3741 for (unsigned i = 0; i != NumOps; ++i) {
3742 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003743 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003744 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003745 N->OperandList[i].setUser(N);
3746 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003747 }
3748 }
3749
3750 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003751 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003752 return InN;
3753}
3754
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003755/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003756/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003757void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003758 // Unlike the code in MorphNodeTo that does this, we don't need to
3759 // watch for dead nodes here.
3760 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3761 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3762
3763 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003764}
Chris Lattner149c58c2005-08-16 18:17:10 +00003765
Dan Gohmane8be6c62008-07-17 19:10:17 +00003766/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3767/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003768///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003769SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003770 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003771 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003772 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003773}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003774
Dan Gohmane8be6c62008-07-17 19:10:17 +00003775SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003776 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003777 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003778 SDOperand Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003779 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003780}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003781
Dan Gohmane8be6c62008-07-17 19:10:17 +00003782SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003783 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003784 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003785 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003786 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003787 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003788}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003789
Dan Gohmane8be6c62008-07-17 19:10:17 +00003790SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003791 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003792 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003793 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003794 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003795 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003796}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003797
Dan Gohmane8be6c62008-07-17 19:10:17 +00003798SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003799 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003800 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003801 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003802 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003803}
3804
Dan Gohmane8be6c62008-07-17 19:10:17 +00003805SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003806 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003807 unsigned NumOps) {
3808 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003809 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003810}
3811
Dan Gohmane8be6c62008-07-17 19:10:17 +00003812SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003813 MVT VT1, MVT VT2) {
3814 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003815 return SelectNodeTo(N, MachineOpc, VTs, (SDOperand *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003816}
3817
Dan Gohmane8be6c62008-07-17 19:10:17 +00003818SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003819 MVT VT1, MVT VT2, MVT VT3,
3820 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003821 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003822 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003823}
3824
Dan Gohmane8be6c62008-07-17 19:10:17 +00003825SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003826 MVT VT1, MVT VT2,
3827 SDOperand Op1) {
3828 SDVTList VTs = getVTList(VT1, VT2);
3829 SDOperand Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003830 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003831}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003832
Dan Gohmane8be6c62008-07-17 19:10:17 +00003833SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003834 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003835 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003836 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003837 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003838 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003839}
3840
Dan Gohmane8be6c62008-07-17 19:10:17 +00003841SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003842 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003843 SDOperand Op1, SDOperand Op2,
3844 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003845 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003846 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003847 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003848}
3849
Dan Gohmane8be6c62008-07-17 19:10:17 +00003850SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003851 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003852 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003853 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3854}
3855
3856SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3857 MVT VT) {
3858 SDVTList VTs = getVTList(VT);
3859 return MorphNodeTo(N, Opc, VTs, 0, 0);
3860}
3861
3862SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3863 MVT VT, SDOperand Op1) {
3864 SDVTList VTs = getVTList(VT);
3865 SDOperand Ops[] = { Op1 };
3866 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3867}
3868
3869SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3870 MVT VT, SDOperand Op1,
3871 SDOperand Op2) {
3872 SDVTList VTs = getVTList(VT);
3873 SDOperand Ops[] = { Op1, Op2 };
3874 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3875}
3876
3877SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3878 MVT VT, SDOperand Op1,
3879 SDOperand Op2, SDOperand Op3) {
3880 SDVTList VTs = getVTList(VT);
3881 SDOperand Ops[] = { Op1, Op2, Op3 };
3882 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3883}
3884
3885SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3886 MVT VT, const SDOperand *Ops,
3887 unsigned NumOps) {
3888 SDVTList VTs = getVTList(VT);
3889 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3890}
3891
3892SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3893 MVT VT1, MVT VT2, const SDOperand *Ops,
3894 unsigned NumOps) {
3895 SDVTList VTs = getVTList(VT1, VT2);
3896 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3897}
3898
3899SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3900 MVT VT1, MVT VT2) {
3901 SDVTList VTs = getVTList(VT1, VT2);
3902 return MorphNodeTo(N, Opc, VTs, (SDOperand *)0, 0);
3903}
3904
3905SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3906 MVT VT1, MVT VT2, MVT VT3,
3907 const SDOperand *Ops, unsigned NumOps) {
3908 SDVTList VTs = getVTList(VT1, VT2, VT3);
3909 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3910}
3911
3912SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3913 MVT VT1, MVT VT2,
3914 SDOperand Op1) {
3915 SDVTList VTs = getVTList(VT1, VT2);
3916 SDOperand Ops[] = { Op1 };
3917 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3918}
3919
3920SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3921 MVT VT1, MVT VT2,
3922 SDOperand Op1, SDOperand Op2) {
3923 SDVTList VTs = getVTList(VT1, VT2);
3924 SDOperand Ops[] = { Op1, Op2 };
3925 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3926}
3927
3928SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3929 MVT VT1, MVT VT2,
3930 SDOperand Op1, SDOperand Op2,
3931 SDOperand Op3) {
3932 SDVTList VTs = getVTList(VT1, VT2);
3933 SDOperand Ops[] = { Op1, Op2, Op3 };
3934 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3935}
3936
3937/// MorphNodeTo - These *mutate* the specified node to have the specified
3938/// return type, opcode, and operands.
3939///
3940/// Note that MorphNodeTo returns the resultant node. If there is already a
3941/// node of the specified opcode and operands, it returns that node instead of
3942/// the current one.
3943///
3944/// Using MorphNodeTo is faster than creating a new node and swapping it in
3945/// with ReplaceAllUsesWith both because it often avoids allocating a new
3946/// node, and because it doesn't require CSE recalulation for any of
3947/// the node's users.
3948///
3949SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3950 SDVTList VTs, const SDOperand *Ops,
3951 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003952 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003953 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3955 FoldingSetNodeID ID;
3956 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3957 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3958 return ON;
3959 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003960
Chris Lattner0fb094f2005-11-19 01:44:53 +00003961 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003962
Dan Gohmane8be6c62008-07-17 19:10:17 +00003963 // Start the morphing.
3964 N->NodeType = Opc;
3965 N->ValueList = VTs.VTs;
3966 N->NumValues = VTs.NumVTs;
3967
3968 // Clear the operands list, updating used nodes to remove this from their
3969 // use list. Keep track of any operands that become dead as a result.
3970 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3971 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
3972 I != E; ++I) {
3973 SDNode *Used = I->getVal();
3974 Used->removeUser(std::distance(B, I), N);
3975 if (Used->use_empty())
3976 DeadNodeSet.insert(Used);
3977 }
3978
3979 // If NumOps is larger than the # of operands we currently have, reallocate
3980 // the operand list.
3981 if (NumOps > N->NumOperands) {
3982 if (N->OperandsNeedDelete)
3983 delete[] N->OperandList;
3984 if (N->isMachineOpcode()) {
3985 // We're creating a final node that will live unmorphed for the
3986 // remainder of this SelectionDAG's duration, so we can allocate the
3987 // operands directly out of the pool with no recycling metadata.
3988 N->OperandList = Allocator.Allocate<SDUse>(NumOps);
3989 N->OperandsNeedDelete = false;
3990 } else {
3991 N->OperandList = new SDUse[NumOps];
3992 N->OperandsNeedDelete = true;
3993 }
3994 }
3995
3996 // Assign the new operands.
3997 N->NumOperands = NumOps;
3998 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3999 N->OperandList[i] = Ops[i];
4000 N->OperandList[i].setUser(N);
4001 SDNode *ToUse = N->OperandList[i].getVal();
4002 ToUse->addUser(i, N);
4003 DeadNodeSet.erase(ToUse);
4004 }
4005
4006 // Delete any nodes that are still dead after adding the uses for the
4007 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004008 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004009 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4010 E = DeadNodeSet.end(); I != E; ++I)
4011 if ((*I)->use_empty())
4012 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004013 RemoveDeadNodes(DeadNodes);
4014
Dan Gohmane8be6c62008-07-17 19:10:17 +00004015 if (IP)
4016 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004017 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004018}
4019
Chris Lattner0fb094f2005-11-19 01:44:53 +00004020
Evan Cheng6ae46c42006-02-09 07:15:23 +00004021/// getTargetNode - These are used for target selectors to create a new node
4022/// with specified return type(s), target opcode, and operands.
4023///
4024/// Note that getTargetNode returns the resultant node. If there is already a
4025/// node of the specified opcode and operands, it returns that node instead of
4026/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004027SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004028 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004029}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004030SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004031 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004032}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00004034 SDOperand Op1, SDOperand Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004035 return getNode(~Opcode, VT, Op1, Op2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004036}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004037SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00004038 SDOperand Op1, SDOperand Op2,
4039 SDOperand Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004040 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004041}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004042SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004043 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004045}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4047 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004048 SDOperand Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004049 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004050}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004051SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4052 MVT VT2, SDOperand Op1) {
4053 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004054 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004055}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004056SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4057 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00004058 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004060 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004061 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004062}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4064 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00004065 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004067 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004068 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004069}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004071 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004072 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004073 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004074}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004075SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00004076 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004077 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004078 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004079 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004080}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004082 SDOperand Op1, SDOperand Op2,
4083 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004085 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004086 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004087}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004089 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004090 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004091 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004092}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004093SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4094 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004095 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004096 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004097 VTList.push_back(VT1);
4098 VTList.push_back(VT2);
4099 VTList.push_back(VT3);
4100 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004102 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004103}
Evan Cheng39305cf2007-10-05 01:10:49 +00004104SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004105 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004106 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004107 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004108 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004109 Ops, NumOps).Val;
4110}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004111
Evan Cheng08b11732008-03-22 01:55:50 +00004112/// getNodeIfExists - Get the specified node if it's already available, or
4113/// else return NULL.
4114SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004115 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004116 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4117 FoldingSetNodeID ID;
4118 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4119 void *IP = 0;
4120 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4121 return E;
4122 }
4123 return NULL;
4124}
4125
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004126
Evan Cheng99157a02006-08-07 22:13:29 +00004127/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004128/// This can cause recursive merging of nodes in the DAG.
4129///
Chris Lattner11d049c2008-02-03 03:35:22 +00004130/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004131///
Chris Lattner11d049c2008-02-03 03:35:22 +00004132void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004133 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004134 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004135 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004136 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004137 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004138
Chris Lattner8b8749f2005-08-17 19:00:20 +00004139 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004140 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004141 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004142
Chris Lattner8b8749f2005-08-17 19:00:20 +00004143 // This node is about to morph, remove its old self from the CSE maps.
4144 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004145 int operandNum = 0;
4146 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4147 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004148 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004149 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004150 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004151 I->setUser(U);
4152 To.Val->addUser(operandNum, U);
4153 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004154
4155 // Now that we have modified U, add it back to the CSE maps. If it already
4156 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004157 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004158 ReplaceAllUsesWith(U, Existing, UpdateListener);
4159 // U is now dead. Inform the listener if it exists and delete it.
4160 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004161 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004162 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004163 } else {
4164 // If the node doesn't already exist, we updated it. Inform a listener if
4165 // it exists.
4166 if (UpdateListener)
4167 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004168 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004169 }
4170}
4171
4172/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4173/// This can cause recursive merging of nodes in the DAG.
4174///
4175/// This version assumes From/To have matching types and numbers of result
4176/// values.
4177///
Chris Lattner1e111c72005-09-07 05:37:01 +00004178void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004179 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004180 assert(From->getVTList().VTs == To->getVTList().VTs &&
4181 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004182 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004183
4184 // Handle the trivial case.
4185 if (From == To)
4186 return;
4187
Chris Lattner8b52f212005-08-26 18:36:28 +00004188 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004189 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004190 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004191
Chris Lattner8b52f212005-08-26 18:36:28 +00004192 // This node is about to morph, remove its old self from the CSE maps.
4193 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004194 int operandNum = 0;
4195 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4196 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004197 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004198 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004199 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004200 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004201 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004202
Chris Lattner8b8749f2005-08-17 19:00:20 +00004203 // Now that we have modified U, add it back to the CSE maps. If it already
4204 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004205 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004206 ReplaceAllUsesWith(U, Existing, UpdateListener);
4207 // U is now dead. Inform the listener if it exists and delete it.
4208 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004209 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004210 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004211 } else {
4212 // If the node doesn't already exist, we updated it. Inform a listener if
4213 // it exists.
4214 if (UpdateListener)
4215 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004216 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004217 }
4218}
4219
Chris Lattner8b52f212005-08-26 18:36:28 +00004220/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4221/// This can cause recursive merging of nodes in the DAG.
4222///
4223/// This version can replace From with any result values. To must match the
4224/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004225void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004226 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004227 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004228 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004229 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004230
4231 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004232 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004233 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004234
Chris Lattner7b2880c2005-08-24 22:44:39 +00004235 // This node is about to morph, remove its old self from the CSE maps.
4236 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004237 int operandNum = 0;
4238 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4239 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004240 if (I->getVal() == From) {
4241 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004242 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004243 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004244 I->setUser(U);
4245 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004246 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004247
Chris Lattner7b2880c2005-08-24 22:44:39 +00004248 // Now that we have modified U, add it back to the CSE maps. If it already
4249 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004250 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004251 ReplaceAllUsesWith(U, Existing, UpdateListener);
4252 // U is now dead. Inform the listener if it exists and delete it.
4253 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004254 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004255 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004256 } else {
4257 // If the node doesn't already exist, we updated it. Inform a listener if
4258 // it exists.
4259 if (UpdateListener)
4260 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004261 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004262 }
4263}
4264
Chris Lattner012f2412006-02-17 21:58:01 +00004265/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4266/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004267/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004268void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004269 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004270 // Handle the really simple, really trivial case efficiently.
4271 if (From == To) return;
4272
Chris Lattner012f2412006-02-17 21:58:01 +00004273 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004274 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004275 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004276 return;
4277 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004278
Chris Lattnercf5640b2007-02-04 00:14:31 +00004279 // Get all of the users of From.Val. We want these in a nice,
4280 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004281 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004282
4283 while (!Users.empty()) {
4284 // We know that this user uses some value of From. If it is the right
4285 // value, update it.
4286 SDNode *User = Users.back();
4287 Users.pop_back();
4288
Chris Lattner01d029b2007-10-15 06:10:22 +00004289 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004290 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004291 for (; Op != E; ++Op)
4292 if (*Op == From) break;
4293
4294 // If there are no matches, the user must use some other result of From.
4295 if (Op == E) continue;
4296
4297 // Okay, we know this user needs to be updated. Remove its old self
4298 // from the CSE maps.
4299 RemoveNodeFromCSEMaps(User);
4300
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004301 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004302 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004303 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004304 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004305 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004306 Op->setUser(User);
4307 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004308 }
4309 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004310
4311 // Now that we have modified User, add it back to the CSE maps. If it
4312 // already exists there, recursively merge the results together.
4313 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004314 if (!Existing) {
4315 if (UpdateListener) UpdateListener->NodeUpdated(User);
4316 continue; // Continue on to next user.
4317 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004318
4319 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004320 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004321 // recursive merging of other unrelated nodes down the line.
4322 ReplaceAllUsesWith(User, Existing, UpdateListener);
4323
4324 // User is now dead. Notify a listener if present.
4325 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4326 DeleteNodeNotInCSEMaps(User);
4327 }
4328}
4329
4330/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4331/// uses of other values produced by From.Val alone. The same value may
4332/// appear in both the From and To list. The Deleted vector is
4333/// handled the same way as for ReplaceAllUsesWith.
4334void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDOperand *From,
4335 const SDOperand *To,
4336 unsigned Num,
4337 DAGUpdateListener *UpdateListener){
4338 // Handle the simple, trivial case efficiently.
4339 if (Num == 1)
4340 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4341
4342 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4343 for (unsigned i = 0; i != Num; ++i)
4344 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4345 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004346 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004347
4348 while (!Users.empty()) {
4349 // We know that this user uses some value of From. If it is the right
4350 // value, update it.
4351 SDNode *User = Users.back().first;
4352 unsigned i = Users.back().second;
4353 Users.pop_back();
4354
4355 // Scan for an operand that matches From.
4356 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4357 for (; Op != E; ++Op)
4358 if (*Op == From[i]) break;
4359
4360 // If there are no matches, the user must use some other result of From.
4361 if (Op == E) continue;
4362
4363 // Okay, we know this user needs to be updated. Remove its old self
4364 // from the CSE maps.
4365 RemoveNodeFromCSEMaps(User);
4366
4367 // Update all operands that match "From" in case there are multiple uses.
4368 for (; Op != E; ++Op) {
4369 if (*Op == From[i]) {
4370 From[i].Val->removeUser(Op-User->op_begin(), User);
4371 *Op = To[i];
4372 Op->setUser(User);
4373 To[i].Val->addUser(Op-User->op_begin(), User);
4374 }
4375 }
4376
4377 // Now that we have modified User, add it back to the CSE maps. If it
4378 // already exists there, recursively merge the results together.
4379 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4380 if (!Existing) {
4381 if (UpdateListener) UpdateListener->NodeUpdated(User);
4382 continue; // Continue on to next user.
4383 }
4384
4385 // If there was already an existing matching node, use ReplaceAllUsesWith
4386 // to replace the dead one with the existing one. This can cause
4387 // recursive merging of other unrelated nodes down the line.
4388 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004389
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004390 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004391 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004392 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004393 }
4394}
4395
Evan Chenge6f35d82006-08-01 08:20:41 +00004396/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004397/// based on their topological order. It returns the maximum id and a vector
4398/// of the SDNodes* in assigned order by reference.
4399unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004400 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004401 std::vector<unsigned> InDegree(DAGSize);
4402 std::vector<SDNode*> Sources;
4403
4404 // Use a two pass approach to avoid using a std::map which is slow.
4405 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004406 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4407 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004408 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004409 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004410 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004411 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004412 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004413 }
4414
Evan Cheng99157a02006-08-07 22:13:29 +00004415 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004416 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004417 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004418 SDNode *N = Sources.back();
4419 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004420 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004421 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004422 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004423 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004424 if (Degree == 0)
4425 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004426 }
4427 }
4428
Evan Chengc384d6c2006-08-02 22:00:34 +00004429 // Second pass, assign the actual topological order as node ids.
4430 Id = 0;
4431 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4432 TI != TE; ++TI)
4433 (*TI)->setNodeId(Id++);
4434
4435 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004436}
4437
4438
Evan Cheng091cba12006-07-27 06:39:06 +00004439
Jim Laskey58b968b2005-08-17 20:08:02 +00004440//===----------------------------------------------------------------------===//
4441// SDNode Class
4442//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004443
Chris Lattner917d2c92006-07-19 00:00:37 +00004444// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004445void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004446void UnarySDNode::ANCHOR() {}
4447void BinarySDNode::ANCHOR() {}
4448void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004449void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004450void ConstantSDNode::ANCHOR() {}
4451void ConstantFPSDNode::ANCHOR() {}
4452void GlobalAddressSDNode::ANCHOR() {}
4453void FrameIndexSDNode::ANCHOR() {}
4454void JumpTableSDNode::ANCHOR() {}
4455void ConstantPoolSDNode::ANCHOR() {}
4456void BasicBlockSDNode::ANCHOR() {}
4457void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004458void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004459void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004460void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004461void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004462void ExternalSymbolSDNode::ANCHOR() {}
4463void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004464void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004465void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004466void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004467void LoadSDNode::ANCHOR() {}
4468void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004469void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004470
Chris Lattner48b85922007-02-04 02:41:42 +00004471HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004472 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004473}
4474
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004475GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004476 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004477 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004478 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004479 // Thread Local
4480 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4481 // Non Thread Local
4482 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4483 getSDVTList(VT)), Offset(o) {
4484 TheGlobal = const_cast<GlobalValue*>(GA);
4485}
Chris Lattner48b85922007-02-04 02:41:42 +00004486
Dan Gohman1ea58a52008-07-09 22:08:04 +00004487MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004488 const Value *srcValue, int SVO,
4489 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004490 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohman492f2762008-07-09 21:23:02 +00004491 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4492
4493 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4494 assert(getAlignment() == alignment && "Alignment representation error!");
4495 assert(isVolatile() == vol && "Volatile representation error!");
4496}
4497
Dan Gohman36b5c132008-04-07 19:35:22 +00004498/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004499/// reference performed by this memory reference.
4500MachineMemOperand MemSDNode::getMemOperand() const {
4501 int Flags;
4502 if (isa<LoadSDNode>(this))
4503 Flags = MachineMemOperand::MOLoad;
4504 else if (isa<StoreSDNode>(this))
4505 Flags = MachineMemOperand::MOStore;
4506 else {
4507 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4508 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4509 }
4510
4511 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004512 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4513
Dan Gohman1ea58a52008-07-09 22:08:04 +00004514 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004515 const FrameIndexSDNode *FI =
4516 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4517 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004518 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4519 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004520 else
4521 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4522 Size, getAlignment());
4523}
4524
Jim Laskey583bd472006-10-27 23:46:08 +00004525/// Profile - Gather unique data for the node.
4526///
4527void SDNode::Profile(FoldingSetNodeID &ID) {
4528 AddNodeIDNode(ID, this);
4529}
4530
Chris Lattnera3255112005-11-08 23:30:28 +00004531/// getValueTypeList - Return a pointer to the specified value type.
4532///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004533const MVT *SDNode::getValueTypeList(MVT VT) {
4534 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004535 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004536 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004537 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004538 static MVT VTs[MVT::LAST_VALUETYPE];
4539 VTs[VT.getSimpleVT()] = VT;
4540 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004541 }
Chris Lattnera3255112005-11-08 23:30:28 +00004542}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004543
Chris Lattner5c884562005-01-12 18:37:47 +00004544/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4545/// indicated value. This method ignores uses of other values defined by this
4546/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004547bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004548 assert(Value < getNumValues() && "Bad value!");
4549
Roman Levensteindc1adac2008-04-07 10:06:32 +00004550 // TODO: Only iterate over uses of a given value of the node
4551 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004552 if (UI.getUse().getSDOperand().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004553 if (NUses == 0)
4554 return false;
4555 --NUses;
4556 }
Chris Lattner5c884562005-01-12 18:37:47 +00004557 }
4558
4559 // Found exactly the right number of uses?
4560 return NUses == 0;
4561}
4562
4563
Evan Cheng33d55952007-08-02 05:29:38 +00004564/// hasAnyUseOfValue - Return true if there are any use of the indicated
4565/// value. This method ignores uses of other values defined by this operation.
4566bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4567 assert(Value < getNumValues() && "Bad value!");
4568
Dan Gohman1373c1c2008-07-09 22:39:01 +00004569 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004570 if (UI.getUse().getSDOperand().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004571 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004572
4573 return false;
4574}
4575
4576
Dan Gohman2a629952008-07-27 18:06:42 +00004577/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004578///
Dan Gohman2a629952008-07-27 18:06:42 +00004579bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004580 bool Seen = false;
4581 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004582 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004583 if (User == this)
4584 Seen = true;
4585 else
4586 return false;
4587 }
4588
4589 return Seen;
4590}
4591
Evan Chenge6e97e62006-11-03 07:31:32 +00004592/// isOperand - Return true if this node is an operand of N.
4593///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004594bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004595 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4596 if (*this == N->getOperand(i))
4597 return true;
4598 return false;
4599}
4600
Evan Cheng917be682008-03-04 00:41:45 +00004601bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004602 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004603 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004604 return true;
4605 return false;
4606}
Evan Cheng4ee62112006-02-05 06:29:23 +00004607
Chris Lattner572dee72008-01-16 05:49:24 +00004608/// reachesChainWithoutSideEffects - Return true if this operand (which must
4609/// be a chain) reaches the specified operand without crossing any
4610/// side-effecting instructions. In practice, this looks through token
4611/// factors and non-volatile loads. In order to remain efficient, this only
4612/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004613bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004614 unsigned Depth) const {
4615 if (*this == Dest) return true;
4616
4617 // Don't search too deeply, we just want to be able to see through
4618 // TokenFactor's etc.
4619 if (Depth == 0) return false;
4620
4621 // If this is a token factor, all inputs to the TF happen in parallel. If any
4622 // of the operands of the TF reach dest, then we can do the xform.
4623 if (getOpcode() == ISD::TokenFactor) {
4624 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4625 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4626 return true;
4627 return false;
4628 }
4629
4630 // Loads don't have side effects, look through them.
4631 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4632 if (!Ld->isVolatile())
4633 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4634 }
4635 return false;
4636}
4637
4638
Evan Chengc5fc57d2006-11-03 03:05:24 +00004639static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004640 SmallPtrSet<SDNode *, 32> &Visited) {
4641 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004642 return;
4643
4644 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4645 SDNode *Op = N->getOperand(i).Val;
4646 if (Op == P) {
4647 found = true;
4648 return;
4649 }
4650 findPredecessor(Op, P, found, Visited);
4651 }
4652}
4653
Evan Cheng917be682008-03-04 00:41:45 +00004654/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004655/// is either an operand of N or it can be reached by recursively traversing
4656/// up the operands.
4657/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004658bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004659 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004660 bool found = false;
4661 findPredecessor(N, this, found, Visited);
4662 return found;
4663}
4664
Evan Chengc5484282006-10-04 00:56:09 +00004665uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4666 assert(Num < NumOperands && "Invalid child # of SDNode!");
4667 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4668}
4669
Reid Spencer577cc322007-04-01 07:32:19 +00004670std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004671 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004672 default:
4673 if (getOpcode() < ISD::BUILTIN_OP_END)
4674 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004675 if (isMachineOpcode()) {
4676 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004677 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004678 if (getMachineOpcode() < TII->getNumOpcodes())
4679 return TII->get(getMachineOpcode()).getName();
4680 return "<<Unknown Machine Node>>";
4681 }
4682 if (G) {
4683 TargetLowering &TLI = G->getTargetLoweringInfo();
4684 const char *Name = TLI.getTargetNodeName(getOpcode());
4685 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004686 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004687 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004688 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004689
Dan Gohmane8be6c62008-07-17 19:10:17 +00004690#ifndef NDEBUG
4691 case ISD::DELETED_NODE:
4692 return "<<Deleted Node!>>";
4693#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004694 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004695 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004696 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4697 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4698 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004699 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4700 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4701 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004702 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004703 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4704 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4705 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4706 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4707 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004708 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004709 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004710 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004711 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004712 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004713 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004714 case ISD::AssertSext: return "AssertSext";
4715 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004716
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004717 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004718 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004719 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004720 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004721
4722 case ISD::Constant: return "Constant";
4723 case ISD::ConstantFP: return "ConstantFP";
4724 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004725 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004726 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004727 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004728 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004729 case ISD::RETURNADDR: return "RETURNADDR";
4730 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004731 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004732 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4733 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004734 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004735 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004736 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004737 case ISD::INTRINSIC_WO_CHAIN: {
4738 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4739 return Intrinsic::getName((Intrinsic::ID)IID);
4740 }
4741 case ISD::INTRINSIC_VOID:
4742 case ISD::INTRINSIC_W_CHAIN: {
4743 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004744 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004745 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004746
Chris Lattnerb2827b02006-03-19 00:52:58 +00004747 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004748 case ISD::TargetConstant: return "TargetConstant";
4749 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004750 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004751 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004752 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004753 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004754 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004755 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004756
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004757 case ISD::CopyToReg: return "CopyToReg";
4758 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004759 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004760 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004761 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004762 case ISD::DBG_LABEL: return "dbg_label";
4763 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004764 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004765 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004766 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004767 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004768
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004769 // Unary operators
4770 case ISD::FABS: return "fabs";
4771 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004772 case ISD::FSQRT: return "fsqrt";
4773 case ISD::FSIN: return "fsin";
4774 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004775 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004776 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004777
4778 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004779 case ISD::ADD: return "add";
4780 case ISD::SUB: return "sub";
4781 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004782 case ISD::MULHU: return "mulhu";
4783 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004784 case ISD::SDIV: return "sdiv";
4785 case ISD::UDIV: return "udiv";
4786 case ISD::SREM: return "srem";
4787 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004788 case ISD::SMUL_LOHI: return "smul_lohi";
4789 case ISD::UMUL_LOHI: return "umul_lohi";
4790 case ISD::SDIVREM: return "sdivrem";
4791 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004792 case ISD::AND: return "and";
4793 case ISD::OR: return "or";
4794 case ISD::XOR: return "xor";
4795 case ISD::SHL: return "shl";
4796 case ISD::SRA: return "sra";
4797 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004798 case ISD::ROTL: return "rotl";
4799 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004800 case ISD::FADD: return "fadd";
4801 case ISD::FSUB: return "fsub";
4802 case ISD::FMUL: return "fmul";
4803 case ISD::FDIV: return "fdiv";
4804 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004805 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004806 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004807
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004808 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004809 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004810 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004811 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004812 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004813 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004814 case ISD::CONCAT_VECTORS: return "concat_vectors";
4815 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004816 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004817 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004818 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004819 case ISD::ADDC: return "addc";
4820 case ISD::ADDE: return "adde";
4821 case ISD::SUBC: return "subc";
4822 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004823 case ISD::SHL_PARTS: return "shl_parts";
4824 case ISD::SRA_PARTS: return "sra_parts";
4825 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004826
4827 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4828 case ISD::INSERT_SUBREG: return "insert_subreg";
4829
Chris Lattner7f644642005-04-28 21:44:03 +00004830 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004831 case ISD::SIGN_EXTEND: return "sign_extend";
4832 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004833 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004834 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004835 case ISD::TRUNCATE: return "truncate";
4836 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004837 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004838 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004839 case ISD::FP_EXTEND: return "fp_extend";
4840
4841 case ISD::SINT_TO_FP: return "sint_to_fp";
4842 case ISD::UINT_TO_FP: return "uint_to_fp";
4843 case ISD::FP_TO_SINT: return "fp_to_sint";
4844 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004845 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004846
4847 // Control flow instructions
4848 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004849 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004850 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004851 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004852 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004853 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004854 case ISD::CALLSEQ_START: return "callseq_start";
4855 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004856
4857 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004858 case ISD::LOAD: return "load";
4859 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004860 case ISD::VAARG: return "vaarg";
4861 case ISD::VACOPY: return "vacopy";
4862 case ISD::VAEND: return "vaend";
4863 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004864 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004865 case ISD::EXTRACT_ELEMENT: return "extract_element";
4866 case ISD::BUILD_PAIR: return "build_pair";
4867 case ISD::STACKSAVE: return "stacksave";
4868 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004869 case ISD::TRAP: return "trap";
4870
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004871 // Bit manipulation
4872 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004873 case ISD::CTPOP: return "ctpop";
4874 case ISD::CTTZ: return "cttz";
4875 case ISD::CTLZ: return "ctlz";
4876
Chris Lattner36ce6912005-11-29 06:21:05 +00004877 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004878 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004879 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004880
Duncan Sands36397f52007-07-27 12:58:54 +00004881 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004882 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004883
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004884 case ISD::CONDCODE:
4885 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004886 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004887 case ISD::SETOEQ: return "setoeq";
4888 case ISD::SETOGT: return "setogt";
4889 case ISD::SETOGE: return "setoge";
4890 case ISD::SETOLT: return "setolt";
4891 case ISD::SETOLE: return "setole";
4892 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004893
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004894 case ISD::SETO: return "seto";
4895 case ISD::SETUO: return "setuo";
4896 case ISD::SETUEQ: return "setue";
4897 case ISD::SETUGT: return "setugt";
4898 case ISD::SETUGE: return "setuge";
4899 case ISD::SETULT: return "setult";
4900 case ISD::SETULE: return "setule";
4901 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004902
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004903 case ISD::SETEQ: return "seteq";
4904 case ISD::SETGT: return "setgt";
4905 case ISD::SETGE: return "setge";
4906 case ISD::SETLT: return "setlt";
4907 case ISD::SETLE: return "setle";
4908 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004909 }
4910 }
4911}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004912
Evan Cheng144d8f02006-11-09 17:55:04 +00004913const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004914 switch (AM) {
4915 default:
4916 return "";
4917 case ISD::PRE_INC:
4918 return "<pre-inc>";
4919 case ISD::PRE_DEC:
4920 return "<pre-dec>";
4921 case ISD::POST_INC:
4922 return "<post-inc>";
4923 case ISD::POST_DEC:
4924 return "<post-dec>";
4925 }
4926}
4927
Duncan Sands276dcbd2008-03-21 09:14:45 +00004928std::string ISD::ArgFlagsTy::getArgFlagsString() {
4929 std::string S = "< ";
4930
4931 if (isZExt())
4932 S += "zext ";
4933 if (isSExt())
4934 S += "sext ";
4935 if (isInReg())
4936 S += "inreg ";
4937 if (isSRet())
4938 S += "sret ";
4939 if (isByVal())
4940 S += "byval ";
4941 if (isNest())
4942 S += "nest ";
4943 if (getByValAlign())
4944 S += "byval-align:" + utostr(getByValAlign()) + " ";
4945 if (getOrigAlign())
4946 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4947 if (getByValSize())
4948 S += "byval-size:" + utostr(getByValSize()) + " ";
4949 return S + ">";
4950}
4951
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004952void SDNode::dump() const { dump(0); }
4953void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004954 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004955
4956 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004957 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004958 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004959 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004960 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004962 }
Bill Wendling832171c2006-12-07 20:04:42 +00004963 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004964
Bill Wendling832171c2006-12-07 20:04:42 +00004965 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004966 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004967 if (i) cerr << ", ";
4968 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004969 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004970 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004971 }
4972
Evan Chengce254432007-12-11 02:08:35 +00004973 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4974 SDNode *Mask = getOperand(2).Val;
4975 cerr << "<";
4976 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4977 if (i) cerr << ",";
4978 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4979 cerr << "u";
4980 else
4981 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4982 }
4983 cerr << ">";
4984 }
4985
Chris Lattnerc3aae252005-01-07 07:46:32 +00004986 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Duncan Sandse1d97b12008-07-10 11:23:14 +00004987 cerr << "<" << CSDN->getAPIntValue().toStringUnsigned() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004988 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004989 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4990 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4991 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4992 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4993 else {
4994 cerr << "<APFloat(";
4995 CSDN->getValueAPF().convertToAPInt().dump();
4996 cerr << ")>";
4997 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004998 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004999 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005000 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00005001 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00005002 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00005003 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005004 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005005 else
Bill Wendling832171c2006-12-07 20:04:42 +00005006 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005007 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005008 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005009 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005010 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005011 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005012 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005013 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00005014 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005015 else
Bill Wendling832171c2006-12-07 20:04:42 +00005016 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005017 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005018 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005019 else
Bill Wendling832171c2006-12-07 20:04:42 +00005020 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005021 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005022 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005023 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5024 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00005025 cerr << LBB->getName() << " ";
5026 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005027 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005028 if (G && R->getReg() &&
5029 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00005030 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005031 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00005032 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005033 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005034 } else if (const ExternalSymbolSDNode *ES =
5035 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005036 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005037 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5038 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00005039 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005040 else
Dan Gohman69de1932008-02-06 22:27:42 +00005041 cerr << "<null>";
5042 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5043 if (M->MO.getValue())
5044 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
5045 else
5046 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005047 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
5048 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005049 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005050 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005051 }
5052 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005053 const Value *SrcValue = LD->getSrcValue();
5054 int SrcOffset = LD->getSrcValueOffset();
5055 cerr << " <";
5056 if (SrcValue)
5057 cerr << SrcValue;
5058 else
5059 cerr << "null";
5060 cerr << ":" << SrcOffset << ">";
5061
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005062 bool doExt = true;
5063 switch (LD->getExtensionType()) {
5064 default: doExt = false; break;
5065 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005066 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005067 break;
5068 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005069 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005070 break;
5071 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005072 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005073 break;
5074 }
5075 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00005076 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005077
Evan Cheng144d8f02006-11-09 17:55:04 +00005078 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005079 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00005080 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005081 if (LD->isVolatile())
5082 cerr << " <volatile>";
5083 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005084 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005085 const Value *SrcValue = ST->getSrcValue();
5086 int SrcOffset = ST->getSrcValueOffset();
5087 cerr << " <";
5088 if (SrcValue)
5089 cerr << SrcValue;
5090 else
5091 cerr << "null";
5092 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005093
5094 if (ST->isTruncatingStore())
5095 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00005096 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005097
5098 const char *AM = getIndexedModeName(ST->getAddressingMode());
5099 if (*AM)
5100 cerr << " " << AM;
5101 if (ST->isVolatile())
5102 cerr << " <volatile>";
5103 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005104 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5105 const Value *SrcValue = AT->getSrcValue();
5106 int SrcOffset = AT->getSrcValueOffset();
5107 cerr << " <";
5108 if (SrcValue)
5109 cerr << SrcValue;
5110 else
5111 cerr << "null";
5112 cerr << ":" << SrcOffset << ">";
5113 if (AT->isVolatile())
5114 cerr << " <volatile>";
5115 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005116 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005117}
5118
Chris Lattnerde202b32005-11-09 23:47:37 +00005119static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005120 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5121 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005122 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005123 else
Bill Wendling832171c2006-12-07 20:04:42 +00005124 cerr << "\n" << std::string(indent+2, ' ')
5125 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005126
Chris Lattnerea946cd2005-01-09 20:38:33 +00005127
Bill Wendling832171c2006-12-07 20:04:42 +00005128 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005129 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005130}
5131
Chris Lattnerc3aae252005-01-07 07:46:32 +00005132void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005133 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005134
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005135 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5136 I != E; ++I) {
5137 const SDNode *N = I;
5138 if (!N->hasOneUse() && N != getRoot().Val)
5139 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005140 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005141
Jim Laskey26f7fa72006-10-17 19:33:52 +00005142 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005143
Bill Wendling832171c2006-12-07 20:04:42 +00005144 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005145}
5146
Evan Chengd6594ae2006-09-12 21:00:35 +00005147const Type *ConstantPoolSDNode::getType() const {
5148 if (isMachineConstantPoolEntry())
5149 return Val.MachineCPVal->getType();
5150 return Val.ConstVal->getType();
5151}