blob: e757133f99bf17c04baa4a5992fc42c1ac59c117 [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
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000743/// getMVTAlignment - Compute the default alignment value for the
744/// given type.
745///
746unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
747 const Type *Ty = VT == MVT::iPTR ?
748 PointerType::get(Type::Int8Ty, 0) :
749 VT.getTypeForMVT();
750
751 return TLI.getTargetData()->getABITypeAlignment(Ty);
752}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000753
Chris Lattner78ec3112003-08-11 14:57:33 +0000754SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000755 while (!AllNodes.empty()) {
756 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000757 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000758 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000759 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000760 }
Chris Lattner65113b22005-11-08 22:07:03 +0000761 N->OperandList = 0;
762 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000763 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000764 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000765}
766
Duncan Sands83ec4b62008-06-06 12:08:01 +0000767SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000768 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000769 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000770 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000771 return getNode(ISD::AND, Op.getValueType(), Op,
772 getConstant(Imm, Op.getValueType()));
773}
774
Duncan Sands83ec4b62008-06-06 12:08:01 +0000775SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000776 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000777 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000778}
779
Duncan Sands83ec4b62008-06-06 12:08:01 +0000780SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
781 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000782
Evan Cheng0ff39b32008-06-30 07:31:25 +0000783 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000784 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000785 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000786
787 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000789 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000790 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000791 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000792 SDNode *N = NULL;
793 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000794 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000795 return SDOperand(N, 0);
796 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000797 N = getAllocator().Allocate<ConstantSDNode>();
798 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000799 CSEMap.InsertNode(N, IP);
800 AllNodes.push_back(N);
801 }
802
803 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000804 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000805 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000806 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000807 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
808 }
809 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000810}
811
Chris Lattner0bd48932008-01-17 07:00:52 +0000812SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
813 return getConstant(Val, TLI.getPointerTy(), isTarget);
814}
815
816
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
818 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000819
Duncan Sands83ec4b62008-06-06 12:08:01 +0000820 MVT EltVT =
821 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822
Chris Lattnerd8658612005-02-17 20:17:32 +0000823 // Do the map lookup using the actual bit pattern for the floating point
824 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
825 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000826 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000827 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000828 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000829 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000830 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000831 SDNode *N = NULL;
832 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000833 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000834 return SDOperand(N, 0);
835 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000836 N = getAllocator().Allocate<ConstantFPSDNode>();
837 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000838 CSEMap.InsertNode(N, IP);
839 AllNodes.push_back(N);
840 }
841
Dan Gohman7f321562007-06-25 16:23:39 +0000842 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000844 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000846 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
847 }
848 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849}
850
Duncan Sands83ec4b62008-06-06 12:08:01 +0000851SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
852 MVT EltVT =
853 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000854 if (EltVT==MVT::f32)
855 return getConstantFP(APFloat((float)Val), VT, isTarget);
856 else
857 return getConstantFP(APFloat(Val), VT, isTarget);
858}
859
Chris Lattnerc3aae252005-01-07 07:46:32 +0000860SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000862 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000863 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000864
865 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
866 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000867 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000868 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
869 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
870 }
871
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000872 if (GVar && GVar->isThreadLocal())
873 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
874 else
875 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000876
Jim Laskey583bd472006-10-27 23:46:08 +0000877 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000878 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000879 ID.AddPointer(GV);
880 ID.AddInteger(Offset);
881 void *IP = 0;
882 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
883 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000884 SDNode *N = getAllocator().Allocate<GlobalAddressSDNode>();
885 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000886 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000887 AllNodes.push_back(N);
888 return SDOperand(N, 0);
889}
890
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000892 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000893 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000894 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000895 ID.AddInteger(FI);
896 void *IP = 0;
897 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
898 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000899 SDNode *N = getAllocator().Allocate<FrameIndexSDNode>();
900 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000901 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000902 AllNodes.push_back(N);
903 return SDOperand(N, 0);
904}
905
Duncan Sands83ec4b62008-06-06 12:08:01 +0000906SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000907 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000908 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000909 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000910 ID.AddInteger(JTI);
911 void *IP = 0;
912 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
913 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000914 SDNode *N = getAllocator().Allocate<JumpTableSDNode>();
915 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000916 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000917 AllNodes.push_back(N);
918 return SDOperand(N, 0);
919}
920
Duncan Sands83ec4b62008-06-06 12:08:01 +0000921SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000922 unsigned Alignment, int Offset,
923 bool isTarget) {
924 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000925 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000926 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000927 ID.AddInteger(Alignment);
928 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000929 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000930 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<ConstantPoolSDNode>();
934 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000935 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000942 unsigned Alignment, int Offset,
943 bool isTarget) {
944 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000945 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000946 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000947 ID.AddInteger(Alignment);
948 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000949 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000950 void *IP = 0;
951 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
952 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000953 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
954 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000955 CSEMap.InsertNode(N, IP);
956 AllNodes.push_back(N);
957 return SDOperand(N, 0);
958}
959
960
Chris Lattnerc3aae252005-01-07 07:46:32 +0000961SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000962 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000963 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000964 ID.AddPointer(MBB);
965 void *IP = 0;
966 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
967 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000968 SDNode *N = getAllocator().Allocate<BasicBlockSDNode>();
969 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000970 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 AllNodes.push_back(N);
972 return SDOperand(N, 0);
973}
974
Duncan Sands276dcbd2008-03-21 09:14:45 +0000975SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
976 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000977 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000978 ID.AddInteger(Flags.getRawBits());
979 void *IP = 0;
980 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
981 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000982 SDNode *N = getAllocator().Allocate<ARG_FLAGSSDNode>();
983 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000984 CSEMap.InsertNode(N, IP);
985 AllNodes.push_back(N);
986 return SDOperand(N, 0);
987}
988
Duncan Sands83ec4b62008-06-06 12:08:01 +0000989SDOperand SelectionDAG::getValueType(MVT VT) {
990 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
991 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000992
Duncan Sands83ec4b62008-06-06 12:08:01 +0000993 SDNode *&N = VT.isExtended() ?
994 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000995
996 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000997 N = getAllocator().Allocate<VTSDNode>();
998 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +0000999 AllNodes.push_back(N);
1000 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001001}
1002
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 SDNode *&N = ExternalSymbols[Sym];
1005 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001006 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1007 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001008 AllNodes.push_back(N);
1009 return SDOperand(N, 0);
1010}
1011
Duncan Sands83ec4b62008-06-06 12:08:01 +00001012SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001013 SDNode *&N = TargetExternalSymbols[Sym];
1014 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001015 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1016 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
1019}
1020
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001021SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
1022 if ((unsigned)Cond >= CondCodeNodes.size())
1023 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001024
Chris Lattner079a27a2005-08-09 20:40:02 +00001025 if (CondCodeNodes[Cond] == 0) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001026 CondCodeSDNode *N = getAllocator().Allocate<CondCodeSDNode>();
1027 new (N) CondCodeSDNode(Cond);
1028 CondCodeNodes[Cond] = N;
1029 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001030 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001031 return SDOperand(CondCodeNodes[Cond], 0);
1032}
1033
Duncan Sands83ec4b62008-06-06 12:08:01 +00001034SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001035 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001036 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001037 ID.AddInteger(RegNo);
1038 void *IP = 0;
1039 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1040 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001041 SDNode *N = getAllocator().Allocate<RegisterSDNode>();
1042 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001043 CSEMap.InsertNode(N, IP);
1044 AllNodes.push_back(N);
1045 return SDOperand(N, 0);
1046}
1047
Dan Gohman7f460202008-06-30 20:59:49 +00001048SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1049 unsigned Line, unsigned Col,
1050 const CompileUnitDesc *CU) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001051 SDNode *N = getAllocator().Allocate<DbgStopPointSDNode>();
1052 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001053 AllNodes.push_back(N);
1054 return SDOperand(N, 0);
1055}
1056
Dan Gohman44066042008-07-01 00:05:16 +00001057SDOperand SelectionDAG::getLabel(unsigned Opcode,
1058 SDOperand Root,
1059 unsigned LabelID) {
1060 FoldingSetNodeID ID;
1061 SDOperand Ops[] = { Root };
1062 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1063 ID.AddInteger(LabelID);
1064 void *IP = 0;
1065 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1066 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001067 SDNode *N = getAllocator().Allocate<LabelSDNode>();
1068 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001069 CSEMap.InsertNode(N, IP);
1070 AllNodes.push_back(N);
1071 return SDOperand(N, 0);
1072}
1073
Dan Gohman69de1932008-02-06 22:27:42 +00001074SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001075 assert((!V || isa<PointerType>(V->getType())) &&
1076 "SrcValue is not a pointer?");
1077
Jim Laskey583bd472006-10-27 23:46:08 +00001078 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001079 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001080 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001081
Chris Lattner61b09412006-08-11 21:01:22 +00001082 void *IP = 0;
1083 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1084 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001085
Dan Gohman0e5f1302008-07-07 23:02:41 +00001086 SDNode *N = getAllocator().Allocate<SrcValueSDNode>();
1087 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001088 CSEMap.InsertNode(N, IP);
1089 AllNodes.push_back(N);
1090 return SDOperand(N, 0);
1091}
1092
Dan Gohman36b5c132008-04-07 19:35:22 +00001093SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001094 const Value *v = MO.getValue();
1095 assert((!v || isa<PointerType>(v->getType())) &&
1096 "SrcValue is not a pointer?");
1097
1098 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001099 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001100 ID.AddPointer(v);
1101 ID.AddInteger(MO.getFlags());
1102 ID.AddInteger(MO.getOffset());
1103 ID.AddInteger(MO.getSize());
1104 ID.AddInteger(MO.getAlignment());
1105
1106 void *IP = 0;
1107 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1108 return SDOperand(E, 0);
1109
Dan Gohman0e5f1302008-07-07 23:02:41 +00001110 SDNode *N = getAllocator().Allocate<MemOperandSDNode>();
1111 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001112 CSEMap.InsertNode(N, IP);
1113 AllNodes.push_back(N);
1114 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001115}
1116
Chris Lattner37ce9df2007-10-15 17:47:20 +00001117/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1118/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001119SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001120 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121 unsigned ByteSize = VT.getSizeInBits()/8;
1122 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001123 unsigned StackAlign =
1124 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1125
Chris Lattner37ce9df2007-10-15 17:47:20 +00001126 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1127 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1128}
1129
Duncan Sands83ec4b62008-06-06 12:08:01 +00001130SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001131 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 // These setcc operations always fold.
1133 switch (Cond) {
1134 default: break;
1135 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001136 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001138 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001139
1140 case ISD::SETOEQ:
1141 case ISD::SETOGT:
1142 case ISD::SETOGE:
1143 case ISD::SETOLT:
1144 case ISD::SETOLE:
1145 case ISD::SETONE:
1146 case ISD::SETO:
1147 case ISD::SETUO:
1148 case ISD::SETUEQ:
1149 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001150 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001151 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001153
Chris Lattner67255a12005-04-07 18:14:58 +00001154 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001155 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001156 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001157 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001158
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159 switch (Cond) {
1160 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001161 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1162 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001163 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1164 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1165 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1166 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1167 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1168 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1169 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1170 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
Chris Lattner67255a12005-04-07 18:14:58 +00001173 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001174 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001175 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001176 // No compile time operations on this type yet.
1177 if (N1C->getValueType(0) == MVT::ppcf128)
1178 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001179
1180 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001181 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001182 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001183 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1184 return getNode(ISD::UNDEF, VT);
1185 // fall through
1186 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1187 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1188 return getNode(ISD::UNDEF, VT);
1189 // fall through
1190 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001191 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001192 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1193 return getNode(ISD::UNDEF, VT);
1194 // fall through
1195 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1196 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1197 return getNode(ISD::UNDEF, VT);
1198 // fall through
1199 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1200 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1201 return getNode(ISD::UNDEF, VT);
1202 // fall through
1203 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001204 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001205 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1206 return getNode(ISD::UNDEF, VT);
1207 // fall through
1208 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001209 R==APFloat::cmpEqual, VT);
1210 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1211 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1212 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1213 R==APFloat::cmpEqual, VT);
1214 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1215 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1216 R==APFloat::cmpLessThan, VT);
1217 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1218 R==APFloat::cmpUnordered, VT);
1219 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1220 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001221 }
1222 } else {
1223 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001224 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001226 }
1227
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001228 // Could not fold it.
1229 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230}
1231
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001232/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1233/// use this predicate to simplify operations downstream.
1234bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1235 unsigned BitWidth = Op.getValueSizeInBits();
1236 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1237}
1238
Dan Gohmanea859be2007-06-22 14:59:07 +00001239/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1240/// this predicate to simplify operations downstream. Mask is known to be zero
1241/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001242bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001243 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001244 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001245 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1246 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1247 return (KnownZero & Mask) == Mask;
1248}
1249
1250/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1251/// known to be either zero or one and return them in the KnownZero/KnownOne
1252/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1253/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001254void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001255 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001256 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001257 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001258 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001259 "Mask size mismatches value type size!");
1260
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001261 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001262 if (Depth == 6 || Mask == 0)
1263 return; // Limit search depth.
1264
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001265 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001266
1267 switch (Op.getOpcode()) {
1268 case ISD::Constant:
1269 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001270 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001271 KnownZero = ~KnownOne & Mask;
1272 return;
1273 case ISD::AND:
1274 // If either the LHS or the RHS are Zero, the result is zero.
1275 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001276 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1277 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001278 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1279 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1280
1281 // Output known-1 bits are only known if set in both the LHS & RHS.
1282 KnownOne &= KnownOne2;
1283 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1284 KnownZero |= KnownZero2;
1285 return;
1286 case ISD::OR:
1287 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001288 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1289 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001290 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1291 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1292
1293 // Output known-0 bits are only known if clear in both the LHS & RHS.
1294 KnownZero &= KnownZero2;
1295 // Output known-1 are known to be set if set in either the LHS | RHS.
1296 KnownOne |= KnownOne2;
1297 return;
1298 case ISD::XOR: {
1299 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1300 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1301 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1302 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1303
1304 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001305 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001306 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1307 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1308 KnownZero = KnownZeroOut;
1309 return;
1310 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001311 case ISD::MUL: {
1312 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1313 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1314 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1315 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1316 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1317
1318 // If low bits are zero in either operand, output low known-0 bits.
1319 // Also compute a conserative estimate for high known-0 bits.
1320 // More trickiness is possible, but this is sufficient for the
1321 // interesting case of alignment computation.
1322 KnownOne.clear();
1323 unsigned TrailZ = KnownZero.countTrailingOnes() +
1324 KnownZero2.countTrailingOnes();
1325 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001326 KnownZero2.countLeadingOnes(),
1327 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001328
1329 TrailZ = std::min(TrailZ, BitWidth);
1330 LeadZ = std::min(LeadZ, BitWidth);
1331 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1332 APInt::getHighBitsSet(BitWidth, LeadZ);
1333 KnownZero &= Mask;
1334 return;
1335 }
1336 case ISD::UDIV: {
1337 // For the purposes of computing leading zeros we can conservatively
1338 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001339 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001340 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1341 ComputeMaskedBits(Op.getOperand(0),
1342 AllOnes, KnownZero2, KnownOne2, Depth+1);
1343 unsigned LeadZ = KnownZero2.countLeadingOnes();
1344
1345 KnownOne2.clear();
1346 KnownZero2.clear();
1347 ComputeMaskedBits(Op.getOperand(1),
1348 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001349 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1350 if (RHSUnknownLeadingOnes != BitWidth)
1351 LeadZ = std::min(BitWidth,
1352 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001353
1354 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1355 return;
1356 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 case ISD::SELECT:
1358 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1359 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1360 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1361 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1362
1363 // Only known if known in both the LHS and RHS.
1364 KnownOne &= KnownOne2;
1365 KnownZero &= KnownZero2;
1366 return;
1367 case ISD::SELECT_CC:
1368 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1369 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1370 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1371 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1372
1373 // Only known if known in both the LHS and RHS.
1374 KnownOne &= KnownOne2;
1375 KnownZero &= KnownZero2;
1376 return;
1377 case ISD::SETCC:
1378 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001379 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1380 BitWidth > 1)
1381 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001382 return;
1383 case ISD::SHL:
1384 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1385 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001386 unsigned ShAmt = SA->getValue();
1387
1388 // If the shift count is an invalid immediate, don't do anything.
1389 if (ShAmt >= BitWidth)
1390 return;
1391
1392 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001393 KnownZero, KnownOne, Depth+1);
1394 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001395 KnownZero <<= ShAmt;
1396 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001397 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001398 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001399 }
1400 return;
1401 case ISD::SRL:
1402 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1403 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001404 unsigned ShAmt = SA->getValue();
1405
Dan Gohmand4cf9922008-02-26 18:50:50 +00001406 // If the shift count is an invalid immediate, don't do anything.
1407 if (ShAmt >= BitWidth)
1408 return;
1409
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001410 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001411 KnownZero, KnownOne, Depth+1);
1412 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001413 KnownZero = KnownZero.lshr(ShAmt);
1414 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001415
Dan Gohman72d2fd52008-02-13 22:43:25 +00001416 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001417 KnownZero |= HighBits; // High bits known zero.
1418 }
1419 return;
1420 case ISD::SRA:
1421 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001422 unsigned ShAmt = SA->getValue();
1423
Dan Gohmand4cf9922008-02-26 18:50:50 +00001424 // If the shift count is an invalid immediate, don't do anything.
1425 if (ShAmt >= BitWidth)
1426 return;
1427
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001428 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001429 // If any of the demanded bits are produced by the sign extension, we also
1430 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001431 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1432 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001433 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001434
1435 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1436 Depth+1);
1437 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001438 KnownZero = KnownZero.lshr(ShAmt);
1439 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001440
1441 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001442 APInt SignBit = APInt::getSignBit(BitWidth);
1443 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001444
Dan Gohmanca93a432008-02-20 16:30:17 +00001445 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001446 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001447 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001448 KnownOne |= HighBits; // New bits are known one.
1449 }
1450 }
1451 return;
1452 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001453 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1454 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001455
1456 // Sign extension. Compute the demanded bits in the result that are not
1457 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001458 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001459
Dan Gohman977a76f2008-02-13 22:28:48 +00001460 APInt InSignBit = APInt::getSignBit(EBits);
1461 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001462
1463 // If the sign extended bits are demanded, we know that the sign
1464 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001465 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001466 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 InputDemandedBits |= InSignBit;
1468
1469 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1470 KnownZero, KnownOne, Depth+1);
1471 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1472
1473 // If the sign bit of the input is known set or clear, then we know the
1474 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001475 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001476 KnownZero |= NewBits;
1477 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001478 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001479 KnownOne |= NewBits;
1480 KnownZero &= ~NewBits;
1481 } else { // Input sign bit unknown
1482 KnownZero &= ~NewBits;
1483 KnownOne &= ~NewBits;
1484 }
1485 return;
1486 }
1487 case ISD::CTTZ:
1488 case ISD::CTLZ:
1489 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001490 unsigned LowBits = Log2_32(BitWidth)+1;
1491 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001492 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001493 return;
1494 }
1495 case ISD::LOAD: {
1496 if (ISD::isZEXTLoad(Op.Val)) {
1497 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001498 MVT VT = LD->getMemoryVT();
1499 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001500 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001501 }
1502 return;
1503 }
1504 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001505 MVT InVT = Op.getOperand(0).getValueType();
1506 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001507 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1508 APInt InMask = Mask;
1509 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001510 KnownZero.trunc(InBits);
1511 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001512 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001513 KnownZero.zext(BitWidth);
1514 KnownOne.zext(BitWidth);
1515 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 return;
1517 }
1518 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001519 MVT InVT = Op.getOperand(0).getValueType();
1520 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001521 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001522 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1523 APInt InMask = Mask;
1524 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001525
1526 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001527 // bit is demanded. Temporarily set this bit in the mask for our callee.
1528 if (NewBits.getBoolValue())
1529 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001530
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001531 KnownZero.trunc(InBits);
1532 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001533 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1534
1535 // Note if the sign bit is known to be zero or one.
1536 bool SignBitKnownZero = KnownZero.isNegative();
1537 bool SignBitKnownOne = KnownOne.isNegative();
1538 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1539 "Sign bit can't be known to be both zero and one!");
1540
1541 // If the sign bit wasn't actually demanded by our caller, we don't
1542 // want it set in the KnownZero and KnownOne result values. Reset the
1543 // mask and reapply it to the result values.
1544 InMask = Mask;
1545 InMask.trunc(InBits);
1546 KnownZero &= InMask;
1547 KnownOne &= InMask;
1548
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001549 KnownZero.zext(BitWidth);
1550 KnownOne.zext(BitWidth);
1551
Dan Gohman977a76f2008-02-13 22:28:48 +00001552 // If the sign bit is known zero or one, the top bits match.
1553 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001554 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001555 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001556 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001557 return;
1558 }
1559 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001560 MVT InVT = Op.getOperand(0).getValueType();
1561 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001562 APInt InMask = Mask;
1563 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001564 KnownZero.trunc(InBits);
1565 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001566 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001567 KnownZero.zext(BitWidth);
1568 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001569 return;
1570 }
1571 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 MVT InVT = Op.getOperand(0).getValueType();
1573 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001574 APInt InMask = Mask;
1575 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001576 KnownZero.zext(InBits);
1577 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001578 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001579 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001580 KnownZero.trunc(BitWidth);
1581 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001582 break;
1583 }
1584 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1586 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001587 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1588 KnownOne, Depth+1);
1589 KnownZero |= (~InMask) & Mask;
1590 return;
1591 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001592 case ISD::FGETSIGN:
1593 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001594 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001595 return;
1596
Dan Gohman23e8b712008-04-28 17:02:21 +00001597 case ISD::SUB: {
1598 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1599 // We know that the top bits of C-X are clear if X contains less bits
1600 // than C (i.e. no wrap-around can happen). For example, 20-X is
1601 // positive if we can prove that X is >= 0 and < 16.
1602 if (CLHS->getAPIntValue().isNonNegative()) {
1603 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1604 // NLZ can't be BitWidth with no sign bit
1605 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1606 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1607 Depth+1);
1608
1609 // If all of the MaskV bits are known to be zero, then we know the
1610 // output top bits are zero, because we now know that the output is
1611 // from [0-C].
1612 if ((KnownZero2 & MaskV) == MaskV) {
1613 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1614 // Top bits known zero.
1615 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1616 }
1617 }
1618 }
1619 }
1620 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001621 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 // Output known-0 bits are known if clear or set in both the low clear bits
1623 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1624 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001625 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1626 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1627 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1628 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1629
1630 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1631 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1632 KnownZeroOut = std::min(KnownZeroOut,
1633 KnownZero2.countTrailingOnes());
1634
1635 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001636 return;
1637 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001638 case ISD::SREM:
1639 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001640 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001641 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001642 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001643 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1644 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001645
Dan Gohman23e8b712008-04-28 17:02:21 +00001646 // The sign of a remainder is equal to the sign of the first
1647 // operand (zero being positive).
1648 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1649 KnownZero2 |= ~LowBits;
1650 else if (KnownOne2[BitWidth-1])
1651 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001652
Dan Gohman23e8b712008-04-28 17:02:21 +00001653 KnownZero |= KnownZero2 & Mask;
1654 KnownOne |= KnownOne2 & Mask;
1655
1656 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001657 }
1658 }
1659 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001660 case ISD::UREM: {
1661 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001662 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001663 if (RA.isPowerOf2()) {
1664 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001665 APInt Mask2 = LowBits & Mask;
1666 KnownZero |= ~LowBits & Mask;
1667 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1668 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1669 break;
1670 }
1671 }
1672
1673 // Since the result is less than or equal to either operand, any leading
1674 // zero bits in either operand must also exist in the result.
1675 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1676 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1677 Depth+1);
1678 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1679 Depth+1);
1680
1681 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1682 KnownZero2.countLeadingOnes());
1683 KnownOne.clear();
1684 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1685 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001686 }
1687 default:
1688 // Allow the target to implement this method for its nodes.
1689 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1690 case ISD::INTRINSIC_WO_CHAIN:
1691 case ISD::INTRINSIC_W_CHAIN:
1692 case ISD::INTRINSIC_VOID:
1693 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1694 }
1695 return;
1696 }
1697}
1698
1699/// ComputeNumSignBits - Return the number of times the sign bit of the
1700/// register is replicated into the other bits. We know that at least 1 bit
1701/// is always equal to the sign bit (itself), but other cases can give us
1702/// information. For example, immediately after an "SRA X, 2", we know that
1703/// the top 3 bits are all equal to each other, so we return 3.
1704unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001705 MVT VT = Op.getValueType();
1706 assert(VT.isInteger() && "Invalid VT!");
1707 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001708 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001709 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001710
1711 if (Depth == 6)
1712 return 1; // Limit search depth.
1713
1714 switch (Op.getOpcode()) {
1715 default: break;
1716 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001718 return VTBits-Tmp+1;
1719 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001720 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001721 return VTBits-Tmp;
1722
1723 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001724 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1725 // If negative, return # leading ones.
1726 if (Val.isNegative())
1727 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001728
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001729 // Return # leading zeros.
1730 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 }
1732
1733 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001734 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001735 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1736
1737 case ISD::SIGN_EXTEND_INREG:
1738 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001740 Tmp = VTBits-Tmp+1;
1741
1742 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1743 return std::max(Tmp, Tmp2);
1744
1745 case ISD::SRA:
1746 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1747 // SRA X, C -> adds C sign bits.
1748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1749 Tmp += C->getValue();
1750 if (Tmp > VTBits) Tmp = VTBits;
1751 }
1752 return Tmp;
1753 case ISD::SHL:
1754 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1755 // shl destroys sign bits.
1756 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1757 if (C->getValue() >= VTBits || // Bad shift.
1758 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1759 return Tmp - C->getValue();
1760 }
1761 break;
1762 case ISD::AND:
1763 case ISD::OR:
1764 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001765 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001766 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001767 if (Tmp != 1) {
1768 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1769 FirstAnswer = std::min(Tmp, Tmp2);
1770 // We computed what we know about the sign bits as our first
1771 // answer. Now proceed to the generic code that uses
1772 // ComputeMaskedBits, and pick whichever answer is better.
1773 }
1774 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001775
1776 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001777 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001779 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001780 return std::min(Tmp, Tmp2);
1781
1782 case ISD::SETCC:
1783 // If setcc returns 0/-1, all bits are sign bits.
1784 if (TLI.getSetCCResultContents() ==
1785 TargetLowering::ZeroOrNegativeOneSetCCResult)
1786 return VTBits;
1787 break;
1788 case ISD::ROTL:
1789 case ISD::ROTR:
1790 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1791 unsigned RotAmt = C->getValue() & (VTBits-1);
1792
1793 // Handle rotate right by N like a rotate left by 32-N.
1794 if (Op.getOpcode() == ISD::ROTR)
1795 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1796
1797 // If we aren't rotating out all of the known-in sign bits, return the
1798 // number that are left. This handles rotl(sext(x), 1) for example.
1799 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1800 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1801 }
1802 break;
1803 case ISD::ADD:
1804 // Add can have at most one carry bit. Thus we know that the output
1805 // is, at worst, one more bit than the inputs.
1806 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1807 if (Tmp == 1) return 1; // Early out.
1808
1809 // Special case decrementing a value (ADD X, -1):
1810 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1811 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001812 APInt KnownZero, KnownOne;
1813 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001814 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1815
1816 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1817 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001818 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 return VTBits;
1820
1821 // If we are subtracting one from a positive number, there is no carry
1822 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001823 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001824 return Tmp;
1825 }
1826
1827 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1828 if (Tmp2 == 1) return 1;
1829 return std::min(Tmp, Tmp2)-1;
1830 break;
1831
1832 case ISD::SUB:
1833 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1834 if (Tmp2 == 1) return 1;
1835
1836 // Handle NEG.
1837 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001838 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001839 APInt KnownZero, KnownOne;
1840 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1842 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1843 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001844 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001845 return VTBits;
1846
1847 // If the input is known to be positive (the sign bit is known clear),
1848 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001849 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001850 return Tmp2;
1851
1852 // Otherwise, we treat this like a SUB.
1853 }
1854
1855 // Sub can have at most one carry bit. Thus we know that the output
1856 // is, at worst, one more bit than the inputs.
1857 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1858 if (Tmp == 1) return 1; // Early out.
1859 return std::min(Tmp, Tmp2)-1;
1860 break;
1861 case ISD::TRUNCATE:
1862 // FIXME: it's tricky to do anything useful for this, but it is an important
1863 // case for targets like X86.
1864 break;
1865 }
1866
1867 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1868 if (Op.getOpcode() == ISD::LOAD) {
1869 LoadSDNode *LD = cast<LoadSDNode>(Op);
1870 unsigned ExtType = LD->getExtensionType();
1871 switch (ExtType) {
1872 default: break;
1873 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001874 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001875 return VTBits-Tmp+1;
1876 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001877 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001878 return VTBits-Tmp;
1879 }
1880 }
1881
1882 // Allow the target to implement this method for its nodes.
1883 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1884 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1885 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1886 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1887 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001888 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 }
1890
1891 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1892 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001893 APInt KnownZero, KnownOne;
1894 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001895 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1896
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001897 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001898 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001899 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001900 Mask = KnownOne;
1901 } else {
1902 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001903 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001904 }
1905
1906 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1907 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001908 Mask = ~Mask;
1909 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 // Return # leading zeros. We use 'min' here in case Val was zero before
1911 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001912 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001913}
1914
Chris Lattner51dabfb2006-10-14 00:41:01 +00001915
Evan Chenga844bde2008-02-02 04:07:54 +00001916bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1917 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1918 if (!GA) return false;
1919 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1920 if (!GV) return false;
1921 MachineModuleInfo *MMI = getMachineModuleInfo();
1922 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1923}
1924
1925
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001926/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1927/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001928SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001929 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001930 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001931 SDOperand Idx = PermMask.getOperand(i);
1932 if (Idx.getOpcode() == ISD::UNDEF)
1933 return getNode(ISD::UNDEF, VT.getVectorElementType());
1934 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001935 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001936 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1937 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001938
1939 if (V.getOpcode() == ISD::BIT_CONVERT) {
1940 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001941 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001942 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001943 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001944 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001945 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001946 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001947 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001948 return V.getOperand(Index);
1949 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1950 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001951 return SDOperand();
1952}
1953
1954
Chris Lattnerc3aae252005-01-07 07:46:32 +00001955/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001956///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001957SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001958 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001959 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001960 void *IP = 0;
1961 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1962 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001963 SDNode *N = getAllocator().Allocate<SDNode>();
1964 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001965 CSEMap.InsertNode(N, IP);
1966
1967 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001968 return SDOperand(N, 0);
1969}
1970
Duncan Sands83ec4b62008-06-06 12:08:01 +00001971SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001972 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001973 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001974 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001975 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001976 switch (Opcode) {
1977 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001978 case ISD::SIGN_EXTEND:
1979 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001980 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001981 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001982 case ISD::TRUNCATE:
1983 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001984 case ISD::UINT_TO_FP:
1985 case ISD::SINT_TO_FP: {
1986 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001987 // No compile time operations on this type.
1988 if (VT==MVT::ppcf128)
1989 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001990 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1991 (void)apf.convertFromAPInt(Val,
1992 Opcode==ISD::SINT_TO_FP,
1993 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001994 return getConstantFP(apf, VT);
1995 }
Chris Lattner94683772005-12-23 05:30:37 +00001996 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001997 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001998 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001999 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002000 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002001 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002002 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002003 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002004 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002005 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002006 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002007 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002008 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002009 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002010 }
2011 }
2012
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002013 // Constant fold unary operations with a floating point constant operand.
2014 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2015 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002016 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002017 switch (Opcode) {
2018 case ISD::FNEG:
2019 V.changeSign();
2020 return getConstantFP(V, VT);
2021 case ISD::FABS:
2022 V.clearSign();
2023 return getConstantFP(V, VT);
2024 case ISD::FP_ROUND:
2025 case ISD::FP_EXTEND:
2026 // This can return overflow, underflow, or inexact; we don't care.
2027 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002028 (void)V.convert(*MVTToAPFloatSemantics(VT),
2029 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002030 return getConstantFP(V, VT);
2031 case ISD::FP_TO_SINT:
2032 case ISD::FP_TO_UINT: {
2033 integerPart x;
2034 assert(integerPartWidth >= 64);
2035 // FIXME need to be more flexible about rounding mode.
2036 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2037 Opcode==ISD::FP_TO_SINT,
2038 APFloat::rmTowardZero);
2039 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2040 break;
2041 return getConstant(x, VT);
2042 }
2043 case ISD::BIT_CONVERT:
2044 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2045 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2046 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2047 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002048 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002050 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002051 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002052
2053 unsigned OpOpcode = Operand.Val->getOpcode();
2054 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002055 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002056 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002057 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002058 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002059 assert(VT.isFloatingPoint() &&
2060 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002061 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002062 if (Operand.getOpcode() == ISD::UNDEF)
2063 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002064 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002065 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002066 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002067 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002068 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002069 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002070 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002071 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2072 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2073 break;
2074 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002076 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002077 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002078 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002079 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002080 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002081 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002082 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002083 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002084 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002085 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002086 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002087 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002088 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002089 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2090 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2091 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2092 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002094 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002095 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002096 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002097 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002098 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099 if (OpOpcode == ISD::TRUNCATE)
2100 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002101 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2102 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002103 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002104 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002105 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002106 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002107 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2108 else
2109 return Operand.Val->getOperand(0);
2110 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002111 break;
Chris Lattner35481892005-12-23 00:16:34 +00002112 case ISD::BIT_CONVERT:
2113 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002114 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002115 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002116 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002117 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2118 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002119 if (OpOpcode == ISD::UNDEF)
2120 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002121 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002122 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002123 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2124 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002125 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002126 if (OpOpcode == ISD::UNDEF)
2127 return getNode(ISD::UNDEF, VT);
2128 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2129 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2130 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2131 Operand.getConstantOperandVal(1) == 0 &&
2132 Operand.getOperand(0).getValueType() == VT)
2133 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002134 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002135 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002136 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2137 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002138 Operand.Val->getOperand(0));
2139 if (OpOpcode == ISD::FNEG) // --X -> X
2140 return Operand.Val->getOperand(0);
2141 break;
2142 case ISD::FABS:
2143 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2144 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2145 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002146 }
2147
Chris Lattner43247a12005-08-25 19:12:10 +00002148 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002149 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002150 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002151 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002152 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002153 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002154 void *IP = 0;
2155 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2156 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002157 N = getAllocator().Allocate<UnarySDNode>();
2158 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002159 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002160 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002161 N = getAllocator().Allocate<UnarySDNode>();
2162 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002163 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002164 AllNodes.push_back(N);
2165 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002166}
2167
Chris Lattner36019aa2005-04-18 03:48:41 +00002168
2169
Duncan Sands83ec4b62008-06-06 12:08:01 +00002170SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002171 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2173 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002174 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002175 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002176 case ISD::TokenFactor:
2177 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2178 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002179 // Fold trivial token factors.
2180 if (N1.getOpcode() == ISD::EntryToken) return N2;
2181 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002182 break;
Chris Lattner76365122005-01-16 02:23:22 +00002183 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002184 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002185 N1.getValueType() == VT && "Binary operator types must match!");
2186 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2187 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002188 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002189 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002190 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2191 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002192 break;
Chris Lattner76365122005-01-16 02:23:22 +00002193 case ISD::OR:
2194 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002195 case ISD::ADD:
2196 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002197 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002198 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002199 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2200 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002201 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002202 return N1;
2203 break;
Chris Lattner76365122005-01-16 02:23:22 +00002204 case ISD::UDIV:
2205 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002206 case ISD::MULHU:
2207 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002208 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002209 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002210 case ISD::MUL:
2211 case ISD::SDIV:
2212 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002213 case ISD::FADD:
2214 case ISD::FSUB:
2215 case ISD::FMUL:
2216 case ISD::FDIV:
2217 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002218 assert(N1.getValueType() == N2.getValueType() &&
2219 N1.getValueType() == VT && "Binary operator types must match!");
2220 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002221 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2222 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 N1.getValueType().isFloatingPoint() &&
2224 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002225 "Invalid FCOPYSIGN!");
2226 break;
Chris Lattner76365122005-01-16 02:23:22 +00002227 case ISD::SHL:
2228 case ISD::SRA:
2229 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002230 case ISD::ROTL:
2231 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002232 assert(VT == N1.getValueType() &&
2233 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002235 "Shifts only work on integers");
2236
2237 // Always fold shifts of i1 values so the code generator doesn't need to
2238 // handle them. Since we know the size of the shift has to be less than the
2239 // size of the value, the shift/rotate count is guaranteed to be zero.
2240 if (VT == MVT::i1)
2241 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002242 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002243 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002245 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002246 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002247 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002248 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002249 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002250 break;
2251 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002252 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002253 assert(VT.isFloatingPoint() &&
2254 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002255 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002256 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002257 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002258 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002259 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002260 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002262 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002264 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002265 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002266 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002267 break;
2268 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002269 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002270 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002271 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002272 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002273 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002274 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002275 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002276
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002277 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002278 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002279 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002280 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002281 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002282 return getConstant(Val, VT);
2283 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002284 break;
2285 }
2286 case ISD::EXTRACT_VECTOR_ELT:
2287 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2288
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002289 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2290 if (N1.getOpcode() == ISD::UNDEF)
2291 return getNode(ISD::UNDEF, VT);
2292
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002293 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2294 // expanding copies of large vectors from registers.
2295 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2296 N1.getNumOperands() > 0) {
2297 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002298 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002299 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2300 N1.getOperand(N2C->getValue() / Factor),
2301 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2302 }
2303
2304 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2305 // expanding large vector constants.
2306 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2307 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002308
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002309 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2310 // operations are lowered to scalars.
2311 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2312 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2313 if (IEC == N2C)
2314 return N1.getOperand(1);
2315 else
2316 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2317 }
2318 break;
2319 case ISD::EXTRACT_ELEMENT:
2320 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002321 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2322 (N1.getValueType().isInteger() == VT.isInteger()) &&
2323 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002324
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002325 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2326 // 64-bit integers into 32-bit parts. Instead of building the extract of
2327 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2328 if (N1.getOpcode() == ISD::BUILD_PAIR)
2329 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002330
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002331 // EXTRACT_ELEMENT of a constant int is also very common.
2332 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002333 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002334 unsigned Shift = ElementSize * N2C->getValue();
2335 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2336 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002337 }
2338 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002339 case ISD::EXTRACT_SUBVECTOR:
2340 if (N1.getValueType() == VT) // Trivial extraction.
2341 return N1;
2342 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002343 }
2344
2345 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002346 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002347 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002348 switch (Opcode) {
2349 case ISD::ADD: return getConstant(C1 + C2, VT);
2350 case ISD::SUB: return getConstant(C1 - C2, VT);
2351 case ISD::MUL: return getConstant(C1 * C2, VT);
2352 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002353 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354 break;
2355 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002356 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002357 break;
2358 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002359 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002360 break;
2361 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002362 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002363 break;
2364 case ISD::AND : return getConstant(C1 & C2, VT);
2365 case ISD::OR : return getConstant(C1 | C2, VT);
2366 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002367 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002368 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2369 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2370 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2371 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 default: break;
2373 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 } else { // Cannonicalize constant to RHS if commutative
2375 if (isCommutativeBinOp(Opcode)) {
2376 std::swap(N1C, N2C);
2377 std::swap(N1, N2);
2378 }
2379 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002380 }
2381
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002382 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002383 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2384 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002385 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002386 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2387 // Cannonicalize constant to RHS if commutative
2388 std::swap(N1CFP, N2CFP);
2389 std::swap(N1, N2);
2390 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002391 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2392 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002393 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002394 case ISD::FADD:
2395 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002396 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002397 return getConstantFP(V1, VT);
2398 break;
2399 case ISD::FSUB:
2400 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2401 if (s!=APFloat::opInvalidOp)
2402 return getConstantFP(V1, VT);
2403 break;
2404 case ISD::FMUL:
2405 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2406 if (s!=APFloat::opInvalidOp)
2407 return getConstantFP(V1, VT);
2408 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002409 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002410 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2411 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2412 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002413 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002414 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002415 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2416 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2417 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002418 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002419 case ISD::FCOPYSIGN:
2420 V1.copySign(V2);
2421 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002422 default: break;
2423 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002424 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002425 }
Chris Lattner62b57722006-04-20 05:39:12 +00002426
2427 // Canonicalize an UNDEF to the RHS, even over a constant.
2428 if (N1.getOpcode() == ISD::UNDEF) {
2429 if (isCommutativeBinOp(Opcode)) {
2430 std::swap(N1, N2);
2431 } else {
2432 switch (Opcode) {
2433 case ISD::FP_ROUND_INREG:
2434 case ISD::SIGN_EXTEND_INREG:
2435 case ISD::SUB:
2436 case ISD::FSUB:
2437 case ISD::FDIV:
2438 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002439 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002440 return N1; // fold op(undef, arg2) -> undef
2441 case ISD::UDIV:
2442 case ISD::SDIV:
2443 case ISD::UREM:
2444 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002445 case ISD::SRL:
2446 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002447 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002448 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2449 // For vectors, we can't easily build an all zero vector, just return
2450 // the LHS.
2451 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002452 }
2453 }
2454 }
2455
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002456 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002457 if (N2.getOpcode() == ISD::UNDEF) {
2458 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002459 case ISD::XOR:
2460 if (N1.getOpcode() == ISD::UNDEF)
2461 // Handle undef ^ undef -> 0 special case. This is a common
2462 // idiom (misuse).
2463 return getConstant(0, VT);
2464 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002465 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002466 case ISD::ADDC:
2467 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002468 case ISD::SUB:
2469 case ISD::FADD:
2470 case ISD::FSUB:
2471 case ISD::FMUL:
2472 case ISD::FDIV:
2473 case ISD::FREM:
2474 case ISD::UDIV:
2475 case ISD::SDIV:
2476 case ISD::UREM:
2477 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002478 return N2; // fold op(arg1, undef) -> undef
2479 case ISD::MUL:
2480 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002481 case ISD::SRL:
2482 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002483 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002484 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2485 // For vectors, we can't easily build an all zero vector, just return
2486 // the LHS.
2487 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002488 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002489 if (!VT.isVector())
2490 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002491 // For vectors, we can't easily build an all one vector, just return
2492 // the LHS.
2493 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002494 case ISD::SRA:
2495 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002496 }
2497 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002498
Chris Lattner27e9b412005-05-11 18:57:39 +00002499 // Memoize this node if possible.
2500 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002501 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002502 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002503 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002504 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002505 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002506 void *IP = 0;
2507 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2508 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002509 N = getAllocator().Allocate<BinarySDNode>();
2510 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002511 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002512 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002513 N = getAllocator().Allocate<BinarySDNode>();
2514 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002515 }
2516
Chris Lattnerc3aae252005-01-07 07:46:32 +00002517 AllNodes.push_back(N);
2518 return SDOperand(N, 0);
2519}
2520
Duncan Sands83ec4b62008-06-06 12:08:01 +00002521SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522 SDOperand N1, SDOperand N2, SDOperand N3) {
2523 // Perform various simplifications.
2524 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2525 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002526 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002527 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002528 // Use FoldSetCC to simplify SETCC's.
2529 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002530 if (Simp.Val) return Simp;
2531 break;
2532 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002533 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002534 if (N1C) {
2535 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002536 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002537 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002538 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002539 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002540
2541 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002542 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002543 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002544 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002545 if (N2C->getValue()) // Unconditional branch
2546 return getNode(ISD::BR, MVT::Other, N1, N3);
2547 else
2548 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002549 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002550 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002551 case ISD::VECTOR_SHUFFLE:
2552 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002553 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002554 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002555 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002556 "Illegal VECTOR_SHUFFLE node!");
2557 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002558 case ISD::BIT_CONVERT:
2559 // Fold bit_convert nodes from a type to themselves.
2560 if (N1.getValueType() == VT)
2561 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002562 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563 }
2564
Chris Lattner43247a12005-08-25 19:12:10 +00002565 // Memoize node if it doesn't produce a flag.
2566 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002567 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002568 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002569 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002570 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002571 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002572 void *IP = 0;
2573 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2574 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002575 N = getAllocator().Allocate<TernarySDNode>();
2576 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002577 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002578 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002579 N = getAllocator().Allocate<TernarySDNode>();
2580 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002581 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002582 AllNodes.push_back(N);
2583 return SDOperand(N, 0);
2584}
2585
Duncan Sands83ec4b62008-06-06 12:08:01 +00002586SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002587 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002588 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002589 SDOperand Ops[] = { N1, N2, N3, N4 };
2590 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002591}
2592
Duncan Sands83ec4b62008-06-06 12:08:01 +00002593SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002594 SDOperand N1, SDOperand N2, SDOperand N3,
2595 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002596 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2597 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002598}
2599
Dan Gohman707e0182008-04-12 04:36:06 +00002600/// getMemsetValue - Vectorized representation of the memset value
2601/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002602static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2603 unsigned NumBits = VT.isVector() ?
2604 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002606 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002607 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002608 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002609 Val = (Val << Shift) | Val;
2610 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002611 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002613 return DAG.getConstant(Val, VT);
2614 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002615 }
Evan Chengf0df0312008-05-15 08:39:06 +00002616
2617 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2618 unsigned Shift = 8;
2619 for (unsigned i = NumBits; i > 8; i >>= 1) {
2620 Value = DAG.getNode(ISD::OR, VT,
2621 DAG.getNode(ISD::SHL, VT, Value,
2622 DAG.getConstant(Shift, MVT::i8)), Value);
2623 Shift <<= 1;
2624 }
2625
2626 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002627}
2628
Dan Gohman707e0182008-04-12 04:36:06 +00002629/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2630/// used when a memcpy is turned into a memset when the source is a constant
2631/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002632static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002633 const TargetLowering &TLI,
2634 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002635 // Handle vector with all elements zero.
2636 if (Str.empty()) {
2637 if (VT.isInteger())
2638 return DAG.getConstant(0, VT);
2639 unsigned NumElts = VT.getVectorNumElements();
2640 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2641 return DAG.getNode(ISD::BIT_CONVERT, VT,
2642 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2643 }
2644
Duncan Sands83ec4b62008-06-06 12:08:01 +00002645 assert(!VT.isVector() && "Can't handle vector type here!");
2646 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002647 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002648 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002649 if (TLI.isLittleEndian())
2650 Offset = Offset + MSB - 1;
2651 for (unsigned i = 0; i != MSB; ++i) {
2652 Val = (Val << 8) | (unsigned char)Str[Offset];
2653 Offset += TLI.isLittleEndian() ? -1 : 1;
2654 }
2655 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002656}
2657
Dan Gohman707e0182008-04-12 04:36:06 +00002658/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002659///
Dan Gohman707e0182008-04-12 04:36:06 +00002660static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2661 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002663 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2664}
2665
Evan Chengf0df0312008-05-15 08:39:06 +00002666/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2667///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002668static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002669 unsigned SrcDelta = 0;
2670 GlobalAddressSDNode *G = NULL;
2671 if (Src.getOpcode() == ISD::GlobalAddress)
2672 G = cast<GlobalAddressSDNode>(Src);
2673 else if (Src.getOpcode() == ISD::ADD &&
2674 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2675 Src.getOperand(1).getOpcode() == ISD::Constant) {
2676 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2677 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2678 }
2679 if (!G)
2680 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002681
Evan Chengf0df0312008-05-15 08:39:06 +00002682 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002683 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2684 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002685
Evan Chengf0df0312008-05-15 08:39:06 +00002686 return false;
2687}
Dan Gohman707e0182008-04-12 04:36:06 +00002688
Evan Chengf0df0312008-05-15 08:39:06 +00002689/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2690/// to replace the memset / memcpy is below the threshold. It also returns the
2691/// types of the sequence of memory ops to perform memset / memcpy.
2692static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002693bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002694 SDOperand Dst, SDOperand Src,
2695 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002696 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002697 SelectionDAG &DAG,
2698 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002699 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002700 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002701 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002703 if (VT != MVT::iAny) {
2704 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002705 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002706 // If source is a string constant, this will require an unaligned load.
2707 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2708 if (Dst.getOpcode() != ISD::FrameIndex) {
2709 // Can't change destination alignment. It requires a unaligned store.
2710 if (AllowUnalign)
2711 VT = MVT::iAny;
2712 } else {
2713 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2714 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2715 if (MFI->isFixedObjectIndex(FI)) {
2716 // Can't change destination alignment. It requires a unaligned store.
2717 if (AllowUnalign)
2718 VT = MVT::iAny;
2719 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002720 // Give the stack frame object a larger alignment if needed.
2721 if (MFI->getObjectAlignment(FI) < NewAlign)
2722 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002723 Align = NewAlign;
2724 }
2725 }
2726 }
2727 }
2728
2729 if (VT == MVT::iAny) {
2730 if (AllowUnalign) {
2731 VT = MVT::i64;
2732 } else {
2733 switch (Align & 7) {
2734 case 0: VT = MVT::i64; break;
2735 case 4: VT = MVT::i32; break;
2736 case 2: VT = MVT::i16; break;
2737 default: VT = MVT::i8; break;
2738 }
2739 }
2740
Duncan Sands83ec4b62008-06-06 12:08:01 +00002741 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002742 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002743 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2744 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002745
Duncan Sands8e4eb092008-06-08 20:54:56 +00002746 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002747 VT = LVT;
2748 }
Dan Gohman707e0182008-04-12 04:36:06 +00002749
2750 unsigned NumMemOps = 0;
2751 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002753 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002754 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002755 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002756 VT = MVT::i64;
2757 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2759 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002760 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002762 VTSize >>= 1;
2763 }
Dan Gohman707e0182008-04-12 04:36:06 +00002764 }
Dan Gohman707e0182008-04-12 04:36:06 +00002765
2766 if (++NumMemOps > Limit)
2767 return false;
2768 MemOps.push_back(VT);
2769 Size -= VTSize;
2770 }
2771
2772 return true;
2773}
2774
2775static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2776 SDOperand Chain, SDOperand Dst,
2777 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002778 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002779 const Value *DstSV, uint64_t DstSVOff,
2780 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002781 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2782
Dan Gohman21323f32008-05-29 19:42:22 +00002783 // Expand memcpy to a series of load and store ops if the size operand falls
2784 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002786 uint64_t Limit = -1;
2787 if (!AlwaysInline)
2788 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002789 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002790 std::string Str;
2791 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002792 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002793 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002794 return SDOperand();
2795
Dan Gohman707e0182008-04-12 04:36:06 +00002796
Evan Cheng0ff39b32008-06-30 07:31:25 +00002797 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002798 SmallVector<SDOperand, 8> OutChains;
2799 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002800 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002801 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002802 MVT VT = MemOps[i];
2803 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002804 SDOperand Value, Store;
2805
Evan Cheng0ff39b32008-06-30 07:31:25 +00002806 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002807 // It's unlikely a store of a vector immediate can be done in a single
2808 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002809 // We also handle store a vector with all zero's.
2810 // FIXME: Handle other cases where store of vector immediate is done in
2811 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002812 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002813 Store = DAG.getStore(Chain, Value,
2814 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002815 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002816 } else {
2817 Value = DAG.getLoad(VT, Chain,
2818 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002819 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002820 Store = DAG.getStore(Chain, Value,
2821 getMemBasePlusOffset(Dst, DstOff, DAG),
2822 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002823 }
2824 OutChains.push_back(Store);
2825 SrcOff += VTSize;
2826 DstOff += VTSize;
2827 }
2828
2829 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2830 &OutChains[0], OutChains.size());
2831}
2832
Dan Gohman21323f32008-05-29 19:42:22 +00002833static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2834 SDOperand Chain, SDOperand Dst,
2835 SDOperand Src, uint64_t Size,
2836 unsigned Align, bool AlwaysInline,
2837 const Value *DstSV, uint64_t DstSVOff,
2838 const Value *SrcSV, uint64_t SrcSVOff){
2839 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2840
2841 // Expand memmove to a series of load and store ops if the size operand falls
2842 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002844 uint64_t Limit = -1;
2845 if (!AlwaysInline)
2846 Limit = TLI.getMaxStoresPerMemmove();
2847 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002848 std::string Str;
2849 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002850 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002851 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002852 return SDOperand();
2853
Dan Gohman21323f32008-05-29 19:42:22 +00002854 uint64_t SrcOff = 0, DstOff = 0;
2855
2856 SmallVector<SDOperand, 8> LoadValues;
2857 SmallVector<SDOperand, 8> LoadChains;
2858 SmallVector<SDOperand, 8> OutChains;
2859 unsigned NumMemOps = MemOps.size();
2860 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002861 MVT VT = MemOps[i];
2862 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002863 SDOperand Value, Store;
2864
2865 Value = DAG.getLoad(VT, Chain,
2866 getMemBasePlusOffset(Src, SrcOff, DAG),
2867 SrcSV, SrcSVOff + SrcOff, false, Align);
2868 LoadValues.push_back(Value);
2869 LoadChains.push_back(Value.getValue(1));
2870 SrcOff += VTSize;
2871 }
2872 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2873 &LoadChains[0], LoadChains.size());
2874 OutChains.clear();
2875 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002876 MVT VT = MemOps[i];
2877 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002878 SDOperand Value, Store;
2879
2880 Store = DAG.getStore(Chain, LoadValues[i],
2881 getMemBasePlusOffset(Dst, DstOff, DAG),
2882 DstSV, DstSVOff + DstOff, false, DstAlign);
2883 OutChains.push_back(Store);
2884 DstOff += VTSize;
2885 }
2886
2887 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2888 &OutChains[0], OutChains.size());
2889}
2890
Dan Gohman707e0182008-04-12 04:36:06 +00002891static SDOperand getMemsetStores(SelectionDAG &DAG,
2892 SDOperand Chain, SDOperand Dst,
2893 SDOperand Src, uint64_t Size,
2894 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002895 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002896 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2897
2898 // Expand memset to a series of load/store ops if the size operand
2899 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002900 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002901 std::string Str;
2902 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002903 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002904 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002905 return SDOperand();
2906
2907 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002908 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002909
2910 unsigned NumMemOps = MemOps.size();
2911 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002912 MVT VT = MemOps[i];
2913 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002914 SDOperand Value = getMemsetValue(Src, VT, DAG);
2915 SDOperand Store = DAG.getStore(Chain, Value,
2916 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002917 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002918 OutChains.push_back(Store);
2919 DstOff += VTSize;
2920 }
2921
2922 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2923 &OutChains[0], OutChains.size());
2924}
2925
2926SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002927 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002928 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002929 const Value *DstSV, uint64_t DstSVOff,
2930 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002931
2932 // Check to see if we should lower the memcpy to loads and stores first.
2933 // For cases within the target-specified limits, this is the best choice.
2934 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2935 if (ConstantSize) {
2936 // Memcpy with size zero? Just return the original chain.
2937 if (ConstantSize->isNullValue())
2938 return Chain;
2939
2940 SDOperand Result =
2941 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002942 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002943 if (Result.Val)
2944 return Result;
2945 }
2946
2947 // Then check to see if we should lower the memcpy with target-specific
2948 // code. If the target chooses to do this, this is the next best.
2949 SDOperand Result =
2950 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2951 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002952 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002953 if (Result.Val)
2954 return Result;
2955
2956 // If we really need inline code and the target declined to provide it,
2957 // use a (potentially long) sequence of loads and stores.
2958 if (AlwaysInline) {
2959 assert(ConstantSize && "AlwaysInline requires a constant size!");
2960 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2961 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002962 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002963 }
2964
2965 // Emit a library call.
2966 TargetLowering::ArgListTy Args;
2967 TargetLowering::ArgListEntry Entry;
2968 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2969 Entry.Node = Dst; Args.push_back(Entry);
2970 Entry.Node = Src; Args.push_back(Entry);
2971 Entry.Node = Size; Args.push_back(Entry);
2972 std::pair<SDOperand,SDOperand> CallResult =
2973 TLI.LowerCallTo(Chain, Type::VoidTy,
2974 false, false, false, CallingConv::C, false,
2975 getExternalSymbol("memcpy", TLI.getPointerTy()),
2976 Args, *this);
2977 return CallResult.second;
2978}
2979
2980SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2981 SDOperand Src, SDOperand Size,
2982 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002983 const Value *DstSV, uint64_t DstSVOff,
2984 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002985
Dan Gohman21323f32008-05-29 19:42:22 +00002986 // Check to see if we should lower the memmove to loads and stores first.
2987 // For cases within the target-specified limits, this is the best choice.
2988 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2989 if (ConstantSize) {
2990 // Memmove with size zero? Just return the original chain.
2991 if (ConstantSize->isNullValue())
2992 return Chain;
2993
2994 SDOperand Result =
2995 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2996 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2997 if (Result.Val)
2998 return Result;
2999 }
Dan Gohman707e0182008-04-12 04:36:06 +00003000
3001 // Then check to see if we should lower the memmove with target-specific
3002 // code. If the target chooses to do this, this is the next best.
3003 SDOperand Result =
3004 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003005 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003006 if (Result.Val)
3007 return Result;
3008
3009 // Emit a library call.
3010 TargetLowering::ArgListTy Args;
3011 TargetLowering::ArgListEntry Entry;
3012 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3013 Entry.Node = Dst; Args.push_back(Entry);
3014 Entry.Node = Src; Args.push_back(Entry);
3015 Entry.Node = Size; Args.push_back(Entry);
3016 std::pair<SDOperand,SDOperand> CallResult =
3017 TLI.LowerCallTo(Chain, Type::VoidTy,
3018 false, false, false, CallingConv::C, false,
3019 getExternalSymbol("memmove", TLI.getPointerTy()),
3020 Args, *this);
3021 return CallResult.second;
3022}
3023
3024SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
3025 SDOperand Src, SDOperand Size,
3026 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003027 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003028
3029 // Check to see if we should lower the memset to stores first.
3030 // For cases within the target-specified limits, this is the best choice.
3031 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3032 if (ConstantSize) {
3033 // Memset with size zero? Just return the original chain.
3034 if (ConstantSize->isNullValue())
3035 return Chain;
3036
3037 SDOperand Result =
3038 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003039 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003040 if (Result.Val)
3041 return Result;
3042 }
3043
3044 // Then check to see if we should lower the memset with target-specific
3045 // code. If the target chooses to do this, this is the next best.
3046 SDOperand Result =
3047 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003048 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003049 if (Result.Val)
3050 return Result;
3051
3052 // Emit a library call.
3053 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3054 TargetLowering::ArgListTy Args;
3055 TargetLowering::ArgListEntry Entry;
3056 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3057 Args.push_back(Entry);
3058 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003059 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003060 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3061 else
3062 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3063 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3064 Args.push_back(Entry);
3065 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3066 Args.push_back(Entry);
3067 std::pair<SDOperand,SDOperand> CallResult =
3068 TLI.LowerCallTo(Chain, Type::VoidTy,
3069 false, false, false, CallingConv::C, false,
3070 getExternalSymbol("memset", TLI.getPointerTy()),
3071 Args, *this);
3072 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003073}
3074
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003075SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003076 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003077 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003078 unsigned Alignment) {
3079 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003080 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003081
3082 MVT VT = Cmp.getValueType();
3083
3084 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3085 Alignment = getMVTAlignment(VT);
3086
3087 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003088 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003089 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003090 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003091 void* IP = 0;
3092 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3093 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003094 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3095 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003096 CSEMap.InsertNode(N, IP);
3097 AllNodes.push_back(N);
3098 return SDOperand(N, 0);
3099}
3100
3101SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003102 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003103 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003104 unsigned Alignment) {
3105 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003106 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3107 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003108 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003109 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3110 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003111 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003112
3113 MVT VT = Val.getValueType();
3114
3115 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3116 Alignment = getMVTAlignment(VT);
3117
3118 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003119 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003120 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003121 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003122 void* IP = 0;
3123 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3124 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003125 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3126 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003127 CSEMap.InsertNode(N, IP);
3128 AllNodes.push_back(N);
3129 return SDOperand(N, 0);
3130}
3131
Duncan Sands4bdcb612008-07-02 17:40:58 +00003132/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3133/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003134SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003135 bool Simplify) {
3136 if (Simplify && NumOps == 1)
3137 return Ops[0];
3138
3139 SmallVector<MVT, 4> VTs;
3140 VTs.reserve(NumOps);
3141 for (unsigned i = 0; i < NumOps; ++i)
3142 VTs.push_back(Ops[i].getValueType());
3143 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3144}
3145
Duncan Sands14ea39c2008-03-27 20:23:40 +00003146SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003147SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003148 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003149 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003150 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003151 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003152 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3153 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003154
Duncan Sands14ea39c2008-03-27 20:23:40 +00003155 if (VT == EVT) {
3156 ExtType = ISD::NON_EXTLOAD;
3157 } else if (ExtType == ISD::NON_EXTLOAD) {
3158 assert(VT == EVT && "Non-extending load from different memory type!");
3159 } else {
3160 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161 if (VT.isVector())
3162 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003163 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003164 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003165 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003166 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003167 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003168 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003169 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003170 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003171
3172 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003173 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003174 "Unindexed load with an offset!");
3175
3176 SDVTList VTs = Indexed ?
3177 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3178 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003179 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003180 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003181 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003182 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003183 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003184 ID.AddInteger(Alignment);
3185 ID.AddInteger(isVolatile);
3186 void *IP = 0;
3187 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3188 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003189 SDNode *N = getAllocator().Allocate<LoadSDNode>();
3190 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3191 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003192 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003193 AllNodes.push_back(N);
3194 return SDOperand(N, 0);
3195}
3196
Duncan Sands83ec4b62008-06-06 12:08:01 +00003197SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003198 SDOperand Chain, SDOperand Ptr,
3199 const Value *SV, int SVOffset,
3200 bool isVolatile, unsigned Alignment) {
3201 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003202 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3203 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003204}
3205
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003207 SDOperand Chain, SDOperand Ptr,
3208 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003209 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003210 bool isVolatile, unsigned Alignment) {
3211 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003212 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3213 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003214}
3215
Evan Cheng144d8f02006-11-09 17:55:04 +00003216SDOperand
3217SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3218 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003219 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003220 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3221 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003222 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3223 LD->getChain(), Base, Offset, LD->getSrcValue(),
3224 LD->getSrcValueOffset(), LD->getMemoryVT(),
3225 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003226}
3227
Jeff Cohend41b30d2006-11-05 19:31:28 +00003228SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003229 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003230 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003232
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003233 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3234 Alignment = getMVTAlignment(VT);
3235
Evan Chengad071e12006-10-05 22:57:11 +00003236 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003237 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003238 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003239 FoldingSetNodeID ID;
3240 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003241 ID.AddInteger(ISD::UNINDEXED);
3242 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003243 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003244 ID.AddInteger(Alignment);
3245 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003246 void *IP = 0;
3247 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3248 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003249 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3250 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3251 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003252 CSEMap.InsertNode(N, IP);
3253 AllNodes.push_back(N);
3254 return SDOperand(N, 0);
3255}
3256
Jeff Cohend41b30d2006-11-05 19:31:28 +00003257SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003258 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003259 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003260 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003261 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003262
3263 if (VT == SVT)
3264 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003265
Duncan Sands8e4eb092008-06-08 20:54:56 +00003266 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003267 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003268 "Can't do FP-INT conversion!");
3269
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003270 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3271 Alignment = getMVTAlignment(VT);
3272
Evan Cheng8b2794a2006-10-13 21:14:26 +00003273 SDVTList VTs = getVTList(MVT::Other);
3274 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003275 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003276 FoldingSetNodeID ID;
3277 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003278 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003279 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003280 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003281 ID.AddInteger(Alignment);
3282 ID.AddInteger(isVolatile);
3283 void *IP = 0;
3284 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3285 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003286 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3287 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3288 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003289 CSEMap.InsertNode(N, IP);
3290 AllNodes.push_back(N);
3291 return SDOperand(N, 0);
3292}
3293
Evan Cheng144d8f02006-11-09 17:55:04 +00003294SDOperand
3295SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3296 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003297 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3298 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3299 "Store is already a indexed store!");
3300 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3301 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3302 FoldingSetNodeID ID;
3303 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3304 ID.AddInteger(AM);
3305 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003306 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003307 ID.AddInteger(ST->getAlignment());
3308 ID.AddInteger(ST->isVolatile());
3309 void *IP = 0;
3310 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3311 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003312 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3313 new (N) StoreSDNode(Ops, VTs, AM,
3314 ST->isTruncatingStore(), ST->getMemoryVT(),
3315 ST->getSrcValue(), ST->getSrcValueOffset(),
3316 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003317 CSEMap.InsertNode(N, IP);
3318 AllNodes.push_back(N);
3319 return SDOperand(N, 0);
3320}
3321
Duncan Sands83ec4b62008-06-06 12:08:01 +00003322SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003323 SDOperand Chain, SDOperand Ptr,
3324 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003325 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003326 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003327}
3328
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003330 const SDUse *Ops, unsigned NumOps) {
3331 switch (NumOps) {
3332 case 0: return getNode(Opcode, VT);
3333 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3334 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3335 Ops[1].getSDOperand());
3336 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3337 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3338 default: break;
3339 }
3340
3341 // Copy from an SDUse array into an SDOperand array for use with
3342 // the regular getNode logic.
3343 SmallVector<SDOperand, 8> NewOps;
3344 NewOps.reserve(NumOps);
3345 for (unsigned i = 0; i != NumOps; ++i)
3346 NewOps.push_back(Ops[i].getSDOperand());
3347 return getNode(Opcode, VT, Ops, NumOps);
3348}
3349
3350SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3351 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003352 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003353 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003354 case 1: return getNode(Opcode, VT, Ops[0]);
3355 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3356 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003357 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003358 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003359
Chris Lattneref847df2005-04-09 03:27:28 +00003360 switch (Opcode) {
3361 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003362 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003363 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003364 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3365 "LHS and RHS of condition must have same type!");
3366 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3367 "True and False arms of SelectCC must have same type!");
3368 assert(Ops[2].getValueType() == VT &&
3369 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003370 break;
3371 }
3372 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003373 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003374 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3375 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003376 break;
3377 }
Chris Lattneref847df2005-04-09 03:27:28 +00003378 }
3379
Chris Lattner385328c2005-05-14 07:42:29 +00003380 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003381 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003382 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003383 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003384 FoldingSetNodeID ID;
3385 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003386 void *IP = 0;
3387 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3388 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003389 N = getAllocator().Allocate<SDNode>();
3390 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003391 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003392 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003393 N = getAllocator().Allocate<SDNode>();
3394 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003395 }
Chris Lattneref847df2005-04-09 03:27:28 +00003396 AllNodes.push_back(N);
3397 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003398}
3399
Chris Lattner89c34632005-05-14 06:20:26 +00003400SDOperand SelectionDAG::getNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00003401 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003402 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003403 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3404 Ops, NumOps);
3405}
3406
3407SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003408 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003409 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003410 if (NumVTs == 1)
3411 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003412 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3413}
3414
3415SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003416 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003417 if (VTList.NumVTs == 1)
3418 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003419
Chris Lattner5f056bf2005-07-10 01:55:33 +00003420 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003421 // FIXME: figure out how to safely handle things like
3422 // int foo(int x) { return 1 << (x & 255); }
3423 // int bar() { return foo(256); }
3424#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003425 case ISD::SRA_PARTS:
3426 case ISD::SRL_PARTS:
3427 case ISD::SHL_PARTS:
3428 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003429 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003430 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3431 else if (N3.getOpcode() == ISD::AND)
3432 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3433 // If the and is only masking out bits that cannot effect the shift,
3434 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003435 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003436 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3437 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3438 }
3439 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003440#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003441 }
Chris Lattner89c34632005-05-14 06:20:26 +00003442
Chris Lattner43247a12005-08-25 19:12:10 +00003443 // Memoize the node unless it returns a flag.
3444 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003445 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003446 FoldingSetNodeID ID;
3447 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003448 void *IP = 0;
3449 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3450 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003451 if (NumOps == 1) {
3452 N = getAllocator().Allocate<UnarySDNode>();
3453 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3454 } else if (NumOps == 2) {
3455 N = getAllocator().Allocate<BinarySDNode>();
3456 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3457 } else if (NumOps == 3) {
3458 N = getAllocator().Allocate<TernarySDNode>();
3459 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3460 } else {
3461 N = getAllocator().Allocate<SDNode>();
3462 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3463 }
Chris Lattnera5682852006-08-07 23:03:03 +00003464 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003465 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003466 if (NumOps == 1) {
3467 N = getAllocator().Allocate<UnarySDNode>();
3468 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3469 } else if (NumOps == 2) {
3470 N = getAllocator().Allocate<BinarySDNode>();
3471 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3472 } else if (NumOps == 3) {
3473 N = getAllocator().Allocate<TernarySDNode>();
3474 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3475 } else {
3476 N = getAllocator().Allocate<SDNode>();
3477 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3478 }
Chris Lattner43247a12005-08-25 19:12:10 +00003479 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003480 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003481 return SDOperand(N, 0);
3482}
3483
Dan Gohman08ce9762007-10-08 15:49:58 +00003484SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003485 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003486}
3487
3488SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3489 SDOperand N1) {
3490 SDOperand Ops[] = { N1 };
3491 return getNode(Opcode, VTList, Ops, 1);
3492}
3493
3494SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3495 SDOperand N1, SDOperand N2) {
3496 SDOperand Ops[] = { N1, N2 };
3497 return getNode(Opcode, VTList, Ops, 2);
3498}
3499
3500SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3501 SDOperand N1, SDOperand N2, SDOperand N3) {
3502 SDOperand Ops[] = { N1, N2, N3 };
3503 return getNode(Opcode, VTList, Ops, 3);
3504}
3505
3506SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3507 SDOperand N1, SDOperand N2, SDOperand N3,
3508 SDOperand N4) {
3509 SDOperand Ops[] = { N1, N2, N3, N4 };
3510 return getNode(Opcode, VTList, Ops, 4);
3511}
3512
3513SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3514 SDOperand N1, SDOperand N2, SDOperand N3,
3515 SDOperand N4, SDOperand N5) {
3516 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3517 return getNode(Opcode, VTList, Ops, 5);
3518}
3519
Duncan Sands83ec4b62008-06-06 12:08:01 +00003520SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003521 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003522}
3523
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003525 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3526 E = VTList.rend(); I != E; ++I)
3527 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3528 return *I;
3529
3530 MVT *Array = Allocator.Allocate<MVT>(2);
3531 Array[0] = VT1;
3532 Array[1] = VT2;
3533 SDVTList Result = makeVTList(Array, 2);
3534 VTList.push_back(Result);
3535 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003536}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003537
3538SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3539 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3540 E = VTList.rend(); I != E; ++I)
3541 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3542 I->VTs[2] == VT3)
3543 return *I;
3544
3545 MVT *Array = Allocator.Allocate<MVT>(3);
3546 Array[0] = VT1;
3547 Array[1] = VT2;
3548 Array[2] = VT3;
3549 SDVTList Result = makeVTList(Array, 3);
3550 VTList.push_back(Result);
3551 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003552}
3553
Duncan Sands83ec4b62008-06-06 12:08:01 +00003554SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003555 switch (NumVTs) {
3556 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003557 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003558 case 2: return getVTList(VTs[0], VTs[1]);
3559 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3560 default: break;
3561 }
3562
Dan Gohmane8be6c62008-07-17 19:10:17 +00003563 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3564 E = VTList.rend(); I != E; ++I) {
3565 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3566 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003567
3568 bool NoMatch = false;
3569 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003570 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003571 NoMatch = true;
3572 break;
3573 }
3574 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003575 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003576 }
3577
Dan Gohmane8be6c62008-07-17 19:10:17 +00003578 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3579 std::copy(VTs, VTs+NumVTs, Array);
3580 SDVTList Result = makeVTList(Array, NumVTs);
3581 VTList.push_back(Result);
3582 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003583}
3584
3585
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003586/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3587/// specified operands. If the resultant node already exists in the DAG,
3588/// this does not modify the specified node, instead it returns the node that
3589/// already exists. If the resultant node does not exist in the DAG, the
3590/// input node is returned. As a degenerate case, if you specify the same
3591/// input operands as the node already has, the input node is returned.
3592SDOperand SelectionDAG::
3593UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3594 SDNode *N = InN.Val;
3595 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3596
3597 // Check to see if there is no change.
3598 if (Op == N->getOperand(0)) return InN;
3599
3600 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003601 void *InsertPos = 0;
3602 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3603 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003604
3605 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003606 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003607 RemoveNodeFromCSEMaps(N);
3608
3609 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003610 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003611 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003612 N->OperandList[0].setUser(N);
3613 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003614
3615 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003616 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003617 return InN;
3618}
3619
3620SDOperand SelectionDAG::
3621UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3622 SDNode *N = InN.Val;
3623 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3624
3625 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003626 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3627 return InN; // No operands changed, just return the input node.
3628
3629 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003630 void *InsertPos = 0;
3631 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3632 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003633
3634 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003635 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003636 RemoveNodeFromCSEMaps(N);
3637
3638 // Now we update the operands.
3639 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003640 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003641 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003642 N->OperandList[0].setUser(N);
3643 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003644 }
3645 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003646 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003647 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003648 N->OperandList[1].setUser(N);
3649 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003650 }
3651
3652 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003653 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003654 return InN;
3655}
3656
3657SDOperand SelectionDAG::
3658UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003659 SDOperand Ops[] = { Op1, Op2, Op3 };
3660 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003661}
3662
3663SDOperand SelectionDAG::
3664UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3665 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003666 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3667 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003668}
3669
3670SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003671UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3672 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003673 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3674 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003675}
3676
Chris Lattner809ec112006-01-28 10:09:25 +00003677SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003678UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003679 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003680 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003681 "Update with wrong number of operands");
3682
3683 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003684 bool AnyChange = false;
3685 for (unsigned i = 0; i != NumOps; ++i) {
3686 if (Ops[i] != N->getOperand(i)) {
3687 AnyChange = true;
3688 break;
3689 }
3690 }
3691
3692 // No operands changed, just return the input node.
3693 if (!AnyChange) return InN;
3694
3695 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003696 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003697 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003698 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003699
Dan Gohman7ceda162008-05-02 00:05:03 +00003700 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003701 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003702 RemoveNodeFromCSEMaps(N);
3703
3704 // Now we update the operands.
3705 for (unsigned i = 0; i != NumOps; ++i) {
3706 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003707 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003708 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003709 N->OperandList[i].setUser(N);
3710 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003711 }
3712 }
3713
3714 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003715 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003716 return InN;
3717}
3718
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003719/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003720/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003721void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003722 // Unlike the code in MorphNodeTo that does this, we don't need to
3723 // watch for dead nodes here.
3724 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3725 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3726
3727 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003728}
Chris Lattner149c58c2005-08-16 18:17:10 +00003729
Dan Gohmane8be6c62008-07-17 19:10:17 +00003730/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3731/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003732///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003733SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003734 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003735 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003736 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003737}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003738
Dan Gohmane8be6c62008-07-17 19:10:17 +00003739SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003741 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003742 SDOperand Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003743 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003744}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003745
Dan Gohmane8be6c62008-07-17 19:10:17 +00003746SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003747 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003748 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003749 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003750 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003751 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003752}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003753
Dan Gohmane8be6c62008-07-17 19:10:17 +00003754SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003756 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003757 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003758 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003759 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003760}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003761
Dan Gohmane8be6c62008-07-17 19:10:17 +00003762SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003763 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003764 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003765 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003766 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003767}
3768
Dan Gohmane8be6c62008-07-17 19:10:17 +00003769SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003770 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003771 unsigned NumOps) {
3772 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003773 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003774}
3775
Dan Gohmane8be6c62008-07-17 19:10:17 +00003776SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003777 MVT VT1, MVT VT2) {
3778 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003779 return SelectNodeTo(N, MachineOpc, VTs, (SDOperand *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003780}
3781
Dan Gohmane8be6c62008-07-17 19:10:17 +00003782SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003783 MVT VT1, MVT VT2, MVT VT3,
3784 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003785 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003786 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003787}
3788
Dan Gohmane8be6c62008-07-17 19:10:17 +00003789SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003790 MVT VT1, MVT VT2,
3791 SDOperand Op1) {
3792 SDVTList VTs = getVTList(VT1, VT2);
3793 SDOperand Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003794 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003795}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003796
Dan Gohmane8be6c62008-07-17 19:10:17 +00003797SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003798 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003799 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003800 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003801 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003802 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003803}
3804
Dan Gohmane8be6c62008-07-17 19:10:17 +00003805SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003806 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003807 SDOperand Op1, SDOperand Op2,
3808 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003809 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003810 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003811 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003812}
3813
Dan Gohmane8be6c62008-07-17 19:10:17 +00003814SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003815 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003816 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003817 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3818}
3819
3820SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3821 MVT VT) {
3822 SDVTList VTs = getVTList(VT);
3823 return MorphNodeTo(N, Opc, VTs, 0, 0);
3824}
3825
3826SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3827 MVT VT, SDOperand Op1) {
3828 SDVTList VTs = getVTList(VT);
3829 SDOperand Ops[] = { Op1 };
3830 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3831}
3832
3833SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3834 MVT VT, SDOperand Op1,
3835 SDOperand Op2) {
3836 SDVTList VTs = getVTList(VT);
3837 SDOperand Ops[] = { Op1, Op2 };
3838 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3839}
3840
3841SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3842 MVT VT, SDOperand Op1,
3843 SDOperand Op2, SDOperand Op3) {
3844 SDVTList VTs = getVTList(VT);
3845 SDOperand Ops[] = { Op1, Op2, Op3 };
3846 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3847}
3848
3849SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3850 MVT VT, const SDOperand *Ops,
3851 unsigned NumOps) {
3852 SDVTList VTs = getVTList(VT);
3853 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3854}
3855
3856SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3857 MVT VT1, MVT VT2, const SDOperand *Ops,
3858 unsigned NumOps) {
3859 SDVTList VTs = getVTList(VT1, VT2);
3860 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3861}
3862
3863SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3864 MVT VT1, MVT VT2) {
3865 SDVTList VTs = getVTList(VT1, VT2);
3866 return MorphNodeTo(N, Opc, VTs, (SDOperand *)0, 0);
3867}
3868
3869SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3870 MVT VT1, MVT VT2, MVT VT3,
3871 const SDOperand *Ops, unsigned NumOps) {
3872 SDVTList VTs = getVTList(VT1, VT2, VT3);
3873 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3874}
3875
3876SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3877 MVT VT1, MVT VT2,
3878 SDOperand Op1) {
3879 SDVTList VTs = getVTList(VT1, VT2);
3880 SDOperand Ops[] = { Op1 };
3881 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3882}
3883
3884SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3885 MVT VT1, MVT VT2,
3886 SDOperand Op1, SDOperand Op2) {
3887 SDVTList VTs = getVTList(VT1, VT2);
3888 SDOperand Ops[] = { Op1, Op2 };
3889 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3890}
3891
3892SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3893 MVT VT1, MVT VT2,
3894 SDOperand Op1, SDOperand Op2,
3895 SDOperand Op3) {
3896 SDVTList VTs = getVTList(VT1, VT2);
3897 SDOperand Ops[] = { Op1, Op2, Op3 };
3898 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3899}
3900
3901/// MorphNodeTo - These *mutate* the specified node to have the specified
3902/// return type, opcode, and operands.
3903///
3904/// Note that MorphNodeTo returns the resultant node. If there is already a
3905/// node of the specified opcode and operands, it returns that node instead of
3906/// the current one.
3907///
3908/// Using MorphNodeTo is faster than creating a new node and swapping it in
3909/// with ReplaceAllUsesWith both because it often avoids allocating a new
3910/// node, and because it doesn't require CSE recalulation for any of
3911/// the node's users.
3912///
3913SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3914 SDVTList VTs, const SDOperand *Ops,
3915 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003916 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003917 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003918 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3919 FoldingSetNodeID ID;
3920 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3921 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3922 return ON;
3923 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003924
Chris Lattner0fb094f2005-11-19 01:44:53 +00003925 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003926
Dan Gohmane8be6c62008-07-17 19:10:17 +00003927 // Start the morphing.
3928 N->NodeType = Opc;
3929 N->ValueList = VTs.VTs;
3930 N->NumValues = VTs.NumVTs;
3931
3932 // Clear the operands list, updating used nodes to remove this from their
3933 // use list. Keep track of any operands that become dead as a result.
3934 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3935 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
3936 I != E; ++I) {
3937 SDNode *Used = I->getVal();
3938 Used->removeUser(std::distance(B, I), N);
3939 if (Used->use_empty())
3940 DeadNodeSet.insert(Used);
3941 }
3942
3943 // If NumOps is larger than the # of operands we currently have, reallocate
3944 // the operand list.
3945 if (NumOps > N->NumOperands) {
3946 if (N->OperandsNeedDelete)
3947 delete[] N->OperandList;
3948 if (N->isMachineOpcode()) {
3949 // We're creating a final node that will live unmorphed for the
3950 // remainder of this SelectionDAG's duration, so we can allocate the
3951 // operands directly out of the pool with no recycling metadata.
3952 N->OperandList = Allocator.Allocate<SDUse>(NumOps);
3953 N->OperandsNeedDelete = false;
3954 } else {
3955 N->OperandList = new SDUse[NumOps];
3956 N->OperandsNeedDelete = true;
3957 }
3958 }
3959
3960 // Assign the new operands.
3961 N->NumOperands = NumOps;
3962 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3963 N->OperandList[i] = Ops[i];
3964 N->OperandList[i].setUser(N);
3965 SDNode *ToUse = N->OperandList[i].getVal();
3966 ToUse->addUser(i, N);
3967 DeadNodeSet.erase(ToUse);
3968 }
3969
3970 // Delete any nodes that are still dead after adding the uses for the
3971 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003972 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003973 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
3974 E = DeadNodeSet.end(); I != E; ++I)
3975 if ((*I)->use_empty())
3976 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003977 RemoveDeadNodes(DeadNodes);
3978
Dan Gohmane8be6c62008-07-17 19:10:17 +00003979 if (IP)
3980 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003981 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003982}
3983
Chris Lattner0fb094f2005-11-19 01:44:53 +00003984
Evan Cheng6ae46c42006-02-09 07:15:23 +00003985/// getTargetNode - These are used for target selectors to create a new node
3986/// with specified return type(s), target opcode, and operands.
3987///
3988/// Note that getTargetNode returns the resultant node. If there is already a
3989/// node of the specified opcode and operands, it returns that node instead of
3990/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003991SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003992 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003993}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003994SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003995 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003996}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003997SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003998 SDOperand Op1, SDOperand Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003999 return getNode(~Opcode, VT, Op1, Op2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004000}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004001SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00004002 SDOperand Op1, SDOperand Op2,
4003 SDOperand Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004004 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004005}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004006SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004007 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004009}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004010SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4011 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004012 SDOperand Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004013 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004014}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004015SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4016 MVT VT2, SDOperand Op1) {
4017 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004018 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004019}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004020SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4021 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00004022 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004023 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004024 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004025 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004026}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004027SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4028 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00004029 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004030 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004031 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004032 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004033}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004035 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004036 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004037 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004038}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004039SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00004040 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004042 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004043 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004044}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004045SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004046 SDOperand Op1, SDOperand Op2,
4047 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004048 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004049 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004050 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004051}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004052SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004053 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004054 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004055 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004056}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004057SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4058 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004059 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004060 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004061 VTList.push_back(VT1);
4062 VTList.push_back(VT2);
4063 VTList.push_back(VT3);
4064 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004065 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004066 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004067}
Evan Cheng39305cf2007-10-05 01:10:49 +00004068SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004069 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004070 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004071 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004072 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004073 Ops, NumOps).Val;
4074}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004075
Evan Cheng08b11732008-03-22 01:55:50 +00004076/// getNodeIfExists - Get the specified node if it's already available, or
4077/// else return NULL.
4078SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004079 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004080 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4081 FoldingSetNodeID ID;
4082 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4083 void *IP = 0;
4084 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4085 return E;
4086 }
4087 return NULL;
4088}
4089
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004090
Evan Cheng99157a02006-08-07 22:13:29 +00004091/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004092/// This can cause recursive merging of nodes in the DAG.
4093///
Chris Lattner11d049c2008-02-03 03:35:22 +00004094/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004095///
Chris Lattner11d049c2008-02-03 03:35:22 +00004096void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004097 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004098 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004099 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004100 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004101 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004102
Chris Lattner8b8749f2005-08-17 19:00:20 +00004103 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004104 SDNode::use_iterator UI = From->use_begin();
4105 SDNode *U = UI->getUser();
4106
Chris Lattner8b8749f2005-08-17 19:00:20 +00004107 // This node is about to morph, remove its old self from the CSE maps.
4108 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004109 int operandNum = 0;
4110 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4111 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004112 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004113 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004114 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004115 I->setUser(U);
4116 To.Val->addUser(operandNum, U);
4117 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004118
4119 // Now that we have modified U, add it back to the CSE maps. If it already
4120 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004121 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004122 ReplaceAllUsesWith(U, Existing, UpdateListener);
4123 // U is now dead. Inform the listener if it exists and delete it.
4124 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004125 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004126 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004127 } else {
4128 // If the node doesn't already exist, we updated it. Inform a listener if
4129 // it exists.
4130 if (UpdateListener)
4131 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004132 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004133 }
4134}
4135
4136/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4137/// This can cause recursive merging of nodes in the DAG.
4138///
4139/// This version assumes From/To have matching types and numbers of result
4140/// values.
4141///
Chris Lattner1e111c72005-09-07 05:37:01 +00004142void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004143 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004144 assert(From->getVTList().VTs == To->getVTList().VTs &&
4145 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004146 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004147
4148 // Handle the trivial case.
4149 if (From == To)
4150 return;
4151
Chris Lattner8b52f212005-08-26 18:36:28 +00004152 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004153 SDNode::use_iterator UI = From->use_begin();
4154 SDNode *U = UI->getUser();
4155
Chris Lattner8b52f212005-08-26 18:36:28 +00004156 // This node is about to morph, remove its old self from the CSE maps.
4157 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004158 int operandNum = 0;
4159 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4160 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004161 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004162 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004163 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004164 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004165 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004166
Chris Lattner8b8749f2005-08-17 19:00:20 +00004167 // Now that we have modified U, add it back to the CSE maps. If it already
4168 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004169 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004170 ReplaceAllUsesWith(U, Existing, UpdateListener);
4171 // U is now dead. Inform the listener if it exists and delete it.
4172 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004173 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004174 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004175 } else {
4176 // If the node doesn't already exist, we updated it. Inform a listener if
4177 // it exists.
4178 if (UpdateListener)
4179 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004180 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004181 }
4182}
4183
Chris Lattner8b52f212005-08-26 18:36:28 +00004184/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4185/// This can cause recursive merging of nodes in the DAG.
4186///
4187/// This version can replace From with any result values. To must match the
4188/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004189void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004190 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004191 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004192 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004193 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004194
4195 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004196 SDNode::use_iterator UI = From->use_begin();
4197 SDNode *U = UI->getUser();
4198
Chris Lattner7b2880c2005-08-24 22:44:39 +00004199 // This node is about to morph, remove its old self from the CSE maps.
4200 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004201 int operandNum = 0;
4202 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4203 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004204 if (I->getVal() == From) {
4205 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004206 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004207 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004208 I->setUser(U);
4209 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004210 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004211
Chris Lattner7b2880c2005-08-24 22:44:39 +00004212 // Now that we have modified U, add it back to the CSE maps. If it already
4213 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004214 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004215 ReplaceAllUsesWith(U, Existing, UpdateListener);
4216 // U is now dead. Inform the listener if it exists and delete it.
4217 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004218 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004219 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004220 } else {
4221 // If the node doesn't already exist, we updated it. Inform a listener if
4222 // it exists.
4223 if (UpdateListener)
4224 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004225 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004226 }
4227}
4228
Chris Lattner012f2412006-02-17 21:58:01 +00004229/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4230/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004231/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004232void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004233 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004234 // Handle the really simple, really trivial case efficiently.
4235 if (From == To) return;
4236
Chris Lattner012f2412006-02-17 21:58:01 +00004237 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004238 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004239 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004240 return;
4241 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004242
4243 if (From.use_empty()) return;
4244
Chris Lattnercf5640b2007-02-04 00:14:31 +00004245 // Get all of the users of From.Val. We want these in a nice,
4246 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004247 SmallSetVector<SDNode*, 16> Users;
4248 for (SDNode::use_iterator UI = From.Val->use_begin(),
4249 E = From.Val->use_end(); UI != E; ++UI) {
4250 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004251 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004252 }
Chris Lattner012f2412006-02-17 21:58:01 +00004253
4254 while (!Users.empty()) {
4255 // We know that this user uses some value of From. If it is the right
4256 // value, update it.
4257 SDNode *User = Users.back();
4258 Users.pop_back();
4259
Chris Lattner01d029b2007-10-15 06:10:22 +00004260 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004261 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004262 for (; Op != E; ++Op)
4263 if (*Op == From) break;
4264
4265 // If there are no matches, the user must use some other result of From.
4266 if (Op == E) continue;
4267
4268 // Okay, we know this user needs to be updated. Remove its old self
4269 // from the CSE maps.
4270 RemoveNodeFromCSEMaps(User);
4271
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004272 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004273 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004274 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004275 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004276 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004277 Op->setUser(User);
4278 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004279 }
4280 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004281
4282 // Now that we have modified User, add it back to the CSE maps. If it
4283 // already exists there, recursively merge the results together.
4284 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004285 if (!Existing) {
4286 if (UpdateListener) UpdateListener->NodeUpdated(User);
4287 continue; // Continue on to next user.
4288 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004289
4290 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004291 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004292 // recursive merging of other unrelated nodes down the line.
4293 ReplaceAllUsesWith(User, Existing, UpdateListener);
4294
4295 // User is now dead. Notify a listener if present.
4296 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4297 DeleteNodeNotInCSEMaps(User);
4298 }
4299}
4300
4301/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4302/// uses of other values produced by From.Val alone. The same value may
4303/// appear in both the From and To list. The Deleted vector is
4304/// handled the same way as for ReplaceAllUsesWith.
4305void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDOperand *From,
4306 const SDOperand *To,
4307 unsigned Num,
4308 DAGUpdateListener *UpdateListener){
4309 // Handle the simple, trivial case efficiently.
4310 if (Num == 1)
4311 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4312
4313 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4314 for (unsigned i = 0; i != Num; ++i)
4315 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4316 E = From[i].Val->use_end(); UI != E; ++UI)
4317 Users.push_back(std::make_pair(UI->getUser(), i));
4318
4319 while (!Users.empty()) {
4320 // We know that this user uses some value of From. If it is the right
4321 // value, update it.
4322 SDNode *User = Users.back().first;
4323 unsigned i = Users.back().second;
4324 Users.pop_back();
4325
4326 // Scan for an operand that matches From.
4327 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4328 for (; Op != E; ++Op)
4329 if (*Op == From[i]) break;
4330
4331 // If there are no matches, the user must use some other result of From.
4332 if (Op == E) continue;
4333
4334 // Okay, we know this user needs to be updated. Remove its old self
4335 // from the CSE maps.
4336 RemoveNodeFromCSEMaps(User);
4337
4338 // Update all operands that match "From" in case there are multiple uses.
4339 for (; Op != E; ++Op) {
4340 if (*Op == From[i]) {
4341 From[i].Val->removeUser(Op-User->op_begin(), User);
4342 *Op = To[i];
4343 Op->setUser(User);
4344 To[i].Val->addUser(Op-User->op_begin(), User);
4345 }
4346 }
4347
4348 // Now that we have modified User, add it back to the CSE maps. If it
4349 // already exists there, recursively merge the results together.
4350 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4351 if (!Existing) {
4352 if (UpdateListener) UpdateListener->NodeUpdated(User);
4353 continue; // Continue on to next user.
4354 }
4355
4356 // If there was already an existing matching node, use ReplaceAllUsesWith
4357 // to replace the dead one with the existing one. This can cause
4358 // recursive merging of other unrelated nodes down the line.
4359 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004360
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004361 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004362 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004363 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004364 }
4365}
4366
Evan Chenge6f35d82006-08-01 08:20:41 +00004367/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004368/// based on their topological order. It returns the maximum id and a vector
4369/// of the SDNodes* in assigned order by reference.
4370unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004371 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004372 std::vector<unsigned> InDegree(DAGSize);
4373 std::vector<SDNode*> Sources;
4374
4375 // Use a two pass approach to avoid using a std::map which is slow.
4376 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004377 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4378 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004379 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004380 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004381 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004382 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004383 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004384 }
4385
Evan Cheng99157a02006-08-07 22:13:29 +00004386 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004387 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004388 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004389 SDNode *N = Sources.back();
4390 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004391 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004392 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004393 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004394 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004395 if (Degree == 0)
4396 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004397 }
4398 }
4399
Evan Chengc384d6c2006-08-02 22:00:34 +00004400 // Second pass, assign the actual topological order as node ids.
4401 Id = 0;
4402 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4403 TI != TE; ++TI)
4404 (*TI)->setNodeId(Id++);
4405
4406 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004407}
4408
4409
Evan Cheng091cba12006-07-27 06:39:06 +00004410
Jim Laskey58b968b2005-08-17 20:08:02 +00004411//===----------------------------------------------------------------------===//
4412// SDNode Class
4413//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004414
Chris Lattner917d2c92006-07-19 00:00:37 +00004415// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004416void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004417void UnarySDNode::ANCHOR() {}
4418void BinarySDNode::ANCHOR() {}
4419void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004420void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004421void ConstantSDNode::ANCHOR() {}
4422void ConstantFPSDNode::ANCHOR() {}
4423void GlobalAddressSDNode::ANCHOR() {}
4424void FrameIndexSDNode::ANCHOR() {}
4425void JumpTableSDNode::ANCHOR() {}
4426void ConstantPoolSDNode::ANCHOR() {}
4427void BasicBlockSDNode::ANCHOR() {}
4428void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004429void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004430void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004431void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004432void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004433void ExternalSymbolSDNode::ANCHOR() {}
4434void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004435void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004436void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004437void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004438void LoadSDNode::ANCHOR() {}
4439void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004440void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004441
Chris Lattner48b85922007-02-04 02:41:42 +00004442HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004443 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004444}
4445
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004446GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004447 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004448 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004449 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004450 // Thread Local
4451 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4452 // Non Thread Local
4453 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4454 getSDVTList(VT)), Offset(o) {
4455 TheGlobal = const_cast<GlobalValue*>(GA);
4456}
Chris Lattner48b85922007-02-04 02:41:42 +00004457
Dan Gohman1ea58a52008-07-09 22:08:04 +00004458MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004459 const Value *srcValue, int SVO,
4460 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004461 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohman492f2762008-07-09 21:23:02 +00004462 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4463
4464 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4465 assert(getAlignment() == alignment && "Alignment representation error!");
4466 assert(isVolatile() == vol && "Volatile representation error!");
4467}
4468
Dan Gohman36b5c132008-04-07 19:35:22 +00004469/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004470/// reference performed by this memory reference.
4471MachineMemOperand MemSDNode::getMemOperand() const {
4472 int Flags;
4473 if (isa<LoadSDNode>(this))
4474 Flags = MachineMemOperand::MOLoad;
4475 else if (isa<StoreSDNode>(this))
4476 Flags = MachineMemOperand::MOStore;
4477 else {
4478 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4479 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4480 }
4481
4482 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004483 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4484
Dan Gohman1ea58a52008-07-09 22:08:04 +00004485 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004486 const FrameIndexSDNode *FI =
4487 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4488 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004489 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4490 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004491 else
4492 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4493 Size, getAlignment());
4494}
4495
Jim Laskey583bd472006-10-27 23:46:08 +00004496/// Profile - Gather unique data for the node.
4497///
4498void SDNode::Profile(FoldingSetNodeID &ID) {
4499 AddNodeIDNode(ID, this);
4500}
4501
Chris Lattnera3255112005-11-08 23:30:28 +00004502/// getValueTypeList - Return a pointer to the specified value type.
4503///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004504const MVT *SDNode::getValueTypeList(MVT VT) {
4505 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004506 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004507 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004508 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004509 static MVT VTs[MVT::LAST_VALUETYPE];
4510 VTs[VT.getSimpleVT()] = VT;
4511 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004512 }
Chris Lattnera3255112005-11-08 23:30:28 +00004513}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004514
Chris Lattner5c884562005-01-12 18:37:47 +00004515/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4516/// indicated value. This method ignores uses of other values defined by this
4517/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004518bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004519 assert(Value < getNumValues() && "Bad value!");
4520
Roman Levensteindc1adac2008-04-07 10:06:32 +00004521 // TODO: Only iterate over uses of a given value of the node
4522 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmanb9c33c32008-07-09 23:03:14 +00004523 if (UI->getSDOperand().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004524 if (NUses == 0)
4525 return false;
4526 --NUses;
4527 }
Chris Lattner5c884562005-01-12 18:37:47 +00004528 }
4529
4530 // Found exactly the right number of uses?
4531 return NUses == 0;
4532}
4533
4534
Evan Cheng33d55952007-08-02 05:29:38 +00004535/// hasAnyUseOfValue - Return true if there are any use of the indicated
4536/// value. This method ignores uses of other values defined by this operation.
4537bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4538 assert(Value < getNumValues() && "Bad value!");
4539
Dan Gohman1373c1c2008-07-09 22:39:01 +00004540 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmanb9c33c32008-07-09 23:03:14 +00004541 if (UI->getSDOperand().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004542 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004543
4544 return false;
4545}
4546
4547
Evan Cheng917be682008-03-04 00:41:45 +00004548/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004549///
Evan Cheng917be682008-03-04 00:41:45 +00004550bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004551 bool Seen = false;
4552 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004553 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004554 if (User == this)
4555 Seen = true;
4556 else
4557 return false;
4558 }
4559
4560 return Seen;
4561}
4562
Evan Chenge6e97e62006-11-03 07:31:32 +00004563/// isOperand - Return true if this node is an operand of N.
4564///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004565bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004566 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4567 if (*this == N->getOperand(i))
4568 return true;
4569 return false;
4570}
4571
Evan Cheng917be682008-03-04 00:41:45 +00004572bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004573 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004574 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004575 return true;
4576 return false;
4577}
Evan Cheng4ee62112006-02-05 06:29:23 +00004578
Chris Lattner572dee72008-01-16 05:49:24 +00004579/// reachesChainWithoutSideEffects - Return true if this operand (which must
4580/// be a chain) reaches the specified operand without crossing any
4581/// side-effecting instructions. In practice, this looks through token
4582/// factors and non-volatile loads. In order to remain efficient, this only
4583/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004584bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004585 unsigned Depth) const {
4586 if (*this == Dest) return true;
4587
4588 // Don't search too deeply, we just want to be able to see through
4589 // TokenFactor's etc.
4590 if (Depth == 0) return false;
4591
4592 // If this is a token factor, all inputs to the TF happen in parallel. If any
4593 // of the operands of the TF reach dest, then we can do the xform.
4594 if (getOpcode() == ISD::TokenFactor) {
4595 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4596 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4597 return true;
4598 return false;
4599 }
4600
4601 // Loads don't have side effects, look through them.
4602 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4603 if (!Ld->isVolatile())
4604 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4605 }
4606 return false;
4607}
4608
4609
Evan Chengc5fc57d2006-11-03 03:05:24 +00004610static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004611 SmallPtrSet<SDNode *, 32> &Visited) {
4612 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004613 return;
4614
4615 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4616 SDNode *Op = N->getOperand(i).Val;
4617 if (Op == P) {
4618 found = true;
4619 return;
4620 }
4621 findPredecessor(Op, P, found, Visited);
4622 }
4623}
4624
Evan Cheng917be682008-03-04 00:41:45 +00004625/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004626/// is either an operand of N or it can be reached by recursively traversing
4627/// up the operands.
4628/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004629bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004630 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004631 bool found = false;
4632 findPredecessor(N, this, found, Visited);
4633 return found;
4634}
4635
Evan Chengc5484282006-10-04 00:56:09 +00004636uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4637 assert(Num < NumOperands && "Invalid child # of SDNode!");
4638 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4639}
4640
Reid Spencer577cc322007-04-01 07:32:19 +00004641std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004642 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004643 default:
4644 if (getOpcode() < ISD::BUILTIN_OP_END)
4645 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004646 if (isMachineOpcode()) {
4647 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004648 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004649 if (getMachineOpcode() < TII->getNumOpcodes())
4650 return TII->get(getMachineOpcode()).getName();
4651 return "<<Unknown Machine Node>>";
4652 }
4653 if (G) {
4654 TargetLowering &TLI = G->getTargetLoweringInfo();
4655 const char *Name = TLI.getTargetNodeName(getOpcode());
4656 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004657 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004658 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004659 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004660
Dan Gohmane8be6c62008-07-17 19:10:17 +00004661#ifndef NDEBUG
4662 case ISD::DELETED_NODE:
4663 return "<<Deleted Node!>>";
4664#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004665 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004666 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004667 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4668 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4669 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004670 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4671 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4672 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004673 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004674 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4675 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4676 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4677 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4678 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004679 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004680 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004681 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004682 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004683 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004684 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004685 case ISD::AssertSext: return "AssertSext";
4686 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004687
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004688 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004689 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004690 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004691 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004692
4693 case ISD::Constant: return "Constant";
4694 case ISD::ConstantFP: return "ConstantFP";
4695 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004696 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004697 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004698 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004699 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004700 case ISD::RETURNADDR: return "RETURNADDR";
4701 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004702 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004703 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4704 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004705 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004706 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004707 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004708 case ISD::INTRINSIC_WO_CHAIN: {
4709 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4710 return Intrinsic::getName((Intrinsic::ID)IID);
4711 }
4712 case ISD::INTRINSIC_VOID:
4713 case ISD::INTRINSIC_W_CHAIN: {
4714 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004715 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004716 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004717
Chris Lattnerb2827b02006-03-19 00:52:58 +00004718 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004719 case ISD::TargetConstant: return "TargetConstant";
4720 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004721 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004722 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004723 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004724 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004725 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004726 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004727
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004728 case ISD::CopyToReg: return "CopyToReg";
4729 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004730 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004731 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004732 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004733 case ISD::DBG_LABEL: return "dbg_label";
4734 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004735 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004736 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004737 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004738 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004739
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004740 // Unary operators
4741 case ISD::FABS: return "fabs";
4742 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004743 case ISD::FSQRT: return "fsqrt";
4744 case ISD::FSIN: return "fsin";
4745 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004746 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004747 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004748
4749 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004750 case ISD::ADD: return "add";
4751 case ISD::SUB: return "sub";
4752 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004753 case ISD::MULHU: return "mulhu";
4754 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004755 case ISD::SDIV: return "sdiv";
4756 case ISD::UDIV: return "udiv";
4757 case ISD::SREM: return "srem";
4758 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004759 case ISD::SMUL_LOHI: return "smul_lohi";
4760 case ISD::UMUL_LOHI: return "umul_lohi";
4761 case ISD::SDIVREM: return "sdivrem";
4762 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004763 case ISD::AND: return "and";
4764 case ISD::OR: return "or";
4765 case ISD::XOR: return "xor";
4766 case ISD::SHL: return "shl";
4767 case ISD::SRA: return "sra";
4768 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004769 case ISD::ROTL: return "rotl";
4770 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004771 case ISD::FADD: return "fadd";
4772 case ISD::FSUB: return "fsub";
4773 case ISD::FMUL: return "fmul";
4774 case ISD::FDIV: return "fdiv";
4775 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004776 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004777 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004778
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004779 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004780 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004781 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004782 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004783 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004784 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004785 case ISD::CONCAT_VECTORS: return "concat_vectors";
4786 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004787 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004788 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004789 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004790 case ISD::ADDC: return "addc";
4791 case ISD::ADDE: return "adde";
4792 case ISD::SUBC: return "subc";
4793 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004794 case ISD::SHL_PARTS: return "shl_parts";
4795 case ISD::SRA_PARTS: return "sra_parts";
4796 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004797
4798 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4799 case ISD::INSERT_SUBREG: return "insert_subreg";
4800
Chris Lattner7f644642005-04-28 21:44:03 +00004801 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004802 case ISD::SIGN_EXTEND: return "sign_extend";
4803 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004804 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004805 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004806 case ISD::TRUNCATE: return "truncate";
4807 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004808 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004809 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004810 case ISD::FP_EXTEND: return "fp_extend";
4811
4812 case ISD::SINT_TO_FP: return "sint_to_fp";
4813 case ISD::UINT_TO_FP: return "uint_to_fp";
4814 case ISD::FP_TO_SINT: return "fp_to_sint";
4815 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004816 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004817
4818 // Control flow instructions
4819 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004820 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004821 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004822 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004823 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004824 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004825 case ISD::CALLSEQ_START: return "callseq_start";
4826 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004827
4828 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004829 case ISD::LOAD: return "load";
4830 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004831 case ISD::VAARG: return "vaarg";
4832 case ISD::VACOPY: return "vacopy";
4833 case ISD::VAEND: return "vaend";
4834 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004835 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004836 case ISD::EXTRACT_ELEMENT: return "extract_element";
4837 case ISD::BUILD_PAIR: return "build_pair";
4838 case ISD::STACKSAVE: return "stacksave";
4839 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004840 case ISD::TRAP: return "trap";
4841
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004842 // Bit manipulation
4843 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004844 case ISD::CTPOP: return "ctpop";
4845 case ISD::CTTZ: return "cttz";
4846 case ISD::CTLZ: return "ctlz";
4847
Chris Lattner36ce6912005-11-29 06:21:05 +00004848 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004849 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004850 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004851
Duncan Sands36397f52007-07-27 12:58:54 +00004852 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004853 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004854
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004855 case ISD::CONDCODE:
4856 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004857 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004858 case ISD::SETOEQ: return "setoeq";
4859 case ISD::SETOGT: return "setogt";
4860 case ISD::SETOGE: return "setoge";
4861 case ISD::SETOLT: return "setolt";
4862 case ISD::SETOLE: return "setole";
4863 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004864
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004865 case ISD::SETO: return "seto";
4866 case ISD::SETUO: return "setuo";
4867 case ISD::SETUEQ: return "setue";
4868 case ISD::SETUGT: return "setugt";
4869 case ISD::SETUGE: return "setuge";
4870 case ISD::SETULT: return "setult";
4871 case ISD::SETULE: return "setule";
4872 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004873
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004874 case ISD::SETEQ: return "seteq";
4875 case ISD::SETGT: return "setgt";
4876 case ISD::SETGE: return "setge";
4877 case ISD::SETLT: return "setlt";
4878 case ISD::SETLE: return "setle";
4879 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004880 }
4881 }
4882}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004883
Evan Cheng144d8f02006-11-09 17:55:04 +00004884const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004885 switch (AM) {
4886 default:
4887 return "";
4888 case ISD::PRE_INC:
4889 return "<pre-inc>";
4890 case ISD::PRE_DEC:
4891 return "<pre-dec>";
4892 case ISD::POST_INC:
4893 return "<post-inc>";
4894 case ISD::POST_DEC:
4895 return "<post-dec>";
4896 }
4897}
4898
Duncan Sands276dcbd2008-03-21 09:14:45 +00004899std::string ISD::ArgFlagsTy::getArgFlagsString() {
4900 std::string S = "< ";
4901
4902 if (isZExt())
4903 S += "zext ";
4904 if (isSExt())
4905 S += "sext ";
4906 if (isInReg())
4907 S += "inreg ";
4908 if (isSRet())
4909 S += "sret ";
4910 if (isByVal())
4911 S += "byval ";
4912 if (isNest())
4913 S += "nest ";
4914 if (getByValAlign())
4915 S += "byval-align:" + utostr(getByValAlign()) + " ";
4916 if (getOrigAlign())
4917 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4918 if (getByValSize())
4919 S += "byval-size:" + utostr(getByValSize()) + " ";
4920 return S + ">";
4921}
4922
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004923void SDNode::dump() const { dump(0); }
4924void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004925 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004926
4927 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004928 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004929 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004930 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004931 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004932 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004933 }
Bill Wendling832171c2006-12-07 20:04:42 +00004934 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004935
Bill Wendling832171c2006-12-07 20:04:42 +00004936 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004937 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004938 if (i) cerr << ", ";
4939 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004940 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004941 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004942 }
4943
Evan Chengce254432007-12-11 02:08:35 +00004944 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4945 SDNode *Mask = getOperand(2).Val;
4946 cerr << "<";
4947 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4948 if (i) cerr << ",";
4949 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4950 cerr << "u";
4951 else
4952 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4953 }
4954 cerr << ">";
4955 }
4956
Chris Lattnerc3aae252005-01-07 07:46:32 +00004957 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Duncan Sandse1d97b12008-07-10 11:23:14 +00004958 cerr << "<" << CSDN->getAPIntValue().toStringUnsigned() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004959 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004960 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4961 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4962 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4963 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4964 else {
4965 cerr << "<APFloat(";
4966 CSDN->getValueAPF().convertToAPInt().dump();
4967 cerr << ")>";
4968 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004969 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004970 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004971 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004972 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004973 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004974 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004975 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004976 else
Bill Wendling832171c2006-12-07 20:04:42 +00004977 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004978 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004979 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004980 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004981 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004982 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004983 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004984 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004985 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004986 else
Bill Wendling832171c2006-12-07 20:04:42 +00004987 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004988 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004989 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004990 else
Bill Wendling832171c2006-12-07 20:04:42 +00004991 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004992 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004993 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004994 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4995 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004996 cerr << LBB->getName() << " ";
4997 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004998 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004999 if (G && R->getReg() &&
5000 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00005001 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005002 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00005003 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005004 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005005 } else if (const ExternalSymbolSDNode *ES =
5006 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005007 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005008 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5009 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00005010 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005011 else
Dan Gohman69de1932008-02-06 22:27:42 +00005012 cerr << "<null>";
5013 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5014 if (M->MO.getValue())
5015 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
5016 else
5017 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005018 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
5019 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005020 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005021 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005022 }
5023 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005024 const Value *SrcValue = LD->getSrcValue();
5025 int SrcOffset = LD->getSrcValueOffset();
5026 cerr << " <";
5027 if (SrcValue)
5028 cerr << SrcValue;
5029 else
5030 cerr << "null";
5031 cerr << ":" << SrcOffset << ">";
5032
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005033 bool doExt = true;
5034 switch (LD->getExtensionType()) {
5035 default: doExt = false; break;
5036 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005037 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005038 break;
5039 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005040 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005041 break;
5042 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005043 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005044 break;
5045 }
5046 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00005047 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005048
Evan Cheng144d8f02006-11-09 17:55:04 +00005049 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005050 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00005051 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005052 if (LD->isVolatile())
5053 cerr << " <volatile>";
5054 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005055 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005056 const Value *SrcValue = ST->getSrcValue();
5057 int SrcOffset = ST->getSrcValueOffset();
5058 cerr << " <";
5059 if (SrcValue)
5060 cerr << SrcValue;
5061 else
5062 cerr << "null";
5063 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005064
5065 if (ST->isTruncatingStore())
5066 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00005067 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005068
5069 const char *AM = getIndexedModeName(ST->getAddressingMode());
5070 if (*AM)
5071 cerr << " " << AM;
5072 if (ST->isVolatile())
5073 cerr << " <volatile>";
5074 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005075 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5076 const Value *SrcValue = AT->getSrcValue();
5077 int SrcOffset = AT->getSrcValueOffset();
5078 cerr << " <";
5079 if (SrcValue)
5080 cerr << SrcValue;
5081 else
5082 cerr << "null";
5083 cerr << ":" << SrcOffset << ">";
5084 if (AT->isVolatile())
5085 cerr << " <volatile>";
5086 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005087 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005088}
5089
Chris Lattnerde202b32005-11-09 23:47:37 +00005090static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005091 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5092 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005093 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005094 else
Bill Wendling832171c2006-12-07 20:04:42 +00005095 cerr << "\n" << std::string(indent+2, ' ')
5096 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005097
Chris Lattnerea946cd2005-01-09 20:38:33 +00005098
Bill Wendling832171c2006-12-07 20:04:42 +00005099 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005100 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005101}
5102
Chris Lattnerc3aae252005-01-07 07:46:32 +00005103void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005104 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005105
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005106 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5107 I != E; ++I) {
5108 const SDNode *N = I;
5109 if (!N->hasOneUse() && N != getRoot().Val)
5110 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005111 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005112
Jim Laskey26f7fa72006-10-17 19:33:52 +00005113 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005114
Bill Wendling832171c2006-12-07 20:04:42 +00005115 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005116}
5117
Evan Chengd6594ae2006-09-12 21:00:35 +00005118const Type *ConstantPoolSDNode::getType() const {
5119 if (isMachineConstantPoolEntry())
5120 return Val.MachineCPVal->getType();
5121 return Val.ConstVal->getType();
5122}