blob: 4c2700426a4c52a80441ca3215db77d30649a2cd [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"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000032#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000034#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000035#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000037#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000038#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000039#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000040#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattner0b3e5252006-08-15 19:11:05 +000043/// makeVTList - Return an instance of the SDVTList struct initialized with the
44/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000045static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000046 SDVTList Res = {VTs, NumVTs};
47 return Res;
48}
49
Duncan Sands83ec4b62008-06-06 12:08:01 +000050static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
51 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000052 default: assert(0 && "Unknown FP format");
53 case MVT::f32: return &APFloat::IEEEsingle;
54 case MVT::f64: return &APFloat::IEEEdouble;
55 case MVT::f80: return &APFloat::x87DoubleExtended;
56 case MVT::f128: return &APFloat::IEEEquad;
57 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
58 }
59}
60
Chris Lattnerf8dc0612008-02-03 06:49:24 +000061SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000071bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000072 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000073}
74
Duncan Sands83ec4b62008-06-06 12:08:01 +000075bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000076 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000077 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000078
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000079 // PPC long double cannot be converted to any other type.
80 if (VT == MVT::ppcf128 ||
81 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000082 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000140 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000196/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000197bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000199 if (N->getOpcode() == ISD::DBG_LABEL)
200 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000201 if (N->isMachineOpcode() &&
202 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000203 return true;
204 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000205}
206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
208/// when given the operation for (X op Y).
209ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
210 // To perform this operation, we just need to swap the L and G bits of the
211 // operation.
212 unsigned OldL = (Operation >> 2) & 1;
213 unsigned OldG = (Operation >> 1) & 1;
214 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
215 (OldL << 1) | // New G bit
216 (OldG << 2)); // New L bit.
217}
218
219/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
220/// 'op' is a valid SetCC operation.
221ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
222 unsigned Operation = Op;
223 if (isInteger)
224 Operation ^= 7; // Flip L, G, E bits, but not U.
225 else
226 Operation ^= 15; // Flip all of the condition bits.
227 if (Operation > ISD::SETTRUE2)
228 Operation &= ~8; // Don't let N and U bits get set.
229 return ISD::CondCode(Operation);
230}
231
232
233/// isSignedOp - For an integer comparison, return 1 if the comparison is a
234/// signed operation and 2 if the result is an unsigned comparison. Return zero
235/// if the operation does not depend on the sign of the input (setne and seteq).
236static int isSignedOp(ISD::CondCode Opcode) {
237 switch (Opcode) {
238 default: assert(0 && "Illegal integer setcc operation!");
239 case ISD::SETEQ:
240 case ISD::SETNE: return 0;
241 case ISD::SETLT:
242 case ISD::SETLE:
243 case ISD::SETGT:
244 case ISD::SETGE: return 1;
245 case ISD::SETULT:
246 case ISD::SETULE:
247 case ISD::SETUGT:
248 case ISD::SETUGE: return 2;
249 }
250}
251
252/// getSetCCOrOperation - Return the result of a logical OR between different
253/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
254/// returns SETCC_INVALID if it is not possible to represent the resultant
255/// comparison.
256ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
257 bool isInteger) {
258 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
259 // Cannot fold a signed integer setcc with an unsigned integer setcc.
260 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 // If the N and U bits get set then the resultant comparison DOES suddenly
265 // care about orderedness, and is true when ordered.
266 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000267 Op &= ~16; // Clear the U bit if the N bit is set.
268
269 // Canonicalize illegal integer setcc's.
270 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
271 Op = ISD::SETNE;
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Op);
274}
275
276/// getSetCCAndOperation - Return the result of a logical AND between different
277/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
278/// function returns zero if it is not possible to represent the resultant
279/// comparison.
280ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
281 bool isInteger) {
282 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
283 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285
286 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000287 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
288
289 // Canonicalize illegal integer setcc's.
290 if (isInteger) {
291 switch (Result) {
292 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000293 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000294 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
296 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
297 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 }
299 }
300
301 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302}
303
Chris Lattnerb48da392005-01-23 04:39:44 +0000304const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000305 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000306}
307
Jim Laskey58b968b2005-08-17 20:08:02 +0000308//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000309// SDNode Profile Support
310//===----------------------------------------------------------------------===//
311
Jim Laskeydef69b92006-10-27 23:52:51 +0000312/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
313///
Jim Laskey583bd472006-10-27 23:46:08 +0000314static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
315 ID.AddInteger(OpC);
316}
317
318/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
319/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000320static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000321 ID.AddPointer(VTList.VTs);
322}
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000327 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000328 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000329 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000330 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 }
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
336static void AddNodeIDOperands(FoldingSetNodeID &ID,
337 const SDUse *Ops, unsigned NumOps) {
338 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000339 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000341 }
342}
343
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID,
345 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000346 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000347 AddNodeIDOpcode(ID, OpC);
348 AddNodeIDValueTypes(ID, VTList);
349 AddNodeIDOperands(ID, OpList, N);
350}
351
Roman Levenstein9cac5252008-04-16 16:15:27 +0000352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
354/// data.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, N->getOpcode());
357 // Add the return value info.
358 AddNodeIDValueTypes(ID, N->getVTList());
359 // Add the operand info.
360 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
361
362 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 switch (N->getOpcode()) {
364 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 case ISD::ARG_FLAGS:
366 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
367 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetConstant:
369 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000370 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 break;
372 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000374 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000378 case ISD::GlobalAddress:
379 case ISD::TargetGlobalTLSAddress:
380 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000381 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 ID.AddPointer(GA->getGlobal());
383 ID.AddInteger(GA->getOffset());
384 break;
385 }
386 case ISD::BasicBlock:
387 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
388 break;
389 case ISD::Register:
390 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
391 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000392 case ISD::DBG_STOPPOINT: {
393 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
394 ID.AddInteger(DSP->getLine());
395 ID.AddInteger(DSP->getColumn());
396 ID.AddPointer(DSP->getCompileUnit());
397 break;
398 }
Dan Gohman69de1932008-02-06 22:27:42 +0000399 case ISD::SRCVALUE:
400 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
401 break;
402 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000403 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000404 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000405 break;
406 }
407 case ISD::FrameIndex:
408 case ISD::TargetFrameIndex:
409 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
410 break;
411 case ISD::JumpTable:
412 case ISD::TargetJumpTable:
413 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
414 break;
415 case ISD::ConstantPool:
416 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000417 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 ID.AddInteger(CP->getAlignment());
419 ID.AddInteger(CP->getOffset());
420 if (CP->isMachineConstantPoolEntry())
421 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
422 else
423 ID.AddPointer(CP->getConstVal());
424 break;
425 }
426 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000427 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAddressingMode());
429 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000430 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 break;
433 }
434 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 ID.AddInteger(ST->getAddressingMode());
437 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Dale Johannesene00a8a22008-08-28 02:44:49 +0000442 case ISD::ATOMIC_CMP_SWAP_8:
443 case ISD::ATOMIC_SWAP_8:
444 case ISD::ATOMIC_LOAD_ADD_8:
445 case ISD::ATOMIC_LOAD_SUB_8:
446 case ISD::ATOMIC_LOAD_AND_8:
447 case ISD::ATOMIC_LOAD_OR_8:
448 case ISD::ATOMIC_LOAD_XOR_8:
449 case ISD::ATOMIC_LOAD_NAND_8:
450 case ISD::ATOMIC_LOAD_MIN_8:
451 case ISD::ATOMIC_LOAD_MAX_8:
452 case ISD::ATOMIC_LOAD_UMIN_8:
453 case ISD::ATOMIC_LOAD_UMAX_8:
454 case ISD::ATOMIC_CMP_SWAP_16:
455 case ISD::ATOMIC_SWAP_16:
456 case ISD::ATOMIC_LOAD_ADD_16:
457 case ISD::ATOMIC_LOAD_SUB_16:
458 case ISD::ATOMIC_LOAD_AND_16:
459 case ISD::ATOMIC_LOAD_OR_16:
460 case ISD::ATOMIC_LOAD_XOR_16:
461 case ISD::ATOMIC_LOAD_NAND_16:
462 case ISD::ATOMIC_LOAD_MIN_16:
463 case ISD::ATOMIC_LOAD_MAX_16:
464 case ISD::ATOMIC_LOAD_UMIN_16:
465 case ISD::ATOMIC_LOAD_UMAX_16:
466 case ISD::ATOMIC_CMP_SWAP_32:
467 case ISD::ATOMIC_SWAP_32:
468 case ISD::ATOMIC_LOAD_ADD_32:
469 case ISD::ATOMIC_LOAD_SUB_32:
470 case ISD::ATOMIC_LOAD_AND_32:
471 case ISD::ATOMIC_LOAD_OR_32:
472 case ISD::ATOMIC_LOAD_XOR_32:
473 case ISD::ATOMIC_LOAD_NAND_32:
474 case ISD::ATOMIC_LOAD_MIN_32:
475 case ISD::ATOMIC_LOAD_MAX_32:
476 case ISD::ATOMIC_LOAD_UMIN_32:
477 case ISD::ATOMIC_LOAD_UMAX_32:
478 case ISD::ATOMIC_CMP_SWAP_64:
479 case ISD::ATOMIC_SWAP_64:
480 case ISD::ATOMIC_LOAD_ADD_64:
481 case ISD::ATOMIC_LOAD_SUB_64:
482 case ISD::ATOMIC_LOAD_AND_64:
483 case ISD::ATOMIC_LOAD_OR_64:
484 case ISD::ATOMIC_LOAD_XOR_64:
485 case ISD::ATOMIC_LOAD_NAND_64:
486 case ISD::ATOMIC_LOAD_MIN_64:
487 case ISD::ATOMIC_LOAD_MAX_64:
488 case ISD::ATOMIC_LOAD_UMIN_64:
489 case ISD::ATOMIC_LOAD_UMAX_64: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000490 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
491 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000492 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000493 }
Mon P Wang28873102008-06-25 08:15:39 +0000494 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000495}
496
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000497/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
498/// the CSE map that carries both alignment and volatility information.
499///
500static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
501 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
502}
503
Jim Laskey583bd472006-10-27 23:46:08 +0000504//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000505// SelectionDAG Class
506//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000507
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000509/// SelectionDAG.
510void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000511 // Create a dummy node (which is not added to allnodes), that adds a reference
512 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000513 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000514
Chris Lattner190a4182006-08-04 17:45:20 +0000515 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000516
Chris Lattner190a4182006-08-04 17:45:20 +0000517 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000518 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000519 if (I->use_empty())
520 DeadNodes.push_back(I);
521
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000522 RemoveDeadNodes(DeadNodes);
523
524 // If the root changed (e.g. it was a dead load, update the root).
525 setRoot(Dummy.getValue());
526}
527
528/// RemoveDeadNodes - This method deletes the unreachable nodes in the
529/// given list, and any nodes that become unreachable as a result.
530void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
531 DAGUpdateListener *UpdateListener) {
532
Chris Lattner190a4182006-08-04 17:45:20 +0000533 // Process the worklist, deleting the nodes and adding their uses to the
534 // worklist.
535 while (!DeadNodes.empty()) {
536 SDNode *N = DeadNodes.back();
537 DeadNodes.pop_back();
538
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000539 if (UpdateListener)
540 UpdateListener->NodeDeleted(N, 0);
541
Chris Lattner190a4182006-08-04 17:45:20 +0000542 // Take the node out of the appropriate CSE map.
543 RemoveNodeFromCSEMaps(N);
544
545 // Next, brutally remove the operand list. This is safe to do, as there are
546 // no cycles in the graph.
547 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000548 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000549 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000550
551 // Now that we removed this operand, see if there are no uses of it left.
552 if (Operand->use_empty())
553 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000554 }
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 Lattner190a4182006-08-04 17:45:20 +0000558 N->OperandList = 0;
559 N->NumOperands = 0;
560
561 // Finally, remove N itself.
Dan Gohman11467282008-08-26 01:44:34 +0000562 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerf469cb62005-11-08 18:52:27 +0000563 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564}
565
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000566void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000567 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000568 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000569}
570
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000571void SelectionDAG::DeleteNode(SDNode *N) {
572 assert(N->use_empty() && "Cannot delete a node that is not dead!");
573
574 // First take this out of the appropriate CSE map.
575 RemoveNodeFromCSEMaps(N);
576
Chris Lattner1e111c72005-09-07 05:37:01 +0000577 // Finally, remove uses due to operands of this node, remove from the
578 // AllNodes list, and delete the node.
579 DeleteNodeNotInCSEMaps(N);
580}
581
582void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
583
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000584 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000585 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000586 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf350b272008-08-23 02:25:05 +0000587 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000588 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000589
Dan Gohman11467282008-08-26 01:44:34 +0000590 assert(N != AllNodes.begin());
591 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000592}
593
Chris Lattner149c58c2005-08-16 18:17:10 +0000594/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
595/// correspond to it. This is useful when we're about to delete or repurpose
596/// the node. We don't want future request for structurally identical nodes
597/// to return N anymore.
598void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000599 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000600 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000601 case ISD::EntryToken:
602 assert(0 && "EntryToken should not be in CSEMaps!");
603 return;
Chris Lattner95038592005-10-05 06:35:28 +0000604 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000605 case ISD::CONDCODE:
606 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
607 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000608 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000609 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
610 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000612 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000613 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000614 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000615 Erased =
616 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000617 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000618 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000619 MVT VT = cast<VTSDNode>(N)->getVT();
620 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000621 Erased = ExtendedValueTypeNodes.erase(VT);
622 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000623 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
624 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000625 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000626 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000627 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000628 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000629 // Remove it from the CSE Map.
630 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000631 break;
632 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000633#ifndef NDEBUG
634 // Verify that the node was actually in one of the CSE maps, unless it has a
635 // flag result (which cannot be CSE'd) or is one of the special cases that are
636 // not subject to CSE.
637 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000638 !N->isTargetOpcode() &&
639 N->getOpcode() != ISD::DBG_LABEL &&
640 N->getOpcode() != ISD::DBG_STOPPOINT &&
641 N->getOpcode() != ISD::EH_LABEL &&
642 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000643 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000644 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000645 assert(0 && "Node is not in map!");
646 }
647#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000648}
649
Chris Lattner8b8749f2005-08-17 19:00:20 +0000650/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
651/// has been taken out and modified in some way. If the specified node already
652/// exists in the CSE maps, do not modify the maps, but return the existing node
653/// instead. If it doesn't exist, add it and return null.
654///
655SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
656 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000657
658 if (N->getValueType(0) == MVT::Flag)
659 return 0; // Never CSE anything that produces a flag.
660
661 switch (N->getOpcode()) {
662 default: break;
663 case ISD::HANDLENODE:
664 case ISD::DBG_LABEL:
665 case ISD::DBG_STOPPOINT:
666 case ISD::EH_LABEL:
667 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000668 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000669 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000670
Chris Lattner9f8cc692005-12-19 22:21:21 +0000671 // Check that remaining values produced are not flags.
672 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
673 if (N->getValueType(i) == MVT::Flag)
674 return 0; // Never CSE anything that produces a flag.
675
Chris Lattnera5682852006-08-07 23:03:03 +0000676 SDNode *New = CSEMap.GetOrInsertNode(N);
677 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000678 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000679}
680
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000681/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
682/// were replaced with those specified. If this node is never memoized,
683/// return null, otherwise return a pointer to the slot it would take. If a
684/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000685SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000686 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000687 if (N->getValueType(0) == MVT::Flag)
688 return 0; // Never CSE anything that produces a flag.
689
690 switch (N->getOpcode()) {
691 default: break;
692 case ISD::HANDLENODE:
693 case ISD::DBG_LABEL:
694 case ISD::DBG_STOPPOINT:
695 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000696 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000697 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000698
699 // Check that remaining values produced are not flags.
700 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
701 if (N->getValueType(i) == MVT::Flag)
702 return 0; // Never CSE anything that produces a flag.
703
Dan Gohman475871a2008-07-27 21:46:04 +0000704 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000705 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000706 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000707 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000708}
709
710/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
711/// were replaced with those specified. If this node is never memoized,
712/// return null, otherwise return a pointer to the slot it would take. If a
713/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000714SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000715 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000716 void *&InsertPos) {
717 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000718
719 // Check that remaining values produced are not flags.
720 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
721 if (N->getValueType(i) == MVT::Flag)
722 return 0; // Never CSE anything that produces a flag.
723
Dan Gohman475871a2008-07-27 21:46:04 +0000724 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000725 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000726 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000727 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
728}
729
730
731/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
732/// were replaced with those specified. If this node is never memoized,
733/// return null, otherwise return a pointer to the slot it would take. If a
734/// node already exists with these operands, the slot will be non-null.
735SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000736 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000737 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000738 if (N->getValueType(0) == MVT::Flag)
739 return 0; // Never CSE anything that produces a flag.
740
741 switch (N->getOpcode()) {
742 default: break;
743 case ISD::HANDLENODE:
744 case ISD::DBG_LABEL:
745 case ISD::DBG_STOPPOINT:
746 case ISD::EH_LABEL:
747 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000748 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000749 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000750
751 // Check that remaining values produced are not flags.
752 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
753 if (N->getValueType(i) == MVT::Flag)
754 return 0; // Never CSE anything that produces a flag.
755
Jim Laskey583bd472006-10-27 23:46:08 +0000756 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000757 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000758
Evan Cheng9629aba2006-10-11 01:47:58 +0000759 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
760 ID.AddInteger(LD->getAddressingMode());
761 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000762 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000763 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000764 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
765 ID.AddInteger(ST->getAddressingMode());
766 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000767 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000768 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000769 }
Jim Laskey583bd472006-10-27 23:46:08 +0000770
Chris Lattnera5682852006-08-07 23:03:03 +0000771 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000772}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000773
Duncan Sandsd038e042008-07-21 10:20:31 +0000774/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
775void SelectionDAG::VerifyNode(SDNode *N) {
776 switch (N->getOpcode()) {
777 default:
778 break;
779 case ISD::BUILD_VECTOR: {
780 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
781 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
782 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
783 "Wrong number of BUILD_VECTOR operands!");
784 MVT EltVT = N->getValueType(0).getVectorElementType();
785 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000786 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000787 "Wrong BUILD_VECTOR operand type!");
788 break;
789 }
790 }
791}
792
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000793/// getMVTAlignment - Compute the default alignment value for the
794/// given type.
795///
796unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
797 const Type *Ty = VT == MVT::iPTR ?
798 PointerType::get(Type::Int8Ty, 0) :
799 VT.getTypeForMVT();
800
801 return TLI.getTargetData()->getABITypeAlignment(Ty);
802}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000803
Dan Gohman7c3234c2008-08-27 23:52:12 +0000804SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
805 : TLI(tli), FLI(fli),
806 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000807 Root(getEntryNode()) {
808 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000809}
810
Dan Gohman7c3234c2008-08-27 23:52:12 +0000811void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi) {
812 MF = &mf;
813 MMI = mmi;
814}
815
Chris Lattner78ec3112003-08-11 14:57:33 +0000816SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000817 allnodes_clear();
818}
819
820void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000821 assert(&*AllNodes.begin() == &EntryNode);
822 AllNodes.remove(AllNodes.begin());
Chris Lattnerde202b32005-11-09 23:47:37 +0000823 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000824 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000825 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000826 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000827 delete [] N->OperandList;
Dan Gohman11467282008-08-26 01:44:34 +0000828 NodeAllocator.Deallocate(N);
Chris Lattner65113b22005-11-08 22:07:03 +0000829 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000830}
831
Dan Gohman7c3234c2008-08-27 23:52:12 +0000832void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000833 allnodes_clear();
834 OperandAllocator.Reset();
835 CSEMap.clear();
836
837 ExtendedValueTypeNodes.clear();
838 ExternalSymbols.clear();
839 TargetExternalSymbols.clear();
840 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
841 static_cast<CondCodeSDNode*>(0));
842 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
843 static_cast<SDNode*>(0));
844
845 EntryNode.Uses = 0;
846 AllNodes.push_back(&EntryNode);
847 Root = getEntryNode();
848}
849
Dan Gohman475871a2008-07-27 21:46:04 +0000850SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000851 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000852 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000854 return getNode(ISD::AND, Op.getValueType(), Op,
855 getConstant(Imm, Op.getValueType()));
856}
857
Dan Gohman475871a2008-07-27 21:46:04 +0000858SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000859 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000860 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000861}
862
Dan Gohman475871a2008-07-27 21:46:04 +0000863SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000865
Evan Cheng0ff39b32008-06-30 07:31:25 +0000866 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000868 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000869
870 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000871 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000872 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000873 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000874 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000875 SDNode *N = NULL;
876 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000878 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000879 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000880 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000881 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000882 CSEMap.InsertNode(N, IP);
883 AllNodes.push_back(N);
884 }
885
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000887 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000888 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000890 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
891 }
892 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000893}
894
Dan Gohman475871a2008-07-27 21:46:04 +0000895SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000896 return getConstant(Val, TLI.getPointerTy(), isTarget);
897}
898
899
Dan Gohman475871a2008-07-27 21:46:04 +0000900SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000901 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000902
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 MVT EltVT =
904 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905
Chris Lattnerd8658612005-02-17 20:17:32 +0000906 // Do the map lookup using the actual bit pattern for the floating point
907 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
908 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000909 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000910 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000911 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000912 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000913 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000914 SDNode *N = NULL;
915 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000917 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000918 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000919 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000920 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 }
924
Dan Gohman475871a2008-07-27 21:46:04 +0000925 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000926 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000927 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000928 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000929 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
930 }
931 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932}
933
Dan Gohman475871a2008-07-27 21:46:04 +0000934SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000935 MVT EltVT =
936 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000937 if (EltVT==MVT::f32)
938 return getConstantFP(APFloat((float)Val), VT, isTarget);
939 else
940 return getConstantFP(APFloat(Val), VT, isTarget);
941}
942
Dan Gohman475871a2008-07-27 21:46:04 +0000943SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
944 MVT VT, int Offset,
945 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000946 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000947
948 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
949 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000950 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000951 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000952 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000953 }
954
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000955 if (GVar && GVar->isThreadLocal())
956 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
957 else
958 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000959
Jim Laskey583bd472006-10-27 23:46:08 +0000960 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000961 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000962 ID.AddPointer(GV);
963 ID.AddInteger(Offset);
964 void *IP = 0;
965 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000966 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000967 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000968 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000969 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000970 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000971 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972}
973
Dan Gohman475871a2008-07-27 21:46:04 +0000974SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000975 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000976 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000977 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000978 ID.AddInteger(FI);
979 void *IP = 0;
980 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000981 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000982 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000983 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000984 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000985 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000986 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000987}
988
Dan Gohman475871a2008-07-27 21:46:04 +0000989SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000990 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000991 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000992 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000993 ID.AddInteger(JTI);
994 void *IP = 0;
995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000996 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000997 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000998 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000999 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001000 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001001 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001002}
1003
Dan Gohman475871a2008-07-27 21:46:04 +00001004SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1005 unsigned Alignment, int Offset,
1006 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001007 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001008 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001009 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001010 ID.AddInteger(Alignment);
1011 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001012 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001013 void *IP = 0;
1014 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001015 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001016 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001017 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001018 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001019 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001020 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001021}
1022
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023
Dan Gohman475871a2008-07-27 21:46:04 +00001024SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1025 unsigned Alignment, int Offset,
1026 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001027 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001028 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001029 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001030 ID.AddInteger(Alignment);
1031 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001032 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001033 void *IP = 0;
1034 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001035 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001036 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001037 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001038 CSEMap.InsertNode(N, IP);
1039 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001040 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001041}
1042
1043
Dan Gohman475871a2008-07-27 21:46:04 +00001044SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001045 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001046 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001047 ID.AddPointer(MBB);
1048 void *IP = 0;
1049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001050 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001051 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001052 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001053 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001055 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056}
1057
Dan Gohman475871a2008-07-27 21:46:04 +00001058SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001059 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001060 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001061 ID.AddInteger(Flags.getRawBits());
1062 void *IP = 0;
1063 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001064 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001065 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001066 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001067 CSEMap.InsertNode(N, IP);
1068 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001069 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001070}
1071
Dan Gohman475871a2008-07-27 21:46:04 +00001072SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001073 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1074 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001075
Duncan Sands83ec4b62008-06-06 12:08:01 +00001076 SDNode *&N = VT.isExtended() ?
1077 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001078
Dan Gohman475871a2008-07-27 21:46:04 +00001079 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001080 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001081 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001082 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001083 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001084}
1085
Dan Gohman475871a2008-07-27 21:46:04 +00001086SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001088 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001089 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001090 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001091 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001092 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001093}
1094
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001096 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001097 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001098 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001099 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001101 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102}
1103
Dan Gohman475871a2008-07-27 21:46:04 +00001104SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001105 if ((unsigned)Cond >= CondCodeNodes.size())
1106 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001107
Chris Lattner079a27a2005-08-09 20:40:02 +00001108 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001109 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001110 new (N) CondCodeSDNode(Cond);
1111 CondCodeNodes[Cond] = N;
1112 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001113 }
Dan Gohman475871a2008-07-27 21:46:04 +00001114 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001115}
1116
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001118 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001119 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001120 ID.AddInteger(RegNo);
1121 void *IP = 0;
1122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001123 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001124 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001125 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001126 CSEMap.InsertNode(N, IP);
1127 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001128 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001129}
1130
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001132 unsigned Line, unsigned Col,
1133 const CompileUnitDesc *CU) {
1134 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001135 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001136 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001137 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001138}
1139
Dan Gohman475871a2008-07-27 21:46:04 +00001140SDValue SelectionDAG::getLabel(unsigned Opcode,
1141 SDValue Root,
1142 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001143 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001144 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001145 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1146 ID.AddInteger(LabelID);
1147 void *IP = 0;
1148 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001149 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001150 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001151 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001152 CSEMap.InsertNode(N, IP);
1153 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001154 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001155}
1156
Dan Gohman475871a2008-07-27 21:46:04 +00001157SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001158 assert((!V || isa<PointerType>(V->getType())) &&
1159 "SrcValue is not a pointer?");
1160
Jim Laskey583bd472006-10-27 23:46:08 +00001161 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001162 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001163 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001164
Chris Lattner61b09412006-08-11 21:01:22 +00001165 void *IP = 0;
1166 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001167 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001168
Dan Gohmanfed90b62008-07-28 21:51:04 +00001169 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001170 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001171 CSEMap.InsertNode(N, IP);
1172 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001173 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001174}
1175
Dan Gohman475871a2008-07-27 21:46:04 +00001176SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001177 const Value *v = MO.getValue();
1178 assert((!v || isa<PointerType>(v->getType())) &&
1179 "SrcValue is not a pointer?");
1180
1181 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001182 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001183 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001184
1185 void *IP = 0;
1186 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001187 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001188
Dan Gohmanfed90b62008-07-28 21:51:04 +00001189 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001190 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001191 CSEMap.InsertNode(N, IP);
1192 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001193 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001194}
1195
Chris Lattner37ce9df2007-10-15 17:47:20 +00001196/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1197/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001198SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001199 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001200 unsigned ByteSize = VT.getSizeInBits()/8;
1201 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001202 unsigned StackAlign =
1203 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1204
Chris Lattner37ce9df2007-10-15 17:47:20 +00001205 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1206 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1207}
1208
Dan Gohman475871a2008-07-27 21:46:04 +00001209SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1210 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001211 // These setcc operations always fold.
1212 switch (Cond) {
1213 default: break;
1214 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001215 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001216 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001217 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001218
1219 case ISD::SETOEQ:
1220 case ISD::SETOGT:
1221 case ISD::SETOGE:
1222 case ISD::SETOLT:
1223 case ISD::SETOLE:
1224 case ISD::SETONE:
1225 case ISD::SETO:
1226 case ISD::SETUO:
1227 case ISD::SETUEQ:
1228 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001230 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001232
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001234 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001236 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001237
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 switch (Cond) {
1239 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001240 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1241 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001242 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1243 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1244 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1245 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1246 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1247 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1248 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1249 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251 }
Chris Lattner67255a12005-04-07 18:14:58 +00001252 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001253 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1254 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001255 // No compile time operations on this type yet.
1256 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001257 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001258
1259 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001261 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001262 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1263 return getNode(ISD::UNDEF, VT);
1264 // fall through
1265 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1266 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1267 return getNode(ISD::UNDEF, VT);
1268 // fall through
1269 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001270 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001271 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1272 return getNode(ISD::UNDEF, VT);
1273 // fall through
1274 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1275 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1276 return getNode(ISD::UNDEF, VT);
1277 // fall through
1278 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1279 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1280 return getNode(ISD::UNDEF, VT);
1281 // fall through
1282 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001283 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001284 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1285 return getNode(ISD::UNDEF, VT);
1286 // fall through
1287 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001288 R==APFloat::cmpEqual, VT);
1289 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1290 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1291 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1292 R==APFloat::cmpEqual, VT);
1293 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1294 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1295 R==APFloat::cmpLessThan, VT);
1296 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1297 R==APFloat::cmpUnordered, VT);
1298 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1299 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001300 }
1301 } else {
1302 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001303 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001304 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001305 }
1306
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001307 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001308 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309}
1310
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001311/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1312/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001313bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001314 unsigned BitWidth = Op.getValueSizeInBits();
1315 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1316}
1317
Dan Gohmanea859be2007-06-22 14:59:07 +00001318/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1319/// this predicate to simplify operations downstream. Mask is known to be zero
1320/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001321bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001322 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001323 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001324 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1325 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1326 return (KnownZero & Mask) == Mask;
1327}
1328
1329/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1330/// known to be either zero or one and return them in the KnownZero/KnownOne
1331/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1332/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001333void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001334 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001335 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001336 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001337 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001338 "Mask size mismatches value type size!");
1339
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001340 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001341 if (Depth == 6 || Mask == 0)
1342 return; // Limit search depth.
1343
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001344 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001345
1346 switch (Op.getOpcode()) {
1347 case ISD::Constant:
1348 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001349 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001350 KnownZero = ~KnownOne & Mask;
1351 return;
1352 case ISD::AND:
1353 // If either the LHS or the RHS are Zero, the result is zero.
1354 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001355 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1356 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1358 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1359
1360 // Output known-1 bits are only known if set in both the LHS & RHS.
1361 KnownOne &= KnownOne2;
1362 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1363 KnownZero |= KnownZero2;
1364 return;
1365 case ISD::OR:
1366 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001367 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1368 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001369 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1370 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1371
1372 // Output known-0 bits are only known if clear in both the LHS & RHS.
1373 KnownZero &= KnownZero2;
1374 // Output known-1 are known to be set if set in either the LHS | RHS.
1375 KnownOne |= KnownOne2;
1376 return;
1377 case ISD::XOR: {
1378 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1379 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1380 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1381 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1382
1383 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001384 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001385 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1386 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1387 KnownZero = KnownZeroOut;
1388 return;
1389 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001390 case ISD::MUL: {
1391 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1392 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1393 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1394 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1395 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1396
1397 // If low bits are zero in either operand, output low known-0 bits.
1398 // Also compute a conserative estimate for high known-0 bits.
1399 // More trickiness is possible, but this is sufficient for the
1400 // interesting case of alignment computation.
1401 KnownOne.clear();
1402 unsigned TrailZ = KnownZero.countTrailingOnes() +
1403 KnownZero2.countTrailingOnes();
1404 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001405 KnownZero2.countLeadingOnes(),
1406 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001407
1408 TrailZ = std::min(TrailZ, BitWidth);
1409 LeadZ = std::min(LeadZ, BitWidth);
1410 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1411 APInt::getHighBitsSet(BitWidth, LeadZ);
1412 KnownZero &= Mask;
1413 return;
1414 }
1415 case ISD::UDIV: {
1416 // For the purposes of computing leading zeros we can conservatively
1417 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001418 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001419 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1420 ComputeMaskedBits(Op.getOperand(0),
1421 AllOnes, KnownZero2, KnownOne2, Depth+1);
1422 unsigned LeadZ = KnownZero2.countLeadingOnes();
1423
1424 KnownOne2.clear();
1425 KnownZero2.clear();
1426 ComputeMaskedBits(Op.getOperand(1),
1427 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001428 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1429 if (RHSUnknownLeadingOnes != BitWidth)
1430 LeadZ = std::min(BitWidth,
1431 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001432
1433 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1434 return;
1435 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 case ISD::SELECT:
1437 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1438 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1439 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1440 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1441
1442 // Only known if known in both the LHS and RHS.
1443 KnownOne &= KnownOne2;
1444 KnownZero &= KnownZero2;
1445 return;
1446 case ISD::SELECT_CC:
1447 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1448 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1449 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1450 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1451
1452 // Only known if known in both the LHS and RHS.
1453 KnownOne &= KnownOne2;
1454 KnownZero &= KnownZero2;
1455 return;
1456 case ISD::SETCC:
1457 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001458 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1459 BitWidth > 1)
1460 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001461 return;
1462 case ISD::SHL:
1463 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1464 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001465 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001466
1467 // If the shift count is an invalid immediate, don't do anything.
1468 if (ShAmt >= BitWidth)
1469 return;
1470
1471 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001472 KnownZero, KnownOne, Depth+1);
1473 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001474 KnownZero <<= ShAmt;
1475 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001476 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001477 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001478 }
1479 return;
1480 case ISD::SRL:
1481 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1482 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001483 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001484
Dan Gohmand4cf9922008-02-26 18:50:50 +00001485 // If the shift count is an invalid immediate, don't do anything.
1486 if (ShAmt >= BitWidth)
1487 return;
1488
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001489 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001490 KnownZero, KnownOne, Depth+1);
1491 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001492 KnownZero = KnownZero.lshr(ShAmt);
1493 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001494
Dan Gohman72d2fd52008-02-13 22:43:25 +00001495 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001496 KnownZero |= HighBits; // High bits known zero.
1497 }
1498 return;
1499 case ISD::SRA:
1500 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001501 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001502
Dan Gohmand4cf9922008-02-26 18:50:50 +00001503 // If the shift count is an invalid immediate, don't do anything.
1504 if (ShAmt >= BitWidth)
1505 return;
1506
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001507 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001508 // If any of the demanded bits are produced by the sign extension, we also
1509 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001510 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1511 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001512 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001513
1514 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1515 Depth+1);
1516 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001517 KnownZero = KnownZero.lshr(ShAmt);
1518 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001519
1520 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001521 APInt SignBit = APInt::getSignBit(BitWidth);
1522 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001523
Dan Gohmanca93a432008-02-20 16:30:17 +00001524 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001525 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001526 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001527 KnownOne |= HighBits; // New bits are known one.
1528 }
1529 }
1530 return;
1531 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1533 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001534
1535 // Sign extension. Compute the demanded bits in the result that are not
1536 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001537 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001538
Dan Gohman977a76f2008-02-13 22:28:48 +00001539 APInt InSignBit = APInt::getSignBit(EBits);
1540 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001541
1542 // If the sign extended bits are demanded, we know that the sign
1543 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001544 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001545 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001546 InputDemandedBits |= InSignBit;
1547
1548 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1549 KnownZero, KnownOne, Depth+1);
1550 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1551
1552 // If the sign bit of the input is known set or clear, then we know the
1553 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001554 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 KnownZero |= NewBits;
1556 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001557 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001558 KnownOne |= NewBits;
1559 KnownZero &= ~NewBits;
1560 } else { // Input sign bit unknown
1561 KnownZero &= ~NewBits;
1562 KnownOne &= ~NewBits;
1563 }
1564 return;
1565 }
1566 case ISD::CTTZ:
1567 case ISD::CTLZ:
1568 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001569 unsigned LowBits = Log2_32(BitWidth)+1;
1570 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001571 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001572 return;
1573 }
1574 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001575 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001576 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001577 MVT VT = LD->getMemoryVT();
1578 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001579 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001580 }
1581 return;
1582 }
1583 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001584 MVT InVT = Op.getOperand(0).getValueType();
1585 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001586 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1587 APInt InMask = Mask;
1588 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001589 KnownZero.trunc(InBits);
1590 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001591 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001592 KnownZero.zext(BitWidth);
1593 KnownOne.zext(BitWidth);
1594 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 return;
1596 }
1597 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001598 MVT InVT = Op.getOperand(0).getValueType();
1599 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001600 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001601 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1602 APInt InMask = Mask;
1603 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001604
1605 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001606 // bit is demanded. Temporarily set this bit in the mask for our callee.
1607 if (NewBits.getBoolValue())
1608 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001609
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001610 KnownZero.trunc(InBits);
1611 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001612 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1613
1614 // Note if the sign bit is known to be zero or one.
1615 bool SignBitKnownZero = KnownZero.isNegative();
1616 bool SignBitKnownOne = KnownOne.isNegative();
1617 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1618 "Sign bit can't be known to be both zero and one!");
1619
1620 // If the sign bit wasn't actually demanded by our caller, we don't
1621 // want it set in the KnownZero and KnownOne result values. Reset the
1622 // mask and reapply it to the result values.
1623 InMask = Mask;
1624 InMask.trunc(InBits);
1625 KnownZero &= InMask;
1626 KnownOne &= InMask;
1627
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001628 KnownZero.zext(BitWidth);
1629 KnownOne.zext(BitWidth);
1630
Dan Gohman977a76f2008-02-13 22:28:48 +00001631 // If the sign bit is known zero or one, the top bits match.
1632 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001633 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001634 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001636 return;
1637 }
1638 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001639 MVT InVT = Op.getOperand(0).getValueType();
1640 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001641 APInt InMask = Mask;
1642 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001643 KnownZero.trunc(InBits);
1644 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001645 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001646 KnownZero.zext(BitWidth);
1647 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001648 return;
1649 }
1650 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 MVT InVT = Op.getOperand(0).getValueType();
1652 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001653 APInt InMask = Mask;
1654 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001655 KnownZero.zext(InBits);
1656 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001657 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001658 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001659 KnownZero.trunc(BitWidth);
1660 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001661 break;
1662 }
1663 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001664 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1665 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001666 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1667 KnownOne, Depth+1);
1668 KnownZero |= (~InMask) & Mask;
1669 return;
1670 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001671 case ISD::FGETSIGN:
1672 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001673 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001674 return;
1675
Dan Gohman23e8b712008-04-28 17:02:21 +00001676 case ISD::SUB: {
1677 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1678 // We know that the top bits of C-X are clear if X contains less bits
1679 // than C (i.e. no wrap-around can happen). For example, 20-X is
1680 // positive if we can prove that X is >= 0 and < 16.
1681 if (CLHS->getAPIntValue().isNonNegative()) {
1682 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1683 // NLZ can't be BitWidth with no sign bit
1684 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1685 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1686 Depth+1);
1687
1688 // If all of the MaskV bits are known to be zero, then we know the
1689 // output top bits are zero, because we now know that the output is
1690 // from [0-C].
1691 if ((KnownZero2 & MaskV) == MaskV) {
1692 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1693 // Top bits known zero.
1694 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1695 }
1696 }
1697 }
1698 }
1699 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001701 // Output known-0 bits are known if clear or set in both the low clear bits
1702 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1703 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001704 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1705 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1706 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1707 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1708
1709 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1710 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1711 KnownZeroOut = std::min(KnownZeroOut,
1712 KnownZero2.countTrailingOnes());
1713
1714 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 return;
1716 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001717 case ISD::SREM:
1718 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001719 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001720 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001721 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001722 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1723 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001724
Dan Gohmana60832b2008-08-13 23:12:35 +00001725 // If the sign bit of the first operand is zero, the sign bit of
1726 // the result is zero. If the first operand has no one bits below
1727 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001728 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1729 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001730
Dan Gohman23e8b712008-04-28 17:02:21 +00001731 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001732
1733 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001734 }
1735 }
1736 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001737 case ISD::UREM: {
1738 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001739 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001740 if (RA.isPowerOf2()) {
1741 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001742 APInt Mask2 = LowBits & Mask;
1743 KnownZero |= ~LowBits & Mask;
1744 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1745 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1746 break;
1747 }
1748 }
1749
1750 // Since the result is less than or equal to either operand, any leading
1751 // zero bits in either operand must also exist in the result.
1752 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1753 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1754 Depth+1);
1755 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1756 Depth+1);
1757
1758 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1759 KnownZero2.countLeadingOnes());
1760 KnownOne.clear();
1761 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1762 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001763 }
1764 default:
1765 // Allow the target to implement this method for its nodes.
1766 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1767 case ISD::INTRINSIC_WO_CHAIN:
1768 case ISD::INTRINSIC_W_CHAIN:
1769 case ISD::INTRINSIC_VOID:
1770 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1771 }
1772 return;
1773 }
1774}
1775
1776/// ComputeNumSignBits - Return the number of times the sign bit of the
1777/// register is replicated into the other bits. We know that at least 1 bit
1778/// is always equal to the sign bit (itself), but other cases can give us
1779/// information. For example, immediately after an "SRA X, 2", we know that
1780/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001781unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001782 MVT VT = Op.getValueType();
1783 assert(VT.isInteger() && "Invalid VT!");
1784 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001785 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001786 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001787
1788 if (Depth == 6)
1789 return 1; // Limit search depth.
1790
1791 switch (Op.getOpcode()) {
1792 default: break;
1793 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001794 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001795 return VTBits-Tmp+1;
1796 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001797 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001798 return VTBits-Tmp;
1799
1800 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001801 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1802 // If negative, return # leading ones.
1803 if (Val.isNegative())
1804 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001805
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001806 // Return # leading zeros.
1807 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 }
1809
1810 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001811 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001812 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1813
1814 case ISD::SIGN_EXTEND_INREG:
1815 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 Tmp = VTBits-Tmp+1;
1818
1819 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1820 return std::max(Tmp, Tmp2);
1821
1822 case ISD::SRA:
1823 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1824 // SRA X, C -> adds C sign bits.
1825 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001826 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 if (Tmp > VTBits) Tmp = VTBits;
1828 }
1829 return Tmp;
1830 case ISD::SHL:
1831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1832 // shl destroys sign bits.
1833 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001834 if (C->getZExtValue() >= VTBits || // Bad shift.
1835 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1836 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001837 }
1838 break;
1839 case ISD::AND:
1840 case ISD::OR:
1841 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001842 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001843 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001844 if (Tmp != 1) {
1845 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1846 FirstAnswer = std::min(Tmp, Tmp2);
1847 // We computed what we know about the sign bits as our first
1848 // answer. Now proceed to the generic code that uses
1849 // ComputeMaskedBits, and pick whichever answer is better.
1850 }
1851 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001852
1853 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001854 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001856 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 return std::min(Tmp, Tmp2);
1858
1859 case ISD::SETCC:
1860 // If setcc returns 0/-1, all bits are sign bits.
1861 if (TLI.getSetCCResultContents() ==
1862 TargetLowering::ZeroOrNegativeOneSetCCResult)
1863 return VTBits;
1864 break;
1865 case ISD::ROTL:
1866 case ISD::ROTR:
1867 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001868 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001869
1870 // Handle rotate right by N like a rotate left by 32-N.
1871 if (Op.getOpcode() == ISD::ROTR)
1872 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1873
1874 // If we aren't rotating out all of the known-in sign bits, return the
1875 // number that are left. This handles rotl(sext(x), 1) for example.
1876 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1877 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1878 }
1879 break;
1880 case ISD::ADD:
1881 // Add can have at most one carry bit. Thus we know that the output
1882 // is, at worst, one more bit than the inputs.
1883 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1884 if (Tmp == 1) return 1; // Early out.
1885
1886 // Special case decrementing a value (ADD X, -1):
1887 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1888 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001889 APInt KnownZero, KnownOne;
1890 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001891 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1892
1893 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1894 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001895 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001896 return VTBits;
1897
1898 // If we are subtracting one from a positive number, there is no carry
1899 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001900 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 return Tmp;
1902 }
1903
1904 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1905 if (Tmp2 == 1) return 1;
1906 return std::min(Tmp, Tmp2)-1;
1907 break;
1908
1909 case ISD::SUB:
1910 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1911 if (Tmp2 == 1) return 1;
1912
1913 // Handle NEG.
1914 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001915 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001916 APInt KnownZero, KnownOne;
1917 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001918 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1919 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1920 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001921 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001922 return VTBits;
1923
1924 // If the input is known to be positive (the sign bit is known clear),
1925 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001926 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001927 return Tmp2;
1928
1929 // Otherwise, we treat this like a SUB.
1930 }
1931
1932 // Sub can have at most one carry bit. Thus we know that the output
1933 // is, at worst, one more bit than the inputs.
1934 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1935 if (Tmp == 1) return 1; // Early out.
1936 return std::min(Tmp, Tmp2)-1;
1937 break;
1938 case ISD::TRUNCATE:
1939 // FIXME: it's tricky to do anything useful for this, but it is an important
1940 // case for targets like X86.
1941 break;
1942 }
1943
1944 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1945 if (Op.getOpcode() == ISD::LOAD) {
1946 LoadSDNode *LD = cast<LoadSDNode>(Op);
1947 unsigned ExtType = LD->getExtensionType();
1948 switch (ExtType) {
1949 default: break;
1950 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001951 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001952 return VTBits-Tmp+1;
1953 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001954 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001955 return VTBits-Tmp;
1956 }
1957 }
1958
1959 // Allow the target to implement this method for its nodes.
1960 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1961 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1962 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1963 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1964 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001965 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001966 }
1967
1968 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1969 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001970 APInt KnownZero, KnownOne;
1971 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001972 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1973
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001974 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001975 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001976 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001977 Mask = KnownOne;
1978 } else {
1979 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001980 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001981 }
1982
1983 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1984 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001985 Mask = ~Mask;
1986 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 // Return # leading zeros. We use 'min' here in case Val was zero before
1988 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001989 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001990}
1991
Chris Lattner51dabfb2006-10-14 00:41:01 +00001992
Dan Gohman475871a2008-07-27 21:46:04 +00001993bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001994 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1995 if (!GA) return false;
1996 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1997 if (!GV) return false;
1998 MachineModuleInfo *MMI = getMachineModuleInfo();
1999 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
2000}
2001
2002
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002003/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2004/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002005SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002006 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue PermMask = N->getOperand(2);
2008 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002009 if (Idx.getOpcode() == ISD::UNDEF)
2010 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002011 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002012 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002013 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002014 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002015
2016 if (V.getOpcode() == ISD::BIT_CONVERT) {
2017 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002018 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002019 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002020 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002021 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002022 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002023 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002024 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002025 return V.getOperand(Index);
2026 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002027 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002028 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002029}
2030
2031
Chris Lattnerc3aae252005-01-07 07:46:32 +00002032/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002033///
Dan Gohman475871a2008-07-27 21:46:04 +00002034SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002035 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002036 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002037 void *IP = 0;
2038 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002039 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002040 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002041 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002042 CSEMap.InsertNode(N, IP);
2043
2044 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002045#ifndef NDEBUG
2046 VerifyNode(N);
2047#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002048 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002049}
2050
Dan Gohman475871a2008-07-27 21:46:04 +00002051SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002052 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002053 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002054 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002055 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002056 switch (Opcode) {
2057 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002058 case ISD::SIGN_EXTEND:
2059 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002060 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002061 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002062 case ISD::TRUNCATE:
2063 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002064 case ISD::UINT_TO_FP:
2065 case ISD::SINT_TO_FP: {
2066 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002067 // No compile time operations on this type.
2068 if (VT==MVT::ppcf128)
2069 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002070 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2071 (void)apf.convertFromAPInt(Val,
2072 Opcode==ISD::SINT_TO_FP,
2073 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002074 return getConstantFP(apf, VT);
2075 }
Chris Lattner94683772005-12-23 05:30:37 +00002076 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002077 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002078 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002079 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002080 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002081 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002082 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002083 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002084 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002085 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002086 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002087 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002088 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002089 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090 }
2091 }
2092
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002093 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002094 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002095 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002096 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002097 switch (Opcode) {
2098 case ISD::FNEG:
2099 V.changeSign();
2100 return getConstantFP(V, VT);
2101 case ISD::FABS:
2102 V.clearSign();
2103 return getConstantFP(V, VT);
2104 case ISD::FP_ROUND:
2105 case ISD::FP_EXTEND:
2106 // This can return overflow, underflow, or inexact; we don't care.
2107 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002108 (void)V.convert(*MVTToAPFloatSemantics(VT),
2109 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002110 return getConstantFP(V, VT);
2111 case ISD::FP_TO_SINT:
2112 case ISD::FP_TO_UINT: {
2113 integerPart x;
2114 assert(integerPartWidth >= 64);
2115 // FIXME need to be more flexible about rounding mode.
2116 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2117 Opcode==ISD::FP_TO_SINT,
2118 APFloat::rmTowardZero);
2119 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2120 break;
2121 return getConstant(x, VT);
2122 }
2123 case ISD::BIT_CONVERT:
2124 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2125 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2126 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2127 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002128 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002129 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002130 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002131 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002132
Gabor Greifba36cb52008-08-28 21:40:38 +00002133 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002134 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002135 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002136 case ISD::CONCAT_VECTORS:
2137 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002138 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002139 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 assert(VT.isFloatingPoint() &&
2141 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002142 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002143 if (Operand.getOpcode() == ISD::UNDEF)
2144 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002145 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002146 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002147 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002148 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002150 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002151 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002153 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002154 break;
2155 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002157 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002158 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002159 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002160 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002161 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002162 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002164 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002165 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002166 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002167 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002168 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002169 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002170 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2171 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002173 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002174 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002175 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002176 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002177 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002178 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002179 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002180 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002181 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002182 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2183 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002184 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002185 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2186 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2187 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2188 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002189 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002190 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002191 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002192 break;
Chris Lattner35481892005-12-23 00:16:34 +00002193 case ISD::BIT_CONVERT:
2194 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002195 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002196 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002197 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002198 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2199 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002200 if (OpOpcode == ISD::UNDEF)
2201 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002202 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002203 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2205 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002206 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002207 if (OpOpcode == ISD::UNDEF)
2208 return getNode(ISD::UNDEF, VT);
2209 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2210 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2211 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2212 Operand.getConstantOperandVal(1) == 0 &&
2213 Operand.getOperand(0).getValueType() == VT)
2214 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002215 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002216 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002217 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002218 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2219 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002220 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002221 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002222 break;
2223 case ISD::FABS:
2224 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002225 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002226 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002227 }
2228
Chris Lattner43247a12005-08-25 19:12:10 +00002229 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002230 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002231 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002232 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002234 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002235 void *IP = 0;
2236 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002237 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002238 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002239 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002240 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002241 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002242 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002243 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002244 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002245
Chris Lattnerc3aae252005-01-07 07:46:32 +00002246 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002247#ifndef NDEBUG
2248 VerifyNode(N);
2249#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002250 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002251}
2252
Dan Gohman475871a2008-07-27 21:46:04 +00002253SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2254 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002255 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2256 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002257 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002258 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002259 case ISD::TokenFactor:
2260 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2261 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002262 // Fold trivial token factors.
2263 if (N1.getOpcode() == ISD::EntryToken) return N2;
2264 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002265 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002266 case ISD::CONCAT_VECTORS:
2267 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2268 // one big BUILD_VECTOR.
2269 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2270 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002271 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2272 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002273 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2274 }
2275 break;
Chris Lattner76365122005-01-16 02:23:22 +00002276 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002278 N1.getValueType() == VT && "Binary operator types must match!");
2279 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2280 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002281 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002282 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002283 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2284 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002285 break;
Chris Lattner76365122005-01-16 02:23:22 +00002286 case ISD::OR:
2287 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002288 case ISD::ADD:
2289 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002290 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002291 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002292 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2293 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002294 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002295 return N1;
2296 break;
Chris Lattner76365122005-01-16 02:23:22 +00002297 case ISD::UDIV:
2298 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002299 case ISD::MULHU:
2300 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002301 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002302 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002303 case ISD::MUL:
2304 case ISD::SDIV:
2305 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002306 case ISD::FADD:
2307 case ISD::FSUB:
2308 case ISD::FMUL:
2309 case ISD::FDIV:
2310 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002311 assert(N1.getValueType() == N2.getValueType() &&
2312 N1.getValueType() == VT && "Binary operator types must match!");
2313 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002314 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2315 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 N1.getValueType().isFloatingPoint() &&
2317 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002318 "Invalid FCOPYSIGN!");
2319 break;
Chris Lattner76365122005-01-16 02:23:22 +00002320 case ISD::SHL:
2321 case ISD::SRA:
2322 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002323 case ISD::ROTL:
2324 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002325 assert(VT == N1.getValueType() &&
2326 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002327 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002328 "Shifts only work on integers");
2329
2330 // Always fold shifts of i1 values so the code generator doesn't need to
2331 // handle them. Since we know the size of the shift has to be less than the
2332 // size of the value, the shift/rotate count is guaranteed to be zero.
2333 if (VT == MVT::i1)
2334 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002335 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002336 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002337 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002338 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002339 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002340 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002341 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002342 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002343 break;
2344 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002345 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002346 assert(VT.isFloatingPoint() &&
2347 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002348 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002349 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002350 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002351 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002352 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002353 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002354 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002355 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002356 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002358 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002359 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002360 break;
2361 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002362 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002363 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002364 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002365 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002366 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002367 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002369
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002370 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002371 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002372 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002373 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002374 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002375 return getConstant(Val, VT);
2376 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002377 break;
2378 }
2379 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002380 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2381 if (N1.getOpcode() == ISD::UNDEF)
2382 return getNode(ISD::UNDEF, VT);
2383
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002384 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2385 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002386 if (N2C &&
2387 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002388 N1.getNumOperands() > 0) {
2389 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002390 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002391 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002392 N1.getOperand(N2C->getZExtValue() / Factor),
2393 getConstant(N2C->getZExtValue() % Factor,
2394 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002395 }
2396
2397 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2398 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002399 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002400 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002401
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002402 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2403 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002404 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2405 if (N1.getOperand(2) == N2)
2406 return N1.getOperand(1);
2407 else
2408 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2409 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002410 break;
2411 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002412 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002413 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2414 (N1.getValueType().isInteger() == VT.isInteger()) &&
2415 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002416
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002417 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2418 // 64-bit integers into 32-bit parts. Instead of building the extract of
2419 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2420 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002421 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002422
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002423 // EXTRACT_ELEMENT of a constant int is also very common.
2424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002425 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002426 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002427 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2428 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002429 }
2430 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002431 case ISD::EXTRACT_SUBVECTOR:
2432 if (N1.getValueType() == VT) // Trivial extraction.
2433 return N1;
2434 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002435 }
2436
2437 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002438 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002439 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002440 switch (Opcode) {
2441 case ISD::ADD: return getConstant(C1 + C2, VT);
2442 case ISD::SUB: return getConstant(C1 - C2, VT);
2443 case ISD::MUL: return getConstant(C1 * C2, VT);
2444 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002445 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002446 break;
2447 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002448 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002449 break;
2450 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002451 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002452 break;
2453 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002454 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002455 break;
2456 case ISD::AND : return getConstant(C1 & C2, VT);
2457 case ISD::OR : return getConstant(C1 | C2, VT);
2458 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002459 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002460 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2461 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2462 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2463 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002464 default: break;
2465 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466 } else { // Cannonicalize constant to RHS if commutative
2467 if (isCommutativeBinOp(Opcode)) {
2468 std::swap(N1C, N2C);
2469 std::swap(N1, N2);
2470 }
2471 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002472 }
2473
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002474 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002475 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2476 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002477 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002478 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2479 // Cannonicalize constant to RHS if commutative
2480 std::swap(N1CFP, N2CFP);
2481 std::swap(N1, N2);
2482 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002483 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2484 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002485 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002486 case ISD::FADD:
2487 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002488 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002489 return getConstantFP(V1, VT);
2490 break;
2491 case ISD::FSUB:
2492 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2493 if (s!=APFloat::opInvalidOp)
2494 return getConstantFP(V1, VT);
2495 break;
2496 case ISD::FMUL:
2497 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2498 if (s!=APFloat::opInvalidOp)
2499 return getConstantFP(V1, VT);
2500 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002501 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002502 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2503 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2504 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002505 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002506 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002507 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2508 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2509 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002510 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002511 case ISD::FCOPYSIGN:
2512 V1.copySign(V2);
2513 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002514 default: break;
2515 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002516 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002517 }
Chris Lattner62b57722006-04-20 05:39:12 +00002518
2519 // Canonicalize an UNDEF to the RHS, even over a constant.
2520 if (N1.getOpcode() == ISD::UNDEF) {
2521 if (isCommutativeBinOp(Opcode)) {
2522 std::swap(N1, N2);
2523 } else {
2524 switch (Opcode) {
2525 case ISD::FP_ROUND_INREG:
2526 case ISD::SIGN_EXTEND_INREG:
2527 case ISD::SUB:
2528 case ISD::FSUB:
2529 case ISD::FDIV:
2530 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002531 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002532 return N1; // fold op(undef, arg2) -> undef
2533 case ISD::UDIV:
2534 case ISD::SDIV:
2535 case ISD::UREM:
2536 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002537 case ISD::SRL:
2538 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002539 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002540 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2541 // For vectors, we can't easily build an all zero vector, just return
2542 // the LHS.
2543 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002544 }
2545 }
2546 }
2547
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002548 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002549 if (N2.getOpcode() == ISD::UNDEF) {
2550 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002551 case ISD::XOR:
2552 if (N1.getOpcode() == ISD::UNDEF)
2553 // Handle undef ^ undef -> 0 special case. This is a common
2554 // idiom (misuse).
2555 return getConstant(0, VT);
2556 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002557 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002558 case ISD::ADDC:
2559 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002560 case ISD::SUB:
2561 case ISD::FADD:
2562 case ISD::FSUB:
2563 case ISD::FMUL:
2564 case ISD::FDIV:
2565 case ISD::FREM:
2566 case ISD::UDIV:
2567 case ISD::SDIV:
2568 case ISD::UREM:
2569 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002570 return N2; // fold op(arg1, undef) -> undef
2571 case ISD::MUL:
2572 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002573 case ISD::SRL:
2574 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002576 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2577 // For vectors, we can't easily build an all zero vector, just return
2578 // the LHS.
2579 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002580 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002581 if (!VT.isVector())
2582 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002583 // For vectors, we can't easily build an all one vector, just return
2584 // the LHS.
2585 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002586 case ISD::SRA:
2587 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002588 }
2589 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002590
Chris Lattner27e9b412005-05-11 18:57:39 +00002591 // Memoize this node if possible.
2592 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002593 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002594 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002596 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002597 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002598 void *IP = 0;
2599 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002600 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002601 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002602 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002603 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002604 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002605 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002606 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002607 }
2608
Chris Lattnerc3aae252005-01-07 07:46:32 +00002609 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002610#ifndef NDEBUG
2611 VerifyNode(N);
2612#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002613 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002614}
2615
Dan Gohman475871a2008-07-27 21:46:04 +00002616SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2617 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002618 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2620 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002621 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002622 case ISD::CONCAT_VECTORS:
2623 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2624 // one big BUILD_VECTOR.
2625 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2626 N2.getOpcode() == ISD::BUILD_VECTOR &&
2627 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002628 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2629 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2630 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002631 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2632 }
2633 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002634 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002635 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002636 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002637 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002638 break;
2639 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002640 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002641 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002642 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002643 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002644 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002645 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002646 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002647
2648 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002649 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002650 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002651 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002652 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002653 return getNode(ISD::BR, MVT::Other, N1, N3);
2654 else
2655 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002656 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002657 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002658 case ISD::VECTOR_SHUFFLE:
2659 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002660 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002661 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002663 "Illegal VECTOR_SHUFFLE node!");
2664 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002665 case ISD::BIT_CONVERT:
2666 // Fold bit_convert nodes from a type to themselves.
2667 if (N1.getValueType() == VT)
2668 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002669 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002670 }
2671
Chris Lattner43247a12005-08-25 19:12:10 +00002672 // Memoize node if it doesn't produce a flag.
2673 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002674 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002675 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002676 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002677 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002678 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002679 void *IP = 0;
2680 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002681 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002682 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002683 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002684 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002685 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002686 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002687 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002688 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002689 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002690#ifndef NDEBUG
2691 VerifyNode(N);
2692#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002693 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002694}
2695
Dan Gohman475871a2008-07-27 21:46:04 +00002696SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2697 SDValue N1, SDValue N2, SDValue N3,
2698 SDValue N4) {
2699 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002700 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002701}
2702
Dan Gohman475871a2008-07-27 21:46:04 +00002703SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2704 SDValue N1, SDValue N2, SDValue N3,
2705 SDValue N4, SDValue N5) {
2706 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002707 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002708}
2709
Dan Gohman707e0182008-04-12 04:36:06 +00002710/// getMemsetValue - Vectorized representation of the memset value
2711/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002712static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002713 unsigned NumBits = VT.isVector() ?
2714 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002715 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002716 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002717 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002718 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002719 Val = (Val << Shift) | Val;
2720 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002721 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002722 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002723 return DAG.getConstant(Val, VT);
2724 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002725 }
Evan Chengf0df0312008-05-15 08:39:06 +00002726
2727 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2728 unsigned Shift = 8;
2729 for (unsigned i = NumBits; i > 8; i >>= 1) {
2730 Value = DAG.getNode(ISD::OR, VT,
2731 DAG.getNode(ISD::SHL, VT, Value,
2732 DAG.getConstant(Shift, MVT::i8)), Value);
2733 Shift <<= 1;
2734 }
2735
2736 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002737}
2738
Dan Gohman707e0182008-04-12 04:36:06 +00002739/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2740/// used when a memcpy is turned into a memset when the source is a constant
2741/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002742static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002743 const TargetLowering &TLI,
2744 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002745 // Handle vector with all elements zero.
2746 if (Str.empty()) {
2747 if (VT.isInteger())
2748 return DAG.getConstant(0, VT);
2749 unsigned NumElts = VT.getVectorNumElements();
2750 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2751 return DAG.getNode(ISD::BIT_CONVERT, VT,
2752 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2753 }
2754
Duncan Sands83ec4b62008-06-06 12:08:01 +00002755 assert(!VT.isVector() && "Can't handle vector type here!");
2756 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002757 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002758 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002759 if (TLI.isLittleEndian())
2760 Offset = Offset + MSB - 1;
2761 for (unsigned i = 0; i != MSB; ++i) {
2762 Val = (Val << 8) | (unsigned char)Str[Offset];
2763 Offset += TLI.isLittleEndian() ? -1 : 1;
2764 }
2765 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002766}
2767
Dan Gohman707e0182008-04-12 04:36:06 +00002768/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002769///
Dan Gohman475871a2008-07-27 21:46:04 +00002770static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002771 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002772 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002773 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2774}
2775
Evan Chengf0df0312008-05-15 08:39:06 +00002776/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2777///
Dan Gohman475871a2008-07-27 21:46:04 +00002778static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002779 unsigned SrcDelta = 0;
2780 GlobalAddressSDNode *G = NULL;
2781 if (Src.getOpcode() == ISD::GlobalAddress)
2782 G = cast<GlobalAddressSDNode>(Src);
2783 else if (Src.getOpcode() == ISD::ADD &&
2784 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2785 Src.getOperand(1).getOpcode() == ISD::Constant) {
2786 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002787 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002788 }
2789 if (!G)
2790 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002791
Evan Chengf0df0312008-05-15 08:39:06 +00002792 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002793 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2794 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002795
Evan Chengf0df0312008-05-15 08:39:06 +00002796 return false;
2797}
Dan Gohman707e0182008-04-12 04:36:06 +00002798
Evan Chengf0df0312008-05-15 08:39:06 +00002799/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2800/// to replace the memset / memcpy is below the threshold. It also returns the
2801/// types of the sequence of memory ops to perform memset / memcpy.
2802static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002803bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002804 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002805 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002806 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002807 SelectionDAG &DAG,
2808 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002809 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002810 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002811 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002812 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002813 if (VT != MVT::iAny) {
2814 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002815 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002816 // If source is a string constant, this will require an unaligned load.
2817 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2818 if (Dst.getOpcode() != ISD::FrameIndex) {
2819 // Can't change destination alignment. It requires a unaligned store.
2820 if (AllowUnalign)
2821 VT = MVT::iAny;
2822 } else {
2823 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2824 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2825 if (MFI->isFixedObjectIndex(FI)) {
2826 // Can't change destination alignment. It requires a unaligned store.
2827 if (AllowUnalign)
2828 VT = MVT::iAny;
2829 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002830 // Give the stack frame object a larger alignment if needed.
2831 if (MFI->getObjectAlignment(FI) < NewAlign)
2832 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002833 Align = NewAlign;
2834 }
2835 }
2836 }
2837 }
2838
2839 if (VT == MVT::iAny) {
2840 if (AllowUnalign) {
2841 VT = MVT::i64;
2842 } else {
2843 switch (Align & 7) {
2844 case 0: VT = MVT::i64; break;
2845 case 4: VT = MVT::i32; break;
2846 case 2: VT = MVT::i16; break;
2847 default: VT = MVT::i8; break;
2848 }
2849 }
2850
Duncan Sands83ec4b62008-06-06 12:08:01 +00002851 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002852 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002853 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2854 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002855
Duncan Sands8e4eb092008-06-08 20:54:56 +00002856 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002857 VT = LVT;
2858 }
Dan Gohman707e0182008-04-12 04:36:06 +00002859
2860 unsigned NumMemOps = 0;
2861 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002863 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002864 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002865 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002866 VT = MVT::i64;
2867 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2869 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002870 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002871 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002872 VTSize >>= 1;
2873 }
Dan Gohman707e0182008-04-12 04:36:06 +00002874 }
Dan Gohman707e0182008-04-12 04:36:06 +00002875
2876 if (++NumMemOps > Limit)
2877 return false;
2878 MemOps.push_back(VT);
2879 Size -= VTSize;
2880 }
2881
2882 return true;
2883}
2884
Dan Gohman475871a2008-07-27 21:46:04 +00002885static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2886 SDValue Chain, SDValue Dst,
2887 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002888 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002889 const Value *DstSV, uint64_t DstSVOff,
2890 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002891 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2892
Dan Gohman21323f32008-05-29 19:42:22 +00002893 // Expand memcpy to a series of load and store ops if the size operand falls
2894 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002896 uint64_t Limit = -1;
2897 if (!AlwaysInline)
2898 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002899 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002900 std::string Str;
2901 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002902 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002903 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002904 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002905
Dan Gohman707e0182008-04-12 04:36:06 +00002906
Evan Cheng0ff39b32008-06-30 07:31:25 +00002907 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002909 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002910 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002911 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 Gohman475871a2008-07-27 21:46:04 +00002914 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002915
Evan Cheng0ff39b32008-06-30 07:31:25 +00002916 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002917 // It's unlikely a store of a vector immediate can be done in a single
2918 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002919 // We also handle store a vector with all zero's.
2920 // FIXME: Handle other cases where store of vector immediate is done in
2921 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002922 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002923 Store = DAG.getStore(Chain, Value,
2924 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002925 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002926 } else {
2927 Value = DAG.getLoad(VT, Chain,
2928 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002929 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002930 Store = DAG.getStore(Chain, Value,
2931 getMemBasePlusOffset(Dst, DstOff, DAG),
2932 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002933 }
2934 OutChains.push_back(Store);
2935 SrcOff += VTSize;
2936 DstOff += VTSize;
2937 }
2938
2939 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2940 &OutChains[0], OutChains.size());
2941}
2942
Dan Gohman475871a2008-07-27 21:46:04 +00002943static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2944 SDValue Chain, SDValue Dst,
2945 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002946 unsigned Align, bool AlwaysInline,
2947 const Value *DstSV, uint64_t DstSVOff,
2948 const Value *SrcSV, uint64_t SrcSVOff){
2949 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2950
2951 // Expand memmove to a series of load and store ops if the size operand falls
2952 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002953 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002954 uint64_t Limit = -1;
2955 if (!AlwaysInline)
2956 Limit = TLI.getMaxStoresPerMemmove();
2957 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002958 std::string Str;
2959 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002960 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002961 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002962 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002963
Dan Gohman21323f32008-05-29 19:42:22 +00002964 uint64_t SrcOff = 0, DstOff = 0;
2965
Dan Gohman475871a2008-07-27 21:46:04 +00002966 SmallVector<SDValue, 8> LoadValues;
2967 SmallVector<SDValue, 8> LoadChains;
2968 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002969 unsigned NumMemOps = MemOps.size();
2970 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002971 MVT VT = MemOps[i];
2972 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002973 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002974
2975 Value = DAG.getLoad(VT, Chain,
2976 getMemBasePlusOffset(Src, SrcOff, DAG),
2977 SrcSV, SrcSVOff + SrcOff, false, Align);
2978 LoadValues.push_back(Value);
2979 LoadChains.push_back(Value.getValue(1));
2980 SrcOff += VTSize;
2981 }
2982 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2983 &LoadChains[0], LoadChains.size());
2984 OutChains.clear();
2985 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 MVT VT = MemOps[i];
2987 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002988 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002989
2990 Store = DAG.getStore(Chain, LoadValues[i],
2991 getMemBasePlusOffset(Dst, DstOff, DAG),
2992 DstSV, DstSVOff + DstOff, false, DstAlign);
2993 OutChains.push_back(Store);
2994 DstOff += VTSize;
2995 }
2996
2997 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2998 &OutChains[0], OutChains.size());
2999}
3000
Dan Gohman475871a2008-07-27 21:46:04 +00003001static SDValue getMemsetStores(SelectionDAG &DAG,
3002 SDValue Chain, SDValue Dst,
3003 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003004 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003005 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003006 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3007
3008 // Expand memset to a series of load/store ops if the size operand
3009 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003010 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003011 std::string Str;
3012 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003013 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003014 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003015 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003016
Dan Gohman475871a2008-07-27 21:46:04 +00003017 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003018 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003019
3020 unsigned NumMemOps = MemOps.size();
3021 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003022 MVT VT = MemOps[i];
3023 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003024 SDValue Value = getMemsetValue(Src, VT, DAG);
3025 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00003026 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003027 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003028 OutChains.push_back(Store);
3029 DstOff += VTSize;
3030 }
3031
3032 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3033 &OutChains[0], OutChains.size());
3034}
3035
Dan Gohman475871a2008-07-27 21:46:04 +00003036SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3037 SDValue Src, SDValue Size,
3038 unsigned Align, bool AlwaysInline,
3039 const Value *DstSV, uint64_t DstSVOff,
3040 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003041
3042 // Check to see if we should lower the memcpy to loads and stores first.
3043 // For cases within the target-specified limits, this is the best choice.
3044 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3045 if (ConstantSize) {
3046 // Memcpy with size zero? Just return the original chain.
3047 if (ConstantSize->isNullValue())
3048 return Chain;
3049
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003051 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3052 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003053 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003054 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003055 return Result;
3056 }
3057
3058 // Then check to see if we should lower the memcpy with target-specific
3059 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003060 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003061 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3062 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003063 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003064 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003065 return Result;
3066
3067 // If we really need inline code and the target declined to provide it,
3068 // use a (potentially long) sequence of loads and stores.
3069 if (AlwaysInline) {
3070 assert(ConstantSize && "AlwaysInline requires a constant size!");
3071 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003072 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003073 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003074 }
3075
3076 // Emit a library call.
3077 TargetLowering::ArgListTy Args;
3078 TargetLowering::ArgListEntry Entry;
3079 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3080 Entry.Node = Dst; Args.push_back(Entry);
3081 Entry.Node = Src; Args.push_back(Entry);
3082 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003083 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003084 TLI.LowerCallTo(Chain, Type::VoidTy,
3085 false, false, false, CallingConv::C, false,
3086 getExternalSymbol("memcpy", TLI.getPointerTy()),
3087 Args, *this);
3088 return CallResult.second;
3089}
3090
Dan Gohman475871a2008-07-27 21:46:04 +00003091SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3092 SDValue Src, SDValue Size,
3093 unsigned Align,
3094 const Value *DstSV, uint64_t DstSVOff,
3095 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003096
Dan Gohman21323f32008-05-29 19:42:22 +00003097 // Check to see if we should lower the memmove to loads and stores first.
3098 // For cases within the target-specified limits, this is the best choice.
3099 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3100 if (ConstantSize) {
3101 // Memmove with size zero? Just return the original chain.
3102 if (ConstantSize->isNullValue())
3103 return Chain;
3104
Dan Gohman475871a2008-07-27 21:46:04 +00003105 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003106 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3107 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003108 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003109 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003110 return Result;
3111 }
Dan Gohman707e0182008-04-12 04:36:06 +00003112
3113 // Then check to see if we should lower the memmove with target-specific
3114 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003115 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003116 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003117 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003118 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003119 return Result;
3120
3121 // Emit a library call.
3122 TargetLowering::ArgListTy Args;
3123 TargetLowering::ArgListEntry Entry;
3124 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3125 Entry.Node = Dst; Args.push_back(Entry);
3126 Entry.Node = Src; Args.push_back(Entry);
3127 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003128 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003129 TLI.LowerCallTo(Chain, Type::VoidTy,
3130 false, false, false, CallingConv::C, false,
3131 getExternalSymbol("memmove", TLI.getPointerTy()),
3132 Args, *this);
3133 return CallResult.second;
3134}
3135
Dan Gohman475871a2008-07-27 21:46:04 +00003136SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3137 SDValue Src, SDValue Size,
3138 unsigned Align,
3139 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003140
3141 // Check to see if we should lower the memset to stores first.
3142 // For cases within the target-specified limits, this is the best choice.
3143 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3144 if (ConstantSize) {
3145 // Memset with size zero? Just return the original chain.
3146 if (ConstantSize->isNullValue())
3147 return Chain;
3148
Dan Gohman475871a2008-07-27 21:46:04 +00003149 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003150 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3151 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003152 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003153 return Result;
3154 }
3155
3156 // Then check to see if we should lower the memset with target-specific
3157 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003158 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003159 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003160 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003161 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003162 return Result;
3163
3164 // Emit a library call.
3165 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3166 TargetLowering::ArgListTy Args;
3167 TargetLowering::ArgListEntry Entry;
3168 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3169 Args.push_back(Entry);
3170 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003171 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003172 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3173 else
3174 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3175 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3176 Args.push_back(Entry);
3177 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3178 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003179 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003180 TLI.LowerCallTo(Chain, Type::VoidTy,
3181 false, false, false, CallingConv::C, false,
3182 getExternalSymbol("memset", TLI.getPointerTy()),
3183 Args, *this);
3184 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003185}
3186
Dan Gohman475871a2008-07-27 21:46:04 +00003187SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3188 SDValue Ptr, SDValue Cmp,
3189 SDValue Swp, const Value* PtrVal,
3190 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003191 assert((Opcode == ISD::ATOMIC_CMP_SWAP_8 ||
3192 Opcode == ISD::ATOMIC_CMP_SWAP_16 ||
3193 Opcode == ISD::ATOMIC_CMP_SWAP_32 ||
3194 Opcode == ISD::ATOMIC_CMP_SWAP_64) && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003195 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003196
3197 MVT VT = Cmp.getValueType();
3198
3199 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3200 Alignment = getMVTAlignment(VT);
3201
3202 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003203 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003205 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003206 void* IP = 0;
3207 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003208 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003209 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003210 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003211 CSEMap.InsertNode(N, IP);
3212 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003213 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003214}
3215
Dan Gohman475871a2008-07-27 21:46:04 +00003216SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3217 SDValue Ptr, SDValue Val,
3218 const Value* PtrVal,
3219 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003220 assert((Opcode == ISD::ATOMIC_LOAD_ADD_8 ||
3221 Opcode == ISD::ATOMIC_LOAD_SUB_8 ||
3222 Opcode == ISD::ATOMIC_LOAD_AND_8 ||
3223 Opcode == ISD::ATOMIC_LOAD_OR_8 ||
3224 Opcode == ISD::ATOMIC_LOAD_XOR_8 ||
3225 Opcode == ISD::ATOMIC_LOAD_NAND_8 ||
3226 Opcode == ISD::ATOMIC_LOAD_MIN_8 ||
3227 Opcode == ISD::ATOMIC_LOAD_MAX_8 ||
3228 Opcode == ISD::ATOMIC_LOAD_UMIN_8 ||
3229 Opcode == ISD::ATOMIC_LOAD_UMAX_8 ||
3230 Opcode == ISD::ATOMIC_SWAP_8 ||
3231 Opcode == ISD::ATOMIC_LOAD_ADD_16 ||
3232 Opcode == ISD::ATOMIC_LOAD_SUB_16 ||
3233 Opcode == ISD::ATOMIC_LOAD_AND_16 ||
3234 Opcode == ISD::ATOMIC_LOAD_OR_16 ||
3235 Opcode == ISD::ATOMIC_LOAD_XOR_16 ||
3236 Opcode == ISD::ATOMIC_LOAD_NAND_16 ||
3237 Opcode == ISD::ATOMIC_LOAD_MIN_16 ||
3238 Opcode == ISD::ATOMIC_LOAD_MAX_16 ||
3239 Opcode == ISD::ATOMIC_LOAD_UMIN_16 ||
3240 Opcode == ISD::ATOMIC_LOAD_UMAX_16 ||
3241 Opcode == ISD::ATOMIC_SWAP_16 ||
3242 Opcode == ISD::ATOMIC_LOAD_ADD_32 ||
3243 Opcode == ISD::ATOMIC_LOAD_SUB_32 ||
3244 Opcode == ISD::ATOMIC_LOAD_AND_32 ||
3245 Opcode == ISD::ATOMIC_LOAD_OR_32 ||
3246 Opcode == ISD::ATOMIC_LOAD_XOR_32 ||
3247 Opcode == ISD::ATOMIC_LOAD_NAND_32 ||
3248 Opcode == ISD::ATOMIC_LOAD_MIN_32 ||
3249 Opcode == ISD::ATOMIC_LOAD_MAX_32 ||
3250 Opcode == ISD::ATOMIC_LOAD_UMIN_32 ||
3251 Opcode == ISD::ATOMIC_LOAD_UMAX_32 ||
3252 Opcode == ISD::ATOMIC_SWAP_32 ||
3253 Opcode == ISD::ATOMIC_LOAD_ADD_64 ||
3254 Opcode == ISD::ATOMIC_LOAD_SUB_64 ||
3255 Opcode == ISD::ATOMIC_LOAD_AND_64 ||
3256 Opcode == ISD::ATOMIC_LOAD_OR_64 ||
3257 Opcode == ISD::ATOMIC_LOAD_XOR_64 ||
3258 Opcode == ISD::ATOMIC_LOAD_NAND_64 ||
3259 Opcode == ISD::ATOMIC_LOAD_MIN_64 ||
3260 Opcode == ISD::ATOMIC_LOAD_MAX_64 ||
3261 Opcode == ISD::ATOMIC_LOAD_UMIN_64 ||
3262 Opcode == ISD::ATOMIC_LOAD_UMAX_64 ||
3263 Opcode == ISD::ATOMIC_SWAP_64) && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003264
3265 MVT VT = Val.getValueType();
3266
3267 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3268 Alignment = getMVTAlignment(VT);
3269
3270 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003271 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003272 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003273 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003274 void* IP = 0;
3275 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003276 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003277 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003278 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003279 CSEMap.InsertNode(N, IP);
3280 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003281 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003282}
3283
Duncan Sands4bdcb612008-07-02 17:40:58 +00003284/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3285/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003286SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3287 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003288 if (Simplify && NumOps == 1)
3289 return Ops[0];
3290
3291 SmallVector<MVT, 4> VTs;
3292 VTs.reserve(NumOps);
3293 for (unsigned i = 0; i < NumOps; ++i)
3294 VTs.push_back(Ops[i].getValueType());
3295 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3296}
3297
Dan Gohman475871a2008-07-27 21:46:04 +00003298SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003299SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003300 MVT VT, SDValue Chain,
3301 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003302 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003303 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003304 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3305 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003306
Duncan Sands14ea39c2008-03-27 20:23:40 +00003307 if (VT == EVT) {
3308 ExtType = ISD::NON_EXTLOAD;
3309 } else if (ExtType == ISD::NON_EXTLOAD) {
3310 assert(VT == EVT && "Non-extending load from different memory type!");
3311 } else {
3312 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003313 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003314 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3315 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003316 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003317 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003318 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003319 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003320 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003321 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003322 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003323 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003324
3325 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003326 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003327 "Unindexed load with an offset!");
3328
3329 SDVTList VTs = Indexed ?
3330 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003331 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003332 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003333 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003334 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003335 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003336 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003337 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003338 void *IP = 0;
3339 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003340 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003341 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003342 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3343 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003344 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003345 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003346 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003347}
3348
Dan Gohman475871a2008-07-27 21:46:04 +00003349SDValue SelectionDAG::getLoad(MVT VT,
3350 SDValue Chain, SDValue Ptr,
3351 const Value *SV, int SVOffset,
3352 bool isVolatile, unsigned Alignment) {
3353 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003354 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3355 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003356}
3357
Dan Gohman475871a2008-07-27 21:46:04 +00003358SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3359 SDValue Chain, SDValue Ptr,
3360 const Value *SV,
3361 int SVOffset, MVT EVT,
3362 bool isVolatile, unsigned Alignment) {
3363 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003364 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3365 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003366}
3367
Dan Gohman475871a2008-07-27 21:46:04 +00003368SDValue
3369SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3370 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003371 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003372 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3373 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003374 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3375 LD->getChain(), Base, Offset, LD->getSrcValue(),
3376 LD->getSrcValueOffset(), LD->getMemoryVT(),
3377 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003378}
3379
Dan Gohman475871a2008-07-27 21:46:04 +00003380SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3381 SDValue Ptr, const Value *SV, int SVOffset,
3382 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003383 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003384
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003385 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3386 Alignment = getMVTAlignment(VT);
3387
Evan Chengad071e12006-10-05 22:57:11 +00003388 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003389 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3390 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003391 FoldingSetNodeID ID;
3392 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003393 ID.AddInteger(ISD::UNINDEXED);
3394 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003395 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003396 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003397 void *IP = 0;
3398 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003399 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003400 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003401 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3402 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003403 CSEMap.InsertNode(N, IP);
3404 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003405 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003406}
3407
Dan Gohman475871a2008-07-27 21:46:04 +00003408SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3409 SDValue Ptr, const Value *SV,
3410 int SVOffset, MVT SVT,
3411 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003413
3414 if (VT == SVT)
3415 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003416
Duncan Sands8e4eb092008-06-08 20:54:56 +00003417 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003419 "Can't do FP-INT conversion!");
3420
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003421 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3422 Alignment = getMVTAlignment(VT);
3423
Evan Cheng8b2794a2006-10-13 21:14:26 +00003424 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003425 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3426 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003427 FoldingSetNodeID ID;
3428 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003429 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003430 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003431 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003432 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003433 void *IP = 0;
3434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003435 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003436 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003437 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3438 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003439 CSEMap.InsertNode(N, IP);
3440 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003441 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003442}
3443
Dan Gohman475871a2008-07-27 21:46:04 +00003444SDValue
3445SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3446 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003447 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3448 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3449 "Store is already a indexed store!");
3450 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003452 FoldingSetNodeID ID;
3453 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3454 ID.AddInteger(AM);
3455 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003456 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003457 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003458 void *IP = 0;
3459 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003460 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003461 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003462 new (N) StoreSDNode(Ops, VTs, AM,
3463 ST->isTruncatingStore(), ST->getMemoryVT(),
3464 ST->getSrcValue(), ST->getSrcValueOffset(),
3465 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003466 CSEMap.InsertNode(N, IP);
3467 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003468 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003469}
3470
Dan Gohman475871a2008-07-27 21:46:04 +00003471SDValue SelectionDAG::getVAArg(MVT VT,
3472 SDValue Chain, SDValue Ptr,
3473 SDValue SV) {
3474 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003475 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003476}
3477
Dan Gohman475871a2008-07-27 21:46:04 +00003478SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3479 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003480 switch (NumOps) {
3481 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003482 case 1: return getNode(Opcode, VT, Ops[0]);
3483 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3484 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003485 default: break;
3486 }
3487
Dan Gohman475871a2008-07-27 21:46:04 +00003488 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003489 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003490 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3491 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003492}
3493
Dan Gohman475871a2008-07-27 21:46:04 +00003494SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3495 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003496 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003497 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003498 case 1: return getNode(Opcode, VT, Ops[0]);
3499 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3500 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003501 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003502 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003503
Chris Lattneref847df2005-04-09 03:27:28 +00003504 switch (Opcode) {
3505 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003506 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003507 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003508 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3509 "LHS and RHS of condition must have same type!");
3510 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3511 "True and False arms of SelectCC must have same type!");
3512 assert(Ops[2].getValueType() == VT &&
3513 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003514 break;
3515 }
3516 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003517 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003518 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3519 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003520 break;
3521 }
Chris Lattneref847df2005-04-09 03:27:28 +00003522 }
3523
Chris Lattner385328c2005-05-14 07:42:29 +00003524 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003525 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003526 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003527 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003528 FoldingSetNodeID ID;
3529 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003530 void *IP = 0;
3531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003532 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003533 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003534 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003535 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003536 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003537 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003538 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003539 }
Chris Lattneref847df2005-04-09 03:27:28 +00003540 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003541#ifndef NDEBUG
3542 VerifyNode(N);
3543#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003544 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003545}
3546
Dan Gohman475871a2008-07-27 21:46:04 +00003547SDValue SelectionDAG::getNode(unsigned Opcode,
3548 const std::vector<MVT> &ResultTys,
3549 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003550 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3551 Ops, NumOps);
3552}
3553
Dan Gohman475871a2008-07-27 21:46:04 +00003554SDValue SelectionDAG::getNode(unsigned Opcode,
3555 const MVT *VTs, unsigned NumVTs,
3556 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003557 if (NumVTs == 1)
3558 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003559 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3560}
3561
Dan Gohman475871a2008-07-27 21:46:04 +00003562SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3563 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003564 if (VTList.NumVTs == 1)
3565 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003566
Chris Lattner5f056bf2005-07-10 01:55:33 +00003567 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003568 // FIXME: figure out how to safely handle things like
3569 // int foo(int x) { return 1 << (x & 255); }
3570 // int bar() { return foo(256); }
3571#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003572 case ISD::SRA_PARTS:
3573 case ISD::SRL_PARTS:
3574 case ISD::SHL_PARTS:
3575 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003576 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003577 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3578 else if (N3.getOpcode() == ISD::AND)
3579 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3580 // If the and is only masking out bits that cannot effect the shift,
3581 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003582 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003583 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3584 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3585 }
3586 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003587#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003588 }
Chris Lattner89c34632005-05-14 06:20:26 +00003589
Chris Lattner43247a12005-08-25 19:12:10 +00003590 // Memoize the node unless it returns a flag.
3591 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003592 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003593 FoldingSetNodeID ID;
3594 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003595 void *IP = 0;
3596 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003597 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003598 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003599 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003600 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3601 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003602 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003603 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3604 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003605 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003606 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3607 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003608 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003609 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3610 }
Chris Lattnera5682852006-08-07 23:03:03 +00003611 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003612 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003613 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003614 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003615 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3616 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003617 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003618 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3619 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003620 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003621 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3622 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003623 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003624 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3625 }
Chris Lattner43247a12005-08-25 19:12:10 +00003626 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003627 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003628#ifndef NDEBUG
3629 VerifyNode(N);
3630#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003631 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003632}
3633
Dan Gohman475871a2008-07-27 21:46:04 +00003634SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003635 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003636}
3637
Dan Gohman475871a2008-07-27 21:46:04 +00003638SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3639 SDValue N1) {
3640 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003641 return getNode(Opcode, VTList, Ops, 1);
3642}
3643
Dan Gohman475871a2008-07-27 21:46:04 +00003644SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3645 SDValue N1, SDValue N2) {
3646 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003647 return getNode(Opcode, VTList, Ops, 2);
3648}
3649
Dan Gohman475871a2008-07-27 21:46:04 +00003650SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3651 SDValue N1, SDValue N2, SDValue N3) {
3652 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003653 return getNode(Opcode, VTList, Ops, 3);
3654}
3655
Dan Gohman475871a2008-07-27 21:46:04 +00003656SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3657 SDValue N1, SDValue N2, SDValue N3,
3658 SDValue N4) {
3659 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003660 return getNode(Opcode, VTList, Ops, 4);
3661}
3662
Dan Gohman475871a2008-07-27 21:46:04 +00003663SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3664 SDValue N1, SDValue N2, SDValue N3,
3665 SDValue N4, SDValue N5) {
3666 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003667 return getNode(Opcode, VTList, Ops, 5);
3668}
3669
Duncan Sands83ec4b62008-06-06 12:08:01 +00003670SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003671 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003672}
3673
Duncan Sands83ec4b62008-06-06 12:08:01 +00003674SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003675 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3676 E = VTList.rend(); I != E; ++I)
3677 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3678 return *I;
3679
3680 MVT *Array = Allocator.Allocate<MVT>(2);
3681 Array[0] = VT1;
3682 Array[1] = VT2;
3683 SDVTList Result = makeVTList(Array, 2);
3684 VTList.push_back(Result);
3685 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003686}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003687
3688SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3689 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3690 E = VTList.rend(); I != E; ++I)
3691 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3692 I->VTs[2] == VT3)
3693 return *I;
3694
3695 MVT *Array = Allocator.Allocate<MVT>(3);
3696 Array[0] = VT1;
3697 Array[1] = VT2;
3698 Array[2] = VT3;
3699 SDVTList Result = makeVTList(Array, 3);
3700 VTList.push_back(Result);
3701 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003702}
3703
Duncan Sands83ec4b62008-06-06 12:08:01 +00003704SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003705 switch (NumVTs) {
3706 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003707 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003708 case 2: return getVTList(VTs[0], VTs[1]);
3709 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3710 default: break;
3711 }
3712
Dan Gohmane8be6c62008-07-17 19:10:17 +00003713 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3714 E = VTList.rend(); I != E; ++I) {
3715 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3716 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003717
3718 bool NoMatch = false;
3719 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003720 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003721 NoMatch = true;
3722 break;
3723 }
3724 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003725 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003726 }
3727
Dan Gohmane8be6c62008-07-17 19:10:17 +00003728 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3729 std::copy(VTs, VTs+NumVTs, Array);
3730 SDVTList Result = makeVTList(Array, NumVTs);
3731 VTList.push_back(Result);
3732 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003733}
3734
3735
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003736/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3737/// specified operands. If the resultant node already exists in the DAG,
3738/// this does not modify the specified node, instead it returns the node that
3739/// already exists. If the resultant node does not exist in the DAG, the
3740/// input node is returned. As a degenerate case, if you specify the same
3741/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003742SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003743 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003744 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3745
3746 // Check to see if there is no change.
3747 if (Op == N->getOperand(0)) return InN;
3748
3749 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003750 void *InsertPos = 0;
3751 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003752 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003753
Dan Gohman79acd2b2008-07-21 22:38:59 +00003754 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003755 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003756 RemoveNodeFromCSEMaps(N);
3757
3758 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003759 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003760 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003761 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003762 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003763
3764 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003765 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003766 return InN;
3767}
3768
Dan Gohman475871a2008-07-27 21:46:04 +00003769SDValue SelectionDAG::
3770UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003771 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003772 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3773
3774 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003775 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3776 return InN; // No operands changed, just return the input node.
3777
3778 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003779 void *InsertPos = 0;
3780 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003781 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003782
Dan Gohman79acd2b2008-07-21 22:38:59 +00003783 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003784 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003785 RemoveNodeFromCSEMaps(N);
3786
3787 // Now we update the operands.
3788 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003789 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003790 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003791 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003792 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003793 }
3794 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003795 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003796 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003797 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003798 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003799 }
3800
3801 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003802 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003803 return InN;
3804}
3805
Dan Gohman475871a2008-07-27 21:46:04 +00003806SDValue SelectionDAG::
3807UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3808 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003809 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003810}
3811
Dan Gohman475871a2008-07-27 21:46:04 +00003812SDValue SelectionDAG::
3813UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3814 SDValue Op3, SDValue Op4) {
3815 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003816 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003817}
3818
Dan Gohman475871a2008-07-27 21:46:04 +00003819SDValue SelectionDAG::
3820UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3821 SDValue Op3, SDValue Op4, SDValue Op5) {
3822 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003823 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003824}
3825
Dan Gohman475871a2008-07-27 21:46:04 +00003826SDValue SelectionDAG::
3827UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003828 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003829 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003830 "Update with wrong number of operands");
3831
3832 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003833 bool AnyChange = false;
3834 for (unsigned i = 0; i != NumOps; ++i) {
3835 if (Ops[i] != N->getOperand(i)) {
3836 AnyChange = true;
3837 break;
3838 }
3839 }
3840
3841 // No operands changed, just return the input node.
3842 if (!AnyChange) return InN;
3843
3844 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003845 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003846 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003847 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003848
Dan Gohman7ceda162008-05-02 00:05:03 +00003849 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003850 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003851 RemoveNodeFromCSEMaps(N);
3852
3853 // Now we update the operands.
3854 for (unsigned i = 0; i != NumOps; ++i) {
3855 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003856 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003857 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003858 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003859 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003860 }
3861 }
3862
3863 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003864 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003865 return InN;
3866}
3867
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003868/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003869/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003870void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003871 // Unlike the code in MorphNodeTo that does this, we don't need to
3872 // watch for dead nodes here.
3873 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3874 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3875
3876 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003877}
Chris Lattner149c58c2005-08-16 18:17:10 +00003878
Dan Gohmane8be6c62008-07-17 19:10:17 +00003879/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3880/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003881///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003882SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003883 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003884 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003885 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003886}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003887
Dan Gohmane8be6c62008-07-17 19:10:17 +00003888SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003889 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003890 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003891 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003892 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003893}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003894
Dan Gohmane8be6c62008-07-17 19:10:17 +00003895SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003896 MVT VT, SDValue Op1,
3897 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003898 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003900 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003901}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003902
Dan Gohmane8be6c62008-07-17 19:10:17 +00003903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003904 MVT VT, SDValue Op1,
3905 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003906 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003909}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003910
Dan Gohmane8be6c62008-07-17 19:10:17 +00003911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003912 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003913 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003914 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003915 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003916}
3917
Dan Gohmane8be6c62008-07-17 19:10:17 +00003918SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003919 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003920 unsigned NumOps) {
3921 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003922 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003923}
3924
Dan Gohmane8be6c62008-07-17 19:10:17 +00003925SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003926 MVT VT1, MVT VT2) {
3927 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003928 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003929}
3930
Dan Gohmane8be6c62008-07-17 19:10:17 +00003931SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003932 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003933 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003934 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003935 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003936}
3937
Dan Gohmane8be6c62008-07-17 19:10:17 +00003938SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003939 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003941 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003942 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003943 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003944}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003945
Dan Gohmane8be6c62008-07-17 19:10:17 +00003946SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003949 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003952}
3953
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003955 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue Op1, SDValue Op2,
3957 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003958 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003960 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003961}
3962
Dan Gohmane8be6c62008-07-17 19:10:17 +00003963SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003964 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003965 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003966 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3967}
3968
3969SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3970 MVT VT) {
3971 SDVTList VTs = getVTList(VT);
3972 return MorphNodeTo(N, Opc, VTs, 0, 0);
3973}
3974
3975SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003976 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003977 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003978 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003979 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3980}
3981
3982SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003983 MVT VT, SDValue Op1,
3984 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003985 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003986 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003987 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3988}
3989
3990SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003991 MVT VT, SDValue Op1,
3992 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003993 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003994 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003995 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3996}
3997
3998SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003999 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004000 unsigned NumOps) {
4001 SDVTList VTs = getVTList(VT);
4002 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4003}
4004
4005SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004006 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004007 unsigned NumOps) {
4008 SDVTList VTs = getVTList(VT1, VT2);
4009 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4010}
4011
4012SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4013 MVT VT1, MVT VT2) {
4014 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004015 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004016}
4017
4018SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4019 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004020 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004021 SDVTList VTs = getVTList(VT1, VT2, VT3);
4022 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4023}
4024
4025SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4026 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004027 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004028 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004029 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004030 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4031}
4032
4033SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4034 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004035 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004036 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004037 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4039}
4040
4041SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4042 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Op1, SDValue Op2,
4044 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004045 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004046 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004047 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4048}
4049
4050/// MorphNodeTo - These *mutate* the specified node to have the specified
4051/// return type, opcode, and operands.
4052///
4053/// Note that MorphNodeTo returns the resultant node. If there is already a
4054/// node of the specified opcode and operands, it returns that node instead of
4055/// the current one.
4056///
4057/// Using MorphNodeTo is faster than creating a new node and swapping it in
4058/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004059/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004060/// the node's users.
4061///
4062SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004063 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004065 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004066 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004067 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4068 FoldingSetNodeID ID;
4069 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4070 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4071 return ON;
4072 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004073
Chris Lattner0fb094f2005-11-19 01:44:53 +00004074 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00004075
Dan Gohmane8be6c62008-07-17 19:10:17 +00004076 // Start the morphing.
4077 N->NodeType = Opc;
4078 N->ValueList = VTs.VTs;
4079 N->NumValues = VTs.NumVTs;
4080
4081 // Clear the operands list, updating used nodes to remove this from their
4082 // use list. Keep track of any operands that become dead as a result.
4083 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4084 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4085 I != E; ++I) {
4086 SDNode *Used = I->getVal();
4087 Used->removeUser(std::distance(B, I), N);
4088 if (Used->use_empty())
4089 DeadNodeSet.insert(Used);
4090 }
4091
4092 // If NumOps is larger than the # of operands we currently have, reallocate
4093 // the operand list.
4094 if (NumOps > N->NumOperands) {
4095 if (N->OperandsNeedDelete)
4096 delete[] N->OperandList;
4097 if (N->isMachineOpcode()) {
4098 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004099 // remainder of the current SelectionDAG iteration, so we can allocate
4100 // the operands directly out of a pool with no recycling metadata.
4101 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004102 N->OperandsNeedDelete = false;
4103 } else {
4104 N->OperandList = new SDUse[NumOps];
4105 N->OperandsNeedDelete = true;
4106 }
4107 }
4108
4109 // Assign the new operands.
4110 N->NumOperands = NumOps;
4111 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4112 N->OperandList[i] = Ops[i];
4113 N->OperandList[i].setUser(N);
4114 SDNode *ToUse = N->OperandList[i].getVal();
4115 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004116 }
4117
4118 // Delete any nodes that are still dead after adding the uses for the
4119 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004120 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004121 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4122 E = DeadNodeSet.end(); I != E; ++I)
4123 if ((*I)->use_empty())
4124 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004125 RemoveDeadNodes(DeadNodes);
4126
Dan Gohmane8be6c62008-07-17 19:10:17 +00004127 if (IP)
4128 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004129 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004130}
4131
Chris Lattner0fb094f2005-11-19 01:44:53 +00004132
Evan Cheng6ae46c42006-02-09 07:15:23 +00004133/// getTargetNode - These are used for target selectors to create a new node
4134/// with specified return type(s), target opcode, and operands.
4135///
4136/// Note that getTargetNode returns the resultant node. If there is already a
4137/// node of the specified opcode and operands, it returns that node instead of
4138/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004139SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004140 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004141}
Dan Gohman475871a2008-07-27 21:46:04 +00004142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004143 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004144}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004145SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004146 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004147 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004148}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004150 SDValue Op1, SDValue Op2,
4151 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004152 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004153}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004154SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004155 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004156 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004157}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4159 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004161 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004162}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004163SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004164 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004165 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004166 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004167}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004168SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004169 MVT VT2, SDValue Op1,
4170 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004171 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004172 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004173 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004174}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004175SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004176 MVT VT2, SDValue Op1,
4177 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004178 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004179 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004180 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004181}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004182SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004183 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004184 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004185 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004186}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004187SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004188 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004189 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004190 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004191 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004192}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 SDValue Op1, SDValue Op2,
4195 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004196 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004197 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004198 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004199}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004200SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004201 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004202 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004203 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004204}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004205SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4206 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004207 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004209 VTList.push_back(VT1);
4210 VTList.push_back(VT2);
4211 VTList.push_back(VT3);
4212 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004213 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004214 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004215}
Evan Cheng39305cf2007-10-05 01:10:49 +00004216SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004217 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004218 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004219 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004220 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004221 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004222}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004223
Evan Cheng08b11732008-03-22 01:55:50 +00004224/// getNodeIfExists - Get the specified node if it's already available, or
4225/// else return NULL.
4226SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004227 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004228 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4229 FoldingSetNodeID ID;
4230 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4231 void *IP = 0;
4232 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4233 return E;
4234 }
4235 return NULL;
4236}
4237
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004238
Evan Cheng99157a02006-08-07 22:13:29 +00004239/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004240/// This can cause recursive merging of nodes in the DAG.
4241///
Chris Lattner11d049c2008-02-03 03:35:22 +00004242/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004243///
Dan Gohman475871a2008-07-27 21:46:04 +00004244void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004245 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004246 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004247 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004248 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004249 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004250
Chris Lattner8b8749f2005-08-17 19:00:20 +00004251 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004252 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004253 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004254
Chris Lattner8b8749f2005-08-17 19:00:20 +00004255 // This node is about to morph, remove its old self from the CSE maps.
4256 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004257 int operandNum = 0;
4258 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4259 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004260 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004261 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004262 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004263 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004264 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004265 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004266
4267 // Now that we have modified U, add it back to the CSE maps. If it already
4268 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004269 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004270 ReplaceAllUsesWith(U, Existing, UpdateListener);
4271 // U is now dead. Inform the listener if it exists and delete it.
4272 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004273 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004274 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004275 } else {
4276 // If the node doesn't already exist, we updated it. Inform a listener if
4277 // it exists.
4278 if (UpdateListener)
4279 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004280 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004281 }
4282}
4283
4284/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4285/// This can cause recursive merging of nodes in the DAG.
4286///
4287/// This version assumes From/To have matching types and numbers of result
4288/// values.
4289///
Chris Lattner1e111c72005-09-07 05:37:01 +00004290void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004291 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004292 assert(From->getVTList().VTs == To->getVTList().VTs &&
4293 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004294 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004295
4296 // Handle the trivial case.
4297 if (From == To)
4298 return;
4299
Chris Lattner8b52f212005-08-26 18:36:28 +00004300 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004301 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004302 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004303
Chris Lattner8b52f212005-08-26 18:36:28 +00004304 // This node is about to morph, remove its old self from the CSE maps.
4305 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004306 int operandNum = 0;
4307 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4308 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004309 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004310 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004311 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004312 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004313 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004314
Chris Lattner8b8749f2005-08-17 19:00:20 +00004315 // Now that we have modified U, add it back to the CSE maps. If it already
4316 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004317 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004318 ReplaceAllUsesWith(U, Existing, UpdateListener);
4319 // U is now dead. Inform the listener if it exists and delete it.
4320 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004321 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004322 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004323 } else {
4324 // If the node doesn't already exist, we updated it. Inform a listener if
4325 // it exists.
4326 if (UpdateListener)
4327 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004328 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004329 }
4330}
4331
Chris Lattner8b52f212005-08-26 18:36:28 +00004332/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4333/// This can cause recursive merging of nodes in the DAG.
4334///
4335/// This version can replace From with any result values. To must match the
4336/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004337void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004338 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004339 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004340 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004341 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004342
4343 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004344 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004345 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004346
Chris Lattner7b2880c2005-08-24 22:44:39 +00004347 // This node is about to morph, remove its old self from the CSE maps.
4348 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004349 int operandNum = 0;
4350 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4351 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004352 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004353 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004354 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004355 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004356 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004357 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004358 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004359
Chris Lattner7b2880c2005-08-24 22:44:39 +00004360 // Now that we have modified U, add it back to the CSE maps. If it already
4361 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004362 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004363 ReplaceAllUsesWith(U, Existing, UpdateListener);
4364 // U is now dead. Inform the listener if it exists and delete it.
4365 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004366 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004367 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004368 } else {
4369 // If the node doesn't already exist, we updated it. Inform a listener if
4370 // it exists.
4371 if (UpdateListener)
4372 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004373 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004374 }
4375}
4376
Chris Lattner012f2412006-02-17 21:58:01 +00004377/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004378/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004379/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004380void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004381 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004382 // Handle the really simple, really trivial case efficiently.
4383 if (From == To) return;
4384
Chris Lattner012f2412006-02-17 21:58:01 +00004385 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004386 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004387 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004388 return;
4389 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004390
Gabor Greifba36cb52008-08-28 21:40:38 +00004391 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004392 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004393 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004394
4395 while (!Users.empty()) {
4396 // We know that this user uses some value of From. If it is the right
4397 // value, update it.
4398 SDNode *User = Users.back();
4399 Users.pop_back();
4400
Chris Lattner01d029b2007-10-15 06:10:22 +00004401 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004402 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004403 for (; Op != E; ++Op)
4404 if (*Op == From) break;
4405
4406 // If there are no matches, the user must use some other result of From.
4407 if (Op == E) continue;
4408
4409 // Okay, we know this user needs to be updated. Remove its old self
4410 // from the CSE maps.
4411 RemoveNodeFromCSEMaps(User);
4412
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004413 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004414 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004415 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004416 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004417 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004418 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004419 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004420 }
4421 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004422
4423 // Now that we have modified User, add it back to the CSE maps. If it
4424 // already exists there, recursively merge the results together.
4425 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004426 if (!Existing) {
4427 if (UpdateListener) UpdateListener->NodeUpdated(User);
4428 continue; // Continue on to next user.
4429 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004430
4431 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004432 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004433 // recursive merging of other unrelated nodes down the line.
4434 ReplaceAllUsesWith(User, Existing, UpdateListener);
4435
4436 // User is now dead. Notify a listener if present.
4437 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4438 DeleteNodeNotInCSEMaps(User);
4439 }
4440}
4441
4442/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004443/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004444/// appear in both the From and To list. The Deleted vector is
4445/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004446void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4447 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004448 unsigned Num,
4449 DAGUpdateListener *UpdateListener){
4450 // Handle the simple, trivial case efficiently.
4451 if (Num == 1)
4452 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4453
4454 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4455 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004456 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4457 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004458 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004459
4460 while (!Users.empty()) {
4461 // We know that this user uses some value of From. If it is the right
4462 // value, update it.
4463 SDNode *User = Users.back().first;
4464 unsigned i = Users.back().second;
4465 Users.pop_back();
4466
4467 // Scan for an operand that matches From.
4468 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4469 for (; Op != E; ++Op)
4470 if (*Op == From[i]) break;
4471
4472 // If there are no matches, the user must use some other result of From.
4473 if (Op == E) continue;
4474
4475 // Okay, we know this user needs to be updated. Remove its old self
4476 // from the CSE maps.
4477 RemoveNodeFromCSEMaps(User);
4478
4479 // Update all operands that match "From" in case there are multiple uses.
4480 for (; Op != E; ++Op) {
4481 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004482 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004483 *Op = To[i];
4484 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004485 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004486 }
4487 }
4488
4489 // Now that we have modified User, add it back to the CSE maps. If it
4490 // already exists there, recursively merge the results together.
4491 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4492 if (!Existing) {
4493 if (UpdateListener) UpdateListener->NodeUpdated(User);
4494 continue; // Continue on to next user.
4495 }
4496
4497 // If there was already an existing matching node, use ReplaceAllUsesWith
4498 // to replace the dead one with the existing one. This can cause
4499 // recursive merging of other unrelated nodes down the line.
4500 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004501
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004502 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004503 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004504 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004505 }
4506}
4507
Evan Chenge6f35d82006-08-01 08:20:41 +00004508/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004509/// based on their topological order. It returns the maximum id and a vector
4510/// of the SDNodes* in assigned order by reference.
4511unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004512 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004513 std::vector<SDNode*> Sources;
4514
Evan Chenge6f35d82006-08-01 08:20:41 +00004515 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4516 SDNode *N = I;
4517 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004518 // Temporarily use the Node Id as scratch space for the degree count.
4519 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004520 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004521 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004522 }
4523
Evan Cheng99157a02006-08-07 22:13:29 +00004524 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004525 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004526 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004527 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004528 SDNode *N = Sources.back();
4529 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004530 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004531 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004532 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004533 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004534 unsigned Degree = P->getNodeId();
4535 --Degree;
4536 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004537 if (Degree == 0)
4538 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004539 }
4540 }
4541
Evan Chengc384d6c2006-08-02 22:00:34 +00004542 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004543}
4544
4545
Evan Cheng091cba12006-07-27 06:39:06 +00004546
Jim Laskey58b968b2005-08-17 20:08:02 +00004547//===----------------------------------------------------------------------===//
4548// SDNode Class
4549//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004550
Chris Lattner917d2c92006-07-19 00:00:37 +00004551// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004552void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004553void UnarySDNode::ANCHOR() {}
4554void BinarySDNode::ANCHOR() {}
4555void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004556void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004557void ConstantSDNode::ANCHOR() {}
4558void ConstantFPSDNode::ANCHOR() {}
4559void GlobalAddressSDNode::ANCHOR() {}
4560void FrameIndexSDNode::ANCHOR() {}
4561void JumpTableSDNode::ANCHOR() {}
4562void ConstantPoolSDNode::ANCHOR() {}
4563void BasicBlockSDNode::ANCHOR() {}
4564void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004565void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004566void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004567void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004568void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004569void ExternalSymbolSDNode::ANCHOR() {}
4570void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004571void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004572void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004573void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004574void LoadSDNode::ANCHOR() {}
4575void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004576void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004577
Chris Lattner48b85922007-02-04 02:41:42 +00004578HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004579 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004580}
4581
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004582GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004583 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004584 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004585 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004586 // Thread Local
4587 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4588 // Non Thread Local
4589 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4590 getSDVTList(VT)), Offset(o) {
4591 TheGlobal = const_cast<GlobalValue*>(GA);
4592}
Chris Lattner48b85922007-02-04 02:41:42 +00004593
Dan Gohman1ea58a52008-07-09 22:08:04 +00004594MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004595 const Value *srcValue, int SVO,
4596 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004597 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004598 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004599
4600 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4601 assert(getAlignment() == alignment && "Alignment representation error!");
4602 assert(isVolatile() == vol && "Volatile representation error!");
4603}
4604
Dan Gohman36b5c132008-04-07 19:35:22 +00004605/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004606/// reference performed by this memory reference.
4607MachineMemOperand MemSDNode::getMemOperand() const {
4608 int Flags;
4609 if (isa<LoadSDNode>(this))
4610 Flags = MachineMemOperand::MOLoad;
4611 else if (isa<StoreSDNode>(this))
4612 Flags = MachineMemOperand::MOStore;
4613 else {
4614 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4615 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4616 }
4617
4618 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004619 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4620
Dan Gohman1ea58a52008-07-09 22:08:04 +00004621 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004622 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004623 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004624 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004625 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4626 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004627 else
4628 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4629 Size, getAlignment());
4630}
4631
Jim Laskey583bd472006-10-27 23:46:08 +00004632/// Profile - Gather unique data for the node.
4633///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004634void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004635 AddNodeIDNode(ID, this);
4636}
4637
Chris Lattnera3255112005-11-08 23:30:28 +00004638/// getValueTypeList - Return a pointer to the specified value type.
4639///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004640const MVT *SDNode::getValueTypeList(MVT VT) {
4641 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004642 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004643 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004644 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004645 static MVT VTs[MVT::LAST_VALUETYPE];
4646 VTs[VT.getSimpleVT()] = VT;
4647 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004648 }
Chris Lattnera3255112005-11-08 23:30:28 +00004649}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004650
Chris Lattner5c884562005-01-12 18:37:47 +00004651/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4652/// indicated value. This method ignores uses of other values defined by this
4653/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004654bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004655 assert(Value < getNumValues() && "Bad value!");
4656
Roman Levensteindc1adac2008-04-07 10:06:32 +00004657 // TODO: Only iterate over uses of a given value of the node
4658 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004659 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004660 if (NUses == 0)
4661 return false;
4662 --NUses;
4663 }
Chris Lattner5c884562005-01-12 18:37:47 +00004664 }
4665
4666 // Found exactly the right number of uses?
4667 return NUses == 0;
4668}
4669
4670
Evan Cheng33d55952007-08-02 05:29:38 +00004671/// hasAnyUseOfValue - Return true if there are any use of the indicated
4672/// value. This method ignores uses of other values defined by this operation.
4673bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4674 assert(Value < getNumValues() && "Bad value!");
4675
Dan Gohman1373c1c2008-07-09 22:39:01 +00004676 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004677 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004678 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004679
4680 return false;
4681}
4682
4683
Dan Gohman2a629952008-07-27 18:06:42 +00004684/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004685///
Dan Gohman2a629952008-07-27 18:06:42 +00004686bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004687 bool Seen = false;
4688 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004689 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004690 if (User == this)
4691 Seen = true;
4692 else
4693 return false;
4694 }
4695
4696 return Seen;
4697}
4698
Evan Chenge6e97e62006-11-03 07:31:32 +00004699/// isOperand - Return true if this node is an operand of N.
4700///
Dan Gohman475871a2008-07-27 21:46:04 +00004701bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004702 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4703 if (*this == N->getOperand(i))
4704 return true;
4705 return false;
4706}
4707
Evan Cheng917be682008-03-04 00:41:45 +00004708bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004709 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004710 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004711 return true;
4712 return false;
4713}
Evan Cheng4ee62112006-02-05 06:29:23 +00004714
Chris Lattner572dee72008-01-16 05:49:24 +00004715/// reachesChainWithoutSideEffects - Return true if this operand (which must
4716/// be a chain) reaches the specified operand without crossing any
4717/// side-effecting instructions. In practice, this looks through token
4718/// factors and non-volatile loads. In order to remain efficient, this only
4719/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004720bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004721 unsigned Depth) const {
4722 if (*this == Dest) return true;
4723
4724 // Don't search too deeply, we just want to be able to see through
4725 // TokenFactor's etc.
4726 if (Depth == 0) return false;
4727
4728 // If this is a token factor, all inputs to the TF happen in parallel. If any
4729 // of the operands of the TF reach dest, then we can do the xform.
4730 if (getOpcode() == ISD::TokenFactor) {
4731 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4732 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4733 return true;
4734 return false;
4735 }
4736
4737 // Loads don't have side effects, look through them.
4738 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4739 if (!Ld->isVolatile())
4740 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4741 }
4742 return false;
4743}
4744
4745
Evan Chengc5fc57d2006-11-03 03:05:24 +00004746static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004747 SmallPtrSet<SDNode *, 32> &Visited) {
4748 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004749 return;
4750
4751 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004752 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004753 if (Op == P) {
4754 found = true;
4755 return;
4756 }
4757 findPredecessor(Op, P, found, Visited);
4758 }
4759}
4760
Evan Cheng917be682008-03-04 00:41:45 +00004761/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004762/// is either an operand of N or it can be reached by recursively traversing
4763/// up the operands.
4764/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004765bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004766 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004767 bool found = false;
4768 findPredecessor(N, this, found, Visited);
4769 return found;
4770}
4771
Evan Chengc5484282006-10-04 00:56:09 +00004772uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4773 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004774 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004775}
4776
Reid Spencer577cc322007-04-01 07:32:19 +00004777std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004778 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004779 default:
4780 if (getOpcode() < ISD::BUILTIN_OP_END)
4781 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004782 if (isMachineOpcode()) {
4783 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004784 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004785 if (getMachineOpcode() < TII->getNumOpcodes())
4786 return TII->get(getMachineOpcode()).getName();
4787 return "<<Unknown Machine Node>>";
4788 }
4789 if (G) {
4790 TargetLowering &TLI = G->getTargetLoweringInfo();
4791 const char *Name = TLI.getTargetNodeName(getOpcode());
4792 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004793 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004794 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004795 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004796
Dan Gohmane8be6c62008-07-17 19:10:17 +00004797#ifndef NDEBUG
4798 case ISD::DELETED_NODE:
4799 return "<<Deleted Node!>>";
4800#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004801 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004802 case ISD::MEMBARRIER: return "MemBarrier";
Dale Johannesene00a8a22008-08-28 02:44:49 +00004803 case ISD::ATOMIC_CMP_SWAP_8: return "AtomicCmpSwap8";
4804 case ISD::ATOMIC_SWAP_8: return "AtomicSwap8";
4805 case ISD::ATOMIC_LOAD_ADD_8: return "AtomicLoadAdd8";
4806 case ISD::ATOMIC_LOAD_SUB_8: return "AtomicLoadSub8";
4807 case ISD::ATOMIC_LOAD_AND_8: return "AtomicLoadAnd8";
4808 case ISD::ATOMIC_LOAD_OR_8: return "AtomicLoadOr8";
4809 case ISD::ATOMIC_LOAD_XOR_8: return "AtomicLoadXor8";
4810 case ISD::ATOMIC_LOAD_NAND_8: return "AtomicLoadNand8";
4811 case ISD::ATOMIC_LOAD_MIN_8: return "AtomicLoadMin8";
4812 case ISD::ATOMIC_LOAD_MAX_8: return "AtomicLoadMax8";
4813 case ISD::ATOMIC_LOAD_UMIN_8: return "AtomicLoadUMin8";
4814 case ISD::ATOMIC_LOAD_UMAX_8: return "AtomicLoadUMax8";
4815 case ISD::ATOMIC_CMP_SWAP_16: return "AtomicCmpSwap16";
4816 case ISD::ATOMIC_SWAP_16: return "AtomicSwap16";
4817 case ISD::ATOMIC_LOAD_ADD_16: return "AtomicLoadAdd16";
4818 case ISD::ATOMIC_LOAD_SUB_16: return "AtomicLoadSub16";
4819 case ISD::ATOMIC_LOAD_AND_16: return "AtomicLoadAnd16";
4820 case ISD::ATOMIC_LOAD_OR_16: return "AtomicLoadOr16";
4821 case ISD::ATOMIC_LOAD_XOR_16: return "AtomicLoadXor16";
4822 case ISD::ATOMIC_LOAD_NAND_16: return "AtomicLoadNand16";
4823 case ISD::ATOMIC_LOAD_MIN_16: return "AtomicLoadMin16";
4824 case ISD::ATOMIC_LOAD_MAX_16: return "AtomicLoadMax16";
4825 case ISD::ATOMIC_LOAD_UMIN_16: return "AtomicLoadUMin16";
4826 case ISD::ATOMIC_LOAD_UMAX_16: return "AtomicLoadUMax16";
4827 case ISD::ATOMIC_CMP_SWAP_32: return "AtomicCmpSwap32";
4828 case ISD::ATOMIC_SWAP_32: return "AtomicSwap32";
4829 case ISD::ATOMIC_LOAD_ADD_32: return "AtomicLoadAdd32";
4830 case ISD::ATOMIC_LOAD_SUB_32: return "AtomicLoadSub32";
4831 case ISD::ATOMIC_LOAD_AND_32: return "AtomicLoadAnd32";
4832 case ISD::ATOMIC_LOAD_OR_32: return "AtomicLoadOr32";
4833 case ISD::ATOMIC_LOAD_XOR_32: return "AtomicLoadXor32";
4834 case ISD::ATOMIC_LOAD_NAND_32: return "AtomicLoadNand32";
4835 case ISD::ATOMIC_LOAD_MIN_32: return "AtomicLoadMin32";
4836 case ISD::ATOMIC_LOAD_MAX_32: return "AtomicLoadMax32";
4837 case ISD::ATOMIC_LOAD_UMIN_32: return "AtomicLoadUMin32";
4838 case ISD::ATOMIC_LOAD_UMAX_32: return "AtomicLoadUMax32";
4839 case ISD::ATOMIC_CMP_SWAP_64: return "AtomicCmpSwap64";
4840 case ISD::ATOMIC_SWAP_64: return "AtomicSwap64";
4841 case ISD::ATOMIC_LOAD_ADD_64: return "AtomicLoadAdd64";
4842 case ISD::ATOMIC_LOAD_SUB_64: return "AtomicLoadSub64";
4843 case ISD::ATOMIC_LOAD_AND_64: return "AtomicLoadAnd64";
4844 case ISD::ATOMIC_LOAD_OR_64: return "AtomicLoadOr64";
4845 case ISD::ATOMIC_LOAD_XOR_64: return "AtomicLoadXor64";
4846 case ISD::ATOMIC_LOAD_NAND_64: return "AtomicLoadNand64";
4847 case ISD::ATOMIC_LOAD_MIN_64: return "AtomicLoadMin64";
4848 case ISD::ATOMIC_LOAD_MAX_64: return "AtomicLoadMax64";
4849 case ISD::ATOMIC_LOAD_UMIN_64: return "AtomicLoadUMin64";
4850 case ISD::ATOMIC_LOAD_UMAX_64: return "AtomicLoadUMax64";
Andrew Lenharth95762122005-03-31 21:24:06 +00004851 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004852 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004853 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004854 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004855 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004856 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004857 case ISD::AssertSext: return "AssertSext";
4858 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004859
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004860 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004861 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004862 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004863 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004864
4865 case ISD::Constant: return "Constant";
4866 case ISD::ConstantFP: return "ConstantFP";
4867 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004868 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004869 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004870 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004871 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004872 case ISD::RETURNADDR: return "RETURNADDR";
4873 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004874 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004875 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4876 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004877 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004878 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004879 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004880 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004881 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00004882 return Intrinsic::getName((Intrinsic::ID)IID);
4883 }
4884 case ISD::INTRINSIC_VOID:
4885 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004886 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004887 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004888 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004889
Chris Lattnerb2827b02006-03-19 00:52:58 +00004890 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004891 case ISD::TargetConstant: return "TargetConstant";
4892 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004893 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004894 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004895 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004896 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004897 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004898 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004899
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004900 case ISD::CopyToReg: return "CopyToReg";
4901 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004902 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004903 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004904 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004905 case ISD::DBG_LABEL: return "dbg_label";
4906 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004907 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004908 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004909 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004910 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004911
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004912 // Unary operators
4913 case ISD::FABS: return "fabs";
4914 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004915 case ISD::FSQRT: return "fsqrt";
4916 case ISD::FSIN: return "fsin";
4917 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004918 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004919 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004920 case ISD::FTRUNC: return "ftrunc";
4921 case ISD::FFLOOR: return "ffloor";
4922 case ISD::FCEIL: return "fceil";
4923 case ISD::FRINT: return "frint";
4924 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004925
4926 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004927 case ISD::ADD: return "add";
4928 case ISD::SUB: return "sub";
4929 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004930 case ISD::MULHU: return "mulhu";
4931 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004932 case ISD::SDIV: return "sdiv";
4933 case ISD::UDIV: return "udiv";
4934 case ISD::SREM: return "srem";
4935 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004936 case ISD::SMUL_LOHI: return "smul_lohi";
4937 case ISD::UMUL_LOHI: return "umul_lohi";
4938 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00004939 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004940 case ISD::AND: return "and";
4941 case ISD::OR: return "or";
4942 case ISD::XOR: return "xor";
4943 case ISD::SHL: return "shl";
4944 case ISD::SRA: return "sra";
4945 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004946 case ISD::ROTL: return "rotl";
4947 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004948 case ISD::FADD: return "fadd";
4949 case ISD::FSUB: return "fsub";
4950 case ISD::FMUL: return "fmul";
4951 case ISD::FDIV: return "fdiv";
4952 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004953 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004954 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004955
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004956 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004957 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004958 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004959 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004960 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004961 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004962 case ISD::CONCAT_VECTORS: return "concat_vectors";
4963 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004964 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004965 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004966 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004967 case ISD::ADDC: return "addc";
4968 case ISD::ADDE: return "adde";
4969 case ISD::SUBC: return "subc";
4970 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004971 case ISD::SHL_PARTS: return "shl_parts";
4972 case ISD::SRA_PARTS: return "sra_parts";
4973 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004974
4975 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4976 case ISD::INSERT_SUBREG: return "insert_subreg";
4977
Chris Lattner7f644642005-04-28 21:44:03 +00004978 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004979 case ISD::SIGN_EXTEND: return "sign_extend";
4980 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004981 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004982 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004983 case ISD::TRUNCATE: return "truncate";
4984 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004985 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004986 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004987 case ISD::FP_EXTEND: return "fp_extend";
4988
4989 case ISD::SINT_TO_FP: return "sint_to_fp";
4990 case ISD::UINT_TO_FP: return "uint_to_fp";
4991 case ISD::FP_TO_SINT: return "fp_to_sint";
4992 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004993 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004994
4995 // Control flow instructions
4996 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004997 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004998 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004999 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005000 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005001 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005002 case ISD::CALLSEQ_START: return "callseq_start";
5003 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005004
5005 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005006 case ISD::LOAD: return "load";
5007 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005008 case ISD::VAARG: return "vaarg";
5009 case ISD::VACOPY: return "vacopy";
5010 case ISD::VAEND: return "vaend";
5011 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005012 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005013 case ISD::EXTRACT_ELEMENT: return "extract_element";
5014 case ISD::BUILD_PAIR: return "build_pair";
5015 case ISD::STACKSAVE: return "stacksave";
5016 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005017 case ISD::TRAP: return "trap";
5018
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005019 // Bit manipulation
5020 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005021 case ISD::CTPOP: return "ctpop";
5022 case ISD::CTTZ: return "cttz";
5023 case ISD::CTLZ: return "ctlz";
5024
Chris Lattner36ce6912005-11-29 06:21:05 +00005025 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005026 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005027 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005028
Duncan Sands36397f52007-07-27 12:58:54 +00005029 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005030 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005031
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005032 case ISD::CONDCODE:
5033 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005034 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005035 case ISD::SETOEQ: return "setoeq";
5036 case ISD::SETOGT: return "setogt";
5037 case ISD::SETOGE: return "setoge";
5038 case ISD::SETOLT: return "setolt";
5039 case ISD::SETOLE: return "setole";
5040 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005041
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005042 case ISD::SETO: return "seto";
5043 case ISD::SETUO: return "setuo";
5044 case ISD::SETUEQ: return "setue";
5045 case ISD::SETUGT: return "setugt";
5046 case ISD::SETUGE: return "setuge";
5047 case ISD::SETULT: return "setult";
5048 case ISD::SETULE: return "setule";
5049 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005050
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005051 case ISD::SETEQ: return "seteq";
5052 case ISD::SETGT: return "setgt";
5053 case ISD::SETGE: return "setge";
5054 case ISD::SETLT: return "setlt";
5055 case ISD::SETLE: return "setle";
5056 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005057 }
5058 }
5059}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005060
Evan Cheng144d8f02006-11-09 17:55:04 +00005061const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005062 switch (AM) {
5063 default:
5064 return "";
5065 case ISD::PRE_INC:
5066 return "<pre-inc>";
5067 case ISD::PRE_DEC:
5068 return "<pre-dec>";
5069 case ISD::POST_INC:
5070 return "<post-inc>";
5071 case ISD::POST_DEC:
5072 return "<post-dec>";
5073 }
5074}
5075
Duncan Sands276dcbd2008-03-21 09:14:45 +00005076std::string ISD::ArgFlagsTy::getArgFlagsString() {
5077 std::string S = "< ";
5078
5079 if (isZExt())
5080 S += "zext ";
5081 if (isSExt())
5082 S += "sext ";
5083 if (isInReg())
5084 S += "inreg ";
5085 if (isSRet())
5086 S += "sret ";
5087 if (isByVal())
5088 S += "byval ";
5089 if (isNest())
5090 S += "nest ";
5091 if (getByValAlign())
5092 S += "byval-align:" + utostr(getByValAlign()) + " ";
5093 if (getOrigAlign())
5094 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5095 if (getByValSize())
5096 S += "byval-size:" + utostr(getByValSize()) + " ";
5097 return S + ">";
5098}
5099
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005100void SDNode::dump() const { dump(0); }
5101void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005102 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005103 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005104}
5105
5106void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5107 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005108
5109 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005110 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005111 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005112 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005113 else
Chris Lattner944fac72008-08-23 22:23:09 +00005114 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005115 }
Chris Lattner944fac72008-08-23 22:23:09 +00005116 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005117
Chris Lattner944fac72008-08-23 22:23:09 +00005118 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005119 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005120 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005121 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005122 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005123 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005124 }
5125
Evan Chengce254432007-12-11 02:08:35 +00005126 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005127 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005128 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005129 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005130 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005131 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005132 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005133 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005134 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005135 }
Chris Lattner944fac72008-08-23 22:23:09 +00005136 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005137 }
5138
Chris Lattnerc3aae252005-01-07 07:46:32 +00005139 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005140 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005141 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005142 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005143 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005144 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005145 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005146 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005147 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005148 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005149 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005150 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005151 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005152 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005153 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005154 OS << '<';
5155 WriteAsOperand(OS, GADN->getGlobal());
5156 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005157 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005158 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005159 else
Chris Lattner944fac72008-08-23 22:23:09 +00005160 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005161 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005162 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005163 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005164 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005165 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005166 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005167 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005168 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005169 else
Chris Lattner944fac72008-08-23 22:23:09 +00005170 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005171 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005172 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005173 else
Chris Lattner944fac72008-08-23 22:23:09 +00005174 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005175 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005176 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005177 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5178 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005179 OS << LBB->getName() << " ";
5180 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005181 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005182 if (G && R->getReg() &&
5183 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005184 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005185 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005186 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005187 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005188 } else if (const ExternalSymbolSDNode *ES =
5189 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005190 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005191 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5192 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005193 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005194 else
Chris Lattner944fac72008-08-23 22:23:09 +00005195 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005196 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5197 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005198 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005199 else
Chris Lattner944fac72008-08-23 22:23:09 +00005200 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005201 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005202 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005203 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005204 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005205 }
5206 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005207 const Value *SrcValue = LD->getSrcValue();
5208 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005209 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005210 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005211 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005212 else
Chris Lattner944fac72008-08-23 22:23:09 +00005213 OS << "null";
5214 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005215
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005216 bool doExt = true;
5217 switch (LD->getExtensionType()) {
5218 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005219 case ISD::EXTLOAD: OS << " <anyext "; break;
5220 case ISD::SEXTLOAD: OS << " <sext "; break;
5221 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005222 }
5223 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005224 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005225
Evan Cheng144d8f02006-11-09 17:55:04 +00005226 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005227 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005228 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005229 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005230 OS << " <volatile>";
5231 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005232 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005233 const Value *SrcValue = ST->getSrcValue();
5234 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005235 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005236 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005237 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005238 else
Chris Lattner944fac72008-08-23 22:23:09 +00005239 OS << "null";
5240 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005241
5242 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005243 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005244
5245 const char *AM = getIndexedModeName(ST->getAddressingMode());
5246 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005247 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005248 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005249 OS << " <volatile>";
5250 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005251 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5252 const Value *SrcValue = AT->getSrcValue();
5253 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005254 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005255 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005256 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005257 else
Chris Lattner944fac72008-08-23 22:23:09 +00005258 OS << "null";
5259 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005260 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005261 OS << " <volatile>";
5262 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005263 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005264}
5265
Chris Lattnerde202b32005-11-09 23:47:37 +00005266static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005267 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005268 if (N->getOperand(i).getNode()->hasOneUse())
5269 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005270 else
Bill Wendling832171c2006-12-07 20:04:42 +00005271 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005272 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005273
Chris Lattnerea946cd2005-01-09 20:38:33 +00005274
Bill Wendling832171c2006-12-07 20:04:42 +00005275 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005276 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005277}
5278
Chris Lattnerc3aae252005-01-07 07:46:32 +00005279void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005280 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005281
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005282 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5283 I != E; ++I) {
5284 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005285 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005286 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005287 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005288
Gabor Greifba36cb52008-08-28 21:40:38 +00005289 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005290
Bill Wendling832171c2006-12-07 20:04:42 +00005291 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005292}
5293
Evan Chengd6594ae2006-09-12 21:00:35 +00005294const Type *ConstantPoolSDNode::getType() const {
5295 if (isMachineConstantPoolEntry())
5296 return Val.MachineCPVal->getType();
5297 return Val.ConstVal->getType();
5298}