blob: 6ebd51499990cc7af082a6f96e2dca410d08e9fd [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 {
Dan Gohman4fbd7962008-09-12 18:08:03 +000072 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000073}
74
Duncan Sands83ec4b62008-06-06 12:08:01 +000075bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000076 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000077 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000078
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000079 // PPC long double cannot be converted to any other type.
80 if (VT == MVT::ppcf128 ||
81 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000082 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000140 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000196/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000197bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000199 if (N->getOpcode() == ISD::DBG_LABEL)
200 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000201 if (N->isMachineOpcode() &&
202 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000203 return true;
204 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000205}
206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
208/// when given the operation for (X op Y).
209ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
210 // To perform this operation, we just need to swap the L and G bits of the
211 // operation.
212 unsigned OldL = (Operation >> 2) & 1;
213 unsigned OldG = (Operation >> 1) & 1;
214 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
215 (OldL << 1) | // New G bit
216 (OldG << 2)); // New L bit.
217}
218
219/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
220/// 'op' is a valid SetCC operation.
221ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
222 unsigned Operation = Op;
223 if (isInteger)
224 Operation ^= 7; // Flip L, G, E bits, but not U.
225 else
226 Operation ^= 15; // Flip all of the condition bits.
227 if (Operation > ISD::SETTRUE2)
228 Operation &= ~8; // Don't let N and U bits get set.
229 return ISD::CondCode(Operation);
230}
231
232
233/// isSignedOp - For an integer comparison, return 1 if the comparison is a
234/// signed operation and 2 if the result is an unsigned comparison. Return zero
235/// if the operation does not depend on the sign of the input (setne and seteq).
236static int isSignedOp(ISD::CondCode Opcode) {
237 switch (Opcode) {
238 default: assert(0 && "Illegal integer setcc operation!");
239 case ISD::SETEQ:
240 case ISD::SETNE: return 0;
241 case ISD::SETLT:
242 case ISD::SETLE:
243 case ISD::SETGT:
244 case ISD::SETGE: return 1;
245 case ISD::SETULT:
246 case ISD::SETULE:
247 case ISD::SETUGT:
248 case ISD::SETUGE: return 2;
249 }
250}
251
252/// getSetCCOrOperation - Return the result of a logical OR between different
253/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
254/// returns SETCC_INVALID if it is not possible to represent the resultant
255/// comparison.
256ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
257 bool isInteger) {
258 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
259 // Cannot fold a signed integer setcc with an unsigned integer setcc.
260 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 // If the N and U bits get set then the resultant comparison DOES suddenly
265 // care about orderedness, and is true when ordered.
266 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000267 Op &= ~16; // Clear the U bit if the N bit is set.
268
269 // Canonicalize illegal integer setcc's.
270 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
271 Op = ISD::SETNE;
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Op);
274}
275
276/// getSetCCAndOperation - Return the result of a logical AND between different
277/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
278/// function returns zero if it is not possible to represent the resultant
279/// comparison.
280ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
281 bool isInteger) {
282 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
283 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285
286 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000287 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
288
289 // Canonicalize illegal integer setcc's.
290 if (isInteger) {
291 switch (Result) {
292 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000293 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000294 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
296 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
297 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 }
299 }
300
301 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302}
303
Chris Lattnerb48da392005-01-23 04:39:44 +0000304const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000305 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000306}
307
Jim Laskey58b968b2005-08-17 20:08:02 +0000308//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000309// SDNode Profile Support
310//===----------------------------------------------------------------------===//
311
Jim Laskeydef69b92006-10-27 23:52:51 +0000312/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
313///
Jim Laskey583bd472006-10-27 23:46:08 +0000314static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
315 ID.AddInteger(OpC);
316}
317
318/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
319/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000320static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000321 ID.AddPointer(VTList.VTs);
322}
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000327 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000328 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000329 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000330 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 }
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
336static void AddNodeIDOperands(FoldingSetNodeID &ID,
337 const SDUse *Ops, unsigned NumOps) {
338 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000339 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000341 }
342}
343
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID,
345 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000346 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000347 AddNodeIDOpcode(ID, OpC);
348 AddNodeIDValueTypes(ID, VTList);
349 AddNodeIDOperands(ID, OpList, N);
350}
351
Roman Levenstein9cac5252008-04-16 16:15:27 +0000352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
354/// data.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, N->getOpcode());
357 // Add the return value info.
358 AddNodeIDValueTypes(ID, N->getVTList());
359 // Add the operand info.
360 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
361
362 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 switch (N->getOpcode()) {
364 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 case ISD::ARG_FLAGS:
366 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
367 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetConstant:
369 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000370 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 break;
372 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000374 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
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) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000864 return getConstant(*ConstantInt::get(Val), VT, isT);
865}
866
867SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000869
Evan Cheng0ff39b32008-06-30 07:31:25 +0000870 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000872 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000873
874 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000875 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000876 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000877 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000878 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000879 SDNode *N = NULL;
880 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000881 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000882 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000883 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000884 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000885 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000886 CSEMap.InsertNode(N, IP);
887 AllNodes.push_back(N);
888 }
889
Dan Gohman475871a2008-07-27 21:46:04 +0000890 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000892 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000893 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000894 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
895 }
896 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000897}
898
Dan Gohman475871a2008-07-27 21:46:04 +0000899SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000900 return getConstant(Val, TLI.getPointerTy(), isTarget);
901}
902
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000905 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
906}
907
908SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000909 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000910
Duncan Sands83ec4b62008-06-06 12:08:01 +0000911 MVT EltVT =
912 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000913
Chris Lattnerd8658612005-02-17 20:17:32 +0000914 // Do the map lookup using the actual bit pattern for the floating point
915 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
916 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000917 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000918 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000919 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000920 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000921 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000922 SDNode *N = NULL;
923 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000924 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000925 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000926 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000927 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000928 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000929 CSEMap.InsertNode(N, IP);
930 AllNodes.push_back(N);
931 }
932
Dan Gohman475871a2008-07-27 21:46:04 +0000933 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000934 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000935 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000936 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000937 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
938 }
939 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940}
941
Dan Gohman475871a2008-07-27 21:46:04 +0000942SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000943 MVT EltVT =
944 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000945 if (EltVT==MVT::f32)
946 return getConstantFP(APFloat((float)Val), VT, isTarget);
947 else
948 return getConstantFP(APFloat(Val), VT, isTarget);
949}
950
Dan Gohman475871a2008-07-27 21:46:04 +0000951SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
952 MVT VT, int Offset,
953 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000954 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000955
956 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
957 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000958 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000959 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000960 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000961 }
962
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000963 if (GVar && GVar->isThreadLocal())
964 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
965 else
966 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000967
Jim Laskey583bd472006-10-27 23:46:08 +0000968 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000969 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000970 ID.AddPointer(GV);
971 ID.AddInteger(Offset);
972 void *IP = 0;
973 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000974 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000975 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000976 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000977 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980}
981
Dan Gohman475871a2008-07-27 21:46:04 +0000982SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000983 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000984 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000985 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000986 ID.AddInteger(FI);
987 void *IP = 0;
988 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000989 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000990 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000991 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000992 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000993 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000994 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000995}
996
Dan Gohman475871a2008-07-27 21:46:04 +0000997SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000998 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000999 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001000 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001001 ID.AddInteger(JTI);
1002 void *IP = 0;
1003 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001004 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001005 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001006 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001007 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001008 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001009 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001010}
1011
Dan Gohman475871a2008-07-27 21:46:04 +00001012SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1013 unsigned Alignment, int Offset,
1014 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001015 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001016 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001017 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001018 ID.AddInteger(Alignment);
1019 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001020 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001021 void *IP = 0;
1022 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001023 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001024 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001025 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001026 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001027 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001028 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001029}
1030
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031
Dan Gohman475871a2008-07-27 21:46:04 +00001032SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1033 unsigned Alignment, int Offset,
1034 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001035 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001036 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001037 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001038 ID.AddInteger(Alignment);
1039 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001040 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001041 void *IP = 0;
1042 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001043 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001044 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001045 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001046 CSEMap.InsertNode(N, IP);
1047 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001048 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001049}
1050
1051
Dan Gohman475871a2008-07-27 21:46:04 +00001052SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001053 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001054 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001055 ID.AddPointer(MBB);
1056 void *IP = 0;
1057 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001059 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001060 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001061 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001063 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064}
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001067 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001068 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001069 ID.AddInteger(Flags.getRawBits());
1070 void *IP = 0;
1071 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001072 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001073 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001074 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001075 CSEMap.InsertNode(N, IP);
1076 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001077 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001078}
1079
Dan Gohman475871a2008-07-27 21:46:04 +00001080SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1082 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001083
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 SDNode *&N = VT.isExtended() ?
1085 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001086
Dan Gohman475871a2008-07-27 21:46:04 +00001087 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001088 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001089 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001090 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001091 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001092}
1093
Dan Gohman475871a2008-07-27 21:46:04 +00001094SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001096 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001097 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001098 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001099 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001100 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001101}
1102
Dan Gohman475871a2008-07-27 21:46:04 +00001103SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001104 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001105 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001106 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001107 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001109 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001110}
1111
Dan Gohman475871a2008-07-27 21:46:04 +00001112SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001113 if ((unsigned)Cond >= CondCodeNodes.size())
1114 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001115
Chris Lattner079a27a2005-08-09 20:40:02 +00001116 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001117 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001118 new (N) CondCodeSDNode(Cond);
1119 CondCodeNodes[Cond] = N;
1120 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001121 }
Dan Gohman475871a2008-07-27 21:46:04 +00001122 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001123}
1124
Dan Gohman475871a2008-07-27 21:46:04 +00001125SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001126 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001127 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001128 ID.AddInteger(RegNo);
1129 void *IP = 0;
1130 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001131 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001132 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001133 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001134 CSEMap.InsertNode(N, IP);
1135 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001136 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001137}
1138
Dan Gohman475871a2008-07-27 21:46:04 +00001139SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001140 unsigned Line, unsigned Col,
1141 const CompileUnitDesc *CU) {
1142 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001143 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001144 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001145 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001146}
1147
Dan Gohman475871a2008-07-27 21:46:04 +00001148SDValue SelectionDAG::getLabel(unsigned Opcode,
1149 SDValue Root,
1150 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001151 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001153 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1154 ID.AddInteger(LabelID);
1155 void *IP = 0;
1156 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001157 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001158 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001159 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001160 CSEMap.InsertNode(N, IP);
1161 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001162 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001163}
1164
Dan Gohman475871a2008-07-27 21:46:04 +00001165SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001166 assert((!V || isa<PointerType>(V->getType())) &&
1167 "SrcValue is not a pointer?");
1168
Jim Laskey583bd472006-10-27 23:46:08 +00001169 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001170 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001171 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001172
Chris Lattner61b09412006-08-11 21:01:22 +00001173 void *IP = 0;
1174 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001175 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001176
Dan Gohmanfed90b62008-07-28 21:51:04 +00001177 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001178 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001179 CSEMap.InsertNode(N, IP);
1180 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001181 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001182}
1183
Dan Gohman475871a2008-07-27 21:46:04 +00001184SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001185 const Value *v = MO.getValue();
1186 assert((!v || isa<PointerType>(v->getType())) &&
1187 "SrcValue is not a pointer?");
1188
1189 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001190 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001191 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001192
1193 void *IP = 0;
1194 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001195 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001196
Dan Gohmanfed90b62008-07-28 21:51:04 +00001197 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001198 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001199 CSEMap.InsertNode(N, IP);
1200 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001201 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001202}
1203
Chris Lattner37ce9df2007-10-15 17:47:20 +00001204/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1205/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001206SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001207 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001208 unsigned ByteSize = VT.getSizeInBits()/8;
1209 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001210 unsigned StackAlign =
1211 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1212
Chris Lattner37ce9df2007-10-15 17:47:20 +00001213 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1214 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1215}
1216
Dan Gohman475871a2008-07-27 21:46:04 +00001217SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1218 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 // These setcc operations always fold.
1220 switch (Cond) {
1221 default: break;
1222 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001223 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001225 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001226
1227 case ISD::SETOEQ:
1228 case ISD::SETOGT:
1229 case ISD::SETOGE:
1230 case ISD::SETOLT:
1231 case ISD::SETOLE:
1232 case ISD::SETONE:
1233 case ISD::SETO:
1234 case ISD::SETUO:
1235 case ISD::SETUEQ:
1236 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001237 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001238 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001240
Gabor Greifba36cb52008-08-28 21:40:38 +00001241 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001242 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001243 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001244 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001245
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 switch (Cond) {
1247 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001248 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1249 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001250 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1251 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1252 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1253 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1254 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1255 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1256 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1257 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001258 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 }
Chris Lattner67255a12005-04-07 18:14:58 +00001260 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1262 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001263 // No compile time operations on this type yet.
1264 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001265 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001266
1267 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001269 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001270 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1271 return getNode(ISD::UNDEF, VT);
1272 // fall through
1273 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1274 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1275 return getNode(ISD::UNDEF, VT);
1276 // fall through
1277 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001278 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001279 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1280 return getNode(ISD::UNDEF, VT);
1281 // fall through
1282 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1283 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1284 return getNode(ISD::UNDEF, VT);
1285 // fall through
1286 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1287 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1288 return getNode(ISD::UNDEF, VT);
1289 // fall through
1290 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001291 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001292 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1293 return getNode(ISD::UNDEF, VT);
1294 // fall through
1295 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001296 R==APFloat::cmpEqual, VT);
1297 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1298 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1299 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1300 R==APFloat::cmpEqual, VT);
1301 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1302 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1303 R==APFloat::cmpLessThan, VT);
1304 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1305 R==APFloat::cmpUnordered, VT);
1306 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1307 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001308 }
1309 } else {
1310 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001311 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001312 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001313 }
1314
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001315 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001316 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317}
1318
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001319/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1320/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001321bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001322 unsigned BitWidth = Op.getValueSizeInBits();
1323 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1324}
1325
Dan Gohmanea859be2007-06-22 14:59:07 +00001326/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1327/// this predicate to simplify operations downstream. Mask is known to be zero
1328/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001329bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001330 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001331 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001332 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1333 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1334 return (KnownZero & Mask) == Mask;
1335}
1336
1337/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1338/// known to be either zero or one and return them in the KnownZero/KnownOne
1339/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1340/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001341void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001342 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001343 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001344 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001345 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001346 "Mask size mismatches value type size!");
1347
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001348 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001349 if (Depth == 6 || Mask == 0)
1350 return; // Limit search depth.
1351
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001352 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001353
1354 switch (Op.getOpcode()) {
1355 case ISD::Constant:
1356 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001357 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001358 KnownZero = ~KnownOne & Mask;
1359 return;
1360 case ISD::AND:
1361 // If either the LHS or the RHS are Zero, the result is zero.
1362 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001363 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1364 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001365 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1366 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1367
1368 // Output known-1 bits are only known if set in both the LHS & RHS.
1369 KnownOne &= KnownOne2;
1370 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1371 KnownZero |= KnownZero2;
1372 return;
1373 case ISD::OR:
1374 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001375 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1376 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001377 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1378 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1379
1380 // Output known-0 bits are only known if clear in both the LHS & RHS.
1381 KnownZero &= KnownZero2;
1382 // Output known-1 are known to be set if set in either the LHS | RHS.
1383 KnownOne |= KnownOne2;
1384 return;
1385 case ISD::XOR: {
1386 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1387 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1388 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1389 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1390
1391 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001392 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001393 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1394 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1395 KnownZero = KnownZeroOut;
1396 return;
1397 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001398 case ISD::MUL: {
1399 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1400 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1401 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1402 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1403 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1404
1405 // If low bits are zero in either operand, output low known-0 bits.
1406 // Also compute a conserative estimate for high known-0 bits.
1407 // More trickiness is possible, but this is sufficient for the
1408 // interesting case of alignment computation.
1409 KnownOne.clear();
1410 unsigned TrailZ = KnownZero.countTrailingOnes() +
1411 KnownZero2.countTrailingOnes();
1412 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001413 KnownZero2.countLeadingOnes(),
1414 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001415
1416 TrailZ = std::min(TrailZ, BitWidth);
1417 LeadZ = std::min(LeadZ, BitWidth);
1418 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1419 APInt::getHighBitsSet(BitWidth, LeadZ);
1420 KnownZero &= Mask;
1421 return;
1422 }
1423 case ISD::UDIV: {
1424 // For the purposes of computing leading zeros we can conservatively
1425 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001426 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001427 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1428 ComputeMaskedBits(Op.getOperand(0),
1429 AllOnes, KnownZero2, KnownOne2, Depth+1);
1430 unsigned LeadZ = KnownZero2.countLeadingOnes();
1431
1432 KnownOne2.clear();
1433 KnownZero2.clear();
1434 ComputeMaskedBits(Op.getOperand(1),
1435 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001436 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1437 if (RHSUnknownLeadingOnes != BitWidth)
1438 LeadZ = std::min(BitWidth,
1439 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001440
1441 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1442 return;
1443 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001444 case ISD::SELECT:
1445 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1446 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1447 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1448 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1449
1450 // Only known if known in both the LHS and RHS.
1451 KnownOne &= KnownOne2;
1452 KnownZero &= KnownZero2;
1453 return;
1454 case ISD::SELECT_CC:
1455 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1456 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1457 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1458 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1459
1460 // Only known if known in both the LHS and RHS.
1461 KnownOne &= KnownOne2;
1462 KnownZero &= KnownZero2;
1463 return;
1464 case ISD::SETCC:
1465 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001466 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1467 BitWidth > 1)
1468 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001469 return;
1470 case ISD::SHL:
1471 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1472 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001473 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001474
1475 // If the shift count is an invalid immediate, don't do anything.
1476 if (ShAmt >= BitWidth)
1477 return;
1478
1479 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001480 KnownZero, KnownOne, Depth+1);
1481 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001482 KnownZero <<= ShAmt;
1483 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001484 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001485 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001486 }
1487 return;
1488 case ISD::SRL:
1489 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1490 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001491 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001492
Dan Gohmand4cf9922008-02-26 18:50:50 +00001493 // If the shift count is an invalid immediate, don't do anything.
1494 if (ShAmt >= BitWidth)
1495 return;
1496
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001497 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001498 KnownZero, KnownOne, Depth+1);
1499 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001500 KnownZero = KnownZero.lshr(ShAmt);
1501 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001502
Dan Gohman72d2fd52008-02-13 22:43:25 +00001503 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001504 KnownZero |= HighBits; // High bits known zero.
1505 }
1506 return;
1507 case ISD::SRA:
1508 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001509 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001510
Dan Gohmand4cf9922008-02-26 18:50:50 +00001511 // If the shift count is an invalid immediate, don't do anything.
1512 if (ShAmt >= BitWidth)
1513 return;
1514
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001515 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 // If any of the demanded bits are produced by the sign extension, we also
1517 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001518 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1519 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001520 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001521
1522 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1523 Depth+1);
1524 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001525 KnownZero = KnownZero.lshr(ShAmt);
1526 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001527
1528 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001529 APInt SignBit = APInt::getSignBit(BitWidth);
1530 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001531
Dan Gohmanca93a432008-02-20 16:30:17 +00001532 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001533 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001534 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001535 KnownOne |= HighBits; // New bits are known one.
1536 }
1537 }
1538 return;
1539 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001540 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1541 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001542
1543 // Sign extension. Compute the demanded bits in the result that are not
1544 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001545 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001546
Dan Gohman977a76f2008-02-13 22:28:48 +00001547 APInt InSignBit = APInt::getSignBit(EBits);
1548 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001549
1550 // If the sign extended bits are demanded, we know that the sign
1551 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001552 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001553 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001554 InputDemandedBits |= InSignBit;
1555
1556 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1557 KnownZero, KnownOne, Depth+1);
1558 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1559
1560 // If the sign bit of the input is known set or clear, then we know the
1561 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001562 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001563 KnownZero |= NewBits;
1564 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001565 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001566 KnownOne |= NewBits;
1567 KnownZero &= ~NewBits;
1568 } else { // Input sign bit unknown
1569 KnownZero &= ~NewBits;
1570 KnownOne &= ~NewBits;
1571 }
1572 return;
1573 }
1574 case ISD::CTTZ:
1575 case ISD::CTLZ:
1576 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001577 unsigned LowBits = Log2_32(BitWidth)+1;
1578 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001579 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001580 return;
1581 }
1582 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001583 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001584 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 MVT VT = LD->getMemoryVT();
1586 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001587 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 }
1589 return;
1590 }
1591 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 MVT InVT = Op.getOperand(0).getValueType();
1593 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001594 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1595 APInt InMask = Mask;
1596 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001597 KnownZero.trunc(InBits);
1598 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001599 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001600 KnownZero.zext(BitWidth);
1601 KnownOne.zext(BitWidth);
1602 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001603 return;
1604 }
1605 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001606 MVT InVT = Op.getOperand(0).getValueType();
1607 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001608 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001609 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1610 APInt InMask = Mask;
1611 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001612
1613 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001614 // bit is demanded. Temporarily set this bit in the mask for our callee.
1615 if (NewBits.getBoolValue())
1616 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001617
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001618 KnownZero.trunc(InBits);
1619 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001620 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1621
1622 // Note if the sign bit is known to be zero or one.
1623 bool SignBitKnownZero = KnownZero.isNegative();
1624 bool SignBitKnownOne = KnownOne.isNegative();
1625 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1626 "Sign bit can't be known to be both zero and one!");
1627
1628 // If the sign bit wasn't actually demanded by our caller, we don't
1629 // want it set in the KnownZero and KnownOne result values. Reset the
1630 // mask and reapply it to the result values.
1631 InMask = Mask;
1632 InMask.trunc(InBits);
1633 KnownZero &= InMask;
1634 KnownOne &= InMask;
1635
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001636 KnownZero.zext(BitWidth);
1637 KnownOne.zext(BitWidth);
1638
Dan Gohman977a76f2008-02-13 22:28:48 +00001639 // If the sign bit is known zero or one, the top bits match.
1640 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001641 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001642 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001643 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 return;
1645 }
1646 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001647 MVT InVT = Op.getOperand(0).getValueType();
1648 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001649 APInt InMask = Mask;
1650 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001651 KnownZero.trunc(InBits);
1652 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001653 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001654 KnownZero.zext(BitWidth);
1655 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001656 return;
1657 }
1658 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001659 MVT InVT = Op.getOperand(0).getValueType();
1660 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001661 APInt InMask = Mask;
1662 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001663 KnownZero.zext(InBits);
1664 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001665 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001666 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001667 KnownZero.trunc(BitWidth);
1668 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 break;
1670 }
1671 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001672 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1673 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1675 KnownOne, Depth+1);
1676 KnownZero |= (~InMask) & Mask;
1677 return;
1678 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001679 case ISD::FGETSIGN:
1680 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001681 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001682 return;
1683
Dan Gohman23e8b712008-04-28 17:02:21 +00001684 case ISD::SUB: {
1685 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1686 // We know that the top bits of C-X are clear if X contains less bits
1687 // than C (i.e. no wrap-around can happen). For example, 20-X is
1688 // positive if we can prove that X is >= 0 and < 16.
1689 if (CLHS->getAPIntValue().isNonNegative()) {
1690 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1691 // NLZ can't be BitWidth with no sign bit
1692 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1693 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1694 Depth+1);
1695
1696 // If all of the MaskV bits are known to be zero, then we know the
1697 // output top bits are zero, because we now know that the output is
1698 // from [0-C].
1699 if ((KnownZero2 & MaskV) == MaskV) {
1700 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1701 // Top bits known zero.
1702 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1703 }
1704 }
1705 }
1706 }
1707 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001708 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001709 // Output known-0 bits are known if clear or set in both the low clear bits
1710 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1711 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001712 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1713 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1714 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1715 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1716
1717 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1718 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1719 KnownZeroOut = std::min(KnownZeroOut,
1720 KnownZero2.countTrailingOnes());
1721
1722 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001723 return;
1724 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001725 case ISD::SREM:
1726 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001727 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001728 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001729 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001730 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1731 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001732
Dan Gohmana60832b2008-08-13 23:12:35 +00001733 // If the sign bit of the first operand is zero, the sign bit of
1734 // the result is zero. If the first operand has no one bits below
1735 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001736 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1737 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001738
Dan Gohman23e8b712008-04-28 17:02:21 +00001739 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001740
1741 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 }
1743 }
1744 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001745 case ISD::UREM: {
1746 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001747 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001748 if (RA.isPowerOf2()) {
1749 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001750 APInt Mask2 = LowBits & Mask;
1751 KnownZero |= ~LowBits & Mask;
1752 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1753 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1754 break;
1755 }
1756 }
1757
1758 // Since the result is less than or equal to either operand, any leading
1759 // zero bits in either operand must also exist in the result.
1760 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1761 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1762 Depth+1);
1763 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1764 Depth+1);
1765
1766 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1767 KnownZero2.countLeadingOnes());
1768 KnownOne.clear();
1769 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1770 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 }
1772 default:
1773 // Allow the target to implement this method for its nodes.
1774 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1775 case ISD::INTRINSIC_WO_CHAIN:
1776 case ISD::INTRINSIC_W_CHAIN:
1777 case ISD::INTRINSIC_VOID:
1778 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1779 }
1780 return;
1781 }
1782}
1783
1784/// ComputeNumSignBits - Return the number of times the sign bit of the
1785/// register is replicated into the other bits. We know that at least 1 bit
1786/// is always equal to the sign bit (itself), but other cases can give us
1787/// information. For example, immediately after an "SRA X, 2", we know that
1788/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001789unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001790 MVT VT = Op.getValueType();
1791 assert(VT.isInteger() && "Invalid VT!");
1792 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001793 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001794 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001795
1796 if (Depth == 6)
1797 return 1; // Limit search depth.
1798
1799 switch (Op.getOpcode()) {
1800 default: break;
1801 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001802 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001803 return VTBits-Tmp+1;
1804 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001805 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001806 return VTBits-Tmp;
1807
1808 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001809 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1810 // If negative, return # leading ones.
1811 if (Val.isNegative())
1812 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001813
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001814 // Return # leading zeros.
1815 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001816 }
1817
1818 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001820 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1821
1822 case ISD::SIGN_EXTEND_INREG:
1823 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001825 Tmp = VTBits-Tmp+1;
1826
1827 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1828 return std::max(Tmp, Tmp2);
1829
1830 case ISD::SRA:
1831 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1832 // SRA X, C -> adds C sign bits.
1833 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001834 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 if (Tmp > VTBits) Tmp = VTBits;
1836 }
1837 return Tmp;
1838 case ISD::SHL:
1839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1840 // shl destroys sign bits.
1841 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001842 if (C->getZExtValue() >= VTBits || // Bad shift.
1843 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1844 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001845 }
1846 break;
1847 case ISD::AND:
1848 case ISD::OR:
1849 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001850 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001852 if (Tmp != 1) {
1853 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1854 FirstAnswer = std::min(Tmp, Tmp2);
1855 // We computed what we know about the sign bits as our first
1856 // answer. Now proceed to the generic code that uses
1857 // ComputeMaskedBits, and pick whichever answer is better.
1858 }
1859 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001860
1861 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001862 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001864 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001865 return std::min(Tmp, Tmp2);
1866
1867 case ISD::SETCC:
1868 // If setcc returns 0/-1, all bits are sign bits.
1869 if (TLI.getSetCCResultContents() ==
1870 TargetLowering::ZeroOrNegativeOneSetCCResult)
1871 return VTBits;
1872 break;
1873 case ISD::ROTL:
1874 case ISD::ROTR:
1875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001876 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001877
1878 // Handle rotate right by N like a rotate left by 32-N.
1879 if (Op.getOpcode() == ISD::ROTR)
1880 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1881
1882 // If we aren't rotating out all of the known-in sign bits, return the
1883 // number that are left. This handles rotl(sext(x), 1) for example.
1884 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1885 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1886 }
1887 break;
1888 case ISD::ADD:
1889 // Add can have at most one carry bit. Thus we know that the output
1890 // is, at worst, one more bit than the inputs.
1891 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1892 if (Tmp == 1) return 1; // Early out.
1893
1894 // Special case decrementing a value (ADD X, -1):
1895 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1896 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001897 APInt KnownZero, KnownOne;
1898 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1900
1901 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1902 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001903 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001904 return VTBits;
1905
1906 // If we are subtracting one from a positive number, there is no carry
1907 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001908 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001909 return Tmp;
1910 }
1911
1912 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1913 if (Tmp2 == 1) return 1;
1914 return std::min(Tmp, Tmp2)-1;
1915 break;
1916
1917 case ISD::SUB:
1918 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1919 if (Tmp2 == 1) return 1;
1920
1921 // Handle NEG.
1922 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001923 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001924 APInt KnownZero, KnownOne;
1925 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001926 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1927 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1928 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001929 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001930 return VTBits;
1931
1932 // If the input is known to be positive (the sign bit is known clear),
1933 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001934 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001935 return Tmp2;
1936
1937 // Otherwise, we treat this like a SUB.
1938 }
1939
1940 // Sub can have at most one carry bit. Thus we know that the output
1941 // is, at worst, one more bit than the inputs.
1942 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1943 if (Tmp == 1) return 1; // Early out.
1944 return std::min(Tmp, Tmp2)-1;
1945 break;
1946 case ISD::TRUNCATE:
1947 // FIXME: it's tricky to do anything useful for this, but it is an important
1948 // case for targets like X86.
1949 break;
1950 }
1951
1952 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1953 if (Op.getOpcode() == ISD::LOAD) {
1954 LoadSDNode *LD = cast<LoadSDNode>(Op);
1955 unsigned ExtType = LD->getExtensionType();
1956 switch (ExtType) {
1957 default: break;
1958 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001959 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001960 return VTBits-Tmp+1;
1961 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 return VTBits-Tmp;
1964 }
1965 }
1966
1967 // Allow the target to implement this method for its nodes.
1968 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1969 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1970 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1971 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1972 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001973 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001974 }
1975
1976 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1977 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001978 APInt KnownZero, KnownOne;
1979 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001980 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1981
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001982 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001984 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001985 Mask = KnownOne;
1986 } else {
1987 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001988 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001989 }
1990
1991 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1992 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001993 Mask = ~Mask;
1994 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001995 // Return # leading zeros. We use 'min' here in case Val was zero before
1996 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001997 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001998}
1999
Chris Lattner51dabfb2006-10-14 00:41:01 +00002000
Dan Gohman475871a2008-07-27 21:46:04 +00002001bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002002 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2003 if (!GA) return false;
2004 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2005 if (!GV) return false;
2006 MachineModuleInfo *MMI = getMachineModuleInfo();
2007 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
2008}
2009
2010
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002011/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2012/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002013SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002014 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue PermMask = N->getOperand(2);
2016 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002017 if (Idx.getOpcode() == ISD::UNDEF)
2018 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002019 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002020 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002021 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002022 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002023
2024 if (V.getOpcode() == ISD::BIT_CONVERT) {
2025 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002026 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002027 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002028 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002029 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002030 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002032 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002033 return V.getOperand(Index);
2034 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002035 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002036 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002037}
2038
2039
Chris Lattnerc3aae252005-01-07 07:46:32 +00002040/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002041///
Dan Gohman475871a2008-07-27 21:46:04 +00002042SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002043 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002044 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002045 void *IP = 0;
2046 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002047 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002048 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002049 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002050 CSEMap.InsertNode(N, IP);
2051
2052 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002053#ifndef NDEBUG
2054 VerifyNode(N);
2055#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002056 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002057}
2058
Dan Gohman475871a2008-07-27 21:46:04 +00002059SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002060 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002062 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002063 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002064 switch (Opcode) {
2065 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002066 case ISD::SIGN_EXTEND:
2067 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002068 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002069 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002070 case ISD::TRUNCATE:
2071 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002072 case ISD::UINT_TO_FP:
2073 case ISD::SINT_TO_FP: {
2074 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002075 // No compile time operations on this type.
2076 if (VT==MVT::ppcf128)
2077 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002078 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2079 (void)apf.convertFromAPInt(Val,
2080 Opcode==ISD::SINT_TO_FP,
2081 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002082 return getConstantFP(apf, VT);
2083 }
Chris Lattner94683772005-12-23 05:30:37 +00002084 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002085 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002086 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002087 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002088 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002089 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002090 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002091 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002092 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002093 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002094 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002095 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002096 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002097 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002098 }
2099 }
2100
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002101 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002102 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002103 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002104 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002105 switch (Opcode) {
2106 case ISD::FNEG:
2107 V.changeSign();
2108 return getConstantFP(V, VT);
2109 case ISD::FABS:
2110 V.clearSign();
2111 return getConstantFP(V, VT);
2112 case ISD::FP_ROUND:
2113 case ISD::FP_EXTEND:
2114 // This can return overflow, underflow, or inexact; we don't care.
2115 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002116 (void)V.convert(*MVTToAPFloatSemantics(VT),
2117 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002118 return getConstantFP(V, VT);
2119 case ISD::FP_TO_SINT:
2120 case ISD::FP_TO_UINT: {
2121 integerPart x;
2122 assert(integerPartWidth >= 64);
2123 // FIXME need to be more flexible about rounding mode.
2124 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2125 Opcode==ISD::FP_TO_SINT,
2126 APFloat::rmTowardZero);
2127 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2128 break;
2129 return getConstant(x, VT);
2130 }
2131 case ISD::BIT_CONVERT:
2132 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2133 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2134 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2135 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002136 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002137 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002138 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002139 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002140
Gabor Greifba36cb52008-08-28 21:40:38 +00002141 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002142 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002143 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002144 case ISD::CONCAT_VECTORS:
2145 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002146 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002147 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002148 assert(VT.isFloatingPoint() &&
2149 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002150 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002151 if (Operand.getOpcode() == ISD::UNDEF)
2152 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002153 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002154 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002156 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002157 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002158 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002159 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002160 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002161 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162 break;
2163 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002164 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002165 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002166 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002167 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002168 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002169 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002170 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002171 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002172 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002173 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002174 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002175 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002176 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002177 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002178 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2179 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002180 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002181 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002182 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002184 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002185 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002186 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002187 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002188 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002189 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002190 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2191 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002192 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002193 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2194 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2195 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2196 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002197 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002198 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002199 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002200 break;
Chris Lattner35481892005-12-23 00:16:34 +00002201 case ISD::BIT_CONVERT:
2202 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002203 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002204 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002205 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002206 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2207 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002208 if (OpOpcode == ISD::UNDEF)
2209 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002210 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002211 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2213 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002214 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002215 if (OpOpcode == ISD::UNDEF)
2216 return getNode(ISD::UNDEF, VT);
2217 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2218 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2219 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2220 Operand.getConstantOperandVal(1) == 0 &&
2221 Operand.getOperand(0).getValueType() == VT)
2222 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002223 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002224 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002225 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2227 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002228 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002229 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002230 break;
2231 case ISD::FABS:
2232 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002233 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002234 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002235 }
2236
Chris Lattner43247a12005-08-25 19:12:10 +00002237 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002238 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002239 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002240 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002241 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002242 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002243 void *IP = 0;
2244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002245 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002246 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002247 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002248 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002249 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002250 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002251 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002252 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002253
Chris Lattnerc3aae252005-01-07 07:46:32 +00002254 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002255#ifndef NDEBUG
2256 VerifyNode(N);
2257#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002258 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002259}
2260
Dan Gohman475871a2008-07-27 21:46:04 +00002261SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2262 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2264 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002265 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002266 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002267 case ISD::TokenFactor:
2268 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2269 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002270 // Fold trivial token factors.
2271 if (N1.getOpcode() == ISD::EntryToken) return N2;
2272 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002273 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002274 case ISD::CONCAT_VECTORS:
2275 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2276 // one big BUILD_VECTOR.
2277 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2278 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002279 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2280 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002281 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2282 }
2283 break;
Chris Lattner76365122005-01-16 02:23:22 +00002284 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002285 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002286 N1.getValueType() == VT && "Binary operator types must match!");
2287 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2288 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002289 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002290 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002291 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2292 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002293 break;
Chris Lattner76365122005-01-16 02:23:22 +00002294 case ISD::OR:
2295 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002296 case ISD::ADD:
2297 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002298 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002299 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002300 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2301 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002302 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002303 return N1;
2304 break;
Chris Lattner76365122005-01-16 02:23:22 +00002305 case ISD::UDIV:
2306 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002307 case ISD::MULHU:
2308 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002310 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002311 case ISD::MUL:
2312 case ISD::SDIV:
2313 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002314 case ISD::FADD:
2315 case ISD::FSUB:
2316 case ISD::FMUL:
2317 case ISD::FDIV:
2318 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002319 assert(N1.getValueType() == N2.getValueType() &&
2320 N1.getValueType() == VT && "Binary operator types must match!");
2321 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002322 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2323 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002324 N1.getValueType().isFloatingPoint() &&
2325 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002326 "Invalid FCOPYSIGN!");
2327 break;
Chris Lattner76365122005-01-16 02:23:22 +00002328 case ISD::SHL:
2329 case ISD::SRA:
2330 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002331 case ISD::ROTL:
2332 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002333 assert(VT == N1.getValueType() &&
2334 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002335 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002336 "Shifts only work on integers");
2337
2338 // Always fold shifts of i1 values so the code generator doesn't need to
2339 // handle them. Since we know the size of the shift has to be less than the
2340 // size of the value, the shift/rotate count is guaranteed to be zero.
2341 if (VT == MVT::i1)
2342 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002343 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002344 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002345 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002346 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002347 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002348 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002349 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002350 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002351 break;
2352 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002353 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002354 assert(VT.isFloatingPoint() &&
2355 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002356 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002357 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002358 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002359 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002360 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002361 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002363 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002364 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002365 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002366 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002367 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 break;
2369 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002370 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002372 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002374 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002375 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002376 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002377
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002378 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002379 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002380 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002381 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002382 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002383 return getConstant(Val, VT);
2384 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002385 break;
2386 }
2387 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002388 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2389 if (N1.getOpcode() == ISD::UNDEF)
2390 return getNode(ISD::UNDEF, VT);
2391
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002392 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2393 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002394 if (N2C &&
2395 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002396 N1.getNumOperands() > 0) {
2397 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002398 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002399 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002400 N1.getOperand(N2C->getZExtValue() / Factor),
2401 getConstant(N2C->getZExtValue() % Factor,
2402 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002403 }
2404
2405 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2406 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002407 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002408 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002409
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002410 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2411 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002412 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2413 if (N1.getOperand(2) == N2)
2414 return N1.getOperand(1);
2415 else
2416 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2417 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002418 break;
2419 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002420 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002421 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2422 (N1.getValueType().isInteger() == VT.isInteger()) &&
2423 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002424
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002425 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2426 // 64-bit integers into 32-bit parts. Instead of building the extract of
2427 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2428 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002429 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002430
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002431 // EXTRACT_ELEMENT of a constant int is also very common.
2432 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002433 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002434 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002435 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2436 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002437 }
2438 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002439 case ISD::EXTRACT_SUBVECTOR:
2440 if (N1.getValueType() == VT) // Trivial extraction.
2441 return N1;
2442 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002443 }
2444
2445 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002446 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002447 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 switch (Opcode) {
2449 case ISD::ADD: return getConstant(C1 + C2, VT);
2450 case ISD::SUB: return getConstant(C1 - C2, VT);
2451 case ISD::MUL: return getConstant(C1 * C2, VT);
2452 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002453 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002454 break;
2455 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002456 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002457 break;
2458 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002459 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460 break;
2461 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002462 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463 break;
2464 case ISD::AND : return getConstant(C1 & C2, VT);
2465 case ISD::OR : return getConstant(C1 | C2, VT);
2466 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002467 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002468 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2469 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2470 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2471 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002472 default: break;
2473 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002474 } else { // Cannonicalize constant to RHS if commutative
2475 if (isCommutativeBinOp(Opcode)) {
2476 std::swap(N1C, N2C);
2477 std::swap(N1, N2);
2478 }
2479 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002480 }
2481
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002482 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002483 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2484 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002485 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002486 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2487 // Cannonicalize constant to RHS if commutative
2488 std::swap(N1CFP, N2CFP);
2489 std::swap(N1, N2);
2490 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002491 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2492 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002493 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002494 case ISD::FADD:
2495 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002496 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002497 return getConstantFP(V1, VT);
2498 break;
2499 case ISD::FSUB:
2500 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2501 if (s!=APFloat::opInvalidOp)
2502 return getConstantFP(V1, VT);
2503 break;
2504 case ISD::FMUL:
2505 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2506 if (s!=APFloat::opInvalidOp)
2507 return getConstantFP(V1, VT);
2508 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002509 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002510 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2511 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2512 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002513 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002514 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002515 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2516 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2517 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002518 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002519 case ISD::FCOPYSIGN:
2520 V1.copySign(V2);
2521 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522 default: break;
2523 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002524 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002525 }
Chris Lattner62b57722006-04-20 05:39:12 +00002526
2527 // Canonicalize an UNDEF to the RHS, even over a constant.
2528 if (N1.getOpcode() == ISD::UNDEF) {
2529 if (isCommutativeBinOp(Opcode)) {
2530 std::swap(N1, N2);
2531 } else {
2532 switch (Opcode) {
2533 case ISD::FP_ROUND_INREG:
2534 case ISD::SIGN_EXTEND_INREG:
2535 case ISD::SUB:
2536 case ISD::FSUB:
2537 case ISD::FDIV:
2538 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002539 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002540 return N1; // fold op(undef, arg2) -> undef
2541 case ISD::UDIV:
2542 case ISD::SDIV:
2543 case ISD::UREM:
2544 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002545 case ISD::SRL:
2546 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002547 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002548 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2549 // For vectors, we can't easily build an all zero vector, just return
2550 // the LHS.
2551 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002552 }
2553 }
2554 }
2555
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002556 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002557 if (N2.getOpcode() == ISD::UNDEF) {
2558 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002559 case ISD::XOR:
2560 if (N1.getOpcode() == ISD::UNDEF)
2561 // Handle undef ^ undef -> 0 special case. This is a common
2562 // idiom (misuse).
2563 return getConstant(0, VT);
2564 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002565 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002566 case ISD::ADDC:
2567 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002568 case ISD::SUB:
2569 case ISD::FADD:
2570 case ISD::FSUB:
2571 case ISD::FMUL:
2572 case ISD::FDIV:
2573 case ISD::FREM:
2574 case ISD::UDIV:
2575 case ISD::SDIV:
2576 case ISD::UREM:
2577 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002578 return N2; // fold op(arg1, undef) -> undef
2579 case ISD::MUL:
2580 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002581 case ISD::SRL:
2582 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002584 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2585 // For vectors, we can't easily build an all zero vector, just return
2586 // the LHS.
2587 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002588 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002589 if (!VT.isVector())
2590 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002591 // For vectors, we can't easily build an all one vector, just return
2592 // the LHS.
2593 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002594 case ISD::SRA:
2595 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002596 }
2597 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002598
Chris Lattner27e9b412005-05-11 18:57:39 +00002599 // Memoize this node if possible.
2600 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002601 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002602 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002603 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002604 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002605 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002606 void *IP = 0;
2607 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002608 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002609 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002610 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002611 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002612 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002613 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002614 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002615 }
2616
Chris Lattnerc3aae252005-01-07 07:46:32 +00002617 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002618#ifndef NDEBUG
2619 VerifyNode(N);
2620#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002621 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002622}
2623
Dan Gohman475871a2008-07-27 21:46:04 +00002624SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2625 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002626 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002627 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2628 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002629 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002630 case ISD::CONCAT_VECTORS:
2631 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2632 // one big BUILD_VECTOR.
2633 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2634 N2.getOpcode() == ISD::BUILD_VECTOR &&
2635 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002636 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2637 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2638 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002639 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2640 }
2641 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002642 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002643 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002645 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002646 break;
2647 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002648 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002649 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002650 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002651 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002652 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002653 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002654 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002655
2656 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002657 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002658 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002659 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002660 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002661 return getNode(ISD::BR, MVT::Other, N1, N3);
2662 else
2663 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002664 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002665 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002666 case ISD::VECTOR_SHUFFLE:
2667 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002668 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002669 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002670 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002671 "Illegal VECTOR_SHUFFLE node!");
2672 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002673 case ISD::BIT_CONVERT:
2674 // Fold bit_convert nodes from a type to themselves.
2675 if (N1.getValueType() == VT)
2676 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002677 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002678 }
2679
Chris Lattner43247a12005-08-25 19:12:10 +00002680 // Memoize node if it doesn't produce a flag.
2681 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002682 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002683 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002684 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002685 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002686 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002687 void *IP = 0;
2688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002689 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002690 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002691 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002692 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002693 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002694 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002695 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002696 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002697 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002698#ifndef NDEBUG
2699 VerifyNode(N);
2700#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002701 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002702}
2703
Dan Gohman475871a2008-07-27 21:46:04 +00002704SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2705 SDValue N1, SDValue N2, SDValue N3,
2706 SDValue N4) {
2707 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002708 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002709}
2710
Dan Gohman475871a2008-07-27 21:46:04 +00002711SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2712 SDValue N1, SDValue N2, SDValue N3,
2713 SDValue N4, SDValue N5) {
2714 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002715 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002716}
2717
Dan Gohman707e0182008-04-12 04:36:06 +00002718/// getMemsetValue - Vectorized representation of the memset value
2719/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002720static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002721 unsigned NumBits = VT.isVector() ?
2722 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002723 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002724 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002725 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002726 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002727 Val = (Val << Shift) | Val;
2728 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002729 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002730 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002731 return DAG.getConstant(Val, VT);
2732 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002733 }
Evan Chengf0df0312008-05-15 08:39:06 +00002734
2735 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2736 unsigned Shift = 8;
2737 for (unsigned i = NumBits; i > 8; i >>= 1) {
2738 Value = DAG.getNode(ISD::OR, VT,
2739 DAG.getNode(ISD::SHL, VT, Value,
2740 DAG.getConstant(Shift, MVT::i8)), Value);
2741 Shift <<= 1;
2742 }
2743
2744 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002745}
2746
Dan Gohman707e0182008-04-12 04:36:06 +00002747/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2748/// used when a memcpy is turned into a memset when the source is a constant
2749/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002750static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002751 const TargetLowering &TLI,
2752 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002753 // Handle vector with all elements zero.
2754 if (Str.empty()) {
2755 if (VT.isInteger())
2756 return DAG.getConstant(0, VT);
2757 unsigned NumElts = VT.getVectorNumElements();
2758 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2759 return DAG.getNode(ISD::BIT_CONVERT, VT,
2760 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2761 }
2762
Duncan Sands83ec4b62008-06-06 12:08:01 +00002763 assert(!VT.isVector() && "Can't handle vector type here!");
2764 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002765 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002766 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002767 if (TLI.isLittleEndian())
2768 Offset = Offset + MSB - 1;
2769 for (unsigned i = 0; i != MSB; ++i) {
2770 Val = (Val << 8) | (unsigned char)Str[Offset];
2771 Offset += TLI.isLittleEndian() ? -1 : 1;
2772 }
2773 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002774}
2775
Dan Gohman707e0182008-04-12 04:36:06 +00002776/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002777///
Dan Gohman475871a2008-07-27 21:46:04 +00002778static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002779 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002780 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002781 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2782}
2783
Evan Chengf0df0312008-05-15 08:39:06 +00002784/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2785///
Dan Gohman475871a2008-07-27 21:46:04 +00002786static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002787 unsigned SrcDelta = 0;
2788 GlobalAddressSDNode *G = NULL;
2789 if (Src.getOpcode() == ISD::GlobalAddress)
2790 G = cast<GlobalAddressSDNode>(Src);
2791 else if (Src.getOpcode() == ISD::ADD &&
2792 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2793 Src.getOperand(1).getOpcode() == ISD::Constant) {
2794 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002795 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002796 }
2797 if (!G)
2798 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002799
Evan Chengf0df0312008-05-15 08:39:06 +00002800 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002801 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2802 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002803
Evan Chengf0df0312008-05-15 08:39:06 +00002804 return false;
2805}
Dan Gohman707e0182008-04-12 04:36:06 +00002806
Evan Chengf0df0312008-05-15 08:39:06 +00002807/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2808/// to replace the memset / memcpy is below the threshold. It also returns the
2809/// types of the sequence of memory ops to perform memset / memcpy.
2810static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002811bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002812 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002813 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002814 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002815 SelectionDAG &DAG,
2816 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002817 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002818 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002819 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002821 if (VT != MVT::iAny) {
2822 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002823 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002824 // If source is a string constant, this will require an unaligned load.
2825 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2826 if (Dst.getOpcode() != ISD::FrameIndex) {
2827 // Can't change destination alignment. It requires a unaligned store.
2828 if (AllowUnalign)
2829 VT = MVT::iAny;
2830 } else {
2831 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2832 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2833 if (MFI->isFixedObjectIndex(FI)) {
2834 // Can't change destination alignment. It requires a unaligned store.
2835 if (AllowUnalign)
2836 VT = MVT::iAny;
2837 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002838 // Give the stack frame object a larger alignment if needed.
2839 if (MFI->getObjectAlignment(FI) < NewAlign)
2840 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002841 Align = NewAlign;
2842 }
2843 }
2844 }
2845 }
2846
2847 if (VT == MVT::iAny) {
2848 if (AllowUnalign) {
2849 VT = MVT::i64;
2850 } else {
2851 switch (Align & 7) {
2852 case 0: VT = MVT::i64; break;
2853 case 4: VT = MVT::i32; break;
2854 case 2: VT = MVT::i16; break;
2855 default: VT = MVT::i8; break;
2856 }
2857 }
2858
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002860 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002861 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2862 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002863
Duncan Sands8e4eb092008-06-08 20:54:56 +00002864 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002865 VT = LVT;
2866 }
Dan Gohman707e0182008-04-12 04:36:06 +00002867
2868 unsigned NumMemOps = 0;
2869 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002870 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002871 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002872 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002874 VT = MVT::i64;
2875 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002876 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2877 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002878 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002879 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002880 VTSize >>= 1;
2881 }
Dan Gohman707e0182008-04-12 04:36:06 +00002882 }
Dan Gohman707e0182008-04-12 04:36:06 +00002883
2884 if (++NumMemOps > Limit)
2885 return false;
2886 MemOps.push_back(VT);
2887 Size -= VTSize;
2888 }
2889
2890 return true;
2891}
2892
Dan Gohman475871a2008-07-27 21:46:04 +00002893static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2894 SDValue Chain, SDValue Dst,
2895 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002896 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002897 const Value *DstSV, uint64_t DstSVOff,
2898 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002899 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2900
Dan Gohman21323f32008-05-29 19:42:22 +00002901 // Expand memcpy to a series of load and store ops if the size operand falls
2902 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002903 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002904 uint64_t Limit = -1;
2905 if (!AlwaysInline)
2906 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002907 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002908 std::string Str;
2909 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002910 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002911 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002912 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002913
Dan Gohman707e0182008-04-12 04:36:06 +00002914
Evan Cheng0ff39b32008-06-30 07:31:25 +00002915 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002917 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002918 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002919 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002920 MVT VT = MemOps[i];
2921 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002923
Evan Cheng0ff39b32008-06-30 07:31:25 +00002924 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002925 // It's unlikely a store of a vector immediate can be done in a single
2926 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002927 // We also handle store a vector with all zero's.
2928 // FIXME: Handle other cases where store of vector immediate is done in
2929 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002930 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002931 Store = DAG.getStore(Chain, Value,
2932 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002933 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002934 } else {
2935 Value = DAG.getLoad(VT, Chain,
2936 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002937 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002938 Store = DAG.getStore(Chain, Value,
2939 getMemBasePlusOffset(Dst, DstOff, DAG),
2940 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002941 }
2942 OutChains.push_back(Store);
2943 SrcOff += VTSize;
2944 DstOff += VTSize;
2945 }
2946
2947 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2948 &OutChains[0], OutChains.size());
2949}
2950
Dan Gohman475871a2008-07-27 21:46:04 +00002951static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2952 SDValue Chain, SDValue Dst,
2953 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002954 unsigned Align, bool AlwaysInline,
2955 const Value *DstSV, uint64_t DstSVOff,
2956 const Value *SrcSV, uint64_t SrcSVOff){
2957 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2958
2959 // Expand memmove to a series of load and store ops if the size operand falls
2960 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002961 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002962 uint64_t Limit = -1;
2963 if (!AlwaysInline)
2964 Limit = TLI.getMaxStoresPerMemmove();
2965 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002966 std::string Str;
2967 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002968 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002969 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002970 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002971
Dan Gohman21323f32008-05-29 19:42:22 +00002972 uint64_t SrcOff = 0, DstOff = 0;
2973
Dan Gohman475871a2008-07-27 21:46:04 +00002974 SmallVector<SDValue, 8> LoadValues;
2975 SmallVector<SDValue, 8> LoadChains;
2976 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002977 unsigned NumMemOps = MemOps.size();
2978 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002979 MVT VT = MemOps[i];
2980 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002981 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002982
2983 Value = DAG.getLoad(VT, Chain,
2984 getMemBasePlusOffset(Src, SrcOff, DAG),
2985 SrcSV, SrcSVOff + SrcOff, false, Align);
2986 LoadValues.push_back(Value);
2987 LoadChains.push_back(Value.getValue(1));
2988 SrcOff += VTSize;
2989 }
2990 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2991 &LoadChains[0], LoadChains.size());
2992 OutChains.clear();
2993 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002994 MVT VT = MemOps[i];
2995 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002996 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002997
2998 Store = DAG.getStore(Chain, LoadValues[i],
2999 getMemBasePlusOffset(Dst, DstOff, DAG),
3000 DstSV, DstSVOff + DstOff, false, DstAlign);
3001 OutChains.push_back(Store);
3002 DstOff += VTSize;
3003 }
3004
3005 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3006 &OutChains[0], OutChains.size());
3007}
3008
Dan Gohman475871a2008-07-27 21:46:04 +00003009static SDValue getMemsetStores(SelectionDAG &DAG,
3010 SDValue Chain, SDValue Dst,
3011 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003012 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003013 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003014 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3015
3016 // Expand memset to a series of load/store ops if the size operand
3017 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003018 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003019 std::string Str;
3020 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003021 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003022 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003023 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003024
Dan Gohman475871a2008-07-27 21:46:04 +00003025 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003026 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003027
3028 unsigned NumMemOps = MemOps.size();
3029 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003030 MVT VT = MemOps[i];
3031 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003032 SDValue Value = getMemsetValue(Src, VT, DAG);
3033 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00003034 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003035 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003036 OutChains.push_back(Store);
3037 DstOff += VTSize;
3038 }
3039
3040 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3041 &OutChains[0], OutChains.size());
3042}
3043
Dan Gohman475871a2008-07-27 21:46:04 +00003044SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3045 SDValue Src, SDValue Size,
3046 unsigned Align, bool AlwaysInline,
3047 const Value *DstSV, uint64_t DstSVOff,
3048 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003049
3050 // Check to see if we should lower the memcpy to loads and stores first.
3051 // For cases within the target-specified limits, this is the best choice.
3052 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3053 if (ConstantSize) {
3054 // Memcpy with size zero? Just return the original chain.
3055 if (ConstantSize->isNullValue())
3056 return Chain;
3057
Dan Gohman475871a2008-07-27 21:46:04 +00003058 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003059 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3060 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003061 Align, false, 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
3066 // Then check to see if we should lower the memcpy with target-specific
3067 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003069 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3070 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003071 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003072 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003073 return Result;
3074
3075 // If we really need inline code and the target declined to provide it,
3076 // use a (potentially long) sequence of loads and stores.
3077 if (AlwaysInline) {
3078 assert(ConstantSize && "AlwaysInline requires a constant size!");
3079 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003080 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003081 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003082 }
3083
3084 // Emit a library call.
3085 TargetLowering::ArgListTy Args;
3086 TargetLowering::ArgListEntry Entry;
3087 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3088 Entry.Node = Dst; Args.push_back(Entry);
3089 Entry.Node = Src; Args.push_back(Entry);
3090 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003091 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003092 TLI.LowerCallTo(Chain, Type::VoidTy,
3093 false, false, false, CallingConv::C, false,
3094 getExternalSymbol("memcpy", TLI.getPointerTy()),
3095 Args, *this);
3096 return CallResult.second;
3097}
3098
Dan Gohman475871a2008-07-27 21:46:04 +00003099SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3100 SDValue Src, SDValue Size,
3101 unsigned Align,
3102 const Value *DstSV, uint64_t DstSVOff,
3103 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003104
Dan Gohman21323f32008-05-29 19:42:22 +00003105 // Check to see if we should lower the memmove to loads and stores first.
3106 // For cases within the target-specified limits, this is the best choice.
3107 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3108 if (ConstantSize) {
3109 // Memmove with size zero? Just return the original chain.
3110 if (ConstantSize->isNullValue())
3111 return Chain;
3112
Dan Gohman475871a2008-07-27 21:46:04 +00003113 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003114 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3115 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003116 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003117 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003118 return Result;
3119 }
Dan Gohman707e0182008-04-12 04:36:06 +00003120
3121 // Then check to see if we should lower the memmove with target-specific
3122 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003124 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003125 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003126 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003127 return Result;
3128
3129 // Emit a library call.
3130 TargetLowering::ArgListTy Args;
3131 TargetLowering::ArgListEntry Entry;
3132 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3133 Entry.Node = Dst; Args.push_back(Entry);
3134 Entry.Node = Src; Args.push_back(Entry);
3135 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003136 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003137 TLI.LowerCallTo(Chain, Type::VoidTy,
3138 false, false, false, CallingConv::C, false,
3139 getExternalSymbol("memmove", TLI.getPointerTy()),
3140 Args, *this);
3141 return CallResult.second;
3142}
3143
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3145 SDValue Src, SDValue Size,
3146 unsigned Align,
3147 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003148
3149 // Check to see if we should lower the memset to stores first.
3150 // For cases within the target-specified limits, this is the best choice.
3151 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3152 if (ConstantSize) {
3153 // Memset with size zero? Just return the original chain.
3154 if (ConstantSize->isNullValue())
3155 return Chain;
3156
Dan Gohman475871a2008-07-27 21:46:04 +00003157 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003158 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3159 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003160 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003161 return Result;
3162 }
3163
3164 // Then check to see if we should lower the memset with target-specific
3165 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003166 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003167 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003168 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003169 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003170 return Result;
3171
3172 // Emit a library call.
3173 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3174 TargetLowering::ArgListTy Args;
3175 TargetLowering::ArgListEntry Entry;
3176 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3177 Args.push_back(Entry);
3178 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003179 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003180 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3181 else
3182 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3183 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3184 Args.push_back(Entry);
3185 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3186 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003187 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003188 TLI.LowerCallTo(Chain, Type::VoidTy,
3189 false, false, false, CallingConv::C, false,
3190 getExternalSymbol("memset", TLI.getPointerTy()),
3191 Args, *this);
3192 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003193}
3194
Dan Gohman475871a2008-07-27 21:46:04 +00003195SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3196 SDValue Ptr, SDValue Cmp,
3197 SDValue Swp, const Value* PtrVal,
3198 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003199 assert((Opcode == ISD::ATOMIC_CMP_SWAP_8 ||
3200 Opcode == ISD::ATOMIC_CMP_SWAP_16 ||
3201 Opcode == ISD::ATOMIC_CMP_SWAP_32 ||
3202 Opcode == ISD::ATOMIC_CMP_SWAP_64) && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003203 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003204
3205 MVT VT = Cmp.getValueType();
3206
3207 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3208 Alignment = getMVTAlignment(VT);
3209
3210 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003211 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003212 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003213 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003214 void* IP = 0;
3215 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003216 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003217 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003218 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003219 CSEMap.InsertNode(N, IP);
3220 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003221 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003222}
3223
Dan Gohman475871a2008-07-27 21:46:04 +00003224SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3225 SDValue Ptr, SDValue Val,
3226 const Value* PtrVal,
3227 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003228 assert((Opcode == ISD::ATOMIC_LOAD_ADD_8 ||
3229 Opcode == ISD::ATOMIC_LOAD_SUB_8 ||
3230 Opcode == ISD::ATOMIC_LOAD_AND_8 ||
3231 Opcode == ISD::ATOMIC_LOAD_OR_8 ||
3232 Opcode == ISD::ATOMIC_LOAD_XOR_8 ||
3233 Opcode == ISD::ATOMIC_LOAD_NAND_8 ||
3234 Opcode == ISD::ATOMIC_LOAD_MIN_8 ||
3235 Opcode == ISD::ATOMIC_LOAD_MAX_8 ||
3236 Opcode == ISD::ATOMIC_LOAD_UMIN_8 ||
3237 Opcode == ISD::ATOMIC_LOAD_UMAX_8 ||
3238 Opcode == ISD::ATOMIC_SWAP_8 ||
3239 Opcode == ISD::ATOMIC_LOAD_ADD_16 ||
3240 Opcode == ISD::ATOMIC_LOAD_SUB_16 ||
3241 Opcode == ISD::ATOMIC_LOAD_AND_16 ||
3242 Opcode == ISD::ATOMIC_LOAD_OR_16 ||
3243 Opcode == ISD::ATOMIC_LOAD_XOR_16 ||
3244 Opcode == ISD::ATOMIC_LOAD_NAND_16 ||
3245 Opcode == ISD::ATOMIC_LOAD_MIN_16 ||
3246 Opcode == ISD::ATOMIC_LOAD_MAX_16 ||
3247 Opcode == ISD::ATOMIC_LOAD_UMIN_16 ||
3248 Opcode == ISD::ATOMIC_LOAD_UMAX_16 ||
3249 Opcode == ISD::ATOMIC_SWAP_16 ||
3250 Opcode == ISD::ATOMIC_LOAD_ADD_32 ||
3251 Opcode == ISD::ATOMIC_LOAD_SUB_32 ||
3252 Opcode == ISD::ATOMIC_LOAD_AND_32 ||
3253 Opcode == ISD::ATOMIC_LOAD_OR_32 ||
3254 Opcode == ISD::ATOMIC_LOAD_XOR_32 ||
3255 Opcode == ISD::ATOMIC_LOAD_NAND_32 ||
3256 Opcode == ISD::ATOMIC_LOAD_MIN_32 ||
3257 Opcode == ISD::ATOMIC_LOAD_MAX_32 ||
3258 Opcode == ISD::ATOMIC_LOAD_UMIN_32 ||
3259 Opcode == ISD::ATOMIC_LOAD_UMAX_32 ||
3260 Opcode == ISD::ATOMIC_SWAP_32 ||
3261 Opcode == ISD::ATOMIC_LOAD_ADD_64 ||
3262 Opcode == ISD::ATOMIC_LOAD_SUB_64 ||
3263 Opcode == ISD::ATOMIC_LOAD_AND_64 ||
3264 Opcode == ISD::ATOMIC_LOAD_OR_64 ||
3265 Opcode == ISD::ATOMIC_LOAD_XOR_64 ||
3266 Opcode == ISD::ATOMIC_LOAD_NAND_64 ||
3267 Opcode == ISD::ATOMIC_LOAD_MIN_64 ||
3268 Opcode == ISD::ATOMIC_LOAD_MAX_64 ||
3269 Opcode == ISD::ATOMIC_LOAD_UMIN_64 ||
3270 Opcode == ISD::ATOMIC_LOAD_UMAX_64 ||
3271 Opcode == ISD::ATOMIC_SWAP_64) && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003272
3273 MVT VT = Val.getValueType();
3274
3275 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3276 Alignment = getMVTAlignment(VT);
3277
3278 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003279 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003280 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003281 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003282 void* IP = 0;
3283 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003284 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003285 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003286 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003287 CSEMap.InsertNode(N, IP);
3288 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003289 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003290}
3291
Duncan Sands4bdcb612008-07-02 17:40:58 +00003292/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3293/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003294SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3295 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003296 if (Simplify && NumOps == 1)
3297 return Ops[0];
3298
3299 SmallVector<MVT, 4> VTs;
3300 VTs.reserve(NumOps);
3301 for (unsigned i = 0; i < NumOps; ++i)
3302 VTs.push_back(Ops[i].getValueType());
3303 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3304}
3305
Dan Gohman475871a2008-07-27 21:46:04 +00003306SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003307SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003308 MVT VT, SDValue Chain,
3309 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003310 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003311 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003312 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3313 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003314
Duncan Sands14ea39c2008-03-27 20:23:40 +00003315 if (VT == EVT) {
3316 ExtType = ISD::NON_EXTLOAD;
3317 } else if (ExtType == ISD::NON_EXTLOAD) {
3318 assert(VT == EVT && "Non-extending load from different memory type!");
3319 } else {
3320 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003321 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003322 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3323 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003324 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003325 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003326 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003327 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003328 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003330 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003331 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003332
3333 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003334 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003335 "Unindexed load with an offset!");
3336
3337 SDVTList VTs = Indexed ?
3338 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003339 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003340 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003341 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003342 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003343 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003344 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003345 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003346 void *IP = 0;
3347 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003348 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003349 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003350 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3351 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003352 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003353 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003354 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003355}
3356
Dan Gohman475871a2008-07-27 21:46:04 +00003357SDValue SelectionDAG::getLoad(MVT VT,
3358 SDValue Chain, SDValue Ptr,
3359 const Value *SV, int SVOffset,
3360 bool isVolatile, unsigned Alignment) {
3361 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003362 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3363 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003364}
3365
Dan Gohman475871a2008-07-27 21:46:04 +00003366SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3367 SDValue Chain, SDValue Ptr,
3368 const Value *SV,
3369 int SVOffset, MVT EVT,
3370 bool isVolatile, unsigned Alignment) {
3371 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003372 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3373 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003374}
3375
Dan Gohman475871a2008-07-27 21:46:04 +00003376SDValue
3377SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3378 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003379 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003380 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3381 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003382 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3383 LD->getChain(), Base, Offset, LD->getSrcValue(),
3384 LD->getSrcValueOffset(), LD->getMemoryVT(),
3385 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003386}
3387
Dan Gohman475871a2008-07-27 21:46:04 +00003388SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3389 SDValue Ptr, const Value *SV, int SVOffset,
3390 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003391 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003392
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003393 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3394 Alignment = getMVTAlignment(VT);
3395
Evan Chengad071e12006-10-05 22:57:11 +00003396 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003397 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3398 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003399 FoldingSetNodeID ID;
3400 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003401 ID.AddInteger(ISD::UNINDEXED);
3402 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003403 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003404 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003405 void *IP = 0;
3406 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003407 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003408 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003409 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3410 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003411 CSEMap.InsertNode(N, IP);
3412 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003413 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003414}
3415
Dan Gohman475871a2008-07-27 21:46:04 +00003416SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3417 SDValue Ptr, const Value *SV,
3418 int SVOffset, MVT SVT,
3419 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003420 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003421
3422 if (VT == SVT)
3423 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003424
Duncan Sands8e4eb092008-06-08 20:54:56 +00003425 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003426 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003427 "Can't do FP-INT conversion!");
3428
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003429 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3430 Alignment = getMVTAlignment(VT);
3431
Evan Cheng8b2794a2006-10-13 21:14:26 +00003432 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3434 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003435 FoldingSetNodeID ID;
3436 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003437 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003438 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003439 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003440 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003441 void *IP = 0;
3442 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003443 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003444 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003445 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3446 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003447 CSEMap.InsertNode(N, IP);
3448 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003449 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003450}
3451
Dan Gohman475871a2008-07-27 21:46:04 +00003452SDValue
3453SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3454 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003455 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3456 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3457 "Store is already a indexed store!");
3458 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003460 FoldingSetNodeID ID;
3461 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3462 ID.AddInteger(AM);
3463 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003464 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003465 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003466 void *IP = 0;
3467 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003468 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003469 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003470 new (N) StoreSDNode(Ops, VTs, AM,
3471 ST->isTruncatingStore(), ST->getMemoryVT(),
3472 ST->getSrcValue(), ST->getSrcValueOffset(),
3473 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003474 CSEMap.InsertNode(N, IP);
3475 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003476 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003477}
3478
Dan Gohman475871a2008-07-27 21:46:04 +00003479SDValue SelectionDAG::getVAArg(MVT VT,
3480 SDValue Chain, SDValue Ptr,
3481 SDValue SV) {
3482 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003483 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003484}
3485
Dan Gohman475871a2008-07-27 21:46:04 +00003486SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3487 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003488 switch (NumOps) {
3489 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003490 case 1: return getNode(Opcode, VT, Ops[0]);
3491 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3492 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003493 default: break;
3494 }
3495
Dan Gohman475871a2008-07-27 21:46:04 +00003496 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003497 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003498 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3499 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003500}
3501
Dan Gohman475871a2008-07-27 21:46:04 +00003502SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3503 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003504 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003505 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003506 case 1: return getNode(Opcode, VT, Ops[0]);
3507 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3508 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003509 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003510 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003511
Chris Lattneref847df2005-04-09 03:27:28 +00003512 switch (Opcode) {
3513 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003514 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003515 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003516 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3517 "LHS and RHS of condition must have same type!");
3518 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3519 "True and False arms of SelectCC must have same type!");
3520 assert(Ops[2].getValueType() == VT &&
3521 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003522 break;
3523 }
3524 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003525 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003526 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3527 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003528 break;
3529 }
Chris Lattneref847df2005-04-09 03:27:28 +00003530 }
3531
Chris Lattner385328c2005-05-14 07:42:29 +00003532 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003533 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003534 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003535 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003536 FoldingSetNodeID ID;
3537 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003538 void *IP = 0;
3539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003540 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003541 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003542 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003543 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003544 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003545 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003546 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003547 }
Chris Lattneref847df2005-04-09 03:27:28 +00003548 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003549#ifndef NDEBUG
3550 VerifyNode(N);
3551#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003552 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003553}
3554
Dan Gohman475871a2008-07-27 21:46:04 +00003555SDValue SelectionDAG::getNode(unsigned Opcode,
3556 const std::vector<MVT> &ResultTys,
3557 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003558 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3559 Ops, NumOps);
3560}
3561
Dan Gohman475871a2008-07-27 21:46:04 +00003562SDValue SelectionDAG::getNode(unsigned Opcode,
3563 const MVT *VTs, unsigned NumVTs,
3564 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003565 if (NumVTs == 1)
3566 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003567 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3568}
3569
Dan Gohman475871a2008-07-27 21:46:04 +00003570SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3571 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003572 if (VTList.NumVTs == 1)
3573 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003574
Chris Lattner5f056bf2005-07-10 01:55:33 +00003575 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003576 // FIXME: figure out how to safely handle things like
3577 // int foo(int x) { return 1 << (x & 255); }
3578 // int bar() { return foo(256); }
3579#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003580 case ISD::SRA_PARTS:
3581 case ISD::SRL_PARTS:
3582 case ISD::SHL_PARTS:
3583 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003584 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003585 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3586 else if (N3.getOpcode() == ISD::AND)
3587 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3588 // If the and is only masking out bits that cannot effect the shift,
3589 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003590 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003591 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3592 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3593 }
3594 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003595#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003596 }
Chris Lattner89c34632005-05-14 06:20:26 +00003597
Chris Lattner43247a12005-08-25 19:12:10 +00003598 // Memoize the node unless it returns a flag.
3599 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003600 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003601 FoldingSetNodeID ID;
3602 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003603 void *IP = 0;
3604 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003605 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003606 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003607 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003608 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3609 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003610 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003611 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3612 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003613 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003614 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3615 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003616 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003617 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3618 }
Chris Lattnera5682852006-08-07 23:03:03 +00003619 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003620 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003621 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003622 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003623 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3624 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003625 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003626 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3627 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003628 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003629 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3630 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003631 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003632 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3633 }
Chris Lattner43247a12005-08-25 19:12:10 +00003634 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003635 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003636#ifndef NDEBUG
3637 VerifyNode(N);
3638#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003639 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003640}
3641
Dan Gohman475871a2008-07-27 21:46:04 +00003642SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003643 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003644}
3645
Dan Gohman475871a2008-07-27 21:46:04 +00003646SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3647 SDValue N1) {
3648 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003649 return getNode(Opcode, VTList, Ops, 1);
3650}
3651
Dan Gohman475871a2008-07-27 21:46:04 +00003652SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3653 SDValue N1, SDValue N2) {
3654 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003655 return getNode(Opcode, VTList, Ops, 2);
3656}
3657
Dan Gohman475871a2008-07-27 21:46:04 +00003658SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3659 SDValue N1, SDValue N2, SDValue N3) {
3660 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003661 return getNode(Opcode, VTList, Ops, 3);
3662}
3663
Dan Gohman475871a2008-07-27 21:46:04 +00003664SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3665 SDValue N1, SDValue N2, SDValue N3,
3666 SDValue N4) {
3667 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003668 return getNode(Opcode, VTList, Ops, 4);
3669}
3670
Dan Gohman475871a2008-07-27 21:46:04 +00003671SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3672 SDValue N1, SDValue N2, SDValue N3,
3673 SDValue N4, SDValue N5) {
3674 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003675 return getNode(Opcode, VTList, Ops, 5);
3676}
3677
Duncan Sands83ec4b62008-06-06 12:08:01 +00003678SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003679 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003680}
3681
Duncan Sands83ec4b62008-06-06 12:08:01 +00003682SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003683 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3684 E = VTList.rend(); I != E; ++I)
3685 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3686 return *I;
3687
3688 MVT *Array = Allocator.Allocate<MVT>(2);
3689 Array[0] = VT1;
3690 Array[1] = VT2;
3691 SDVTList Result = makeVTList(Array, 2);
3692 VTList.push_back(Result);
3693 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003694}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003695
3696SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3697 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3698 E = VTList.rend(); I != E; ++I)
3699 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3700 I->VTs[2] == VT3)
3701 return *I;
3702
3703 MVT *Array = Allocator.Allocate<MVT>(3);
3704 Array[0] = VT1;
3705 Array[1] = VT2;
3706 Array[2] = VT3;
3707 SDVTList Result = makeVTList(Array, 3);
3708 VTList.push_back(Result);
3709 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003710}
3711
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003713 switch (NumVTs) {
3714 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003715 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003716 case 2: return getVTList(VTs[0], VTs[1]);
3717 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3718 default: break;
3719 }
3720
Dan Gohmane8be6c62008-07-17 19:10:17 +00003721 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3722 E = VTList.rend(); I != E; ++I) {
3723 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3724 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003725
3726 bool NoMatch = false;
3727 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003728 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003729 NoMatch = true;
3730 break;
3731 }
3732 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003733 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003734 }
3735
Dan Gohmane8be6c62008-07-17 19:10:17 +00003736 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3737 std::copy(VTs, VTs+NumVTs, Array);
3738 SDVTList Result = makeVTList(Array, NumVTs);
3739 VTList.push_back(Result);
3740 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003741}
3742
3743
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003744/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3745/// specified operands. If the resultant node already exists in the DAG,
3746/// this does not modify the specified node, instead it returns the node that
3747/// already exists. If the resultant node does not exist in the DAG, the
3748/// input node is returned. As a degenerate case, if you specify the same
3749/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003750SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003751 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003752 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3753
3754 // Check to see if there is no change.
3755 if (Op == N->getOperand(0)) return InN;
3756
3757 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003758 void *InsertPos = 0;
3759 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003760 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003761
Dan Gohman79acd2b2008-07-21 22:38:59 +00003762 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003763 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003764 RemoveNodeFromCSEMaps(N);
3765
3766 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003767 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003768 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003769 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003770 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003771
3772 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003773 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003774 return InN;
3775}
3776
Dan Gohman475871a2008-07-27 21:46:04 +00003777SDValue SelectionDAG::
3778UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003779 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003780 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3781
3782 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003783 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3784 return InN; // No operands changed, just return the input node.
3785
3786 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003787 void *InsertPos = 0;
3788 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003789 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003790
Dan Gohman79acd2b2008-07-21 22:38:59 +00003791 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003792 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003793 RemoveNodeFromCSEMaps(N);
3794
3795 // Now we update the operands.
3796 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003797 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003798 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003799 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003800 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003801 }
3802 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003803 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003804 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003805 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003806 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003807 }
3808
3809 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003810 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003811 return InN;
3812}
3813
Dan Gohman475871a2008-07-27 21:46:04 +00003814SDValue SelectionDAG::
3815UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3816 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003817 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003818}
3819
Dan Gohman475871a2008-07-27 21:46:04 +00003820SDValue SelectionDAG::
3821UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3822 SDValue Op3, SDValue Op4) {
3823 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003824 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003825}
3826
Dan Gohman475871a2008-07-27 21:46:04 +00003827SDValue SelectionDAG::
3828UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3829 SDValue Op3, SDValue Op4, SDValue Op5) {
3830 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003831 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003832}
3833
Dan Gohman475871a2008-07-27 21:46:04 +00003834SDValue SelectionDAG::
3835UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003836 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003837 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003838 "Update with wrong number of operands");
3839
3840 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003841 bool AnyChange = false;
3842 for (unsigned i = 0; i != NumOps; ++i) {
3843 if (Ops[i] != N->getOperand(i)) {
3844 AnyChange = true;
3845 break;
3846 }
3847 }
3848
3849 // No operands changed, just return the input node.
3850 if (!AnyChange) return InN;
3851
3852 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003853 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003854 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003855 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003856
Dan Gohman7ceda162008-05-02 00:05:03 +00003857 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003858 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003859 RemoveNodeFromCSEMaps(N);
3860
3861 // Now we update the operands.
3862 for (unsigned i = 0; i != NumOps; ++i) {
3863 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003864 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003865 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003866 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003867 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003868 }
3869 }
3870
3871 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003872 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003873 return InN;
3874}
3875
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003876/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003877/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003878void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003879 // Unlike the code in MorphNodeTo that does this, we don't need to
3880 // watch for dead nodes here.
3881 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3882 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3883
3884 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003885}
Chris Lattner149c58c2005-08-16 18:17:10 +00003886
Dan Gohmane8be6c62008-07-17 19:10:17 +00003887/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3888/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003889///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003890SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003892 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003893 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003894}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003895
Dan Gohmane8be6c62008-07-17 19:10:17 +00003896SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003897 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003898 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003900 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003901}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003902
Dan Gohmane8be6c62008-07-17 19:10:17 +00003903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003904 MVT VT, SDValue Op1,
3905 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003906 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003909}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003910
Dan Gohmane8be6c62008-07-17 19:10:17 +00003911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003912 MVT VT, SDValue Op1,
3913 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003914 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003916 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003917}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003918
Dan Gohmane8be6c62008-07-17 19:10:17 +00003919SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003920 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003921 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003922 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003923 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003924}
3925
Dan Gohmane8be6c62008-07-17 19:10:17 +00003926SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003927 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003928 unsigned NumOps) {
3929 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003930 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003931}
3932
Dan Gohmane8be6c62008-07-17 19:10:17 +00003933SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003934 MVT VT1, MVT VT2) {
3935 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003936 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003937}
3938
Dan Gohmane8be6c62008-07-17 19:10:17 +00003939SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003940 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003941 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003942 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003943 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003944}
3945
Dan Gohmane8be6c62008-07-17 19:10:17 +00003946SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003947 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003949 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003952}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003953
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003955 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003957 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003958 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003959 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003960}
3961
Dan Gohmane8be6c62008-07-17 19:10:17 +00003962SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003963 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003964 SDValue Op1, SDValue Op2,
3965 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003966 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003967 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003968 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003969}
3970
Dan Gohmane8be6c62008-07-17 19:10:17 +00003971SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003972 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003973 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003974 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3975}
3976
3977SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3978 MVT VT) {
3979 SDVTList VTs = getVTList(VT);
3980 return MorphNodeTo(N, Opc, VTs, 0, 0);
3981}
3982
3983SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003984 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003985 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003986 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003987 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3988}
3989
3990SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003991 MVT VT, SDValue Op1,
3992 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003993 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003994 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003995 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3996}
3997
3998SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003999 MVT VT, SDValue Op1,
4000 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004001 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004002 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004003 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4004}
4005
4006SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004007 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008 unsigned NumOps) {
4009 SDVTList VTs = getVTList(VT);
4010 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4011}
4012
4013SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004014 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004015 unsigned NumOps) {
4016 SDVTList VTs = getVTList(VT1, VT2);
4017 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4018}
4019
4020SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4021 MVT VT1, MVT VT2) {
4022 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004023 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004024}
4025
4026SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4027 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004028 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004029 SDVTList VTs = getVTList(VT1, VT2, VT3);
4030 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4031}
4032
4033SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4034 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004035 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004036 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004037 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4039}
4040
4041SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4042 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004045 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004046 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4047}
4048
4049SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4050 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004051 SDValue Op1, SDValue Op2,
4052 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004053 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004054 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004055 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4056}
4057
4058/// MorphNodeTo - These *mutate* the specified node to have the specified
4059/// return type, opcode, and operands.
4060///
4061/// Note that MorphNodeTo returns the resultant node. If there is already a
4062/// node of the specified opcode and operands, it returns that node instead of
4063/// the current one.
4064///
4065/// Using MorphNodeTo is faster than creating a new node and swapping it in
4066/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004067/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004068/// the node's users.
4069///
4070SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004071 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004072 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004073 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004074 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004075 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4076 FoldingSetNodeID ID;
4077 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4078 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4079 return ON;
4080 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004081
Chris Lattner0fb094f2005-11-19 01:44:53 +00004082 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00004083
Dan Gohmane8be6c62008-07-17 19:10:17 +00004084 // Start the morphing.
4085 N->NodeType = Opc;
4086 N->ValueList = VTs.VTs;
4087 N->NumValues = VTs.NumVTs;
4088
4089 // Clear the operands list, updating used nodes to remove this from their
4090 // use list. Keep track of any operands that become dead as a result.
4091 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4092 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4093 I != E; ++I) {
4094 SDNode *Used = I->getVal();
4095 Used->removeUser(std::distance(B, I), N);
4096 if (Used->use_empty())
4097 DeadNodeSet.insert(Used);
4098 }
4099
4100 // If NumOps is larger than the # of operands we currently have, reallocate
4101 // the operand list.
4102 if (NumOps > N->NumOperands) {
4103 if (N->OperandsNeedDelete)
4104 delete[] N->OperandList;
4105 if (N->isMachineOpcode()) {
4106 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004107 // remainder of the current SelectionDAG iteration, so we can allocate
4108 // the operands directly out of a pool with no recycling metadata.
4109 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004110 N->OperandsNeedDelete = false;
4111 } else {
4112 N->OperandList = new SDUse[NumOps];
4113 N->OperandsNeedDelete = true;
4114 }
4115 }
4116
4117 // Assign the new operands.
4118 N->NumOperands = NumOps;
4119 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4120 N->OperandList[i] = Ops[i];
4121 N->OperandList[i].setUser(N);
4122 SDNode *ToUse = N->OperandList[i].getVal();
4123 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004124 }
4125
4126 // Delete any nodes that are still dead after adding the uses for the
4127 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004128 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004129 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4130 E = DeadNodeSet.end(); I != E; ++I)
4131 if ((*I)->use_empty())
4132 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004133 RemoveDeadNodes(DeadNodes);
4134
Dan Gohmane8be6c62008-07-17 19:10:17 +00004135 if (IP)
4136 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004137 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004138}
4139
Chris Lattner0fb094f2005-11-19 01:44:53 +00004140
Evan Cheng6ae46c42006-02-09 07:15:23 +00004141/// getTargetNode - These are used for target selectors to create a new node
4142/// with specified return type(s), target opcode, and operands.
4143///
4144/// Note that getTargetNode returns the resultant node. If there is already a
4145/// node of the specified opcode and operands, it returns that node instead of
4146/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004148 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004149}
Dan Gohman475871a2008-07-27 21:46:04 +00004150SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004151 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004152}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004153SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004155 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004156}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004157SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004158 SDValue Op1, SDValue Op2,
4159 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004160 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004161}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004162SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004163 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004164 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004165}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004166SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4167 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004168 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004169 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004170}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004171SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004172 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004173 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004174 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004175}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004176SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004177 MVT VT2, SDValue Op1,
4178 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004179 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004180 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004181 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004182}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004184 MVT VT2, SDValue Op1,
4185 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004186 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004187 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004188 return getNode(~Opcode, VTs, 2, Ops, 3).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,
Dan Gohman475871a2008-07-27 21:46:04 +00004191 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004192 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004193 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004194}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004195SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004196 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004198 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004199 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004200}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004201SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004202 SDValue Op1, SDValue Op2,
4203 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004204 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004206 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004207}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004209 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004210 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004211 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004212}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004213SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4214 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004215 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004216 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004217 VTList.push_back(VT1);
4218 VTList.push_back(VT2);
4219 VTList.push_back(VT3);
4220 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004222 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004223}
Evan Cheng39305cf2007-10-05 01:10:49 +00004224SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004225 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004226 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004227 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004228 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004229 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004230}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004231
Evan Cheng08b11732008-03-22 01:55:50 +00004232/// getNodeIfExists - Get the specified node if it's already available, or
4233/// else return NULL.
4234SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004235 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004236 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4237 FoldingSetNodeID ID;
4238 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4239 void *IP = 0;
4240 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4241 return E;
4242 }
4243 return NULL;
4244}
4245
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004246
Evan Cheng99157a02006-08-07 22:13:29 +00004247/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004248/// This can cause recursive merging of nodes in the DAG.
4249///
Chris Lattner11d049c2008-02-03 03:35:22 +00004250/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004251///
Dan Gohman475871a2008-07-27 21:46:04 +00004252void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004253 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004254 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004255 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004256 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004257 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004258
Chris Lattner8b8749f2005-08-17 19:00:20 +00004259 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004260 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004261 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004262
Chris Lattner8b8749f2005-08-17 19:00:20 +00004263 // This node is about to morph, remove its old self from the CSE maps.
4264 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004265 int operandNum = 0;
4266 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4267 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004268 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004269 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004270 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004271 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004272 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004273 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004274
4275 // Now that we have modified U, add it back to the CSE maps. If it already
4276 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004277 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004278 ReplaceAllUsesWith(U, Existing, UpdateListener);
4279 // U is now dead. Inform the listener if it exists and delete it.
4280 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004281 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004282 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004283 } else {
4284 // If the node doesn't already exist, we updated it. Inform a listener if
4285 // it exists.
4286 if (UpdateListener)
4287 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004288 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004289 }
4290}
4291
4292/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4293/// This can cause recursive merging of nodes in the DAG.
4294///
4295/// This version assumes From/To have matching types and numbers of result
4296/// values.
4297///
Chris Lattner1e111c72005-09-07 05:37:01 +00004298void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004299 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004300 assert(From->getVTList().VTs == To->getVTList().VTs &&
4301 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004302 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004303
4304 // Handle the trivial case.
4305 if (From == To)
4306 return;
4307
Chris Lattner8b52f212005-08-26 18:36:28 +00004308 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004309 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004310 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004311
Chris Lattner8b52f212005-08-26 18:36:28 +00004312 // This node is about to morph, remove its old self from the CSE maps.
4313 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004314 int operandNum = 0;
4315 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4316 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004317 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004318 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004319 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004320 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004321 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004322
Chris Lattner8b8749f2005-08-17 19:00:20 +00004323 // Now that we have modified U, add it back to the CSE maps. If it already
4324 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004325 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004326 ReplaceAllUsesWith(U, Existing, UpdateListener);
4327 // U is now dead. Inform the listener if it exists and delete it.
4328 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004329 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004330 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004331 } else {
4332 // If the node doesn't already exist, we updated it. Inform a listener if
4333 // it exists.
4334 if (UpdateListener)
4335 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004336 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004337 }
4338}
4339
Chris Lattner8b52f212005-08-26 18:36:28 +00004340/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4341/// This can cause recursive merging of nodes in the DAG.
4342///
4343/// This version can replace From with any result values. To must match the
4344/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004345void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004346 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004347 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004348 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004349 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004350
4351 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004352 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004353 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004354
Chris Lattner7b2880c2005-08-24 22:44:39 +00004355 // This node is about to morph, remove its old self from the CSE maps.
4356 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004357 int operandNum = 0;
4358 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4359 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004360 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004361 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004362 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004363 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004364 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004365 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004366 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004367
Chris Lattner7b2880c2005-08-24 22:44:39 +00004368 // Now that we have modified U, add it back to the CSE maps. If it already
4369 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004370 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004371 ReplaceAllUsesWith(U, Existing, UpdateListener);
4372 // U is now dead. Inform the listener if it exists and delete it.
4373 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004374 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004375 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004376 } else {
4377 // If the node doesn't already exist, we updated it. Inform a listener if
4378 // it exists.
4379 if (UpdateListener)
4380 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004381 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004382 }
4383}
4384
Chris Lattner012f2412006-02-17 21:58:01 +00004385/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004386/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004387/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004388void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004389 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004390 // Handle the really simple, really trivial case efficiently.
4391 if (From == To) return;
4392
Chris Lattner012f2412006-02-17 21:58:01 +00004393 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004394 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004395 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004396 return;
4397 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004398
Gabor Greifba36cb52008-08-28 21:40:38 +00004399 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004400 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004401 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004402
4403 while (!Users.empty()) {
4404 // We know that this user uses some value of From. If it is the right
4405 // value, update it.
4406 SDNode *User = Users.back();
4407 Users.pop_back();
4408
Chris Lattner01d029b2007-10-15 06:10:22 +00004409 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004410 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004411 for (; Op != E; ++Op)
4412 if (*Op == From) break;
4413
4414 // If there are no matches, the user must use some other result of From.
4415 if (Op == E) continue;
4416
4417 // Okay, we know this user needs to be updated. Remove its old self
4418 // from the CSE maps.
4419 RemoveNodeFromCSEMaps(User);
4420
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004421 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004422 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004423 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004424 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004425 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004426 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004427 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004428 }
4429 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004430
4431 // Now that we have modified User, add it back to the CSE maps. If it
4432 // already exists there, recursively merge the results together.
4433 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004434 if (!Existing) {
4435 if (UpdateListener) UpdateListener->NodeUpdated(User);
4436 continue; // Continue on to next user.
4437 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004438
4439 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004440 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004441 // recursive merging of other unrelated nodes down the line.
4442 ReplaceAllUsesWith(User, Existing, UpdateListener);
4443
4444 // User is now dead. Notify a listener if present.
4445 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4446 DeleteNodeNotInCSEMaps(User);
4447 }
4448}
4449
4450/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004451/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004452/// appear in both the From and To list. The Deleted vector is
4453/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004454void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4455 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004456 unsigned Num,
4457 DAGUpdateListener *UpdateListener){
4458 // Handle the simple, trivial case efficiently.
4459 if (Num == 1)
4460 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4461
4462 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4463 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004464 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4465 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004466 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004467
4468 while (!Users.empty()) {
4469 // We know that this user uses some value of From. If it is the right
4470 // value, update it.
4471 SDNode *User = Users.back().first;
4472 unsigned i = Users.back().second;
4473 Users.pop_back();
4474
4475 // Scan for an operand that matches From.
4476 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4477 for (; Op != E; ++Op)
4478 if (*Op == From[i]) break;
4479
4480 // If there are no matches, the user must use some other result of From.
4481 if (Op == E) continue;
4482
4483 // Okay, we know this user needs to be updated. Remove its old self
4484 // from the CSE maps.
4485 RemoveNodeFromCSEMaps(User);
4486
4487 // Update all operands that match "From" in case there are multiple uses.
4488 for (; Op != E; ++Op) {
4489 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004490 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004491 *Op = To[i];
4492 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004493 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004494 }
4495 }
4496
4497 // Now that we have modified User, add it back to the CSE maps. If it
4498 // already exists there, recursively merge the results together.
4499 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4500 if (!Existing) {
4501 if (UpdateListener) UpdateListener->NodeUpdated(User);
4502 continue; // Continue on to next user.
4503 }
4504
4505 // If there was already an existing matching node, use ReplaceAllUsesWith
4506 // to replace the dead one with the existing one. This can cause
4507 // recursive merging of other unrelated nodes down the line.
4508 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004509
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004510 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004511 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004512 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004513 }
4514}
4515
Evan Chenge6f35d82006-08-01 08:20:41 +00004516/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004517/// based on their topological order. It returns the maximum id and a vector
4518/// of the SDNodes* in assigned order by reference.
4519unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004520 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004521 std::vector<SDNode*> Sources;
4522
Evan Chenge6f35d82006-08-01 08:20:41 +00004523 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4524 SDNode *N = I;
4525 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004526 // Temporarily use the Node Id as scratch space for the degree count.
4527 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004528 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004529 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004530 }
4531
Evan Cheng99157a02006-08-07 22:13:29 +00004532 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004533 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004534 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004535 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004536 SDNode *N = Sources.back();
4537 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004538 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004539 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004540 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004541 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004542 unsigned Degree = P->getNodeId();
4543 --Degree;
4544 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004545 if (Degree == 0)
4546 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004547 }
4548 }
4549
Evan Chengc384d6c2006-08-02 22:00:34 +00004550 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004551}
4552
4553
Evan Cheng091cba12006-07-27 06:39:06 +00004554
Jim Laskey58b968b2005-08-17 20:08:02 +00004555//===----------------------------------------------------------------------===//
4556// SDNode Class
4557//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004558
Chris Lattner917d2c92006-07-19 00:00:37 +00004559// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004560void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004561void UnarySDNode::ANCHOR() {}
4562void BinarySDNode::ANCHOR() {}
4563void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004564void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004565void ConstantSDNode::ANCHOR() {}
4566void ConstantFPSDNode::ANCHOR() {}
4567void GlobalAddressSDNode::ANCHOR() {}
4568void FrameIndexSDNode::ANCHOR() {}
4569void JumpTableSDNode::ANCHOR() {}
4570void ConstantPoolSDNode::ANCHOR() {}
4571void BasicBlockSDNode::ANCHOR() {}
4572void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004573void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004574void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004575void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004576void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004577void ExternalSymbolSDNode::ANCHOR() {}
4578void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004579void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004580void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004581void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004582void LoadSDNode::ANCHOR() {}
4583void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004584void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004585
Chris Lattner48b85922007-02-04 02:41:42 +00004586HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004587 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004588}
4589
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004590GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004591 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004592 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004593 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004594 // Thread Local
4595 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4596 // Non Thread Local
4597 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4598 getSDVTList(VT)), Offset(o) {
4599 TheGlobal = const_cast<GlobalValue*>(GA);
4600}
Chris Lattner48b85922007-02-04 02:41:42 +00004601
Dan Gohman1ea58a52008-07-09 22:08:04 +00004602MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004603 const Value *srcValue, int SVO,
4604 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004605 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004606 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004607
4608 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4609 assert(getAlignment() == alignment && "Alignment representation error!");
4610 assert(isVolatile() == vol && "Volatile representation error!");
4611}
4612
Dan Gohman36b5c132008-04-07 19:35:22 +00004613/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004614/// reference performed by this memory reference.
4615MachineMemOperand MemSDNode::getMemOperand() const {
4616 int Flags;
4617 if (isa<LoadSDNode>(this))
4618 Flags = MachineMemOperand::MOLoad;
4619 else if (isa<StoreSDNode>(this))
4620 Flags = MachineMemOperand::MOStore;
4621 else {
4622 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4623 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4624 }
4625
4626 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004627 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4628
Dan Gohman1ea58a52008-07-09 22:08:04 +00004629 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004630 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004631 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004632 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004633 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4634 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004635 else
4636 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4637 Size, getAlignment());
4638}
4639
Jim Laskey583bd472006-10-27 23:46:08 +00004640/// Profile - Gather unique data for the node.
4641///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004642void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004643 AddNodeIDNode(ID, this);
4644}
4645
Chris Lattnera3255112005-11-08 23:30:28 +00004646/// getValueTypeList - Return a pointer to the specified value type.
4647///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004648const MVT *SDNode::getValueTypeList(MVT VT) {
4649 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004650 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004651 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004652 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004653 static MVT VTs[MVT::LAST_VALUETYPE];
4654 VTs[VT.getSimpleVT()] = VT;
4655 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004656 }
Chris Lattnera3255112005-11-08 23:30:28 +00004657}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004658
Chris Lattner5c884562005-01-12 18:37:47 +00004659/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4660/// indicated value. This method ignores uses of other values defined by this
4661/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004662bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004663 assert(Value < getNumValues() && "Bad value!");
4664
Roman Levensteindc1adac2008-04-07 10:06:32 +00004665 // TODO: Only iterate over uses of a given value of the node
4666 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004667 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004668 if (NUses == 0)
4669 return false;
4670 --NUses;
4671 }
Chris Lattner5c884562005-01-12 18:37:47 +00004672 }
4673
4674 // Found exactly the right number of uses?
4675 return NUses == 0;
4676}
4677
4678
Evan Cheng33d55952007-08-02 05:29:38 +00004679/// hasAnyUseOfValue - Return true if there are any use of the indicated
4680/// value. This method ignores uses of other values defined by this operation.
4681bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4682 assert(Value < getNumValues() && "Bad value!");
4683
Dan Gohman1373c1c2008-07-09 22:39:01 +00004684 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004685 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004686 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004687
4688 return false;
4689}
4690
4691
Dan Gohman2a629952008-07-27 18:06:42 +00004692/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004693///
Dan Gohman2a629952008-07-27 18:06:42 +00004694bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004695 bool Seen = false;
4696 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004697 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004698 if (User == this)
4699 Seen = true;
4700 else
4701 return false;
4702 }
4703
4704 return Seen;
4705}
4706
Evan Chenge6e97e62006-11-03 07:31:32 +00004707/// isOperand - Return true if this node is an operand of N.
4708///
Dan Gohman475871a2008-07-27 21:46:04 +00004709bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004710 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4711 if (*this == N->getOperand(i))
4712 return true;
4713 return false;
4714}
4715
Evan Cheng917be682008-03-04 00:41:45 +00004716bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004717 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004718 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004719 return true;
4720 return false;
4721}
Evan Cheng4ee62112006-02-05 06:29:23 +00004722
Chris Lattner572dee72008-01-16 05:49:24 +00004723/// reachesChainWithoutSideEffects - Return true if this operand (which must
4724/// be a chain) reaches the specified operand without crossing any
4725/// side-effecting instructions. In practice, this looks through token
4726/// factors and non-volatile loads. In order to remain efficient, this only
4727/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004728bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004729 unsigned Depth) const {
4730 if (*this == Dest) return true;
4731
4732 // Don't search too deeply, we just want to be able to see through
4733 // TokenFactor's etc.
4734 if (Depth == 0) return false;
4735
4736 // If this is a token factor, all inputs to the TF happen in parallel. If any
4737 // of the operands of the TF reach dest, then we can do the xform.
4738 if (getOpcode() == ISD::TokenFactor) {
4739 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4740 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4741 return true;
4742 return false;
4743 }
4744
4745 // Loads don't have side effects, look through them.
4746 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4747 if (!Ld->isVolatile())
4748 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4749 }
4750 return false;
4751}
4752
4753
Evan Chengc5fc57d2006-11-03 03:05:24 +00004754static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004755 SmallPtrSet<SDNode *, 32> &Visited) {
4756 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004757 return;
4758
4759 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004760 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004761 if (Op == P) {
4762 found = true;
4763 return;
4764 }
4765 findPredecessor(Op, P, found, Visited);
4766 }
4767}
4768
Evan Cheng917be682008-03-04 00:41:45 +00004769/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004770/// is either an operand of N or it can be reached by recursively traversing
4771/// up the operands.
4772/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004773bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004774 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004775 bool found = false;
4776 findPredecessor(N, this, found, Visited);
4777 return found;
4778}
4779
Evan Chengc5484282006-10-04 00:56:09 +00004780uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4781 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004782 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004783}
4784
Reid Spencer577cc322007-04-01 07:32:19 +00004785std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004786 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004787 default:
4788 if (getOpcode() < ISD::BUILTIN_OP_END)
4789 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004790 if (isMachineOpcode()) {
4791 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004792 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004793 if (getMachineOpcode() < TII->getNumOpcodes())
4794 return TII->get(getMachineOpcode()).getName();
4795 return "<<Unknown Machine Node>>";
4796 }
4797 if (G) {
4798 TargetLowering &TLI = G->getTargetLoweringInfo();
4799 const char *Name = TLI.getTargetNodeName(getOpcode());
4800 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004801 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004802 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004803 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004804
Dan Gohmane8be6c62008-07-17 19:10:17 +00004805#ifndef NDEBUG
4806 case ISD::DELETED_NODE:
4807 return "<<Deleted Node!>>";
4808#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004809 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004810 case ISD::MEMBARRIER: return "MemBarrier";
Dale Johannesene00a8a22008-08-28 02:44:49 +00004811 case ISD::ATOMIC_CMP_SWAP_8: return "AtomicCmpSwap8";
4812 case ISD::ATOMIC_SWAP_8: return "AtomicSwap8";
4813 case ISD::ATOMIC_LOAD_ADD_8: return "AtomicLoadAdd8";
4814 case ISD::ATOMIC_LOAD_SUB_8: return "AtomicLoadSub8";
4815 case ISD::ATOMIC_LOAD_AND_8: return "AtomicLoadAnd8";
4816 case ISD::ATOMIC_LOAD_OR_8: return "AtomicLoadOr8";
4817 case ISD::ATOMIC_LOAD_XOR_8: return "AtomicLoadXor8";
4818 case ISD::ATOMIC_LOAD_NAND_8: return "AtomicLoadNand8";
4819 case ISD::ATOMIC_LOAD_MIN_8: return "AtomicLoadMin8";
4820 case ISD::ATOMIC_LOAD_MAX_8: return "AtomicLoadMax8";
4821 case ISD::ATOMIC_LOAD_UMIN_8: return "AtomicLoadUMin8";
4822 case ISD::ATOMIC_LOAD_UMAX_8: return "AtomicLoadUMax8";
4823 case ISD::ATOMIC_CMP_SWAP_16: return "AtomicCmpSwap16";
4824 case ISD::ATOMIC_SWAP_16: return "AtomicSwap16";
4825 case ISD::ATOMIC_LOAD_ADD_16: return "AtomicLoadAdd16";
4826 case ISD::ATOMIC_LOAD_SUB_16: return "AtomicLoadSub16";
4827 case ISD::ATOMIC_LOAD_AND_16: return "AtomicLoadAnd16";
4828 case ISD::ATOMIC_LOAD_OR_16: return "AtomicLoadOr16";
4829 case ISD::ATOMIC_LOAD_XOR_16: return "AtomicLoadXor16";
4830 case ISD::ATOMIC_LOAD_NAND_16: return "AtomicLoadNand16";
4831 case ISD::ATOMIC_LOAD_MIN_16: return "AtomicLoadMin16";
4832 case ISD::ATOMIC_LOAD_MAX_16: return "AtomicLoadMax16";
4833 case ISD::ATOMIC_LOAD_UMIN_16: return "AtomicLoadUMin16";
4834 case ISD::ATOMIC_LOAD_UMAX_16: return "AtomicLoadUMax16";
4835 case ISD::ATOMIC_CMP_SWAP_32: return "AtomicCmpSwap32";
4836 case ISD::ATOMIC_SWAP_32: return "AtomicSwap32";
4837 case ISD::ATOMIC_LOAD_ADD_32: return "AtomicLoadAdd32";
4838 case ISD::ATOMIC_LOAD_SUB_32: return "AtomicLoadSub32";
4839 case ISD::ATOMIC_LOAD_AND_32: return "AtomicLoadAnd32";
4840 case ISD::ATOMIC_LOAD_OR_32: return "AtomicLoadOr32";
4841 case ISD::ATOMIC_LOAD_XOR_32: return "AtomicLoadXor32";
4842 case ISD::ATOMIC_LOAD_NAND_32: return "AtomicLoadNand32";
4843 case ISD::ATOMIC_LOAD_MIN_32: return "AtomicLoadMin32";
4844 case ISD::ATOMIC_LOAD_MAX_32: return "AtomicLoadMax32";
4845 case ISD::ATOMIC_LOAD_UMIN_32: return "AtomicLoadUMin32";
4846 case ISD::ATOMIC_LOAD_UMAX_32: return "AtomicLoadUMax32";
4847 case ISD::ATOMIC_CMP_SWAP_64: return "AtomicCmpSwap64";
4848 case ISD::ATOMIC_SWAP_64: return "AtomicSwap64";
4849 case ISD::ATOMIC_LOAD_ADD_64: return "AtomicLoadAdd64";
4850 case ISD::ATOMIC_LOAD_SUB_64: return "AtomicLoadSub64";
4851 case ISD::ATOMIC_LOAD_AND_64: return "AtomicLoadAnd64";
4852 case ISD::ATOMIC_LOAD_OR_64: return "AtomicLoadOr64";
4853 case ISD::ATOMIC_LOAD_XOR_64: return "AtomicLoadXor64";
4854 case ISD::ATOMIC_LOAD_NAND_64: return "AtomicLoadNand64";
4855 case ISD::ATOMIC_LOAD_MIN_64: return "AtomicLoadMin64";
4856 case ISD::ATOMIC_LOAD_MAX_64: return "AtomicLoadMax64";
4857 case ISD::ATOMIC_LOAD_UMIN_64: return "AtomicLoadUMin64";
4858 case ISD::ATOMIC_LOAD_UMAX_64: return "AtomicLoadUMax64";
Andrew Lenharth95762122005-03-31 21:24:06 +00004859 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004860 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004861 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004862 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004863 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004864 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004865 case ISD::AssertSext: return "AssertSext";
4866 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004867
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004868 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004869 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004870 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004871 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004872
4873 case ISD::Constant: return "Constant";
4874 case ISD::ConstantFP: return "ConstantFP";
4875 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004876 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004877 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004878 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004879 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004880 case ISD::RETURNADDR: return "RETURNADDR";
4881 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004882 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004883 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4884 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004885 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004886 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004887 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004888 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004889 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00004890 return Intrinsic::getName((Intrinsic::ID)IID);
4891 }
4892 case ISD::INTRINSIC_VOID:
4893 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004894 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004895 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004896 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004897
Chris Lattnerb2827b02006-03-19 00:52:58 +00004898 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004899 case ISD::TargetConstant: return "TargetConstant";
4900 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004901 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004902 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004903 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004904 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004905 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004906 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004907
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004908 case ISD::CopyToReg: return "CopyToReg";
4909 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004910 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004911 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004912 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004913 case ISD::DBG_LABEL: return "dbg_label";
4914 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004915 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004916 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004917 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004918 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004919
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004920 // Unary operators
4921 case ISD::FABS: return "fabs";
4922 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004923 case ISD::FSQRT: return "fsqrt";
4924 case ISD::FSIN: return "fsin";
4925 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004926 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004927 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004928 case ISD::FTRUNC: return "ftrunc";
4929 case ISD::FFLOOR: return "ffloor";
4930 case ISD::FCEIL: return "fceil";
4931 case ISD::FRINT: return "frint";
4932 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004933
4934 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004935 case ISD::ADD: return "add";
4936 case ISD::SUB: return "sub";
4937 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004938 case ISD::MULHU: return "mulhu";
4939 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004940 case ISD::SDIV: return "sdiv";
4941 case ISD::UDIV: return "udiv";
4942 case ISD::SREM: return "srem";
4943 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004944 case ISD::SMUL_LOHI: return "smul_lohi";
4945 case ISD::UMUL_LOHI: return "umul_lohi";
4946 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00004947 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004948 case ISD::AND: return "and";
4949 case ISD::OR: return "or";
4950 case ISD::XOR: return "xor";
4951 case ISD::SHL: return "shl";
4952 case ISD::SRA: return "sra";
4953 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004954 case ISD::ROTL: return "rotl";
4955 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004956 case ISD::FADD: return "fadd";
4957 case ISD::FSUB: return "fsub";
4958 case ISD::FMUL: return "fmul";
4959 case ISD::FDIV: return "fdiv";
4960 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004961 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004962 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004963
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004964 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004965 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004966 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004967 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004968 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004969 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004970 case ISD::CONCAT_VECTORS: return "concat_vectors";
4971 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004972 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004973 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004974 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004975 case ISD::ADDC: return "addc";
4976 case ISD::ADDE: return "adde";
4977 case ISD::SUBC: return "subc";
4978 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004979 case ISD::SHL_PARTS: return "shl_parts";
4980 case ISD::SRA_PARTS: return "sra_parts";
4981 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004982
4983 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4984 case ISD::INSERT_SUBREG: return "insert_subreg";
4985
Chris Lattner7f644642005-04-28 21:44:03 +00004986 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004987 case ISD::SIGN_EXTEND: return "sign_extend";
4988 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004989 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004990 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004991 case ISD::TRUNCATE: return "truncate";
4992 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004993 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004994 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004995 case ISD::FP_EXTEND: return "fp_extend";
4996
4997 case ISD::SINT_TO_FP: return "sint_to_fp";
4998 case ISD::UINT_TO_FP: return "uint_to_fp";
4999 case ISD::FP_TO_SINT: return "fp_to_sint";
5000 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005001 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005002
5003 // Control flow instructions
5004 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005005 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005006 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005007 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005008 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005009 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005010 case ISD::CALLSEQ_START: return "callseq_start";
5011 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005012
5013 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005014 case ISD::LOAD: return "load";
5015 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005016 case ISD::VAARG: return "vaarg";
5017 case ISD::VACOPY: return "vacopy";
5018 case ISD::VAEND: return "vaend";
5019 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005020 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005021 case ISD::EXTRACT_ELEMENT: return "extract_element";
5022 case ISD::BUILD_PAIR: return "build_pair";
5023 case ISD::STACKSAVE: return "stacksave";
5024 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005025 case ISD::TRAP: return "trap";
5026
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005027 // Bit manipulation
5028 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005029 case ISD::CTPOP: return "ctpop";
5030 case ISD::CTTZ: return "cttz";
5031 case ISD::CTLZ: return "ctlz";
5032
Chris Lattner36ce6912005-11-29 06:21:05 +00005033 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005034 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005035 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005036
Duncan Sands36397f52007-07-27 12:58:54 +00005037 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005038 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005039
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005040 case ISD::CONDCODE:
5041 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005042 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005043 case ISD::SETOEQ: return "setoeq";
5044 case ISD::SETOGT: return "setogt";
5045 case ISD::SETOGE: return "setoge";
5046 case ISD::SETOLT: return "setolt";
5047 case ISD::SETOLE: return "setole";
5048 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005049
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005050 case ISD::SETO: return "seto";
5051 case ISD::SETUO: return "setuo";
5052 case ISD::SETUEQ: return "setue";
5053 case ISD::SETUGT: return "setugt";
5054 case ISD::SETUGE: return "setuge";
5055 case ISD::SETULT: return "setult";
5056 case ISD::SETULE: return "setule";
5057 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005058
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005059 case ISD::SETEQ: return "seteq";
5060 case ISD::SETGT: return "setgt";
5061 case ISD::SETGE: return "setge";
5062 case ISD::SETLT: return "setlt";
5063 case ISD::SETLE: return "setle";
5064 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005065 }
5066 }
5067}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005068
Evan Cheng144d8f02006-11-09 17:55:04 +00005069const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005070 switch (AM) {
5071 default:
5072 return "";
5073 case ISD::PRE_INC:
5074 return "<pre-inc>";
5075 case ISD::PRE_DEC:
5076 return "<pre-dec>";
5077 case ISD::POST_INC:
5078 return "<post-inc>";
5079 case ISD::POST_DEC:
5080 return "<post-dec>";
5081 }
5082}
5083
Duncan Sands276dcbd2008-03-21 09:14:45 +00005084std::string ISD::ArgFlagsTy::getArgFlagsString() {
5085 std::string S = "< ";
5086
5087 if (isZExt())
5088 S += "zext ";
5089 if (isSExt())
5090 S += "sext ";
5091 if (isInReg())
5092 S += "inreg ";
5093 if (isSRet())
5094 S += "sret ";
5095 if (isByVal())
5096 S += "byval ";
5097 if (isNest())
5098 S += "nest ";
5099 if (getByValAlign())
5100 S += "byval-align:" + utostr(getByValAlign()) + " ";
5101 if (getOrigAlign())
5102 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5103 if (getByValSize())
5104 S += "byval-size:" + utostr(getByValSize()) + " ";
5105 return S + ">";
5106}
5107
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005108void SDNode::dump() const { dump(0); }
5109void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005110 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005111 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005112}
5113
5114void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5115 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005116
5117 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005118 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005119 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005120 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005121 else
Chris Lattner944fac72008-08-23 22:23:09 +00005122 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005123 }
Chris Lattner944fac72008-08-23 22:23:09 +00005124 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005125
Chris Lattner944fac72008-08-23 22:23:09 +00005126 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005128 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005129 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005130 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005131 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005132 }
5133
Evan Chengce254432007-12-11 02:08:35 +00005134 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005135 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005136 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005137 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005138 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005139 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005140 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005141 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005142 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005143 }
Chris Lattner944fac72008-08-23 22:23:09 +00005144 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005145 }
5146
Chris Lattnerc3aae252005-01-07 07:46:32 +00005147 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005148 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005149 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005150 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005151 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005152 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005153 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005154 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005155 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005156 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005157 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005158 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005159 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005160 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005161 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005162 OS << '<';
5163 WriteAsOperand(OS, GADN->getGlobal());
5164 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005165 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005166 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005167 else
Chris Lattner944fac72008-08-23 22:23:09 +00005168 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005169 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005170 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005171 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005172 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005173 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005174 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005175 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005176 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005177 else
Chris Lattner944fac72008-08-23 22:23:09 +00005178 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005179 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005180 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005181 else
Chris Lattner944fac72008-08-23 22:23:09 +00005182 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005183 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005184 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005185 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5186 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005187 OS << LBB->getName() << " ";
5188 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005189 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005190 if (G && R->getReg() &&
5191 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005192 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005193 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005194 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005195 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005196 } else if (const ExternalSymbolSDNode *ES =
5197 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005198 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005199 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5200 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005201 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005202 else
Chris Lattner944fac72008-08-23 22:23:09 +00005203 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005204 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5205 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005206 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005207 else
Chris Lattner944fac72008-08-23 22:23:09 +00005208 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005209 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005210 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005211 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005212 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005213 }
5214 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005215 const Value *SrcValue = LD->getSrcValue();
5216 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005217 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005218 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005219 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005220 else
Chris Lattner944fac72008-08-23 22:23:09 +00005221 OS << "null";
5222 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005223
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005224 bool doExt = true;
5225 switch (LD->getExtensionType()) {
5226 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005227 case ISD::EXTLOAD: OS << " <anyext "; break;
5228 case ISD::SEXTLOAD: OS << " <sext "; break;
5229 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005230 }
5231 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005232 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005233
Evan Cheng144d8f02006-11-09 17:55:04 +00005234 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005235 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005236 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005237 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005238 OS << " <volatile>";
5239 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005240 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005241 const Value *SrcValue = ST->getSrcValue();
5242 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005243 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005244 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005245 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005246 else
Chris Lattner944fac72008-08-23 22:23:09 +00005247 OS << "null";
5248 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005249
5250 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005251 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005252
5253 const char *AM = getIndexedModeName(ST->getAddressingMode());
5254 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005255 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005256 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005257 OS << " <volatile>";
5258 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005259 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5260 const Value *SrcValue = AT->getSrcValue();
5261 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005262 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005263 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005264 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005265 else
Chris Lattner944fac72008-08-23 22:23:09 +00005266 OS << "null";
5267 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005268 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005269 OS << " <volatile>";
5270 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005271 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005272}
5273
Chris Lattnerde202b32005-11-09 23:47:37 +00005274static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005275 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005276 if (N->getOperand(i).getNode()->hasOneUse())
5277 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005278 else
Bill Wendling832171c2006-12-07 20:04:42 +00005279 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005280 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005281
Chris Lattnerea946cd2005-01-09 20:38:33 +00005282
Bill Wendling832171c2006-12-07 20:04:42 +00005283 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005284 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005285}
5286
Chris Lattnerc3aae252005-01-07 07:46:32 +00005287void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005288 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005289
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005290 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5291 I != E; ++I) {
5292 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005293 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005294 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005295 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005296
Gabor Greifba36cb52008-08-28 21:40:38 +00005297 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005298
Bill Wendling832171c2006-12-07 20:04:42 +00005299 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005300}
5301
Evan Chengd6594ae2006-09-12 21:00:35 +00005302const Type *ConstantPoolSDNode::getType() const {
5303 if (isMachineConstantPoolEntry())
5304 return Val.MachineCPVal->getType();
5305 return Val.ConstVal->getType();
5306}