blob: 1bada5eea2ec0dd2bfc86bce1a0ff3afe407e6a8 [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 {
305 return TLI.getTargetMachine();
306}
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))
952 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
953 }
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 Gohmand4cf9922008-02-26 18:50:50 +00001465 unsigned ShAmt = SA->getValue();
1466
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 Gohmanea859be2007-06-22 14:59:07 +00001483 unsigned ShAmt = SA->getValue();
1484
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 Gohmanea859be2007-06-22 14:59:07 +00001501 unsigned ShAmt = SA->getValue();
1502
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))) {
1826 Tmp += C->getValue();
1827 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);
1834 if (C->getValue() >= VTBits || // Bad shift.
1835 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1836 return Tmp - C->getValue();
1837 }
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))) {
1868 unsigned RotAmt = C->getValue() & (VTBits-1);
1869
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());
2011 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
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,
2392 N1.getOperand(N2C->getValue() / Factor),
2393 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2394 }
2395
2396 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2397 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002398 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002399 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002400
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002401 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2402 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002403 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2404 if (N1.getOperand(2) == N2)
2405 return N1.getOperand(1);
2406 else
2407 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2408 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002409 break;
2410 case ISD::EXTRACT_ELEMENT:
2411 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002412 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2413 (N1.getValueType().isInteger() == VT.isInteger()) &&
2414 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002415
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002416 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2417 // 64-bit integers into 32-bit parts. Instead of building the extract of
2418 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2419 if (N1.getOpcode() == ISD::BUILD_PAIR)
2420 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002421
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002422 // EXTRACT_ELEMENT of a constant int is also very common.
2423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002424 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002425 unsigned Shift = ElementSize * N2C->getValue();
2426 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2427 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002428 }
2429 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002430 case ISD::EXTRACT_SUBVECTOR:
2431 if (N1.getValueType() == VT) // Trivial extraction.
2432 return N1;
2433 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002434 }
2435
2436 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002437 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002438 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002439 switch (Opcode) {
2440 case ISD::ADD: return getConstant(C1 + C2, VT);
2441 case ISD::SUB: return getConstant(C1 - C2, VT);
2442 case ISD::MUL: return getConstant(C1 * C2, VT);
2443 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002444 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002445 break;
2446 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002447 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 break;
2449 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002450 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002451 break;
2452 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002453 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002454 break;
2455 case ISD::AND : return getConstant(C1 & C2, VT);
2456 case ISD::OR : return getConstant(C1 | C2, VT);
2457 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002458 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002459 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2460 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2461 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2462 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463 default: break;
2464 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002465 } else { // Cannonicalize constant to RHS if commutative
2466 if (isCommutativeBinOp(Opcode)) {
2467 std::swap(N1C, N2C);
2468 std::swap(N1, N2);
2469 }
2470 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002471 }
2472
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002473 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002474 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2475 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002476 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002477 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2478 // Cannonicalize constant to RHS if commutative
2479 std::swap(N1CFP, N2CFP);
2480 std::swap(N1, N2);
2481 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002482 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2483 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002485 case ISD::FADD:
2486 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002487 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002488 return getConstantFP(V1, VT);
2489 break;
2490 case ISD::FSUB:
2491 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2492 if (s!=APFloat::opInvalidOp)
2493 return getConstantFP(V1, VT);
2494 break;
2495 case ISD::FMUL:
2496 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2497 if (s!=APFloat::opInvalidOp)
2498 return getConstantFP(V1, VT);
2499 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002500 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002501 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2502 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2503 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002504 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002505 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002506 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2507 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2508 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002509 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002510 case ISD::FCOPYSIGN:
2511 V1.copySign(V2);
2512 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002513 default: break;
2514 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002515 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002516 }
Chris Lattner62b57722006-04-20 05:39:12 +00002517
2518 // Canonicalize an UNDEF to the RHS, even over a constant.
2519 if (N1.getOpcode() == ISD::UNDEF) {
2520 if (isCommutativeBinOp(Opcode)) {
2521 std::swap(N1, N2);
2522 } else {
2523 switch (Opcode) {
2524 case ISD::FP_ROUND_INREG:
2525 case ISD::SIGN_EXTEND_INREG:
2526 case ISD::SUB:
2527 case ISD::FSUB:
2528 case ISD::FDIV:
2529 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002530 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002531 return N1; // fold op(undef, arg2) -> undef
2532 case ISD::UDIV:
2533 case ISD::SDIV:
2534 case ISD::UREM:
2535 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002536 case ISD::SRL:
2537 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002538 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002539 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2540 // For vectors, we can't easily build an all zero vector, just return
2541 // the LHS.
2542 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002543 }
2544 }
2545 }
2546
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002547 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002548 if (N2.getOpcode() == ISD::UNDEF) {
2549 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002550 case ISD::XOR:
2551 if (N1.getOpcode() == ISD::UNDEF)
2552 // Handle undef ^ undef -> 0 special case. This is a common
2553 // idiom (misuse).
2554 return getConstant(0, VT);
2555 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002556 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002557 case ISD::ADDC:
2558 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002559 case ISD::SUB:
2560 case ISD::FADD:
2561 case ISD::FSUB:
2562 case ISD::FMUL:
2563 case ISD::FDIV:
2564 case ISD::FREM:
2565 case ISD::UDIV:
2566 case ISD::SDIV:
2567 case ISD::UREM:
2568 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002569 return N2; // fold op(arg1, undef) -> undef
2570 case ISD::MUL:
2571 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002572 case ISD::SRL:
2573 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002574 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002575 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2576 // For vectors, we can't easily build an all zero vector, just return
2577 // the LHS.
2578 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002579 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 if (!VT.isVector())
2581 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002582 // For vectors, we can't easily build an all one vector, just return
2583 // the LHS.
2584 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002585 case ISD::SRA:
2586 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002587 }
2588 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002589
Chris Lattner27e9b412005-05-11 18:57:39 +00002590 // Memoize this node if possible.
2591 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002592 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002593 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002594 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002595 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002596 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002597 void *IP = 0;
2598 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002599 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002600 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002601 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002602 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002603 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002604 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002605 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002606 }
2607
Chris Lattnerc3aae252005-01-07 07:46:32 +00002608 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002609#ifndef NDEBUG
2610 VerifyNode(N);
2611#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002612 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002613}
2614
Dan Gohman475871a2008-07-27 21:46:04 +00002615SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2616 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002617 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2619 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002620 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002621 case ISD::CONCAT_VECTORS:
2622 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2623 // one big BUILD_VECTOR.
2624 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2625 N2.getOpcode() == ISD::BUILD_VECTOR &&
2626 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002627 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2628 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2629 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002630 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2631 }
2632 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002633 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002634 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002635 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002636 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002637 break;
2638 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002639 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002640 if (N1C) {
2641 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002642 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002643 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002644 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002645 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002646
2647 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002648 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002649 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002650 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002651 if (N2C->getValue()) // Unconditional branch
2652 return getNode(ISD::BR, MVT::Other, N1, N3);
2653 else
2654 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002655 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002656 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002657 case ISD::VECTOR_SHUFFLE:
2658 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002660 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002662 "Illegal VECTOR_SHUFFLE node!");
2663 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002664 case ISD::BIT_CONVERT:
2665 // Fold bit_convert nodes from a type to themselves.
2666 if (N1.getValueType() == VT)
2667 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002668 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002669 }
2670
Chris Lattner43247a12005-08-25 19:12:10 +00002671 // Memoize node if it doesn't produce a flag.
2672 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002673 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002674 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002675 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002676 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002677 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002678 void *IP = 0;
2679 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002680 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002681 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002682 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002683 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002684 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002685 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002686 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002687 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002688 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002689#ifndef NDEBUG
2690 VerifyNode(N);
2691#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002692 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002693}
2694
Dan Gohman475871a2008-07-27 21:46:04 +00002695SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2696 SDValue N1, SDValue N2, SDValue N3,
2697 SDValue N4) {
2698 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002699 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002700}
2701
Dan Gohman475871a2008-07-27 21:46:04 +00002702SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2703 SDValue N1, SDValue N2, SDValue N3,
2704 SDValue N4, SDValue N5) {
2705 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002706 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002707}
2708
Dan Gohman707e0182008-04-12 04:36:06 +00002709/// getMemsetValue - Vectorized representation of the memset value
2710/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002711static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002712 unsigned NumBits = VT.isVector() ?
2713 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002714 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002715 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002716 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002717 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002718 Val = (Val << Shift) | Val;
2719 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002720 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002721 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002722 return DAG.getConstant(Val, VT);
2723 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002724 }
Evan Chengf0df0312008-05-15 08:39:06 +00002725
2726 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2727 unsigned Shift = 8;
2728 for (unsigned i = NumBits; i > 8; i >>= 1) {
2729 Value = DAG.getNode(ISD::OR, VT,
2730 DAG.getNode(ISD::SHL, VT, Value,
2731 DAG.getConstant(Shift, MVT::i8)), Value);
2732 Shift <<= 1;
2733 }
2734
2735 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002736}
2737
Dan Gohman707e0182008-04-12 04:36:06 +00002738/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2739/// used when a memcpy is turned into a memset when the source is a constant
2740/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002741static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002742 const TargetLowering &TLI,
2743 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002744 // Handle vector with all elements zero.
2745 if (Str.empty()) {
2746 if (VT.isInteger())
2747 return DAG.getConstant(0, VT);
2748 unsigned NumElts = VT.getVectorNumElements();
2749 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2750 return DAG.getNode(ISD::BIT_CONVERT, VT,
2751 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2752 }
2753
Duncan Sands83ec4b62008-06-06 12:08:01 +00002754 assert(!VT.isVector() && "Can't handle vector type here!");
2755 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002756 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002757 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002758 if (TLI.isLittleEndian())
2759 Offset = Offset + MSB - 1;
2760 for (unsigned i = 0; i != MSB; ++i) {
2761 Val = (Val << 8) | (unsigned char)Str[Offset];
2762 Offset += TLI.isLittleEndian() ? -1 : 1;
2763 }
2764 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002765}
2766
Dan Gohman707e0182008-04-12 04:36:06 +00002767/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002768///
Dan Gohman475871a2008-07-27 21:46:04 +00002769static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002770 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002772 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2773}
2774
Evan Chengf0df0312008-05-15 08:39:06 +00002775/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2776///
Dan Gohman475871a2008-07-27 21:46:04 +00002777static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002778 unsigned SrcDelta = 0;
2779 GlobalAddressSDNode *G = NULL;
2780 if (Src.getOpcode() == ISD::GlobalAddress)
2781 G = cast<GlobalAddressSDNode>(Src);
2782 else if (Src.getOpcode() == ISD::ADD &&
2783 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2784 Src.getOperand(1).getOpcode() == ISD::Constant) {
2785 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2786 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2787 }
2788 if (!G)
2789 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002790
Evan Chengf0df0312008-05-15 08:39:06 +00002791 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002792 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2793 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002794
Evan Chengf0df0312008-05-15 08:39:06 +00002795 return false;
2796}
Dan Gohman707e0182008-04-12 04:36:06 +00002797
Evan Chengf0df0312008-05-15 08:39:06 +00002798/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2799/// to replace the memset / memcpy is below the threshold. It also returns the
2800/// types of the sequence of memory ops to perform memset / memcpy.
2801static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002802bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002803 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002804 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002805 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002806 SelectionDAG &DAG,
2807 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002808 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002809 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002810 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002811 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002812 if (VT != MVT::iAny) {
2813 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002815 // If source is a string constant, this will require an unaligned load.
2816 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2817 if (Dst.getOpcode() != ISD::FrameIndex) {
2818 // Can't change destination alignment. It requires a unaligned store.
2819 if (AllowUnalign)
2820 VT = MVT::iAny;
2821 } else {
2822 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2823 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2824 if (MFI->isFixedObjectIndex(FI)) {
2825 // Can't change destination alignment. It requires a unaligned store.
2826 if (AllowUnalign)
2827 VT = MVT::iAny;
2828 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002829 // Give the stack frame object a larger alignment if needed.
2830 if (MFI->getObjectAlignment(FI) < NewAlign)
2831 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002832 Align = NewAlign;
2833 }
2834 }
2835 }
2836 }
2837
2838 if (VT == MVT::iAny) {
2839 if (AllowUnalign) {
2840 VT = MVT::i64;
2841 } else {
2842 switch (Align & 7) {
2843 case 0: VT = MVT::i64; break;
2844 case 4: VT = MVT::i32; break;
2845 case 2: VT = MVT::i16; break;
2846 default: VT = MVT::i8; break;
2847 }
2848 }
2849
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002851 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2853 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002854
Duncan Sands8e4eb092008-06-08 20:54:56 +00002855 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002856 VT = LVT;
2857 }
Dan Gohman707e0182008-04-12 04:36:06 +00002858
2859 unsigned NumMemOps = 0;
2860 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002861 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002862 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002863 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002864 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002865 VT = MVT::i64;
2866 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2868 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002869 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002870 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002871 VTSize >>= 1;
2872 }
Dan Gohman707e0182008-04-12 04:36:06 +00002873 }
Dan Gohman707e0182008-04-12 04:36:06 +00002874
2875 if (++NumMemOps > Limit)
2876 return false;
2877 MemOps.push_back(VT);
2878 Size -= VTSize;
2879 }
2880
2881 return true;
2882}
2883
Dan Gohman475871a2008-07-27 21:46:04 +00002884static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2885 SDValue Chain, SDValue Dst,
2886 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002887 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002888 const Value *DstSV, uint64_t DstSVOff,
2889 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002890 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2891
Dan Gohman21323f32008-05-29 19:42:22 +00002892 // Expand memcpy to a series of load and store ops if the size operand falls
2893 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002894 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002895 uint64_t Limit = -1;
2896 if (!AlwaysInline)
2897 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002898 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002899 std::string Str;
2900 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002901 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002902 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002903 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002904
Dan Gohman707e0182008-04-12 04:36:06 +00002905
Evan Cheng0ff39b32008-06-30 07:31:25 +00002906 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002907 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002908 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002909 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002910 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002911 MVT VT = MemOps[i];
2912 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002914
Evan Cheng0ff39b32008-06-30 07:31:25 +00002915 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002916 // It's unlikely a store of a vector immediate can be done in a single
2917 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002918 // We also handle store a vector with all zero's.
2919 // FIXME: Handle other cases where store of vector immediate is done in
2920 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002921 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002922 Store = DAG.getStore(Chain, Value,
2923 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002924 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002925 } else {
2926 Value = DAG.getLoad(VT, Chain,
2927 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002928 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002929 Store = DAG.getStore(Chain, Value,
2930 getMemBasePlusOffset(Dst, DstOff, DAG),
2931 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002932 }
2933 OutChains.push_back(Store);
2934 SrcOff += VTSize;
2935 DstOff += VTSize;
2936 }
2937
2938 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2939 &OutChains[0], OutChains.size());
2940}
2941
Dan Gohman475871a2008-07-27 21:46:04 +00002942static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2943 SDValue Chain, SDValue Dst,
2944 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002945 unsigned Align, bool AlwaysInline,
2946 const Value *DstSV, uint64_t DstSVOff,
2947 const Value *SrcSV, uint64_t SrcSVOff){
2948 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2949
2950 // Expand memmove to a series of load and store ops if the size operand falls
2951 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002952 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002953 uint64_t Limit = -1;
2954 if (!AlwaysInline)
2955 Limit = TLI.getMaxStoresPerMemmove();
2956 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002957 std::string Str;
2958 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002959 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002960 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002961 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002962
Dan Gohman21323f32008-05-29 19:42:22 +00002963 uint64_t SrcOff = 0, DstOff = 0;
2964
Dan Gohman475871a2008-07-27 21:46:04 +00002965 SmallVector<SDValue, 8> LoadValues;
2966 SmallVector<SDValue, 8> LoadChains;
2967 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002968 unsigned NumMemOps = MemOps.size();
2969 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002970 MVT VT = MemOps[i];
2971 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002972 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002973
2974 Value = DAG.getLoad(VT, Chain,
2975 getMemBasePlusOffset(Src, SrcOff, DAG),
2976 SrcSV, SrcSVOff + SrcOff, false, Align);
2977 LoadValues.push_back(Value);
2978 LoadChains.push_back(Value.getValue(1));
2979 SrcOff += VTSize;
2980 }
2981 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2982 &LoadChains[0], LoadChains.size());
2983 OutChains.clear();
2984 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002985 MVT VT = MemOps[i];
2986 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002988
2989 Store = DAG.getStore(Chain, LoadValues[i],
2990 getMemBasePlusOffset(Dst, DstOff, DAG),
2991 DstSV, DstSVOff + DstOff, false, DstAlign);
2992 OutChains.push_back(Store);
2993 DstOff += VTSize;
2994 }
2995
2996 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2997 &OutChains[0], OutChains.size());
2998}
2999
Dan Gohman475871a2008-07-27 21:46:04 +00003000static SDValue getMemsetStores(SelectionDAG &DAG,
3001 SDValue Chain, SDValue Dst,
3002 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003003 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003004 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003005 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3006
3007 // Expand memset to a series of load/store ops if the size operand
3008 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003009 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003010 std::string Str;
3011 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003012 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003013 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003014 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003015
Dan Gohman475871a2008-07-27 21:46:04 +00003016 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003017 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003018
3019 unsigned NumMemOps = MemOps.size();
3020 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003021 MVT VT = MemOps[i];
3022 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003023 SDValue Value = getMemsetValue(Src, VT, DAG);
3024 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00003025 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003026 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003027 OutChains.push_back(Store);
3028 DstOff += VTSize;
3029 }
3030
3031 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3032 &OutChains[0], OutChains.size());
3033}
3034
Dan Gohman475871a2008-07-27 21:46:04 +00003035SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3036 SDValue Src, SDValue Size,
3037 unsigned Align, bool AlwaysInline,
3038 const Value *DstSV, uint64_t DstSVOff,
3039 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003040
3041 // Check to see if we should lower the memcpy to loads and stores first.
3042 // For cases within the target-specified limits, this is the best choice.
3043 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3044 if (ConstantSize) {
3045 // Memcpy with size zero? Just return the original chain.
3046 if (ConstantSize->isNullValue())
3047 return Chain;
3048
Dan Gohman475871a2008-07-27 21:46:04 +00003049 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003050 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003051 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003052 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003053 return Result;
3054 }
3055
3056 // Then check to see if we should lower the memcpy with target-specific
3057 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003058 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003059 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3060 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003061 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003062 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003063 return Result;
3064
3065 // If we really need inline code and the target declined to provide it,
3066 // use a (potentially long) sequence of loads and stores.
3067 if (AlwaysInline) {
3068 assert(ConstantSize && "AlwaysInline requires a constant size!");
3069 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3070 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003071 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003072 }
3073
3074 // Emit a library call.
3075 TargetLowering::ArgListTy Args;
3076 TargetLowering::ArgListEntry Entry;
3077 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3078 Entry.Node = Dst; Args.push_back(Entry);
3079 Entry.Node = Src; Args.push_back(Entry);
3080 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003081 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003082 TLI.LowerCallTo(Chain, Type::VoidTy,
3083 false, false, false, CallingConv::C, false,
3084 getExternalSymbol("memcpy", TLI.getPointerTy()),
3085 Args, *this);
3086 return CallResult.second;
3087}
3088
Dan Gohman475871a2008-07-27 21:46:04 +00003089SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3090 SDValue Src, SDValue Size,
3091 unsigned Align,
3092 const Value *DstSV, uint64_t DstSVOff,
3093 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003094
Dan Gohman21323f32008-05-29 19:42:22 +00003095 // Check to see if we should lower the memmove to loads and stores first.
3096 // For cases within the target-specified limits, this is the best choice.
3097 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3098 if (ConstantSize) {
3099 // Memmove with size zero? Just return the original chain.
3100 if (ConstantSize->isNullValue())
3101 return Chain;
3102
Dan Gohman475871a2008-07-27 21:46:04 +00003103 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003104 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3105 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003106 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003107 return Result;
3108 }
Dan Gohman707e0182008-04-12 04:36:06 +00003109
3110 // Then check to see if we should lower the memmove with target-specific
3111 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003112 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003113 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003114 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003115 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003116 return Result;
3117
3118 // Emit a library call.
3119 TargetLowering::ArgListTy Args;
3120 TargetLowering::ArgListEntry Entry;
3121 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3122 Entry.Node = Dst; Args.push_back(Entry);
3123 Entry.Node = Src; Args.push_back(Entry);
3124 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003125 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003126 TLI.LowerCallTo(Chain, Type::VoidTy,
3127 false, false, false, CallingConv::C, false,
3128 getExternalSymbol("memmove", TLI.getPointerTy()),
3129 Args, *this);
3130 return CallResult.second;
3131}
3132
Dan Gohman475871a2008-07-27 21:46:04 +00003133SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3134 SDValue Src, SDValue Size,
3135 unsigned Align,
3136 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003137
3138 // Check to see if we should lower the memset to stores first.
3139 // For cases within the target-specified limits, this is the best choice.
3140 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3141 if (ConstantSize) {
3142 // Memset with size zero? Just return the original chain.
3143 if (ConstantSize->isNullValue())
3144 return Chain;
3145
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003147 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003148 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003150 return Result;
3151 }
3152
3153 // Then check to see if we should lower the memset with target-specific
3154 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003155 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003156 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003157 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003158 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003159 return Result;
3160
3161 // Emit a library call.
3162 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3163 TargetLowering::ArgListTy Args;
3164 TargetLowering::ArgListEntry Entry;
3165 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3166 Args.push_back(Entry);
3167 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003168 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003169 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3170 else
3171 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3172 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3173 Args.push_back(Entry);
3174 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3175 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003176 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003177 TLI.LowerCallTo(Chain, Type::VoidTy,
3178 false, false, false, CallingConv::C, false,
3179 getExternalSymbol("memset", TLI.getPointerTy()),
3180 Args, *this);
3181 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003182}
3183
Dan Gohman475871a2008-07-27 21:46:04 +00003184SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3185 SDValue Ptr, SDValue Cmp,
3186 SDValue Swp, const Value* PtrVal,
3187 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003188 assert((Opcode == ISD::ATOMIC_CMP_SWAP_8 ||
3189 Opcode == ISD::ATOMIC_CMP_SWAP_16 ||
3190 Opcode == ISD::ATOMIC_CMP_SWAP_32 ||
3191 Opcode == ISD::ATOMIC_CMP_SWAP_64) && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003192 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003193
3194 MVT VT = Cmp.getValueType();
3195
3196 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3197 Alignment = getMVTAlignment(VT);
3198
3199 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003200 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003201 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003202 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003203 void* IP = 0;
3204 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003205 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003206 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003207 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003208 CSEMap.InsertNode(N, IP);
3209 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003210 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003211}
3212
Dan Gohman475871a2008-07-27 21:46:04 +00003213SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3214 SDValue Ptr, SDValue Val,
3215 const Value* PtrVal,
3216 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003217 assert((Opcode == ISD::ATOMIC_LOAD_ADD_8 ||
3218 Opcode == ISD::ATOMIC_LOAD_SUB_8 ||
3219 Opcode == ISD::ATOMIC_LOAD_AND_8 ||
3220 Opcode == ISD::ATOMIC_LOAD_OR_8 ||
3221 Opcode == ISD::ATOMIC_LOAD_XOR_8 ||
3222 Opcode == ISD::ATOMIC_LOAD_NAND_8 ||
3223 Opcode == ISD::ATOMIC_LOAD_MIN_8 ||
3224 Opcode == ISD::ATOMIC_LOAD_MAX_8 ||
3225 Opcode == ISD::ATOMIC_LOAD_UMIN_8 ||
3226 Opcode == ISD::ATOMIC_LOAD_UMAX_8 ||
3227 Opcode == ISD::ATOMIC_SWAP_8 ||
3228 Opcode == ISD::ATOMIC_LOAD_ADD_16 ||
3229 Opcode == ISD::ATOMIC_LOAD_SUB_16 ||
3230 Opcode == ISD::ATOMIC_LOAD_AND_16 ||
3231 Opcode == ISD::ATOMIC_LOAD_OR_16 ||
3232 Opcode == ISD::ATOMIC_LOAD_XOR_16 ||
3233 Opcode == ISD::ATOMIC_LOAD_NAND_16 ||
3234 Opcode == ISD::ATOMIC_LOAD_MIN_16 ||
3235 Opcode == ISD::ATOMIC_LOAD_MAX_16 ||
3236 Opcode == ISD::ATOMIC_LOAD_UMIN_16 ||
3237 Opcode == ISD::ATOMIC_LOAD_UMAX_16 ||
3238 Opcode == ISD::ATOMIC_SWAP_16 ||
3239 Opcode == ISD::ATOMIC_LOAD_ADD_32 ||
3240 Opcode == ISD::ATOMIC_LOAD_SUB_32 ||
3241 Opcode == ISD::ATOMIC_LOAD_AND_32 ||
3242 Opcode == ISD::ATOMIC_LOAD_OR_32 ||
3243 Opcode == ISD::ATOMIC_LOAD_XOR_32 ||
3244 Opcode == ISD::ATOMIC_LOAD_NAND_32 ||
3245 Opcode == ISD::ATOMIC_LOAD_MIN_32 ||
3246 Opcode == ISD::ATOMIC_LOAD_MAX_32 ||
3247 Opcode == ISD::ATOMIC_LOAD_UMIN_32 ||
3248 Opcode == ISD::ATOMIC_LOAD_UMAX_32 ||
3249 Opcode == ISD::ATOMIC_SWAP_32 ||
3250 Opcode == ISD::ATOMIC_LOAD_ADD_64 ||
3251 Opcode == ISD::ATOMIC_LOAD_SUB_64 ||
3252 Opcode == ISD::ATOMIC_LOAD_AND_64 ||
3253 Opcode == ISD::ATOMIC_LOAD_OR_64 ||
3254 Opcode == ISD::ATOMIC_LOAD_XOR_64 ||
3255 Opcode == ISD::ATOMIC_LOAD_NAND_64 ||
3256 Opcode == ISD::ATOMIC_LOAD_MIN_64 ||
3257 Opcode == ISD::ATOMIC_LOAD_MAX_64 ||
3258 Opcode == ISD::ATOMIC_LOAD_UMIN_64 ||
3259 Opcode == ISD::ATOMIC_LOAD_UMAX_64 ||
3260 Opcode == ISD::ATOMIC_SWAP_64) && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003261
3262 MVT VT = Val.getValueType();
3263
3264 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3265 Alignment = getMVTAlignment(VT);
3266
3267 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003268 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003270 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003271 void* IP = 0;
3272 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003273 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003274 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003275 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003276 CSEMap.InsertNode(N, IP);
3277 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003278 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003279}
3280
Duncan Sands4bdcb612008-07-02 17:40:58 +00003281/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3282/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003283SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3284 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003285 if (Simplify && NumOps == 1)
3286 return Ops[0];
3287
3288 SmallVector<MVT, 4> VTs;
3289 VTs.reserve(NumOps);
3290 for (unsigned i = 0; i < NumOps; ++i)
3291 VTs.push_back(Ops[i].getValueType());
3292 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3293}
3294
Dan Gohman475871a2008-07-27 21:46:04 +00003295SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003296SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003297 MVT VT, SDValue Chain,
3298 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003299 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003300 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003301 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3302 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003303
Duncan Sands14ea39c2008-03-27 20:23:40 +00003304 if (VT == EVT) {
3305 ExtType = ISD::NON_EXTLOAD;
3306 } else if (ExtType == ISD::NON_EXTLOAD) {
3307 assert(VT == EVT && "Non-extending load from different memory type!");
3308 } else {
3309 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003310 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003311 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3312 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003313 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003314 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003315 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003316 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003317 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003318 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003319 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003320 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003321
3322 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003323 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003324 "Unindexed load with an offset!");
3325
3326 SDVTList VTs = Indexed ?
3327 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003329 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003330 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003331 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003332 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003333 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003334 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003335 void *IP = 0;
3336 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003337 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003338 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003339 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3340 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003341 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003342 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003343 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003344}
3345
Dan Gohman475871a2008-07-27 21:46:04 +00003346SDValue SelectionDAG::getLoad(MVT VT,
3347 SDValue Chain, SDValue Ptr,
3348 const Value *SV, int SVOffset,
3349 bool isVolatile, unsigned Alignment) {
3350 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003351 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3352 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003353}
3354
Dan Gohman475871a2008-07-27 21:46:04 +00003355SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3356 SDValue Chain, SDValue Ptr,
3357 const Value *SV,
3358 int SVOffset, MVT EVT,
3359 bool isVolatile, unsigned Alignment) {
3360 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003361 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3362 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003363}
3364
Dan Gohman475871a2008-07-27 21:46:04 +00003365SDValue
3366SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3367 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003368 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003369 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3370 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003371 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3372 LD->getChain(), Base, Offset, LD->getSrcValue(),
3373 LD->getSrcValueOffset(), LD->getMemoryVT(),
3374 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003375}
3376
Dan Gohman475871a2008-07-27 21:46:04 +00003377SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3378 SDValue Ptr, const Value *SV, int SVOffset,
3379 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003381
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003382 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3383 Alignment = getMVTAlignment(VT);
3384
Evan Chengad071e12006-10-05 22:57:11 +00003385 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003386 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3387 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003388 FoldingSetNodeID ID;
3389 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003390 ID.AddInteger(ISD::UNINDEXED);
3391 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003392 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003393 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003394 void *IP = 0;
3395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003396 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003397 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003398 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3399 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003400 CSEMap.InsertNode(N, IP);
3401 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003402 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003403}
3404
Dan Gohman475871a2008-07-27 21:46:04 +00003405SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3406 SDValue Ptr, const Value *SV,
3407 int SVOffset, MVT SVT,
3408 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003410
3411 if (VT == SVT)
3412 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003413
Duncan Sands8e4eb092008-06-08 20:54:56 +00003414 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003415 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003416 "Can't do FP-INT conversion!");
3417
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003418 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3419 Alignment = getMVTAlignment(VT);
3420
Evan Cheng8b2794a2006-10-13 21:14:26 +00003421 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003422 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3423 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003424 FoldingSetNodeID ID;
3425 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003426 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003427 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003428 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003429 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003430 void *IP = 0;
3431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003432 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003433 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003434 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3435 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003436 CSEMap.InsertNode(N, IP);
3437 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003438 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003439}
3440
Dan Gohman475871a2008-07-27 21:46:04 +00003441SDValue
3442SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3443 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003444 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3445 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3446 "Store is already a indexed store!");
3447 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003448 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003449 FoldingSetNodeID ID;
3450 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3451 ID.AddInteger(AM);
3452 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003453 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003454 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003455 void *IP = 0;
3456 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003457 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003458 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003459 new (N) StoreSDNode(Ops, VTs, AM,
3460 ST->isTruncatingStore(), ST->getMemoryVT(),
3461 ST->getSrcValue(), ST->getSrcValueOffset(),
3462 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003463 CSEMap.InsertNode(N, IP);
3464 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003465 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003466}
3467
Dan Gohman475871a2008-07-27 21:46:04 +00003468SDValue SelectionDAG::getVAArg(MVT VT,
3469 SDValue Chain, SDValue Ptr,
3470 SDValue SV) {
3471 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003472 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003473}
3474
Dan Gohman475871a2008-07-27 21:46:04 +00003475SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3476 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003477 switch (NumOps) {
3478 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003479 case 1: return getNode(Opcode, VT, Ops[0]);
3480 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3481 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003482 default: break;
3483 }
3484
Dan Gohman475871a2008-07-27 21:46:04 +00003485 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003486 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003487 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3488 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003489}
3490
Dan Gohman475871a2008-07-27 21:46:04 +00003491SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3492 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003493 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003494 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003495 case 1: return getNode(Opcode, VT, Ops[0]);
3496 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3497 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003498 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003499 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003500
Chris Lattneref847df2005-04-09 03:27:28 +00003501 switch (Opcode) {
3502 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003503 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003504 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003505 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3506 "LHS and RHS of condition must have same type!");
3507 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3508 "True and False arms of SelectCC must have same type!");
3509 assert(Ops[2].getValueType() == VT &&
3510 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003511 break;
3512 }
3513 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003514 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003515 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3516 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003517 break;
3518 }
Chris Lattneref847df2005-04-09 03:27:28 +00003519 }
3520
Chris Lattner385328c2005-05-14 07:42:29 +00003521 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003522 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003523 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003524 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003525 FoldingSetNodeID ID;
3526 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003527 void *IP = 0;
3528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003529 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003530 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003531 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003532 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003533 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003534 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003535 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003536 }
Chris Lattneref847df2005-04-09 03:27:28 +00003537 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003538#ifndef NDEBUG
3539 VerifyNode(N);
3540#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003541 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003542}
3543
Dan Gohman475871a2008-07-27 21:46:04 +00003544SDValue SelectionDAG::getNode(unsigned Opcode,
3545 const std::vector<MVT> &ResultTys,
3546 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003547 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3548 Ops, NumOps);
3549}
3550
Dan Gohman475871a2008-07-27 21:46:04 +00003551SDValue SelectionDAG::getNode(unsigned Opcode,
3552 const MVT *VTs, unsigned NumVTs,
3553 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003554 if (NumVTs == 1)
3555 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003556 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3557}
3558
Dan Gohman475871a2008-07-27 21:46:04 +00003559SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3560 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003561 if (VTList.NumVTs == 1)
3562 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003563
Chris Lattner5f056bf2005-07-10 01:55:33 +00003564 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003565 // FIXME: figure out how to safely handle things like
3566 // int foo(int x) { return 1 << (x & 255); }
3567 // int bar() { return foo(256); }
3568#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003569 case ISD::SRA_PARTS:
3570 case ISD::SRL_PARTS:
3571 case ISD::SHL_PARTS:
3572 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003573 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003574 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3575 else if (N3.getOpcode() == ISD::AND)
3576 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3577 // If the and is only masking out bits that cannot effect the shift,
3578 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003579 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003580 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3581 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3582 }
3583 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003584#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003585 }
Chris Lattner89c34632005-05-14 06:20:26 +00003586
Chris Lattner43247a12005-08-25 19:12:10 +00003587 // Memoize the node unless it returns a flag.
3588 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003589 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003590 FoldingSetNodeID ID;
3591 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003592 void *IP = 0;
3593 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003594 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003595 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003596 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003597 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3598 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003599 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003600 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3601 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003602 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003603 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3604 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003605 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003606 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3607 }
Chris Lattnera5682852006-08-07 23:03:03 +00003608 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003609 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003610 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003611 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003612 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3613 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003614 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003615 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3616 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003617 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003618 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3619 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003620 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003621 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3622 }
Chris Lattner43247a12005-08-25 19:12:10 +00003623 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003624 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003625#ifndef NDEBUG
3626 VerifyNode(N);
3627#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003628 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003629}
3630
Dan Gohman475871a2008-07-27 21:46:04 +00003631SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003632 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003633}
3634
Dan Gohman475871a2008-07-27 21:46:04 +00003635SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3636 SDValue N1) {
3637 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003638 return getNode(Opcode, VTList, Ops, 1);
3639}
3640
Dan Gohman475871a2008-07-27 21:46:04 +00003641SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3642 SDValue N1, SDValue N2) {
3643 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003644 return getNode(Opcode, VTList, Ops, 2);
3645}
3646
Dan Gohman475871a2008-07-27 21:46:04 +00003647SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3648 SDValue N1, SDValue N2, SDValue N3) {
3649 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003650 return getNode(Opcode, VTList, Ops, 3);
3651}
3652
Dan Gohman475871a2008-07-27 21:46:04 +00003653SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3654 SDValue N1, SDValue N2, SDValue N3,
3655 SDValue N4) {
3656 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003657 return getNode(Opcode, VTList, Ops, 4);
3658}
3659
Dan Gohman475871a2008-07-27 21:46:04 +00003660SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3661 SDValue N1, SDValue N2, SDValue N3,
3662 SDValue N4, SDValue N5) {
3663 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003664 return getNode(Opcode, VTList, Ops, 5);
3665}
3666
Duncan Sands83ec4b62008-06-06 12:08:01 +00003667SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003668 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003669}
3670
Duncan Sands83ec4b62008-06-06 12:08:01 +00003671SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003672 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3673 E = VTList.rend(); I != E; ++I)
3674 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3675 return *I;
3676
3677 MVT *Array = Allocator.Allocate<MVT>(2);
3678 Array[0] = VT1;
3679 Array[1] = VT2;
3680 SDVTList Result = makeVTList(Array, 2);
3681 VTList.push_back(Result);
3682 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003683}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003684
3685SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3686 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3687 E = VTList.rend(); I != E; ++I)
3688 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3689 I->VTs[2] == VT3)
3690 return *I;
3691
3692 MVT *Array = Allocator.Allocate<MVT>(3);
3693 Array[0] = VT1;
3694 Array[1] = VT2;
3695 Array[2] = VT3;
3696 SDVTList Result = makeVTList(Array, 3);
3697 VTList.push_back(Result);
3698 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003699}
3700
Duncan Sands83ec4b62008-06-06 12:08:01 +00003701SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003702 switch (NumVTs) {
3703 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003704 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003705 case 2: return getVTList(VTs[0], VTs[1]);
3706 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3707 default: break;
3708 }
3709
Dan Gohmane8be6c62008-07-17 19:10:17 +00003710 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3711 E = VTList.rend(); I != E; ++I) {
3712 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3713 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003714
3715 bool NoMatch = false;
3716 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003717 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003718 NoMatch = true;
3719 break;
3720 }
3721 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003722 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003723 }
3724
Dan Gohmane8be6c62008-07-17 19:10:17 +00003725 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3726 std::copy(VTs, VTs+NumVTs, Array);
3727 SDVTList Result = makeVTList(Array, NumVTs);
3728 VTList.push_back(Result);
3729 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003730}
3731
3732
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003733/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3734/// specified operands. If the resultant node already exists in the DAG,
3735/// this does not modify the specified node, instead it returns the node that
3736/// already exists. If the resultant node does not exist in the DAG, the
3737/// input node is returned. As a degenerate case, if you specify the same
3738/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003739SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003740 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003741 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3742
3743 // Check to see if there is no change.
3744 if (Op == N->getOperand(0)) return InN;
3745
3746 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003747 void *InsertPos = 0;
3748 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003749 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003750
Dan Gohman79acd2b2008-07-21 22:38:59 +00003751 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003752 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003753 RemoveNodeFromCSEMaps(N);
3754
3755 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003756 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003757 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003758 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003759 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003760
3761 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003762 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003763 return InN;
3764}
3765
Dan Gohman475871a2008-07-27 21:46:04 +00003766SDValue SelectionDAG::
3767UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003768 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003769 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3770
3771 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003772 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3773 return InN; // No operands changed, just return the input node.
3774
3775 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003776 void *InsertPos = 0;
3777 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003778 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003779
Dan Gohman79acd2b2008-07-21 22:38:59 +00003780 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003781 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003782 RemoveNodeFromCSEMaps(N);
3783
3784 // Now we update the operands.
3785 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003786 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003787 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003788 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003789 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003790 }
3791 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003792 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003793 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003794 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003795 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003796 }
3797
3798 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003799 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003800 return InN;
3801}
3802
Dan Gohman475871a2008-07-27 21:46:04 +00003803SDValue SelectionDAG::
3804UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3805 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003806 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003807}
3808
Dan Gohman475871a2008-07-27 21:46:04 +00003809SDValue SelectionDAG::
3810UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3811 SDValue Op3, SDValue Op4) {
3812 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003813 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003814}
3815
Dan Gohman475871a2008-07-27 21:46:04 +00003816SDValue SelectionDAG::
3817UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3818 SDValue Op3, SDValue Op4, SDValue Op5) {
3819 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003820 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003821}
3822
Dan Gohman475871a2008-07-27 21:46:04 +00003823SDValue SelectionDAG::
3824UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003825 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003826 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003827 "Update with wrong number of operands");
3828
3829 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003830 bool AnyChange = false;
3831 for (unsigned i = 0; i != NumOps; ++i) {
3832 if (Ops[i] != N->getOperand(i)) {
3833 AnyChange = true;
3834 break;
3835 }
3836 }
3837
3838 // No operands changed, just return the input node.
3839 if (!AnyChange) return InN;
3840
3841 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003842 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003843 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003844 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003845
Dan Gohman7ceda162008-05-02 00:05:03 +00003846 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003847 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003848 RemoveNodeFromCSEMaps(N);
3849
3850 // Now we update the operands.
3851 for (unsigned i = 0; i != NumOps; ++i) {
3852 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003853 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003854 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003855 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003856 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003857 }
3858 }
3859
3860 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003861 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003862 return InN;
3863}
3864
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003865/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003866/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003867void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003868 // Unlike the code in MorphNodeTo that does this, we don't need to
3869 // watch for dead nodes here.
3870 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3871 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3872
3873 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003874}
Chris Lattner149c58c2005-08-16 18:17:10 +00003875
Dan Gohmane8be6c62008-07-17 19:10:17 +00003876/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3877/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003878///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003879SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003880 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003881 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003882 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003883}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003884
Dan Gohmane8be6c62008-07-17 19:10:17 +00003885SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003886 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003887 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003888 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003889 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003890}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003891
Dan Gohmane8be6c62008-07-17 19:10:17 +00003892SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003893 MVT VT, SDValue Op1,
3894 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003895 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003896 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003897 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003898}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003899
Dan Gohmane8be6c62008-07-17 19:10:17 +00003900SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003901 MVT VT, SDValue Op1,
3902 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003903 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003904 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003905 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003906}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003907
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003909 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003910 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003911 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003912 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003913}
3914
Dan Gohmane8be6c62008-07-17 19:10:17 +00003915SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003916 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003917 unsigned NumOps) {
3918 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003919 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003920}
3921
Dan Gohmane8be6c62008-07-17 19:10:17 +00003922SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003923 MVT VT1, MVT VT2) {
3924 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003925 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003926}
3927
Dan Gohmane8be6c62008-07-17 19:10:17 +00003928SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003929 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003930 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003931 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003932 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003933}
3934
Dan Gohmane8be6c62008-07-17 19:10:17 +00003935SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003936 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003937 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003938 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003939 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003940 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003941}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003942
Dan Gohmane8be6c62008-07-17 19:10:17 +00003943SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003944 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003946 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003947 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003948 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003949}
3950
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003952 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003953 SDValue Op1, SDValue Op2,
3954 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003955 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003957 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003958}
3959
Dan Gohmane8be6c62008-07-17 19:10:17 +00003960SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003961 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003962 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003963 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3964}
3965
3966SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3967 MVT VT) {
3968 SDVTList VTs = getVTList(VT);
3969 return MorphNodeTo(N, Opc, VTs, 0, 0);
3970}
3971
3972SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003973 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003974 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003975 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003976 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3977}
3978
3979SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003980 MVT VT, SDValue Op1,
3981 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003982 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003983 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003984 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3985}
3986
3987SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003988 MVT VT, SDValue Op1,
3989 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003990 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003991 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003992 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3993}
3994
3995SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003996 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003997 unsigned NumOps) {
3998 SDVTList VTs = getVTList(VT);
3999 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4000}
4001
4002SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004003 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004004 unsigned NumOps) {
4005 SDVTList VTs = getVTList(VT1, VT2);
4006 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4007}
4008
4009SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4010 MVT VT1, MVT VT2) {
4011 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004012 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004013}
4014
4015SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4016 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004017 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004018 SDVTList VTs = getVTList(VT1, VT2, VT3);
4019 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4020}
4021
4022SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4023 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004024 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004025 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004026 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004027 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4028}
4029
4030SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4031 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004032 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004033 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004034 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004035 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4036}
4037
4038SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4039 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004040 SDValue Op1, SDValue Op2,
4041 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004042 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4045}
4046
4047/// MorphNodeTo - These *mutate* the specified node to have the specified
4048/// return type, opcode, and operands.
4049///
4050/// Note that MorphNodeTo returns the resultant node. If there is already a
4051/// node of the specified opcode and operands, it returns that node instead of
4052/// the current one.
4053///
4054/// Using MorphNodeTo is faster than creating a new node and swapping it in
4055/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004056/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004057/// the node's users.
4058///
4059SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004060 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004061 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004062 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004063 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4065 FoldingSetNodeID ID;
4066 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4067 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4068 return ON;
4069 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004070
Chris Lattner0fb094f2005-11-19 01:44:53 +00004071 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00004072
Dan Gohmane8be6c62008-07-17 19:10:17 +00004073 // Start the morphing.
4074 N->NodeType = Opc;
4075 N->ValueList = VTs.VTs;
4076 N->NumValues = VTs.NumVTs;
4077
4078 // Clear the operands list, updating used nodes to remove this from their
4079 // use list. Keep track of any operands that become dead as a result.
4080 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4081 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4082 I != E; ++I) {
4083 SDNode *Used = I->getVal();
4084 Used->removeUser(std::distance(B, I), N);
4085 if (Used->use_empty())
4086 DeadNodeSet.insert(Used);
4087 }
4088
4089 // If NumOps is larger than the # of operands we currently have, reallocate
4090 // the operand list.
4091 if (NumOps > N->NumOperands) {
4092 if (N->OperandsNeedDelete)
4093 delete[] N->OperandList;
4094 if (N->isMachineOpcode()) {
4095 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004096 // remainder of the current SelectionDAG iteration, so we can allocate
4097 // the operands directly out of a pool with no recycling metadata.
4098 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004099 N->OperandsNeedDelete = false;
4100 } else {
4101 N->OperandList = new SDUse[NumOps];
4102 N->OperandsNeedDelete = true;
4103 }
4104 }
4105
4106 // Assign the new operands.
4107 N->NumOperands = NumOps;
4108 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4109 N->OperandList[i] = Ops[i];
4110 N->OperandList[i].setUser(N);
4111 SDNode *ToUse = N->OperandList[i].getVal();
4112 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004113 }
4114
4115 // Delete any nodes that are still dead after adding the uses for the
4116 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004117 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004118 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4119 E = DeadNodeSet.end(); I != E; ++I)
4120 if ((*I)->use_empty())
4121 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004122 RemoveDeadNodes(DeadNodes);
4123
Dan Gohmane8be6c62008-07-17 19:10:17 +00004124 if (IP)
4125 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004126 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004127}
4128
Chris Lattner0fb094f2005-11-19 01:44:53 +00004129
Evan Cheng6ae46c42006-02-09 07:15:23 +00004130/// getTargetNode - These are used for target selectors to create a new node
4131/// with specified return type(s), target opcode, and operands.
4132///
4133/// Note that getTargetNode returns the resultant node. If there is already a
4134/// node of the specified opcode and operands, it returns that node instead of
4135/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004137 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004138}
Dan Gohman475871a2008-07-27 21:46:04 +00004139SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004140 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004141}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004144 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004145}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004146SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SDValue Op1, SDValue Op2,
4148 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004149 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004150}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004151SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004152 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004153 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004154}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4156 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004158 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004159}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004160SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004161 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004162 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004163 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004164}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004165SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004166 MVT VT2, SDValue Op1,
4167 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004168 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004169 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004170 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004171}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004172SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004173 MVT VT2, SDValue Op1,
4174 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004175 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004176 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004177 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004178}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004179SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004180 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004183}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004184SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004185 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004186 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004187 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004188 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004189}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004190SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004191 SDValue Op1, SDValue Op2,
4192 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004193 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004194 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004195 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004196}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004198 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004199 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004200 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004201}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4203 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004204 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004205 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004206 VTList.push_back(VT1);
4207 VTList.push_back(VT2);
4208 VTList.push_back(VT3);
4209 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004210 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004211 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004212}
Evan Cheng39305cf2007-10-05 01:10:49 +00004213SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004214 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004215 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004216 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004217 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004218 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004219}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004220
Evan Cheng08b11732008-03-22 01:55:50 +00004221/// getNodeIfExists - Get the specified node if it's already available, or
4222/// else return NULL.
4223SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004224 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004225 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4226 FoldingSetNodeID ID;
4227 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4228 void *IP = 0;
4229 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4230 return E;
4231 }
4232 return NULL;
4233}
4234
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004235
Evan Cheng99157a02006-08-07 22:13:29 +00004236/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004237/// This can cause recursive merging of nodes in the DAG.
4238///
Chris Lattner11d049c2008-02-03 03:35:22 +00004239/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004240///
Dan Gohman475871a2008-07-27 21:46:04 +00004241void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004242 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004243 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004244 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004245 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004246 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004247
Chris Lattner8b8749f2005-08-17 19:00:20 +00004248 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004249 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004250 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004251
Chris Lattner8b8749f2005-08-17 19:00:20 +00004252 // This node is about to morph, remove its old self from the CSE maps.
4253 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004254 int operandNum = 0;
4255 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4256 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004257 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004258 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004259 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004260 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004261 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004262 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004263
4264 // Now that we have modified U, add it back to the CSE maps. If it already
4265 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004266 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004267 ReplaceAllUsesWith(U, Existing, UpdateListener);
4268 // U is now dead. Inform the listener if it exists and delete it.
4269 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004270 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004271 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004272 } else {
4273 // If the node doesn't already exist, we updated it. Inform a listener if
4274 // it exists.
4275 if (UpdateListener)
4276 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004277 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004278 }
4279}
4280
4281/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4282/// This can cause recursive merging of nodes in the DAG.
4283///
4284/// This version assumes From/To have matching types and numbers of result
4285/// values.
4286///
Chris Lattner1e111c72005-09-07 05:37:01 +00004287void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004288 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004289 assert(From->getVTList().VTs == To->getVTList().VTs &&
4290 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004291 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004292
4293 // Handle the trivial case.
4294 if (From == To)
4295 return;
4296
Chris Lattner8b52f212005-08-26 18:36:28 +00004297 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004298 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004299 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004300
Chris Lattner8b52f212005-08-26 18:36:28 +00004301 // This node is about to morph, remove its old self from the CSE maps.
4302 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004303 int operandNum = 0;
4304 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4305 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004306 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004307 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004309 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004310 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004311
Chris Lattner8b8749f2005-08-17 19:00:20 +00004312 // Now that we have modified U, add it back to the CSE maps. If it already
4313 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004314 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004315 ReplaceAllUsesWith(U, Existing, UpdateListener);
4316 // U is now dead. Inform the listener if it exists and delete it.
4317 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004318 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004319 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004320 } else {
4321 // If the node doesn't already exist, we updated it. Inform a listener if
4322 // it exists.
4323 if (UpdateListener)
4324 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004325 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004326 }
4327}
4328
Chris Lattner8b52f212005-08-26 18:36:28 +00004329/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4330/// This can cause recursive merging of nodes in the DAG.
4331///
4332/// This version can replace From with any result values. To must match the
4333/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004334void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004335 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004336 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004337 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004338 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004339
4340 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004341 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004342 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004343
Chris Lattner7b2880c2005-08-24 22:44:39 +00004344 // This node is about to morph, remove its old self from the CSE maps.
4345 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004346 int operandNum = 0;
4347 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4348 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004349 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004350 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004351 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004352 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004353 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004354 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004355 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004356
Chris Lattner7b2880c2005-08-24 22:44:39 +00004357 // Now that we have modified U, add it back to the CSE maps. If it already
4358 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004359 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004360 ReplaceAllUsesWith(U, Existing, UpdateListener);
4361 // U is now dead. Inform the listener if it exists and delete it.
4362 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004363 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004364 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004365 } else {
4366 // If the node doesn't already exist, we updated it. Inform a listener if
4367 // it exists.
4368 if (UpdateListener)
4369 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004370 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004371 }
4372}
4373
Chris Lattner012f2412006-02-17 21:58:01 +00004374/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004375/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004376/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004377void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004378 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004379 // Handle the really simple, really trivial case efficiently.
4380 if (From == To) return;
4381
Chris Lattner012f2412006-02-17 21:58:01 +00004382 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004383 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004384 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004385 return;
4386 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004387
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004389 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004390 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004391
4392 while (!Users.empty()) {
4393 // We know that this user uses some value of From. If it is the right
4394 // value, update it.
4395 SDNode *User = Users.back();
4396 Users.pop_back();
4397
Chris Lattner01d029b2007-10-15 06:10:22 +00004398 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004399 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004400 for (; Op != E; ++Op)
4401 if (*Op == From) break;
4402
4403 // If there are no matches, the user must use some other result of From.
4404 if (Op == E) continue;
4405
4406 // Okay, we know this user needs to be updated. Remove its old self
4407 // from the CSE maps.
4408 RemoveNodeFromCSEMaps(User);
4409
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004410 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004411 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004412 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004413 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004414 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004415 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004416 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004417 }
4418 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004419
4420 // Now that we have modified User, add it back to the CSE maps. If it
4421 // already exists there, recursively merge the results together.
4422 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004423 if (!Existing) {
4424 if (UpdateListener) UpdateListener->NodeUpdated(User);
4425 continue; // Continue on to next user.
4426 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004427
4428 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004429 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004430 // recursive merging of other unrelated nodes down the line.
4431 ReplaceAllUsesWith(User, Existing, UpdateListener);
4432
4433 // User is now dead. Notify a listener if present.
4434 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4435 DeleteNodeNotInCSEMaps(User);
4436 }
4437}
4438
4439/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004440/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004441/// appear in both the From and To list. The Deleted vector is
4442/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004443void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4444 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004445 unsigned Num,
4446 DAGUpdateListener *UpdateListener){
4447 // Handle the simple, trivial case efficiently.
4448 if (Num == 1)
4449 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4450
4451 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4452 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004453 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4454 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004455 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004456
4457 while (!Users.empty()) {
4458 // We know that this user uses some value of From. If it is the right
4459 // value, update it.
4460 SDNode *User = Users.back().first;
4461 unsigned i = Users.back().second;
4462 Users.pop_back();
4463
4464 // Scan for an operand that matches From.
4465 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4466 for (; Op != E; ++Op)
4467 if (*Op == From[i]) break;
4468
4469 // If there are no matches, the user must use some other result of From.
4470 if (Op == E) continue;
4471
4472 // Okay, we know this user needs to be updated. Remove its old self
4473 // from the CSE maps.
4474 RemoveNodeFromCSEMaps(User);
4475
4476 // Update all operands that match "From" in case there are multiple uses.
4477 for (; Op != E; ++Op) {
4478 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004479 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004480 *Op = To[i];
4481 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004482 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004483 }
4484 }
4485
4486 // Now that we have modified User, add it back to the CSE maps. If it
4487 // already exists there, recursively merge the results together.
4488 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4489 if (!Existing) {
4490 if (UpdateListener) UpdateListener->NodeUpdated(User);
4491 continue; // Continue on to next user.
4492 }
4493
4494 // If there was already an existing matching node, use ReplaceAllUsesWith
4495 // to replace the dead one with the existing one. This can cause
4496 // recursive merging of other unrelated nodes down the line.
4497 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004498
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004499 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004500 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004501 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004502 }
4503}
4504
Evan Chenge6f35d82006-08-01 08:20:41 +00004505/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004506/// based on their topological order. It returns the maximum id and a vector
4507/// of the SDNodes* in assigned order by reference.
4508unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004509 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004510 std::vector<SDNode*> Sources;
4511
Evan Chenge6f35d82006-08-01 08:20:41 +00004512 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4513 SDNode *N = I;
4514 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004515 // Temporarily use the Node Id as scratch space for the degree count.
4516 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004517 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004518 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004519 }
4520
Evan Cheng99157a02006-08-07 22:13:29 +00004521 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004522 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004523 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004524 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004525 SDNode *N = Sources.back();
4526 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004527 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004528 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004529 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004530 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004531 unsigned Degree = P->getNodeId();
4532 --Degree;
4533 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004534 if (Degree == 0)
4535 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004536 }
4537 }
4538
Evan Chengc384d6c2006-08-02 22:00:34 +00004539 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004540}
4541
4542
Evan Cheng091cba12006-07-27 06:39:06 +00004543
Jim Laskey58b968b2005-08-17 20:08:02 +00004544//===----------------------------------------------------------------------===//
4545// SDNode Class
4546//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004547
Chris Lattner917d2c92006-07-19 00:00:37 +00004548// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004549void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004550void UnarySDNode::ANCHOR() {}
4551void BinarySDNode::ANCHOR() {}
4552void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004553void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004554void ConstantSDNode::ANCHOR() {}
4555void ConstantFPSDNode::ANCHOR() {}
4556void GlobalAddressSDNode::ANCHOR() {}
4557void FrameIndexSDNode::ANCHOR() {}
4558void JumpTableSDNode::ANCHOR() {}
4559void ConstantPoolSDNode::ANCHOR() {}
4560void BasicBlockSDNode::ANCHOR() {}
4561void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004562void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004563void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004564void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004565void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004566void ExternalSymbolSDNode::ANCHOR() {}
4567void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004568void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004569void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004570void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004571void LoadSDNode::ANCHOR() {}
4572void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004573void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004574
Chris Lattner48b85922007-02-04 02:41:42 +00004575HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004576 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004577}
4578
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004579GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004580 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004581 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004582 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004583 // Thread Local
4584 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4585 // Non Thread Local
4586 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4587 getSDVTList(VT)), Offset(o) {
4588 TheGlobal = const_cast<GlobalValue*>(GA);
4589}
Chris Lattner48b85922007-02-04 02:41:42 +00004590
Dan Gohman1ea58a52008-07-09 22:08:04 +00004591MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004592 const Value *srcValue, int SVO,
4593 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004594 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004595 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004596
4597 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4598 assert(getAlignment() == alignment && "Alignment representation error!");
4599 assert(isVolatile() == vol && "Volatile representation error!");
4600}
4601
Dan Gohman36b5c132008-04-07 19:35:22 +00004602/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004603/// reference performed by this memory reference.
4604MachineMemOperand MemSDNode::getMemOperand() const {
4605 int Flags;
4606 if (isa<LoadSDNode>(this))
4607 Flags = MachineMemOperand::MOLoad;
4608 else if (isa<StoreSDNode>(this))
4609 Flags = MachineMemOperand::MOStore;
4610 else {
4611 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4612 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4613 }
4614
4615 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004616 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4617
Dan Gohman1ea58a52008-07-09 22:08:04 +00004618 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004619 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004620 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004621 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004622 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4623 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004624 else
4625 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4626 Size, getAlignment());
4627}
4628
Jim Laskey583bd472006-10-27 23:46:08 +00004629/// Profile - Gather unique data for the node.
4630///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004631void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004632 AddNodeIDNode(ID, this);
4633}
4634
Chris Lattnera3255112005-11-08 23:30:28 +00004635/// getValueTypeList - Return a pointer to the specified value type.
4636///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004637const MVT *SDNode::getValueTypeList(MVT VT) {
4638 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004639 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004640 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004641 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004642 static MVT VTs[MVT::LAST_VALUETYPE];
4643 VTs[VT.getSimpleVT()] = VT;
4644 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004645 }
Chris Lattnera3255112005-11-08 23:30:28 +00004646}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004647
Chris Lattner5c884562005-01-12 18:37:47 +00004648/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4649/// indicated value. This method ignores uses of other values defined by this
4650/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004651bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004652 assert(Value < getNumValues() && "Bad value!");
4653
Roman Levensteindc1adac2008-04-07 10:06:32 +00004654 // TODO: Only iterate over uses of a given value of the node
4655 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004656 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004657 if (NUses == 0)
4658 return false;
4659 --NUses;
4660 }
Chris Lattner5c884562005-01-12 18:37:47 +00004661 }
4662
4663 // Found exactly the right number of uses?
4664 return NUses == 0;
4665}
4666
4667
Evan Cheng33d55952007-08-02 05:29:38 +00004668/// hasAnyUseOfValue - Return true if there are any use of the indicated
4669/// value. This method ignores uses of other values defined by this operation.
4670bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4671 assert(Value < getNumValues() && "Bad value!");
4672
Dan Gohman1373c1c2008-07-09 22:39:01 +00004673 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004674 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004675 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004676
4677 return false;
4678}
4679
4680
Dan Gohman2a629952008-07-27 18:06:42 +00004681/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004682///
Dan Gohman2a629952008-07-27 18:06:42 +00004683bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004684 bool Seen = false;
4685 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004686 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004687 if (User == this)
4688 Seen = true;
4689 else
4690 return false;
4691 }
4692
4693 return Seen;
4694}
4695
Evan Chenge6e97e62006-11-03 07:31:32 +00004696/// isOperand - Return true if this node is an operand of N.
4697///
Dan Gohman475871a2008-07-27 21:46:04 +00004698bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004699 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4700 if (*this == N->getOperand(i))
4701 return true;
4702 return false;
4703}
4704
Evan Cheng917be682008-03-04 00:41:45 +00004705bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004706 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004707 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004708 return true;
4709 return false;
4710}
Evan Cheng4ee62112006-02-05 06:29:23 +00004711
Chris Lattner572dee72008-01-16 05:49:24 +00004712/// reachesChainWithoutSideEffects - Return true if this operand (which must
4713/// be a chain) reaches the specified operand without crossing any
4714/// side-effecting instructions. In practice, this looks through token
4715/// factors and non-volatile loads. In order to remain efficient, this only
4716/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004717bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004718 unsigned Depth) const {
4719 if (*this == Dest) return true;
4720
4721 // Don't search too deeply, we just want to be able to see through
4722 // TokenFactor's etc.
4723 if (Depth == 0) return false;
4724
4725 // If this is a token factor, all inputs to the TF happen in parallel. If any
4726 // of the operands of the TF reach dest, then we can do the xform.
4727 if (getOpcode() == ISD::TokenFactor) {
4728 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4729 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4730 return true;
4731 return false;
4732 }
4733
4734 // Loads don't have side effects, look through them.
4735 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4736 if (!Ld->isVolatile())
4737 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4738 }
4739 return false;
4740}
4741
4742
Evan Chengc5fc57d2006-11-03 03:05:24 +00004743static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004744 SmallPtrSet<SDNode *, 32> &Visited) {
4745 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004746 return;
4747
4748 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004749 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004750 if (Op == P) {
4751 found = true;
4752 return;
4753 }
4754 findPredecessor(Op, P, found, Visited);
4755 }
4756}
4757
Evan Cheng917be682008-03-04 00:41:45 +00004758/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004759/// is either an operand of N or it can be reached by recursively traversing
4760/// up the operands.
4761/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004762bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004763 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004764 bool found = false;
4765 findPredecessor(N, this, found, Visited);
4766 return found;
4767}
4768
Evan Chengc5484282006-10-04 00:56:09 +00004769uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4770 assert(Num < NumOperands && "Invalid child # of SDNode!");
4771 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4772}
4773
Reid Spencer577cc322007-04-01 07:32:19 +00004774std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004775 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004776 default:
4777 if (getOpcode() < ISD::BUILTIN_OP_END)
4778 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004779 if (isMachineOpcode()) {
4780 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004781 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004782 if (getMachineOpcode() < TII->getNumOpcodes())
4783 return TII->get(getMachineOpcode()).getName();
4784 return "<<Unknown Machine Node>>";
4785 }
4786 if (G) {
4787 TargetLowering &TLI = G->getTargetLoweringInfo();
4788 const char *Name = TLI.getTargetNodeName(getOpcode());
4789 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004790 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004791 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004792 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004793
Dan Gohmane8be6c62008-07-17 19:10:17 +00004794#ifndef NDEBUG
4795 case ISD::DELETED_NODE:
4796 return "<<Deleted Node!>>";
4797#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004798 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004799 case ISD::MEMBARRIER: return "MemBarrier";
Dale Johannesene00a8a22008-08-28 02:44:49 +00004800 case ISD::ATOMIC_CMP_SWAP_8: return "AtomicCmpSwap8";
4801 case ISD::ATOMIC_SWAP_8: return "AtomicSwap8";
4802 case ISD::ATOMIC_LOAD_ADD_8: return "AtomicLoadAdd8";
4803 case ISD::ATOMIC_LOAD_SUB_8: return "AtomicLoadSub8";
4804 case ISD::ATOMIC_LOAD_AND_8: return "AtomicLoadAnd8";
4805 case ISD::ATOMIC_LOAD_OR_8: return "AtomicLoadOr8";
4806 case ISD::ATOMIC_LOAD_XOR_8: return "AtomicLoadXor8";
4807 case ISD::ATOMIC_LOAD_NAND_8: return "AtomicLoadNand8";
4808 case ISD::ATOMIC_LOAD_MIN_8: return "AtomicLoadMin8";
4809 case ISD::ATOMIC_LOAD_MAX_8: return "AtomicLoadMax8";
4810 case ISD::ATOMIC_LOAD_UMIN_8: return "AtomicLoadUMin8";
4811 case ISD::ATOMIC_LOAD_UMAX_8: return "AtomicLoadUMax8";
4812 case ISD::ATOMIC_CMP_SWAP_16: return "AtomicCmpSwap16";
4813 case ISD::ATOMIC_SWAP_16: return "AtomicSwap16";
4814 case ISD::ATOMIC_LOAD_ADD_16: return "AtomicLoadAdd16";
4815 case ISD::ATOMIC_LOAD_SUB_16: return "AtomicLoadSub16";
4816 case ISD::ATOMIC_LOAD_AND_16: return "AtomicLoadAnd16";
4817 case ISD::ATOMIC_LOAD_OR_16: return "AtomicLoadOr16";
4818 case ISD::ATOMIC_LOAD_XOR_16: return "AtomicLoadXor16";
4819 case ISD::ATOMIC_LOAD_NAND_16: return "AtomicLoadNand16";
4820 case ISD::ATOMIC_LOAD_MIN_16: return "AtomicLoadMin16";
4821 case ISD::ATOMIC_LOAD_MAX_16: return "AtomicLoadMax16";
4822 case ISD::ATOMIC_LOAD_UMIN_16: return "AtomicLoadUMin16";
4823 case ISD::ATOMIC_LOAD_UMAX_16: return "AtomicLoadUMax16";
4824 case ISD::ATOMIC_CMP_SWAP_32: return "AtomicCmpSwap32";
4825 case ISD::ATOMIC_SWAP_32: return "AtomicSwap32";
4826 case ISD::ATOMIC_LOAD_ADD_32: return "AtomicLoadAdd32";
4827 case ISD::ATOMIC_LOAD_SUB_32: return "AtomicLoadSub32";
4828 case ISD::ATOMIC_LOAD_AND_32: return "AtomicLoadAnd32";
4829 case ISD::ATOMIC_LOAD_OR_32: return "AtomicLoadOr32";
4830 case ISD::ATOMIC_LOAD_XOR_32: return "AtomicLoadXor32";
4831 case ISD::ATOMIC_LOAD_NAND_32: return "AtomicLoadNand32";
4832 case ISD::ATOMIC_LOAD_MIN_32: return "AtomicLoadMin32";
4833 case ISD::ATOMIC_LOAD_MAX_32: return "AtomicLoadMax32";
4834 case ISD::ATOMIC_LOAD_UMIN_32: return "AtomicLoadUMin32";
4835 case ISD::ATOMIC_LOAD_UMAX_32: return "AtomicLoadUMax32";
4836 case ISD::ATOMIC_CMP_SWAP_64: return "AtomicCmpSwap64";
4837 case ISD::ATOMIC_SWAP_64: return "AtomicSwap64";
4838 case ISD::ATOMIC_LOAD_ADD_64: return "AtomicLoadAdd64";
4839 case ISD::ATOMIC_LOAD_SUB_64: return "AtomicLoadSub64";
4840 case ISD::ATOMIC_LOAD_AND_64: return "AtomicLoadAnd64";
4841 case ISD::ATOMIC_LOAD_OR_64: return "AtomicLoadOr64";
4842 case ISD::ATOMIC_LOAD_XOR_64: return "AtomicLoadXor64";
4843 case ISD::ATOMIC_LOAD_NAND_64: return "AtomicLoadNand64";
4844 case ISD::ATOMIC_LOAD_MIN_64: return "AtomicLoadMin64";
4845 case ISD::ATOMIC_LOAD_MAX_64: return "AtomicLoadMax64";
4846 case ISD::ATOMIC_LOAD_UMIN_64: return "AtomicLoadUMin64";
4847 case ISD::ATOMIC_LOAD_UMAX_64: return "AtomicLoadUMax64";
Andrew Lenharth95762122005-03-31 21:24:06 +00004848 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004849 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004850 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004851 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004852 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004853 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004854 case ISD::AssertSext: return "AssertSext";
4855 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004856
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004857 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004858 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004859 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004860 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004861
4862 case ISD::Constant: return "Constant";
4863 case ISD::ConstantFP: return "ConstantFP";
4864 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004865 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004866 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004867 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004868 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004869 case ISD::RETURNADDR: return "RETURNADDR";
4870 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004871 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004872 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4873 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004874 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004875 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004876 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004877 case ISD::INTRINSIC_WO_CHAIN: {
4878 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4879 return Intrinsic::getName((Intrinsic::ID)IID);
4880 }
4881 case ISD::INTRINSIC_VOID:
4882 case ISD::INTRINSIC_W_CHAIN: {
4883 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004884 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004885 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004886
Chris Lattnerb2827b02006-03-19 00:52:58 +00004887 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004888 case ISD::TargetConstant: return "TargetConstant";
4889 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004890 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004891 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004892 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004893 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004894 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004895 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004896
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004897 case ISD::CopyToReg: return "CopyToReg";
4898 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004899 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004900 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004901 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004902 case ISD::DBG_LABEL: return "dbg_label";
4903 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004904 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004905 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004906 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004907 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004908
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004909 // Unary operators
4910 case ISD::FABS: return "fabs";
4911 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004912 case ISD::FSQRT: return "fsqrt";
4913 case ISD::FSIN: return "fsin";
4914 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004915 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004916 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004917 case ISD::FTRUNC: return "ftrunc";
4918 case ISD::FFLOOR: return "ffloor";
4919 case ISD::FCEIL: return "fceil";
4920 case ISD::FRINT: return "frint";
4921 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004922
4923 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004924 case ISD::ADD: return "add";
4925 case ISD::SUB: return "sub";
4926 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004927 case ISD::MULHU: return "mulhu";
4928 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004929 case ISD::SDIV: return "sdiv";
4930 case ISD::UDIV: return "udiv";
4931 case ISD::SREM: return "srem";
4932 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004933 case ISD::SMUL_LOHI: return "smul_lohi";
4934 case ISD::UMUL_LOHI: return "umul_lohi";
4935 case ISD::SDIVREM: return "sdivrem";
4936 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004937 case ISD::AND: return "and";
4938 case ISD::OR: return "or";
4939 case ISD::XOR: return "xor";
4940 case ISD::SHL: return "shl";
4941 case ISD::SRA: return "sra";
4942 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004943 case ISD::ROTL: return "rotl";
4944 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004945 case ISD::FADD: return "fadd";
4946 case ISD::FSUB: return "fsub";
4947 case ISD::FMUL: return "fmul";
4948 case ISD::FDIV: return "fdiv";
4949 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004950 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004951 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004952
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004953 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004954 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004955 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004956 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004957 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004958 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004959 case ISD::CONCAT_VECTORS: return "concat_vectors";
4960 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004961 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004962 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004963 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004964 case ISD::ADDC: return "addc";
4965 case ISD::ADDE: return "adde";
4966 case ISD::SUBC: return "subc";
4967 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004968 case ISD::SHL_PARTS: return "shl_parts";
4969 case ISD::SRA_PARTS: return "sra_parts";
4970 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004971
4972 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4973 case ISD::INSERT_SUBREG: return "insert_subreg";
4974
Chris Lattner7f644642005-04-28 21:44:03 +00004975 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004976 case ISD::SIGN_EXTEND: return "sign_extend";
4977 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004978 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004979 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004980 case ISD::TRUNCATE: return "truncate";
4981 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004982 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004983 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004984 case ISD::FP_EXTEND: return "fp_extend";
4985
4986 case ISD::SINT_TO_FP: return "sint_to_fp";
4987 case ISD::UINT_TO_FP: return "uint_to_fp";
4988 case ISD::FP_TO_SINT: return "fp_to_sint";
4989 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004990 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004991
4992 // Control flow instructions
4993 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004994 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004995 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004996 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004997 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004998 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004999 case ISD::CALLSEQ_START: return "callseq_start";
5000 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005001
5002 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005003 case ISD::LOAD: return "load";
5004 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005005 case ISD::VAARG: return "vaarg";
5006 case ISD::VACOPY: return "vacopy";
5007 case ISD::VAEND: return "vaend";
5008 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005009 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005010 case ISD::EXTRACT_ELEMENT: return "extract_element";
5011 case ISD::BUILD_PAIR: return "build_pair";
5012 case ISD::STACKSAVE: return "stacksave";
5013 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005014 case ISD::TRAP: return "trap";
5015
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005016 // Bit manipulation
5017 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005018 case ISD::CTPOP: return "ctpop";
5019 case ISD::CTTZ: return "cttz";
5020 case ISD::CTLZ: return "ctlz";
5021
Chris Lattner36ce6912005-11-29 06:21:05 +00005022 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005023 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005024 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005025
Duncan Sands36397f52007-07-27 12:58:54 +00005026 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005027 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005028
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005029 case ISD::CONDCODE:
5030 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005031 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005032 case ISD::SETOEQ: return "setoeq";
5033 case ISD::SETOGT: return "setogt";
5034 case ISD::SETOGE: return "setoge";
5035 case ISD::SETOLT: return "setolt";
5036 case ISD::SETOLE: return "setole";
5037 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005038
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005039 case ISD::SETO: return "seto";
5040 case ISD::SETUO: return "setuo";
5041 case ISD::SETUEQ: return "setue";
5042 case ISD::SETUGT: return "setugt";
5043 case ISD::SETUGE: return "setuge";
5044 case ISD::SETULT: return "setult";
5045 case ISD::SETULE: return "setule";
5046 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005047
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005048 case ISD::SETEQ: return "seteq";
5049 case ISD::SETGT: return "setgt";
5050 case ISD::SETGE: return "setge";
5051 case ISD::SETLT: return "setlt";
5052 case ISD::SETLE: return "setle";
5053 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005054 }
5055 }
5056}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005057
Evan Cheng144d8f02006-11-09 17:55:04 +00005058const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005059 switch (AM) {
5060 default:
5061 return "";
5062 case ISD::PRE_INC:
5063 return "<pre-inc>";
5064 case ISD::PRE_DEC:
5065 return "<pre-dec>";
5066 case ISD::POST_INC:
5067 return "<post-inc>";
5068 case ISD::POST_DEC:
5069 return "<post-dec>";
5070 }
5071}
5072
Duncan Sands276dcbd2008-03-21 09:14:45 +00005073std::string ISD::ArgFlagsTy::getArgFlagsString() {
5074 std::string S = "< ";
5075
5076 if (isZExt())
5077 S += "zext ";
5078 if (isSExt())
5079 S += "sext ";
5080 if (isInReg())
5081 S += "inreg ";
5082 if (isSRet())
5083 S += "sret ";
5084 if (isByVal())
5085 S += "byval ";
5086 if (isNest())
5087 S += "nest ";
5088 if (getByValAlign())
5089 S += "byval-align:" + utostr(getByValAlign()) + " ";
5090 if (getOrigAlign())
5091 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5092 if (getByValSize())
5093 S += "byval-size:" + utostr(getByValSize()) + " ";
5094 return S + ">";
5095}
5096
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005097void SDNode::dump() const { dump(0); }
5098void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005099 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005100 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005101}
5102
5103void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5104 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005105
5106 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005107 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005108 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005109 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005110 else
Chris Lattner944fac72008-08-23 22:23:09 +00005111 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005112 }
Chris Lattner944fac72008-08-23 22:23:09 +00005113 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005114
Chris Lattner944fac72008-08-23 22:23:09 +00005115 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005116 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005117 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005118 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005119 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005120 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005121 }
5122
Evan Chengce254432007-12-11 02:08:35 +00005123 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005124 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005125 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005126 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005127 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005128 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005129 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005130 else
Chris Lattner944fac72008-08-23 22:23:09 +00005131 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
Evan Chengce254432007-12-11 02:08:35 +00005132 }
Chris Lattner944fac72008-08-23 22:23:09 +00005133 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005134 }
5135
Chris Lattnerc3aae252005-01-07 07:46:32 +00005136 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005137 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005138 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005139 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005140 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005141 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005142 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005143 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005144 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005145 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005146 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005147 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005148 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005149 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005150 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005151 OS << '<';
5152 WriteAsOperand(OS, GADN->getGlobal());
5153 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005154 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005155 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005156 else
Chris Lattner944fac72008-08-23 22:23:09 +00005157 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005158 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005159 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005160 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005161 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005162 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005163 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005164 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005165 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005166 else
Chris Lattner944fac72008-08-23 22:23:09 +00005167 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005168 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005169 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005170 else
Chris Lattner944fac72008-08-23 22:23:09 +00005171 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005172 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005173 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005174 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5175 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005176 OS << LBB->getName() << " ";
5177 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005178 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005179 if (G && R->getReg() &&
5180 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005181 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005182 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005183 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005184 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005185 } else if (const ExternalSymbolSDNode *ES =
5186 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005187 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005188 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5189 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005190 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005191 else
Chris Lattner944fac72008-08-23 22:23:09 +00005192 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005193 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5194 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005195 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005196 else
Chris Lattner944fac72008-08-23 22:23:09 +00005197 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005198 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005199 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005200 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005201 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005202 }
5203 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005204 const Value *SrcValue = LD->getSrcValue();
5205 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005206 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005207 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005208 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005209 else
Chris Lattner944fac72008-08-23 22:23:09 +00005210 OS << "null";
5211 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005212
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005213 bool doExt = true;
5214 switch (LD->getExtensionType()) {
5215 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005216 case ISD::EXTLOAD: OS << " <anyext "; break;
5217 case ISD::SEXTLOAD: OS << " <sext "; break;
5218 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005219 }
5220 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005221 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005222
Evan Cheng144d8f02006-11-09 17:55:04 +00005223 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005224 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005225 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005226 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005227 OS << " <volatile>";
5228 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005229 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005230 const Value *SrcValue = ST->getSrcValue();
5231 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005232 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005233 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005234 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005235 else
Chris Lattner944fac72008-08-23 22:23:09 +00005236 OS << "null";
5237 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005238
5239 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005240 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005241
5242 const char *AM = getIndexedModeName(ST->getAddressingMode());
5243 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005244 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005245 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005246 OS << " <volatile>";
5247 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005248 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5249 const Value *SrcValue = AT->getSrcValue();
5250 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005251 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005252 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005253 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005254 else
Chris Lattner944fac72008-08-23 22:23:09 +00005255 OS << "null";
5256 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005257 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005258 OS << " <volatile>";
5259 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005260 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005261}
5262
Chris Lattnerde202b32005-11-09 23:47:37 +00005263static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005264 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005265 if (N->getOperand(i).getNode()->hasOneUse())
5266 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005267 else
Bill Wendling832171c2006-12-07 20:04:42 +00005268 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005269 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005270
Chris Lattnerea946cd2005-01-09 20:38:33 +00005271
Bill Wendling832171c2006-12-07 20:04:42 +00005272 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005273 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005274}
5275
Chris Lattnerc3aae252005-01-07 07:46:32 +00005276void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005277 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005278
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005279 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5280 I != E; ++I) {
5281 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005282 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005283 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005284 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005285
Gabor Greifba36cb52008-08-28 21:40:38 +00005286 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005287
Bill Wendling832171c2006-12-07 20:04:42 +00005288 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005289}
5290
Evan Chengd6594ae2006-09-12 21:00:35 +00005291const Type *ConstantPoolSDNode::getType() const {
5292 if (isMachineConstantPoolEntry())
5293 return Val.MachineCPVal->getType();
5294 return Val.ConstVal->getType();
5295}