blob: 793f5c999720bdf748aa82427bef6b79abb9deb9 [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 }
Dan Gohman5eb0cec2008-09-15 19:46:03 +0000426 case ISD::CALL: {
427 const CallSDNode *Call = cast<CallSDNode>(N);
428 ID.AddInteger(Call->getCallingConv());
429 ID.AddInteger(Call->isVarArg());
430 break;
431 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000433 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 ID.AddInteger(LD->getAddressingMode());
435 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000436 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000437 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000438 break;
439 }
440 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000441 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000442 ID.AddInteger(ST->getAddressingMode());
443 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000444 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000445 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000446 break;
447 }
Dale Johannesene00a8a22008-08-28 02:44:49 +0000448 case ISD::ATOMIC_CMP_SWAP_8:
449 case ISD::ATOMIC_SWAP_8:
450 case ISD::ATOMIC_LOAD_ADD_8:
451 case ISD::ATOMIC_LOAD_SUB_8:
452 case ISD::ATOMIC_LOAD_AND_8:
453 case ISD::ATOMIC_LOAD_OR_8:
454 case ISD::ATOMIC_LOAD_XOR_8:
455 case ISD::ATOMIC_LOAD_NAND_8:
456 case ISD::ATOMIC_LOAD_MIN_8:
457 case ISD::ATOMIC_LOAD_MAX_8:
458 case ISD::ATOMIC_LOAD_UMIN_8:
459 case ISD::ATOMIC_LOAD_UMAX_8:
460 case ISD::ATOMIC_CMP_SWAP_16:
461 case ISD::ATOMIC_SWAP_16:
462 case ISD::ATOMIC_LOAD_ADD_16:
463 case ISD::ATOMIC_LOAD_SUB_16:
464 case ISD::ATOMIC_LOAD_AND_16:
465 case ISD::ATOMIC_LOAD_OR_16:
466 case ISD::ATOMIC_LOAD_XOR_16:
467 case ISD::ATOMIC_LOAD_NAND_16:
468 case ISD::ATOMIC_LOAD_MIN_16:
469 case ISD::ATOMIC_LOAD_MAX_16:
470 case ISD::ATOMIC_LOAD_UMIN_16:
471 case ISD::ATOMIC_LOAD_UMAX_16:
472 case ISD::ATOMIC_CMP_SWAP_32:
473 case ISD::ATOMIC_SWAP_32:
474 case ISD::ATOMIC_LOAD_ADD_32:
475 case ISD::ATOMIC_LOAD_SUB_32:
476 case ISD::ATOMIC_LOAD_AND_32:
477 case ISD::ATOMIC_LOAD_OR_32:
478 case ISD::ATOMIC_LOAD_XOR_32:
479 case ISD::ATOMIC_LOAD_NAND_32:
480 case ISD::ATOMIC_LOAD_MIN_32:
481 case ISD::ATOMIC_LOAD_MAX_32:
482 case ISD::ATOMIC_LOAD_UMIN_32:
483 case ISD::ATOMIC_LOAD_UMAX_32:
484 case ISD::ATOMIC_CMP_SWAP_64:
485 case ISD::ATOMIC_SWAP_64:
486 case ISD::ATOMIC_LOAD_ADD_64:
487 case ISD::ATOMIC_LOAD_SUB_64:
488 case ISD::ATOMIC_LOAD_AND_64:
489 case ISD::ATOMIC_LOAD_OR_64:
490 case ISD::ATOMIC_LOAD_XOR_64:
491 case ISD::ATOMIC_LOAD_NAND_64:
492 case ISD::ATOMIC_LOAD_MIN_64:
493 case ISD::ATOMIC_LOAD_MAX_64:
494 case ISD::ATOMIC_LOAD_UMIN_64:
495 case ISD::ATOMIC_LOAD_UMAX_64: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000496 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
497 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000498 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000499 }
Mon P Wang28873102008-06-25 08:15:39 +0000500 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000501}
502
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000503/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
504/// the CSE map that carries both alignment and volatility information.
505///
506static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
507 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
508}
509
Jim Laskey583bd472006-10-27 23:46:08 +0000510//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000511// SelectionDAG Class
512//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000513
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000514/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000515/// SelectionDAG.
516void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000517 // Create a dummy node (which is not added to allnodes), that adds a reference
518 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000519 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000520
Chris Lattner190a4182006-08-04 17:45:20 +0000521 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000522
Chris Lattner190a4182006-08-04 17:45:20 +0000523 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000524 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000525 if (I->use_empty())
526 DeadNodes.push_back(I);
527
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000528 RemoveDeadNodes(DeadNodes);
529
530 // If the root changed (e.g. it was a dead load, update the root).
531 setRoot(Dummy.getValue());
532}
533
534/// RemoveDeadNodes - This method deletes the unreachable nodes in the
535/// given list, and any nodes that become unreachable as a result.
536void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
537 DAGUpdateListener *UpdateListener) {
538
Chris Lattner190a4182006-08-04 17:45:20 +0000539 // Process the worklist, deleting the nodes and adding their uses to the
540 // worklist.
541 while (!DeadNodes.empty()) {
542 SDNode *N = DeadNodes.back();
543 DeadNodes.pop_back();
544
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000545 if (UpdateListener)
546 UpdateListener->NodeDeleted(N, 0);
547
Chris Lattner190a4182006-08-04 17:45:20 +0000548 // Take the node out of the appropriate CSE map.
549 RemoveNodeFromCSEMaps(N);
550
551 // Next, brutally remove the operand list. This is safe to do, as there are
552 // no cycles in the graph.
553 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000554 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000555 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000556
557 // Now that we removed this operand, see if there are no uses of it left.
558 if (Operand->use_empty())
559 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000560 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000561 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000562 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000563 }
Chris Lattner190a4182006-08-04 17:45:20 +0000564 N->OperandList = 0;
565 N->NumOperands = 0;
566
567 // Finally, remove N itself.
Dan Gohman11467282008-08-26 01:44:34 +0000568 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerf469cb62005-11-08 18:52:27 +0000569 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000570}
571
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000572void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000573 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000574 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000575}
576
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000577void SelectionDAG::DeleteNode(SDNode *N) {
578 assert(N->use_empty() && "Cannot delete a node that is not dead!");
579
580 // First take this out of the appropriate CSE map.
581 RemoveNodeFromCSEMaps(N);
582
Chris Lattner1e111c72005-09-07 05:37:01 +0000583 // Finally, remove uses due to operands of this node, remove from the
584 // AllNodes list, and delete the node.
585 DeleteNodeNotInCSEMaps(N);
586}
587
588void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
589
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000590 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000591 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000592 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf350b272008-08-23 02:25:05 +0000593 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000594 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000595
Dan Gohman11467282008-08-26 01:44:34 +0000596 assert(N != AllNodes.begin());
597 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000598}
599
Chris Lattner149c58c2005-08-16 18:17:10 +0000600/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
601/// correspond to it. This is useful when we're about to delete or repurpose
602/// the node. We don't want future request for structurally identical nodes
603/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000604bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000605 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000606 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000607 case ISD::EntryToken:
608 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000609 return false;
610 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000611 case ISD::CONDCODE:
612 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
613 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000614 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000615 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
616 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000617 case ISD::ExternalSymbol:
618 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000619 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000620 case ISD::TargetExternalSymbol:
621 Erased =
622 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000623 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000624 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000625 MVT VT = cast<VTSDNode>(N)->getVT();
626 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000627 Erased = ExtendedValueTypeNodes.erase(VT);
628 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
630 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000631 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000632 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000633 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000634 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000635 // Remove it from the CSE Map.
636 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000637 break;
638 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000639#ifndef NDEBUG
640 // Verify that the node was actually in one of the CSE maps, unless it has a
641 // flag result (which cannot be CSE'd) or is one of the special cases that are
642 // not subject to CSE.
643 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Dan Gohman095cc292008-09-13 01:54:27 +0000644 !N->isMachineOpcode() &&
Evan Cheng71e86852008-07-08 20:06:39 +0000645 N->getOpcode() != ISD::DBG_LABEL &&
646 N->getOpcode() != ISD::DBG_STOPPOINT &&
647 N->getOpcode() != ISD::EH_LABEL &&
648 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000649 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000650 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000651 assert(0 && "Node is not in map!");
652 }
653#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000654 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000655}
656
Chris Lattner8b8749f2005-08-17 19:00:20 +0000657/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
658/// has been taken out and modified in some way. If the specified node already
659/// exists in the CSE maps, do not modify the maps, but return the existing node
660/// instead. If it doesn't exist, add it and return null.
661///
662SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
663 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000664
665 if (N->getValueType(0) == MVT::Flag)
666 return 0; // Never CSE anything that produces a flag.
667
668 switch (N->getOpcode()) {
669 default: break;
670 case ISD::HANDLENODE:
671 case ISD::DBG_LABEL:
672 case ISD::DBG_STOPPOINT:
673 case ISD::EH_LABEL:
674 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000675 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000676 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000677
Chris Lattner9f8cc692005-12-19 22:21:21 +0000678 // Check that remaining values produced are not flags.
679 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
680 if (N->getValueType(i) == MVT::Flag)
681 return 0; // Never CSE anything that produces a flag.
682
Chris Lattnera5682852006-08-07 23:03:03 +0000683 SDNode *New = CSEMap.GetOrInsertNode(N);
684 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000685 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000686}
687
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000688/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
689/// were replaced with those specified. If this node is never memoized,
690/// return null, otherwise return a pointer to the slot it would take. If a
691/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000692SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000693 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000694 if (N->getValueType(0) == MVT::Flag)
695 return 0; // Never CSE anything that produces a flag.
696
697 switch (N->getOpcode()) {
698 default: break;
699 case ISD::HANDLENODE:
700 case ISD::DBG_LABEL:
701 case ISD::DBG_STOPPOINT:
702 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000703 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000704 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000705
706 // Check that remaining values produced are not flags.
707 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
708 if (N->getValueType(i) == MVT::Flag)
709 return 0; // Never CSE anything that produces a flag.
710
Dan Gohman475871a2008-07-27 21:46:04 +0000711 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000712 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000713 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000714 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000715}
716
717/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
718/// were replaced with those specified. If this node is never memoized,
719/// return null, otherwise return a pointer to the slot it would take. If a
720/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000721SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000722 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000723 void *&InsertPos) {
724 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000725
726 // Check that remaining values produced are not flags.
727 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
728 if (N->getValueType(i) == MVT::Flag)
729 return 0; // Never CSE anything that produces a flag.
730
Dan Gohman475871a2008-07-27 21:46:04 +0000731 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000732 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000733 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000734 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
735}
736
737
738/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
739/// were replaced with those specified. If this node is never memoized,
740/// return null, otherwise return a pointer to the slot it would take. If a
741/// node already exists with these operands, the slot will be non-null.
742SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000743 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000744 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000745 if (N->getValueType(0) == MVT::Flag)
746 return 0; // Never CSE anything that produces a flag.
747
748 switch (N->getOpcode()) {
749 default: break;
750 case ISD::HANDLENODE:
751 case ISD::DBG_LABEL:
752 case ISD::DBG_STOPPOINT:
753 case ISD::EH_LABEL:
754 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000755 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000756 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000757
758 // Check that remaining values produced are not flags.
759 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
760 if (N->getValueType(i) == MVT::Flag)
761 return 0; // Never CSE anything that produces a flag.
762
Jim Laskey583bd472006-10-27 23:46:08 +0000763 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000764 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000765
Evan Cheng9629aba2006-10-11 01:47:58 +0000766 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
767 ID.AddInteger(LD->getAddressingMode());
768 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000769 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000770 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000771 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
772 ID.AddInteger(ST->getAddressingMode());
773 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000774 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000775 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000776 }
Jim Laskey583bd472006-10-27 23:46:08 +0000777
Chris Lattnera5682852006-08-07 23:03:03 +0000778 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000779}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000780
Duncan Sandsd038e042008-07-21 10:20:31 +0000781/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
782void SelectionDAG::VerifyNode(SDNode *N) {
783 switch (N->getOpcode()) {
784 default:
785 break;
786 case ISD::BUILD_VECTOR: {
787 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
788 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
789 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
790 "Wrong number of BUILD_VECTOR operands!");
791 MVT EltVT = N->getValueType(0).getVectorElementType();
792 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000793 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000794 "Wrong BUILD_VECTOR operand type!");
795 break;
796 }
797 }
798}
799
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000800/// getMVTAlignment - Compute the default alignment value for the
801/// given type.
802///
803unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
804 const Type *Ty = VT == MVT::iPTR ?
805 PointerType::get(Type::Int8Ty, 0) :
806 VT.getTypeForMVT();
807
808 return TLI.getTargetData()->getABITypeAlignment(Ty);
809}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000810
Dan Gohman7c3234c2008-08-27 23:52:12 +0000811SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
812 : TLI(tli), FLI(fli),
813 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000814 Root(getEntryNode()) {
815 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000816}
817
Dan Gohman7c3234c2008-08-27 23:52:12 +0000818void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi) {
819 MF = &mf;
820 MMI = mmi;
821}
822
Chris Lattner78ec3112003-08-11 14:57:33 +0000823SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000824 allnodes_clear();
825}
826
827void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000828 assert(&*AllNodes.begin() == &EntryNode);
829 AllNodes.remove(AllNodes.begin());
Chris Lattnerde202b32005-11-09 23:47:37 +0000830 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000831 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000832 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000833 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000834 delete [] N->OperandList;
Dan Gohman11467282008-08-26 01:44:34 +0000835 NodeAllocator.Deallocate(N);
Chris Lattner65113b22005-11-08 22:07:03 +0000836 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000837}
838
Dan Gohman7c3234c2008-08-27 23:52:12 +0000839void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000840 allnodes_clear();
841 OperandAllocator.Reset();
842 CSEMap.clear();
843
844 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000845 ExternalSymbols.clear();
846 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000847 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
848 static_cast<CondCodeSDNode*>(0));
849 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
850 static_cast<SDNode*>(0));
851
852 EntryNode.Uses = 0;
853 AllNodes.push_back(&EntryNode);
854 Root = getEntryNode();
855}
856
Dan Gohman475871a2008-07-27 21:46:04 +0000857SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000858 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000859 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000860 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000861 return getNode(ISD::AND, Op.getValueType(), Op,
862 getConstant(Imm, Op.getValueType()));
863}
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000866 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000868}
869
Dan Gohman475871a2008-07-27 21:46:04 +0000870SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000871 return getConstant(*ConstantInt::get(Val), VT, isT);
872}
873
874SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000876
Evan Cheng0ff39b32008-06-30 07:31:25 +0000877 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000878 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000879 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000880
881 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000882 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000883 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000884 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000885 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000886 SDNode *N = NULL;
887 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000888 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000889 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000890 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000891 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000892 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000893 CSEMap.InsertNode(N, IP);
894 AllNodes.push_back(N);
895 }
896
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000898 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000900 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000901 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
902 }
903 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000904}
905
Dan Gohman475871a2008-07-27 21:46:04 +0000906SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000907 return getConstant(Val, TLI.getPointerTy(), isTarget);
908}
909
910
Dan Gohman475871a2008-07-27 21:46:04 +0000911SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000912 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
913}
914
915SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000917
Duncan Sands83ec4b62008-06-06 12:08:01 +0000918 MVT EltVT =
919 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920
Chris Lattnerd8658612005-02-17 20:17:32 +0000921 // Do the map lookup using the actual bit pattern for the floating point
922 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
923 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000924 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000925 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000926 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000927 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000928 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000929 SDNode *N = NULL;
930 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000931 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000932 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000933 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000934 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000935 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000936 CSEMap.InsertNode(N, IP);
937 AllNodes.push_back(N);
938 }
939
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000943 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000944 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
945 }
946 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947}
948
Dan Gohman475871a2008-07-27 21:46:04 +0000949SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000950 MVT EltVT =
951 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000952 if (EltVT==MVT::f32)
953 return getConstantFP(APFloat((float)Val), VT, isTarget);
954 else
955 return getConstantFP(APFloat(Val), VT, isTarget);
956}
957
Dan Gohman475871a2008-07-27 21:46:04 +0000958SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
959 MVT VT, int Offset,
960 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000961 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000962
963 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
964 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000965 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000966 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000967 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000968 }
969
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000970 if (GVar && GVar->isThreadLocal())
971 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
972 else
973 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000974
Jim Laskey583bd472006-10-27 23:46:08 +0000975 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000976 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000977 ID.AddPointer(GV);
978 ID.AddInteger(Offset);
979 void *IP = 0;
980 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000981 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000982 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000983 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000984 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000986 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987}
988
Dan Gohman475871a2008-07-27 21:46:04 +0000989SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000990 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000991 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000992 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000993 ID.AddInteger(FI);
994 void *IP = 0;
995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000996 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000997 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000998 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000999 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001000 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001001 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001002}
1003
Dan Gohman475871a2008-07-27 21:46:04 +00001004SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001005 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001006 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001007 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001008 ID.AddInteger(JTI);
1009 void *IP = 0;
1010 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001011 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001012 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001013 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001014 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001015 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001016 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001017}
1018
Dan Gohman475871a2008-07-27 21:46:04 +00001019SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1020 unsigned Alignment, int Offset,
1021 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001022 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001023 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001024 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001025 ID.AddInteger(Alignment);
1026 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001027 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001028 void *IP = 0;
1029 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001030 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001031 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001032 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001033 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001034 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001035 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001036}
1037
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038
Dan Gohman475871a2008-07-27 21:46:04 +00001039SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1040 unsigned Alignment, int Offset,
1041 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001042 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001043 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001044 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001045 ID.AddInteger(Alignment);
1046 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001047 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001048 void *IP = 0;
1049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001050 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001051 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001052 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001053 CSEMap.InsertNode(N, IP);
1054 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001055 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001056}
1057
1058
Dan Gohman475871a2008-07-27 21:46:04 +00001059SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001060 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001061 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001062 ID.AddPointer(MBB);
1063 void *IP = 0;
1064 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001065 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001066 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001067 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001068 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001070 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071}
1072
Dan Gohman475871a2008-07-27 21:46:04 +00001073SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001074 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001075 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001076 ID.AddInteger(Flags.getRawBits());
1077 void *IP = 0;
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001079 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001080 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001081 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001082 CSEMap.InsertNode(N, IP);
1083 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001084 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001085}
1086
Dan Gohman475871a2008-07-27 21:46:04 +00001087SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001088 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1089 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001090
Duncan Sands83ec4b62008-06-06 12:08:01 +00001091 SDNode *&N = VT.isExtended() ?
1092 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001093
Dan Gohman475871a2008-07-27 21:46:04 +00001094 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001095 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001096 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001097 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001098 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001099}
1100
Bill Wendling056292f2008-09-16 21:48:12 +00001101SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1102 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001103 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001104 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1105 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001106 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001107 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001108}
1109
Bill Wendling056292f2008-09-16 21:48:12 +00001110SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1111 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001112 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001113 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1114 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001116 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001117}
1118
Dan Gohman475871a2008-07-27 21:46:04 +00001119SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001120 if ((unsigned)Cond >= CondCodeNodes.size())
1121 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001122
Chris Lattner079a27a2005-08-09 20:40:02 +00001123 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001124 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001125 new (N) CondCodeSDNode(Cond);
1126 CondCodeNodes[Cond] = N;
1127 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001128 }
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001130}
1131
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001133 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001134 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001135 ID.AddInteger(RegNo);
1136 void *IP = 0;
1137 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001138 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001139 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001140 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001141 CSEMap.InsertNode(N, IP);
1142 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001143 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001144}
1145
Dan Gohman475871a2008-07-27 21:46:04 +00001146SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001147 unsigned Line, unsigned Col,
1148 const CompileUnitDesc *CU) {
1149 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001150 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001151 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001152 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001153}
1154
Dan Gohman475871a2008-07-27 21:46:04 +00001155SDValue SelectionDAG::getLabel(unsigned Opcode,
1156 SDValue Root,
1157 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001158 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001160 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1161 ID.AddInteger(LabelID);
1162 void *IP = 0;
1163 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001164 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001165 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001166 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001167 CSEMap.InsertNode(N, IP);
1168 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001169 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001170}
1171
Dan Gohman475871a2008-07-27 21:46:04 +00001172SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001173 assert((!V || isa<PointerType>(V->getType())) &&
1174 "SrcValue is not a pointer?");
1175
Jim Laskey583bd472006-10-27 23:46:08 +00001176 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001177 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001178 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001179
Chris Lattner61b09412006-08-11 21:01:22 +00001180 void *IP = 0;
1181 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001182 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001183
Dan Gohmanfed90b62008-07-28 21:51:04 +00001184 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001185 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001186 CSEMap.InsertNode(N, IP);
1187 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001188 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001189}
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001192 const Value *v = MO.getValue();
1193 assert((!v || isa<PointerType>(v->getType())) &&
1194 "SrcValue is not a pointer?");
1195
1196 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001197 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001198 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001199
1200 void *IP = 0;
1201 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001202 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001203
Dan Gohmanfed90b62008-07-28 21:51:04 +00001204 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001205 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001206 CSEMap.InsertNode(N, IP);
1207 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001208 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001209}
1210
Chris Lattner37ce9df2007-10-15 17:47:20 +00001211/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1212/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001213SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001214 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001215 unsigned ByteSize = VT.getSizeInBits()/8;
1216 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001217 unsigned StackAlign =
1218 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1219
Chris Lattner37ce9df2007-10-15 17:47:20 +00001220 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1221 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1222}
1223
Dan Gohman475871a2008-07-27 21:46:04 +00001224SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1225 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 // These setcc operations always fold.
1227 switch (Cond) {
1228 default: break;
1229 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001230 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001232 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001233
1234 case ISD::SETOEQ:
1235 case ISD::SETOGT:
1236 case ISD::SETOGE:
1237 case ISD::SETOLT:
1238 case ISD::SETOLE:
1239 case ISD::SETONE:
1240 case ISD::SETO:
1241 case ISD::SETUO:
1242 case ISD::SETUEQ:
1243 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001245 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001247
Gabor Greifba36cb52008-08-28 21:40:38 +00001248 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001249 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001250 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001251 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001252
Chris Lattnerc3aae252005-01-07 07:46:32 +00001253 switch (Cond) {
1254 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001255 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1256 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001257 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1258 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1259 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1260 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1261 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1262 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1263 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1264 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001266 }
Chris Lattner67255a12005-04-07 18:14:58 +00001267 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1269 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001270 // No compile time operations on this type yet.
1271 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001272 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001273
1274 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001275 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001276 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001277 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1278 return getNode(ISD::UNDEF, VT);
1279 // fall through
1280 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1281 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1282 return getNode(ISD::UNDEF, VT);
1283 // fall through
1284 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001285 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001286 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1287 return getNode(ISD::UNDEF, VT);
1288 // fall through
1289 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1290 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1291 return getNode(ISD::UNDEF, VT);
1292 // fall through
1293 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1294 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1295 return getNode(ISD::UNDEF, VT);
1296 // fall through
1297 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001298 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001299 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1300 return getNode(ISD::UNDEF, VT);
1301 // fall through
1302 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001303 R==APFloat::cmpEqual, VT);
1304 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1305 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1306 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1307 R==APFloat::cmpEqual, VT);
1308 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1309 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1310 R==APFloat::cmpLessThan, VT);
1311 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1312 R==APFloat::cmpUnordered, VT);
1313 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1314 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315 }
1316 } else {
1317 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001318 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001319 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001320 }
1321
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001322 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001323 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001324}
1325
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001326/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1327/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001328bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001329 unsigned BitWidth = Op.getValueSizeInBits();
1330 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1331}
1332
Dan Gohmanea859be2007-06-22 14:59:07 +00001333/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1334/// this predicate to simplify operations downstream. Mask is known to be zero
1335/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001336bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001337 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001338 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001339 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1340 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1341 return (KnownZero & Mask) == Mask;
1342}
1343
1344/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1345/// known to be either zero or one and return them in the KnownZero/KnownOne
1346/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1347/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001348void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001349 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001350 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001351 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001352 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001353 "Mask size mismatches value type size!");
1354
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001355 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001356 if (Depth == 6 || Mask == 0)
1357 return; // Limit search depth.
1358
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001359 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001360
1361 switch (Op.getOpcode()) {
1362 case ISD::Constant:
1363 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001364 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001365 KnownZero = ~KnownOne & Mask;
1366 return;
1367 case ISD::AND:
1368 // If either the LHS or the RHS are Zero, the result is zero.
1369 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001370 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1371 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001372 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1373 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1374
1375 // Output known-1 bits are only known if set in both the LHS & RHS.
1376 KnownOne &= KnownOne2;
1377 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1378 KnownZero |= KnownZero2;
1379 return;
1380 case ISD::OR:
1381 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001382 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1383 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001384 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1385 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1386
1387 // Output known-0 bits are only known if clear in both the LHS & RHS.
1388 KnownZero &= KnownZero2;
1389 // Output known-1 are known to be set if set in either the LHS | RHS.
1390 KnownOne |= KnownOne2;
1391 return;
1392 case ISD::XOR: {
1393 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1394 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1395 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1396 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1397
1398 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001399 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001400 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1401 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1402 KnownZero = KnownZeroOut;
1403 return;
1404 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001405 case ISD::MUL: {
1406 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1407 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1408 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1410 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1411
1412 // If low bits are zero in either operand, output low known-0 bits.
1413 // Also compute a conserative estimate for high known-0 bits.
1414 // More trickiness is possible, but this is sufficient for the
1415 // interesting case of alignment computation.
1416 KnownOne.clear();
1417 unsigned TrailZ = KnownZero.countTrailingOnes() +
1418 KnownZero2.countTrailingOnes();
1419 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001420 KnownZero2.countLeadingOnes(),
1421 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001422
1423 TrailZ = std::min(TrailZ, BitWidth);
1424 LeadZ = std::min(LeadZ, BitWidth);
1425 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1426 APInt::getHighBitsSet(BitWidth, LeadZ);
1427 KnownZero &= Mask;
1428 return;
1429 }
1430 case ISD::UDIV: {
1431 // For the purposes of computing leading zeros we can conservatively
1432 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001433 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001434 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1435 ComputeMaskedBits(Op.getOperand(0),
1436 AllOnes, KnownZero2, KnownOne2, Depth+1);
1437 unsigned LeadZ = KnownZero2.countLeadingOnes();
1438
1439 KnownOne2.clear();
1440 KnownZero2.clear();
1441 ComputeMaskedBits(Op.getOperand(1),
1442 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001443 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1444 if (RHSUnknownLeadingOnes != BitWidth)
1445 LeadZ = std::min(BitWidth,
1446 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001447
1448 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1449 return;
1450 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001451 case ISD::SELECT:
1452 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1453 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1454 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1455 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1456
1457 // Only known if known in both the LHS and RHS.
1458 KnownOne &= KnownOne2;
1459 KnownZero &= KnownZero2;
1460 return;
1461 case ISD::SELECT_CC:
1462 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1463 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1464 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1465 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1466
1467 // Only known if known in both the LHS and RHS.
1468 KnownOne &= KnownOne2;
1469 KnownZero &= KnownZero2;
1470 return;
1471 case ISD::SETCC:
1472 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001473 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1474 BitWidth > 1)
1475 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001476 return;
1477 case ISD::SHL:
1478 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1479 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001480 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001481
1482 // If the shift count is an invalid immediate, don't do anything.
1483 if (ShAmt >= BitWidth)
1484 return;
1485
1486 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001487 KnownZero, KnownOne, Depth+1);
1488 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001489 KnownZero <<= ShAmt;
1490 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001491 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001492 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001493 }
1494 return;
1495 case ISD::SRL:
1496 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1497 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001498 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001499
Dan Gohmand4cf9922008-02-26 18:50:50 +00001500 // If the shift count is an invalid immediate, don't do anything.
1501 if (ShAmt >= BitWidth)
1502 return;
1503
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001505 KnownZero, KnownOne, Depth+1);
1506 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001507 KnownZero = KnownZero.lshr(ShAmt);
1508 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001509
Dan Gohman72d2fd52008-02-13 22:43:25 +00001510 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001511 KnownZero |= HighBits; // High bits known zero.
1512 }
1513 return;
1514 case ISD::SRA:
1515 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001516 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001517
Dan Gohmand4cf9922008-02-26 18:50:50 +00001518 // If the shift count is an invalid immediate, don't do anything.
1519 if (ShAmt >= BitWidth)
1520 return;
1521
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001522 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001523 // If any of the demanded bits are produced by the sign extension, we also
1524 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001525 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1526 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001527 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001528
1529 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1530 Depth+1);
1531 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001532 KnownZero = KnownZero.lshr(ShAmt);
1533 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001534
1535 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001536 APInt SignBit = APInt::getSignBit(BitWidth);
1537 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001538
Dan Gohmanca93a432008-02-20 16:30:17 +00001539 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001541 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001542 KnownOne |= HighBits; // New bits are known one.
1543 }
1544 }
1545 return;
1546 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001547 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1548 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001549
1550 // Sign extension. Compute the demanded bits in the result that are not
1551 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001552 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001553
Dan Gohman977a76f2008-02-13 22:28:48 +00001554 APInt InSignBit = APInt::getSignBit(EBits);
1555 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001556
1557 // If the sign extended bits are demanded, we know that the sign
1558 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001559 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001560 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001561 InputDemandedBits |= InSignBit;
1562
1563 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1564 KnownZero, KnownOne, Depth+1);
1565 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1566
1567 // If the sign bit of the input is known set or clear, then we know the
1568 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001569 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 KnownZero |= NewBits;
1571 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001572 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 KnownOne |= NewBits;
1574 KnownZero &= ~NewBits;
1575 } else { // Input sign bit unknown
1576 KnownZero &= ~NewBits;
1577 KnownOne &= ~NewBits;
1578 }
1579 return;
1580 }
1581 case ISD::CTTZ:
1582 case ISD::CTLZ:
1583 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001584 unsigned LowBits = Log2_32(BitWidth)+1;
1585 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001586 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001587 return;
1588 }
1589 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001590 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 MVT VT = LD->getMemoryVT();
1593 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001594 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 }
1596 return;
1597 }
1598 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001599 MVT InVT = Op.getOperand(0).getValueType();
1600 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001601 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1602 APInt InMask = Mask;
1603 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001604 KnownZero.trunc(InBits);
1605 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001606 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001607 KnownZero.zext(BitWidth);
1608 KnownOne.zext(BitWidth);
1609 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001610 return;
1611 }
1612 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001613 MVT InVT = Op.getOperand(0).getValueType();
1614 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001615 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001616 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1617 APInt InMask = Mask;
1618 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001619
1620 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001621 // bit is demanded. Temporarily set this bit in the mask for our callee.
1622 if (NewBits.getBoolValue())
1623 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001624
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001625 KnownZero.trunc(InBits);
1626 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001627 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1628
1629 // Note if the sign bit is known to be zero or one.
1630 bool SignBitKnownZero = KnownZero.isNegative();
1631 bool SignBitKnownOne = KnownOne.isNegative();
1632 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1633 "Sign bit can't be known to be both zero and one!");
1634
1635 // If the sign bit wasn't actually demanded by our caller, we don't
1636 // want it set in the KnownZero and KnownOne result values. Reset the
1637 // mask and reapply it to the result values.
1638 InMask = Mask;
1639 InMask.trunc(InBits);
1640 KnownZero &= InMask;
1641 KnownOne &= InMask;
1642
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001643 KnownZero.zext(BitWidth);
1644 KnownOne.zext(BitWidth);
1645
Dan Gohman977a76f2008-02-13 22:28:48 +00001646 // If the sign bit is known zero or one, the top bits match.
1647 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001648 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001649 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001650 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001651 return;
1652 }
1653 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001654 MVT InVT = Op.getOperand(0).getValueType();
1655 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001656 APInt InMask = Mask;
1657 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001658 KnownZero.trunc(InBits);
1659 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001660 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001661 KnownZero.zext(BitWidth);
1662 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001663 return;
1664 }
1665 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001666 MVT InVT = Op.getOperand(0).getValueType();
1667 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001668 APInt InMask = Mask;
1669 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001670 KnownZero.zext(InBits);
1671 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001672 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001673 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001674 KnownZero.trunc(BitWidth);
1675 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001676 break;
1677 }
1678 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001679 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1680 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001681 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1682 KnownOne, Depth+1);
1683 KnownZero |= (~InMask) & Mask;
1684 return;
1685 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001686 case ISD::FGETSIGN:
1687 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001688 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001689 return;
1690
Dan Gohman23e8b712008-04-28 17:02:21 +00001691 case ISD::SUB: {
1692 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1693 // We know that the top bits of C-X are clear if X contains less bits
1694 // than C (i.e. no wrap-around can happen). For example, 20-X is
1695 // positive if we can prove that X is >= 0 and < 16.
1696 if (CLHS->getAPIntValue().isNonNegative()) {
1697 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1698 // NLZ can't be BitWidth with no sign bit
1699 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1700 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1701 Depth+1);
1702
1703 // If all of the MaskV bits are known to be zero, then we know the
1704 // output top bits are zero, because we now know that the output is
1705 // from [0-C].
1706 if ((KnownZero2 & MaskV) == MaskV) {
1707 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1708 // Top bits known zero.
1709 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1710 }
1711 }
1712 }
1713 }
1714 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001716 // Output known-0 bits are known if clear or set in both the low clear bits
1717 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1718 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001719 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1720 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1721 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1722 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1723
1724 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1725 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1726 KnownZeroOut = std::min(KnownZeroOut,
1727 KnownZero2.countTrailingOnes());
1728
1729 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001730 return;
1731 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001732 case ISD::SREM:
1733 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001734 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001735 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001736 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001737 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1738 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001739
Dan Gohmana60832b2008-08-13 23:12:35 +00001740 // If the sign bit of the first operand is zero, the sign bit of
1741 // the result is zero. If the first operand has no one bits below
1742 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001743 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1744 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001745
Dan Gohman23e8b712008-04-28 17:02:21 +00001746 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001747
1748 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001749 }
1750 }
1751 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001752 case ISD::UREM: {
1753 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001754 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001755 if (RA.isPowerOf2()) {
1756 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001757 APInt Mask2 = LowBits & Mask;
1758 KnownZero |= ~LowBits & Mask;
1759 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1760 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1761 break;
1762 }
1763 }
1764
1765 // Since the result is less than or equal to either operand, any leading
1766 // zero bits in either operand must also exist in the result.
1767 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1768 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1769 Depth+1);
1770 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1771 Depth+1);
1772
1773 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1774 KnownZero2.countLeadingOnes());
1775 KnownOne.clear();
1776 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1777 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 }
1779 default:
1780 // Allow the target to implement this method for its nodes.
1781 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1782 case ISD::INTRINSIC_WO_CHAIN:
1783 case ISD::INTRINSIC_W_CHAIN:
1784 case ISD::INTRINSIC_VOID:
1785 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1786 }
1787 return;
1788 }
1789}
1790
1791/// ComputeNumSignBits - Return the number of times the sign bit of the
1792/// register is replicated into the other bits. We know that at least 1 bit
1793/// is always equal to the sign bit (itself), but other cases can give us
1794/// information. For example, immediately after an "SRA X, 2", we know that
1795/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001796unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001797 MVT VT = Op.getValueType();
1798 assert(VT.isInteger() && "Invalid VT!");
1799 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001800 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001801 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001802
1803 if (Depth == 6)
1804 return 1; // Limit search depth.
1805
1806 switch (Op.getOpcode()) {
1807 default: break;
1808 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001809 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001810 return VTBits-Tmp+1;
1811 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001812 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 return VTBits-Tmp;
1814
1815 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001816 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1817 // If negative, return # leading ones.
1818 if (Val.isNegative())
1819 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001820
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001821 // Return # leading zeros.
1822 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001823 }
1824
1825 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1828
1829 case ISD::SIGN_EXTEND_INREG:
1830 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001831 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 Tmp = VTBits-Tmp+1;
1833
1834 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1835 return std::max(Tmp, Tmp2);
1836
1837 case ISD::SRA:
1838 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1839 // SRA X, C -> adds C sign bits.
1840 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001841 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001842 if (Tmp > VTBits) Tmp = VTBits;
1843 }
1844 return Tmp;
1845 case ISD::SHL:
1846 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1847 // shl destroys sign bits.
1848 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001849 if (C->getZExtValue() >= VTBits || // Bad shift.
1850 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1851 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 }
1853 break;
1854 case ISD::AND:
1855 case ISD::OR:
1856 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001857 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001859 if (Tmp != 1) {
1860 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1861 FirstAnswer = std::min(Tmp, Tmp2);
1862 // We computed what we know about the sign bits as our first
1863 // answer. Now proceed to the generic code that uses
1864 // ComputeMaskedBits, and pick whichever answer is better.
1865 }
1866 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001867
1868 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001869 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001871 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001872 return std::min(Tmp, Tmp2);
1873
1874 case ISD::SETCC:
1875 // If setcc returns 0/-1, all bits are sign bits.
1876 if (TLI.getSetCCResultContents() ==
1877 TargetLowering::ZeroOrNegativeOneSetCCResult)
1878 return VTBits;
1879 break;
1880 case ISD::ROTL:
1881 case ISD::ROTR:
1882 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001883 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001884
1885 // Handle rotate right by N like a rotate left by 32-N.
1886 if (Op.getOpcode() == ISD::ROTR)
1887 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1888
1889 // If we aren't rotating out all of the known-in sign bits, return the
1890 // number that are left. This handles rotl(sext(x), 1) for example.
1891 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1892 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1893 }
1894 break;
1895 case ISD::ADD:
1896 // Add can have at most one carry bit. Thus we know that the output
1897 // is, at worst, one more bit than the inputs.
1898 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1899 if (Tmp == 1) return 1; // Early out.
1900
1901 // Special case decrementing a value (ADD X, -1):
1902 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1903 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001904 APInt KnownZero, KnownOne;
1905 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001906 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1907
1908 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1909 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001910 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 return VTBits;
1912
1913 // If we are subtracting one from a positive number, there is no carry
1914 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001915 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001916 return Tmp;
1917 }
1918
1919 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1920 if (Tmp2 == 1) return 1;
1921 return std::min(Tmp, Tmp2)-1;
1922 break;
1923
1924 case ISD::SUB:
1925 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1926 if (Tmp2 == 1) return 1;
1927
1928 // Handle NEG.
1929 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001930 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001931 APInt KnownZero, KnownOne;
1932 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1934 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1935 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001936 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001937 return VTBits;
1938
1939 // If the input is known to be positive (the sign bit is known clear),
1940 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001941 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001942 return Tmp2;
1943
1944 // Otherwise, we treat this like a SUB.
1945 }
1946
1947 // Sub can have at most one carry bit. Thus we know that the output
1948 // is, at worst, one more bit than the inputs.
1949 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1950 if (Tmp == 1) return 1; // Early out.
1951 return std::min(Tmp, Tmp2)-1;
1952 break;
1953 case ISD::TRUNCATE:
1954 // FIXME: it's tricky to do anything useful for this, but it is an important
1955 // case for targets like X86.
1956 break;
1957 }
1958
1959 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1960 if (Op.getOpcode() == ISD::LOAD) {
1961 LoadSDNode *LD = cast<LoadSDNode>(Op);
1962 unsigned ExtType = LD->getExtensionType();
1963 switch (ExtType) {
1964 default: break;
1965 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001966 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001967 return VTBits-Tmp+1;
1968 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001969 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001970 return VTBits-Tmp;
1971 }
1972 }
1973
1974 // Allow the target to implement this method for its nodes.
1975 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1976 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1977 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1978 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1979 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001980 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001981 }
1982
1983 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1984 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001985 APInt KnownZero, KnownOne;
1986 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1988
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001989 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001990 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001991 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001992 Mask = KnownOne;
1993 } else {
1994 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001995 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001996 }
1997
1998 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1999 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002000 Mask = ~Mask;
2001 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002002 // Return # leading zeros. We use 'min' here in case Val was zero before
2003 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002004 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002005}
2006
Chris Lattner51dabfb2006-10-14 00:41:01 +00002007
Dan Gohman475871a2008-07-27 21:46:04 +00002008bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002009 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2010 if (!GA) return false;
2011 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2012 if (!GV) return false;
2013 MachineModuleInfo *MMI = getMachineModuleInfo();
2014 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
2015}
2016
2017
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002018/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2019/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002020SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002021 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002022 SDValue PermMask = N->getOperand(2);
2023 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002024 if (Idx.getOpcode() == ISD::UNDEF)
2025 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002026 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002027 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002029 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002030
2031 if (V.getOpcode() == ISD::BIT_CONVERT) {
2032 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002033 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002034 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002035 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002036 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002037 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002038 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002039 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002040 return V.getOperand(Index);
2041 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002042 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002043 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002044}
2045
2046
Chris Lattnerc3aae252005-01-07 07:46:32 +00002047/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002048///
Dan Gohman475871a2008-07-27 21:46:04 +00002049SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002050 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002051 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002052 void *IP = 0;
2053 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002054 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002055 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002056 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002057 CSEMap.InsertNode(N, IP);
2058
2059 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002060#ifndef NDEBUG
2061 VerifyNode(N);
2062#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002063 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002064}
2065
Dan Gohman475871a2008-07-27 21:46:04 +00002066SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002067 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002069 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002070 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002071 switch (Opcode) {
2072 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002073 case ISD::SIGN_EXTEND:
2074 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002075 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002076 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002077 case ISD::TRUNCATE:
2078 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002079 case ISD::UINT_TO_FP:
2080 case ISD::SINT_TO_FP: {
2081 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002082 // No compile time operations on this type.
2083 if (VT==MVT::ppcf128)
2084 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002085 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2086 (void)apf.convertFromAPInt(Val,
2087 Opcode==ISD::SINT_TO_FP,
2088 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002089 return getConstantFP(apf, VT);
2090 }
Chris Lattner94683772005-12-23 05:30:37 +00002091 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002092 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002093 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002094 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002095 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002096 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002097 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002098 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002099 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002100 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002101 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002102 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002103 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002104 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002105 }
2106 }
2107
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002108 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002109 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002110 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002111 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002112 switch (Opcode) {
2113 case ISD::FNEG:
2114 V.changeSign();
2115 return getConstantFP(V, VT);
2116 case ISD::FABS:
2117 V.clearSign();
2118 return getConstantFP(V, VT);
2119 case ISD::FP_ROUND:
2120 case ISD::FP_EXTEND:
2121 // This can return overflow, underflow, or inexact; we don't care.
2122 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002123 (void)V.convert(*MVTToAPFloatSemantics(VT),
2124 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002125 return getConstantFP(V, VT);
2126 case ISD::FP_TO_SINT:
2127 case ISD::FP_TO_UINT: {
2128 integerPart x;
2129 assert(integerPartWidth >= 64);
2130 // FIXME need to be more flexible about rounding mode.
2131 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2132 Opcode==ISD::FP_TO_SINT,
2133 APFloat::rmTowardZero);
2134 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2135 break;
2136 return getConstant(x, VT);
2137 }
2138 case ISD::BIT_CONVERT:
2139 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2140 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2141 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2142 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002143 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002144 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002145 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002146 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002147
Gabor Greifba36cb52008-08-28 21:40:38 +00002148 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002150 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002151 case ISD::CONCAT_VECTORS:
2152 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002153 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002154 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 assert(VT.isFloatingPoint() &&
2156 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002157 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002158 if (Operand.getOpcode() == ISD::UNDEF)
2159 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002160 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002161 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002162 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002163 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002164 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002165 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002166 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002167 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002168 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002169 break;
2170 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002171 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002172 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002173 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002174 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002175 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002176 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002177 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002178 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002179 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002180 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002181 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002182 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002183 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002184 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002185 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2186 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002187 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002188 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002189 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002190 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002191 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002192 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002193 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002194 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002197 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2198 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002199 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002200 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2201 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2202 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2203 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002204 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002205 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002207 break;
Chris Lattner35481892005-12-23 00:16:34 +00002208 case ISD::BIT_CONVERT:
2209 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002210 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002211 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002212 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002213 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2214 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002215 if (OpOpcode == ISD::UNDEF)
2216 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002217 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002218 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002219 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2220 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002221 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002222 if (OpOpcode == ISD::UNDEF)
2223 return getNode(ISD::UNDEF, VT);
2224 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2225 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2226 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2227 Operand.getConstantOperandVal(1) == 0 &&
2228 Operand.getOperand(0).getValueType() == VT)
2229 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002230 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002231 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002232 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002233 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2234 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002235 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002237 break;
2238 case ISD::FABS:
2239 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002240 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002241 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002242 }
2243
Chris Lattner43247a12005-08-25 19:12:10 +00002244 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002245 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002246 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002247 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002249 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002250 void *IP = 0;
2251 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002252 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002253 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002254 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002255 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002256 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002257 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002258 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002259 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002260
Chris Lattnerc3aae252005-01-07 07:46:32 +00002261 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002262#ifndef NDEBUG
2263 VerifyNode(N);
2264#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002265 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002266}
2267
Dan Gohman475871a2008-07-27 21:46:04 +00002268SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2269 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002270 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2271 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002272 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002273 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002274 case ISD::TokenFactor:
2275 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2276 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002277 // Fold trivial token factors.
2278 if (N1.getOpcode() == ISD::EntryToken) return N2;
2279 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002280 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002281 case ISD::CONCAT_VECTORS:
2282 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2283 // one big BUILD_VECTOR.
2284 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2285 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002286 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2287 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002288 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2289 }
2290 break;
Chris Lattner76365122005-01-16 02:23:22 +00002291 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002292 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002293 N1.getValueType() == VT && "Binary operator types must match!");
2294 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2295 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002296 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002297 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002298 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2299 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002300 break;
Chris Lattner76365122005-01-16 02:23:22 +00002301 case ISD::OR:
2302 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002303 case ISD::ADD:
2304 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002305 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002306 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002307 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2308 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002309 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002310 return N1;
2311 break;
Chris Lattner76365122005-01-16 02:23:22 +00002312 case ISD::UDIV:
2313 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002314 case ISD::MULHU:
2315 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002317 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002318 case ISD::MUL:
2319 case ISD::SDIV:
2320 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002321 case ISD::FADD:
2322 case ISD::FSUB:
2323 case ISD::FMUL:
2324 case ISD::FDIV:
2325 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002326 assert(N1.getValueType() == N2.getValueType() &&
2327 N1.getValueType() == VT && "Binary operator types must match!");
2328 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002329 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2330 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002331 N1.getValueType().isFloatingPoint() &&
2332 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002333 "Invalid FCOPYSIGN!");
2334 break;
Chris Lattner76365122005-01-16 02:23:22 +00002335 case ISD::SHL:
2336 case ISD::SRA:
2337 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002338 case ISD::ROTL:
2339 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002340 assert(VT == N1.getValueType() &&
2341 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002342 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002343 "Shifts only work on integers");
2344
2345 // Always fold shifts of i1 values so the code generator doesn't need to
2346 // handle them. Since we know the size of the shift has to be less than the
2347 // size of the value, the shift/rotate count is guaranteed to be zero.
2348 if (VT == MVT::i1)
2349 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002350 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002351 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002352 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002353 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002354 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002355 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002356 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002358 break;
2359 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002360 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002361 assert(VT.isFloatingPoint() &&
2362 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002363 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002364 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002365 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002366 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002367 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002369 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002370 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002372 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002373 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002374 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002375 break;
2376 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002377 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002378 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002379 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002380 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002381 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002382 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002383 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002384
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002385 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002386 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002387 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002388 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002389 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002390 return getConstant(Val, VT);
2391 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002392 break;
2393 }
2394 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002395 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2396 if (N1.getOpcode() == ISD::UNDEF)
2397 return getNode(ISD::UNDEF, VT);
2398
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002399 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2400 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002401 if (N2C &&
2402 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002403 N1.getNumOperands() > 0) {
2404 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002406 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002407 N1.getOperand(N2C->getZExtValue() / Factor),
2408 getConstant(N2C->getZExtValue() % Factor,
2409 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002410 }
2411
2412 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2413 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002414 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002415 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002416
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002417 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2418 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002419 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2420 if (N1.getOperand(2) == N2)
2421 return N1.getOperand(1);
2422 else
2423 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2424 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002425 break;
2426 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002427 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002428 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2429 (N1.getValueType().isInteger() == VT.isInteger()) &&
2430 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002431
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002432 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2433 // 64-bit integers into 32-bit parts. Instead of building the extract of
2434 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2435 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002436 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002437
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002438 // EXTRACT_ELEMENT of a constant int is also very common.
2439 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002440 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002441 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002442 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2443 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002444 }
2445 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002446 case ISD::EXTRACT_SUBVECTOR:
2447 if (N1.getValueType() == VT) // Trivial extraction.
2448 return N1;
2449 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002450 }
2451
2452 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002453 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002454 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002455 switch (Opcode) {
2456 case ISD::ADD: return getConstant(C1 + C2, VT);
2457 case ISD::SUB: return getConstant(C1 - C2, VT);
2458 case ISD::MUL: return getConstant(C1 * C2, VT);
2459 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002460 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002461 break;
2462 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002463 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002464 break;
2465 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002466 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002467 break;
2468 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002469 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002470 break;
2471 case ISD::AND : return getConstant(C1 & C2, VT);
2472 case ISD::OR : return getConstant(C1 | C2, VT);
2473 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002474 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002475 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2476 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2477 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2478 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002479 default: break;
2480 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002481 } else { // Cannonicalize constant to RHS if commutative
2482 if (isCommutativeBinOp(Opcode)) {
2483 std::swap(N1C, N2C);
2484 std::swap(N1, N2);
2485 }
2486 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002487 }
2488
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002489 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002490 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2491 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002492 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002493 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2494 // Cannonicalize constant to RHS if commutative
2495 std::swap(N1CFP, N2CFP);
2496 std::swap(N1, N2);
2497 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002498 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2499 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002501 case ISD::FADD:
2502 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002503 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002504 return getConstantFP(V1, VT);
2505 break;
2506 case ISD::FSUB:
2507 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2508 if (s!=APFloat::opInvalidOp)
2509 return getConstantFP(V1, VT);
2510 break;
2511 case ISD::FMUL:
2512 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2513 if (s!=APFloat::opInvalidOp)
2514 return getConstantFP(V1, VT);
2515 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002516 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002517 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2518 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2519 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002520 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002521 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002522 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2523 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2524 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002525 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002526 case ISD::FCOPYSIGN:
2527 V1.copySign(V2);
2528 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002529 default: break;
2530 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002531 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002532 }
Chris Lattner62b57722006-04-20 05:39:12 +00002533
2534 // Canonicalize an UNDEF to the RHS, even over a constant.
2535 if (N1.getOpcode() == ISD::UNDEF) {
2536 if (isCommutativeBinOp(Opcode)) {
2537 std::swap(N1, N2);
2538 } else {
2539 switch (Opcode) {
2540 case ISD::FP_ROUND_INREG:
2541 case ISD::SIGN_EXTEND_INREG:
2542 case ISD::SUB:
2543 case ISD::FSUB:
2544 case ISD::FDIV:
2545 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002546 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002547 return N1; // fold op(undef, arg2) -> undef
2548 case ISD::UDIV:
2549 case ISD::SDIV:
2550 case ISD::UREM:
2551 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002552 case ISD::SRL:
2553 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002555 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2556 // For vectors, we can't easily build an all zero vector, just return
2557 // the LHS.
2558 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002559 }
2560 }
2561 }
2562
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002563 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002564 if (N2.getOpcode() == ISD::UNDEF) {
2565 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002566 case ISD::XOR:
2567 if (N1.getOpcode() == ISD::UNDEF)
2568 // Handle undef ^ undef -> 0 special case. This is a common
2569 // idiom (misuse).
2570 return getConstant(0, VT);
2571 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002572 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002573 case ISD::ADDC:
2574 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002575 case ISD::SUB:
2576 case ISD::FADD:
2577 case ISD::FSUB:
2578 case ISD::FMUL:
2579 case ISD::FDIV:
2580 case ISD::FREM:
2581 case ISD::UDIV:
2582 case ISD::SDIV:
2583 case ISD::UREM:
2584 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002585 return N2; // fold op(arg1, undef) -> undef
2586 case ISD::MUL:
2587 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002588 case ISD::SRL:
2589 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002590 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002591 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2592 // For vectors, we can't easily build an all zero vector, just return
2593 // the LHS.
2594 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002595 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 if (!VT.isVector())
2597 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002598 // For vectors, we can't easily build an all one vector, just return
2599 // the LHS.
2600 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002601 case ISD::SRA:
2602 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002603 }
2604 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002605
Chris Lattner27e9b412005-05-11 18:57:39 +00002606 // Memoize this node if possible.
2607 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002608 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002609 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002610 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002611 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002612 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002613 void *IP = 0;
2614 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002615 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002616 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002617 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002618 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002619 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002620 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002621 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002622 }
2623
Chris Lattnerc3aae252005-01-07 07:46:32 +00002624 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002625#ifndef NDEBUG
2626 VerifyNode(N);
2627#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002628 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002629}
2630
Dan Gohman475871a2008-07-27 21:46:04 +00002631SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2632 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002633 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2635 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002636 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002637 case ISD::CONCAT_VECTORS:
2638 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2639 // one big BUILD_VECTOR.
2640 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2641 N2.getOpcode() == ISD::BUILD_VECTOR &&
2642 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002643 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2644 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2645 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002646 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2647 }
2648 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002649 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002650 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002652 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002653 break;
2654 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002655 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002656 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002657 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002658 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002659 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002660 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002661 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002662
2663 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002664 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002665 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002666 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002667 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002668 return getNode(ISD::BR, MVT::Other, N1, N3);
2669 else
2670 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002671 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002672 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002673 case ISD::VECTOR_SHUFFLE:
2674 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002675 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002676 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002678 "Illegal VECTOR_SHUFFLE node!");
2679 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002680 case ISD::BIT_CONVERT:
2681 // Fold bit_convert nodes from a type to themselves.
2682 if (N1.getValueType() == VT)
2683 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002684 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002685 }
2686
Chris Lattner43247a12005-08-25 19:12:10 +00002687 // Memoize node if it doesn't produce a flag.
2688 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002689 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002690 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002691 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002692 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002693 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002694 void *IP = 0;
2695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002696 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002697 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002698 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002699 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002700 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002701 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002702 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002703 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002704 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002705#ifndef NDEBUG
2706 VerifyNode(N);
2707#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002708 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +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) {
2714 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002715 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002716}
2717
Dan Gohman475871a2008-07-27 21:46:04 +00002718SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2719 SDValue N1, SDValue N2, SDValue N3,
2720 SDValue N4, SDValue N5) {
2721 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002722 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002723}
2724
Dan Gohman707e0182008-04-12 04:36:06 +00002725/// getMemsetValue - Vectorized representation of the memset value
2726/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002727static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002728 unsigned NumBits = VT.isVector() ?
2729 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002731 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002732 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002733 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002734 Val = (Val << Shift) | Val;
2735 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002736 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002737 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002738 return DAG.getConstant(Val, VT);
2739 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002740 }
Evan Chengf0df0312008-05-15 08:39:06 +00002741
2742 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2743 unsigned Shift = 8;
2744 for (unsigned i = NumBits; i > 8; i >>= 1) {
2745 Value = DAG.getNode(ISD::OR, VT,
2746 DAG.getNode(ISD::SHL, VT, Value,
2747 DAG.getConstant(Shift, MVT::i8)), Value);
2748 Shift <<= 1;
2749 }
2750
2751 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002752}
2753
Dan Gohman707e0182008-04-12 04:36:06 +00002754/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2755/// used when a memcpy is turned into a memset when the source is a constant
2756/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002757static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002758 const TargetLowering &TLI,
2759 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002760 // Handle vector with all elements zero.
2761 if (Str.empty()) {
2762 if (VT.isInteger())
2763 return DAG.getConstant(0, VT);
2764 unsigned NumElts = VT.getVectorNumElements();
2765 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2766 return DAG.getNode(ISD::BIT_CONVERT, VT,
2767 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2768 }
2769
Duncan Sands83ec4b62008-06-06 12:08:01 +00002770 assert(!VT.isVector() && "Can't handle vector type here!");
2771 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002772 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002773 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002774 if (TLI.isLittleEndian())
2775 Offset = Offset + MSB - 1;
2776 for (unsigned i = 0; i != MSB; ++i) {
2777 Val = (Val << 8) | (unsigned char)Str[Offset];
2778 Offset += TLI.isLittleEndian() ? -1 : 1;
2779 }
2780 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002781}
2782
Dan Gohman707e0182008-04-12 04:36:06 +00002783/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002784///
Dan Gohman475871a2008-07-27 21:46:04 +00002785static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002786 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002788 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2789}
2790
Evan Chengf0df0312008-05-15 08:39:06 +00002791/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2792///
Dan Gohman475871a2008-07-27 21:46:04 +00002793static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002794 unsigned SrcDelta = 0;
2795 GlobalAddressSDNode *G = NULL;
2796 if (Src.getOpcode() == ISD::GlobalAddress)
2797 G = cast<GlobalAddressSDNode>(Src);
2798 else if (Src.getOpcode() == ISD::ADD &&
2799 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2800 Src.getOperand(1).getOpcode() == ISD::Constant) {
2801 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002802 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002803 }
2804 if (!G)
2805 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002806
Evan Chengf0df0312008-05-15 08:39:06 +00002807 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002808 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2809 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002810
Evan Chengf0df0312008-05-15 08:39:06 +00002811 return false;
2812}
Dan Gohman707e0182008-04-12 04:36:06 +00002813
Evan Chengf0df0312008-05-15 08:39:06 +00002814/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2815/// to replace the memset / memcpy is below the threshold. It also returns the
2816/// types of the sequence of memory ops to perform memset / memcpy.
2817static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002818bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002819 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002820 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002821 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002822 SelectionDAG &DAG,
2823 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002824 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002825 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002826 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002828 if (VT != MVT::iAny) {
2829 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002830 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002831 // If source is a string constant, this will require an unaligned load.
2832 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2833 if (Dst.getOpcode() != ISD::FrameIndex) {
2834 // Can't change destination alignment. It requires a unaligned store.
2835 if (AllowUnalign)
2836 VT = MVT::iAny;
2837 } else {
2838 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2839 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2840 if (MFI->isFixedObjectIndex(FI)) {
2841 // Can't change destination alignment. It requires a unaligned store.
2842 if (AllowUnalign)
2843 VT = MVT::iAny;
2844 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002845 // Give the stack frame object a larger alignment if needed.
2846 if (MFI->getObjectAlignment(FI) < NewAlign)
2847 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002848 Align = NewAlign;
2849 }
2850 }
2851 }
2852 }
2853
2854 if (VT == MVT::iAny) {
2855 if (AllowUnalign) {
2856 VT = MVT::i64;
2857 } else {
2858 switch (Align & 7) {
2859 case 0: VT = MVT::i64; break;
2860 case 4: VT = MVT::i32; break;
2861 case 2: VT = MVT::i16; break;
2862 default: VT = MVT::i8; break;
2863 }
2864 }
2865
Duncan Sands83ec4b62008-06-06 12:08:01 +00002866 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002867 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2869 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002870
Duncan Sands8e4eb092008-06-08 20:54:56 +00002871 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002872 VT = LVT;
2873 }
Dan Gohman707e0182008-04-12 04:36:06 +00002874
2875 unsigned NumMemOps = 0;
2876 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002877 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002878 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002879 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002880 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002881 VT = MVT::i64;
2882 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002883 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2884 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002885 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002886 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002887 VTSize >>= 1;
2888 }
Dan Gohman707e0182008-04-12 04:36:06 +00002889 }
Dan Gohman707e0182008-04-12 04:36:06 +00002890
2891 if (++NumMemOps > Limit)
2892 return false;
2893 MemOps.push_back(VT);
2894 Size -= VTSize;
2895 }
2896
2897 return true;
2898}
2899
Dan Gohman475871a2008-07-27 21:46:04 +00002900static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2901 SDValue Chain, SDValue Dst,
2902 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002903 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002904 const Value *DstSV, uint64_t DstSVOff,
2905 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002906 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2907
Dan Gohman21323f32008-05-29 19:42:22 +00002908 // Expand memcpy to a series of load and store ops if the size operand falls
2909 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002910 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002911 uint64_t Limit = -1;
2912 if (!AlwaysInline)
2913 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002914 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002915 std::string Str;
2916 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002917 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002918 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002919 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002920
Dan Gohman707e0182008-04-12 04:36:06 +00002921
Evan Cheng0ff39b32008-06-30 07:31:25 +00002922 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002923 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002924 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002925 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002926 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002927 MVT VT = MemOps[i];
2928 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002929 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002930
Evan Cheng0ff39b32008-06-30 07:31:25 +00002931 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002932 // It's unlikely a store of a vector immediate can be done in a single
2933 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002934 // We also handle store a vector with all zero's.
2935 // FIXME: Handle other cases where store of vector immediate is done in
2936 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002937 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002938 Store = DAG.getStore(Chain, Value,
2939 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002940 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002941 } else {
2942 Value = DAG.getLoad(VT, Chain,
2943 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002944 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002945 Store = DAG.getStore(Chain, Value,
2946 getMemBasePlusOffset(Dst, DstOff, DAG),
2947 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002948 }
2949 OutChains.push_back(Store);
2950 SrcOff += VTSize;
2951 DstOff += VTSize;
2952 }
2953
2954 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2955 &OutChains[0], OutChains.size());
2956}
2957
Dan Gohman475871a2008-07-27 21:46:04 +00002958static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2959 SDValue Chain, SDValue Dst,
2960 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002961 unsigned Align, bool AlwaysInline,
2962 const Value *DstSV, uint64_t DstSVOff,
2963 const Value *SrcSV, uint64_t SrcSVOff){
2964 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2965
2966 // Expand memmove to a series of load and store ops if the size operand falls
2967 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002968 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002969 uint64_t Limit = -1;
2970 if (!AlwaysInline)
2971 Limit = TLI.getMaxStoresPerMemmove();
2972 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002973 std::string Str;
2974 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002975 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002976 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002977 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002978
Dan Gohman21323f32008-05-29 19:42:22 +00002979 uint64_t SrcOff = 0, DstOff = 0;
2980
Dan Gohman475871a2008-07-27 21:46:04 +00002981 SmallVector<SDValue, 8> LoadValues;
2982 SmallVector<SDValue, 8> LoadChains;
2983 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002984 unsigned NumMemOps = MemOps.size();
2985 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 MVT VT = MemOps[i];
2987 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002988 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002989
2990 Value = DAG.getLoad(VT, Chain,
2991 getMemBasePlusOffset(Src, SrcOff, DAG),
2992 SrcSV, SrcSVOff + SrcOff, false, Align);
2993 LoadValues.push_back(Value);
2994 LoadChains.push_back(Value.getValue(1));
2995 SrcOff += VTSize;
2996 }
2997 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2998 &LoadChains[0], LoadChains.size());
2999 OutChains.clear();
3000 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003001 MVT VT = MemOps[i];
3002 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003003 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003004
3005 Store = DAG.getStore(Chain, LoadValues[i],
3006 getMemBasePlusOffset(Dst, DstOff, DAG),
3007 DstSV, DstSVOff + DstOff, false, DstAlign);
3008 OutChains.push_back(Store);
3009 DstOff += VTSize;
3010 }
3011
3012 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3013 &OutChains[0], OutChains.size());
3014}
3015
Dan Gohman475871a2008-07-27 21:46:04 +00003016static SDValue getMemsetStores(SelectionDAG &DAG,
3017 SDValue Chain, SDValue Dst,
3018 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003019 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003020 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003021 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3022
3023 // Expand memset to a series of load/store ops if the size operand
3024 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003025 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003026 std::string Str;
3027 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003028 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003029 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003030 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003031
Dan Gohman475871a2008-07-27 21:46:04 +00003032 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003033 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003034
3035 unsigned NumMemOps = MemOps.size();
3036 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003037 MVT VT = MemOps[i];
3038 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003039 SDValue Value = getMemsetValue(Src, VT, DAG);
3040 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00003041 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003042 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003043 OutChains.push_back(Store);
3044 DstOff += VTSize;
3045 }
3046
3047 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3048 &OutChains[0], OutChains.size());
3049}
3050
Dan Gohman475871a2008-07-27 21:46:04 +00003051SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3052 SDValue Src, SDValue Size,
3053 unsigned Align, bool AlwaysInline,
3054 const Value *DstSV, uint64_t DstSVOff,
3055 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003056
3057 // Check to see if we should lower the memcpy to loads and stores first.
3058 // For cases within the target-specified limits, this is the best choice.
3059 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3060 if (ConstantSize) {
3061 // Memcpy with size zero? Just return the original chain.
3062 if (ConstantSize->isNullValue())
3063 return Chain;
3064
Dan Gohman475871a2008-07-27 21:46:04 +00003065 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003066 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3067 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003068 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003070 return Result;
3071 }
3072
3073 // Then check to see if we should lower the memcpy with target-specific
3074 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003075 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003076 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3077 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003078 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003079 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003080 return Result;
3081
3082 // If we really need inline code and the target declined to provide it,
3083 // use a (potentially long) sequence of loads and stores.
3084 if (AlwaysInline) {
3085 assert(ConstantSize && "AlwaysInline requires a constant size!");
3086 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003087 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003088 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003089 }
3090
3091 // Emit a library call.
3092 TargetLowering::ArgListTy Args;
3093 TargetLowering::ArgListEntry Entry;
3094 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3095 Entry.Node = Dst; Args.push_back(Entry);
3096 Entry.Node = Src; Args.push_back(Entry);
3097 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003098 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003099 TLI.LowerCallTo(Chain, Type::VoidTy,
3100 false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003101 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003102 Args, *this);
3103 return CallResult.second;
3104}
3105
Dan Gohman475871a2008-07-27 21:46:04 +00003106SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3107 SDValue Src, SDValue Size,
3108 unsigned Align,
3109 const Value *DstSV, uint64_t DstSVOff,
3110 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003111
Dan Gohman21323f32008-05-29 19:42:22 +00003112 // Check to see if we should lower the memmove to loads and stores first.
3113 // For cases within the target-specified limits, this is the best choice.
3114 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3115 if (ConstantSize) {
3116 // Memmove with size zero? Just return the original chain.
3117 if (ConstantSize->isNullValue())
3118 return Chain;
3119
Dan Gohman475871a2008-07-27 21:46:04 +00003120 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003121 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3122 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003123 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003124 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003125 return Result;
3126 }
Dan Gohman707e0182008-04-12 04:36:06 +00003127
3128 // Then check to see if we should lower the memmove with target-specific
3129 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003130 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003131 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003132 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003133 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003134 return Result;
3135
3136 // Emit a library call.
3137 TargetLowering::ArgListTy Args;
3138 TargetLowering::ArgListEntry Entry;
3139 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3140 Entry.Node = Dst; Args.push_back(Entry);
3141 Entry.Node = Src; Args.push_back(Entry);
3142 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003143 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003144 TLI.LowerCallTo(Chain, Type::VoidTy,
3145 false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003146 getExternalSymbol("memmove", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003147 Args, *this);
3148 return CallResult.second;
3149}
3150
Dan Gohman475871a2008-07-27 21:46:04 +00003151SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3152 SDValue Src, SDValue Size,
3153 unsigned Align,
3154 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003155
3156 // Check to see if we should lower the memset to stores first.
3157 // For cases within the target-specified limits, this is the best choice.
3158 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3159 if (ConstantSize) {
3160 // Memset with size zero? Just return the original chain.
3161 if (ConstantSize->isNullValue())
3162 return Chain;
3163
Dan Gohman475871a2008-07-27 21:46:04 +00003164 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003165 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3166 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003167 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003168 return Result;
3169 }
3170
3171 // Then check to see if we should lower the memset with target-specific
3172 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003173 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003174 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003175 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003176 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003177 return Result;
3178
3179 // Emit a library call.
3180 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3181 TargetLowering::ArgListTy Args;
3182 TargetLowering::ArgListEntry Entry;
3183 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3184 Args.push_back(Entry);
3185 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003186 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003187 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3188 else
3189 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3190 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3191 Args.push_back(Entry);
3192 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3193 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003194 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003195 TLI.LowerCallTo(Chain, Type::VoidTy,
3196 false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003197 getExternalSymbol("memset", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003198 Args, *this);
3199 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003200}
3201
Dan Gohman475871a2008-07-27 21:46:04 +00003202SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3203 SDValue Ptr, SDValue Cmp,
3204 SDValue Swp, const Value* PtrVal,
3205 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003206 assert((Opcode == ISD::ATOMIC_CMP_SWAP_8 ||
3207 Opcode == ISD::ATOMIC_CMP_SWAP_16 ||
3208 Opcode == ISD::ATOMIC_CMP_SWAP_32 ||
3209 Opcode == ISD::ATOMIC_CMP_SWAP_64) && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003210 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003211
3212 MVT VT = Cmp.getValueType();
3213
3214 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3215 Alignment = getMVTAlignment(VT);
3216
3217 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003218 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003220 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003221 void* IP = 0;
3222 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003223 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003224 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003225 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003226 CSEMap.InsertNode(N, IP);
3227 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003228 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003229}
3230
Dan Gohman475871a2008-07-27 21:46:04 +00003231SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3232 SDValue Ptr, SDValue Val,
3233 const Value* PtrVal,
3234 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003235 assert((Opcode == ISD::ATOMIC_LOAD_ADD_8 ||
3236 Opcode == ISD::ATOMIC_LOAD_SUB_8 ||
3237 Opcode == ISD::ATOMIC_LOAD_AND_8 ||
3238 Opcode == ISD::ATOMIC_LOAD_OR_8 ||
3239 Opcode == ISD::ATOMIC_LOAD_XOR_8 ||
3240 Opcode == ISD::ATOMIC_LOAD_NAND_8 ||
3241 Opcode == ISD::ATOMIC_LOAD_MIN_8 ||
3242 Opcode == ISD::ATOMIC_LOAD_MAX_8 ||
3243 Opcode == ISD::ATOMIC_LOAD_UMIN_8 ||
3244 Opcode == ISD::ATOMIC_LOAD_UMAX_8 ||
3245 Opcode == ISD::ATOMIC_SWAP_8 ||
3246 Opcode == ISD::ATOMIC_LOAD_ADD_16 ||
3247 Opcode == ISD::ATOMIC_LOAD_SUB_16 ||
3248 Opcode == ISD::ATOMIC_LOAD_AND_16 ||
3249 Opcode == ISD::ATOMIC_LOAD_OR_16 ||
3250 Opcode == ISD::ATOMIC_LOAD_XOR_16 ||
3251 Opcode == ISD::ATOMIC_LOAD_NAND_16 ||
3252 Opcode == ISD::ATOMIC_LOAD_MIN_16 ||
3253 Opcode == ISD::ATOMIC_LOAD_MAX_16 ||
3254 Opcode == ISD::ATOMIC_LOAD_UMIN_16 ||
3255 Opcode == ISD::ATOMIC_LOAD_UMAX_16 ||
3256 Opcode == ISD::ATOMIC_SWAP_16 ||
3257 Opcode == ISD::ATOMIC_LOAD_ADD_32 ||
3258 Opcode == ISD::ATOMIC_LOAD_SUB_32 ||
3259 Opcode == ISD::ATOMIC_LOAD_AND_32 ||
3260 Opcode == ISD::ATOMIC_LOAD_OR_32 ||
3261 Opcode == ISD::ATOMIC_LOAD_XOR_32 ||
3262 Opcode == ISD::ATOMIC_LOAD_NAND_32 ||
3263 Opcode == ISD::ATOMIC_LOAD_MIN_32 ||
3264 Opcode == ISD::ATOMIC_LOAD_MAX_32 ||
3265 Opcode == ISD::ATOMIC_LOAD_UMIN_32 ||
3266 Opcode == ISD::ATOMIC_LOAD_UMAX_32 ||
3267 Opcode == ISD::ATOMIC_SWAP_32 ||
3268 Opcode == ISD::ATOMIC_LOAD_ADD_64 ||
3269 Opcode == ISD::ATOMIC_LOAD_SUB_64 ||
3270 Opcode == ISD::ATOMIC_LOAD_AND_64 ||
3271 Opcode == ISD::ATOMIC_LOAD_OR_64 ||
3272 Opcode == ISD::ATOMIC_LOAD_XOR_64 ||
3273 Opcode == ISD::ATOMIC_LOAD_NAND_64 ||
3274 Opcode == ISD::ATOMIC_LOAD_MIN_64 ||
3275 Opcode == ISD::ATOMIC_LOAD_MAX_64 ||
3276 Opcode == ISD::ATOMIC_LOAD_UMIN_64 ||
3277 Opcode == ISD::ATOMIC_LOAD_UMAX_64 ||
3278 Opcode == ISD::ATOMIC_SWAP_64) && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003279
3280 MVT VT = Val.getValueType();
3281
3282 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3283 Alignment = getMVTAlignment(VT);
3284
3285 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003286 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003287 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003288 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003289 void* IP = 0;
3290 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003291 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003292 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003293 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003294 CSEMap.InsertNode(N, IP);
3295 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003296 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003297}
3298
Duncan Sands4bdcb612008-07-02 17:40:58 +00003299/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3300/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003301SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3302 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003303 if (Simplify && NumOps == 1)
3304 return Ops[0];
3305
3306 SmallVector<MVT, 4> VTs;
3307 VTs.reserve(NumOps);
3308 for (unsigned i = 0; i < NumOps; ++i)
3309 VTs.push_back(Ops[i].getValueType());
3310 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3311}
3312
Dan Gohman475871a2008-07-27 21:46:04 +00003313SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003314SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
3315 SDVTList VTs,
3316 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003317 // Do not include isTailCall in the folding set profile.
3318 FoldingSetNodeID ID;
3319 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3320 ID.AddInteger(CallingConv);
3321 ID.AddInteger(IsVarArgs);
3322 void *IP = 0;
3323 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3324 // Instead of including isTailCall in the folding set, we just
3325 // set the flag of the existing node.
3326 if (!IsTailCall)
3327 cast<CallSDNode>(E)->setNotTailCall();
3328 return SDValue(E, 0);
3329 }
Dan Gohman095cc292008-09-13 01:54:27 +00003330 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3331 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall,
3332 VTs, Operands, NumOperands);
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003333 CSEMap.InsertNode(N, IP);
Dan Gohman095cc292008-09-13 01:54:27 +00003334 AllNodes.push_back(N);
3335 return SDValue(N, 0);
3336}
3337
3338SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003339SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003340 MVT VT, SDValue Chain,
3341 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003342 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003343 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003344 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3345 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003346
Duncan Sands14ea39c2008-03-27 20:23:40 +00003347 if (VT == EVT) {
3348 ExtType = ISD::NON_EXTLOAD;
3349 } else if (ExtType == ISD::NON_EXTLOAD) {
3350 assert(VT == EVT && "Non-extending load from different memory type!");
3351 } else {
3352 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003353 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003354 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3355 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003356 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003357 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003358 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003359 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003360 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003361 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003362 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003363 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003364
3365 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003366 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003367 "Unindexed load with an offset!");
3368
3369 SDVTList VTs = Indexed ?
3370 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003371 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003372 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003373 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003374 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003375 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003376 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003377 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003378 void *IP = 0;
3379 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003380 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003381 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003382 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3383 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003384 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003385 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003386 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003387}
3388
Dan Gohman475871a2008-07-27 21:46:04 +00003389SDValue SelectionDAG::getLoad(MVT VT,
3390 SDValue Chain, SDValue Ptr,
3391 const Value *SV, int SVOffset,
3392 bool isVolatile, unsigned Alignment) {
3393 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003394 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3395 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003396}
3397
Dan Gohman475871a2008-07-27 21:46:04 +00003398SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3399 SDValue Chain, SDValue Ptr,
3400 const Value *SV,
3401 int SVOffset, MVT EVT,
3402 bool isVolatile, unsigned Alignment) {
3403 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003404 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3405 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003406}
3407
Dan Gohman475871a2008-07-27 21:46:04 +00003408SDValue
3409SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3410 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003411 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003412 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3413 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003414 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3415 LD->getChain(), Base, Offset, LD->getSrcValue(),
3416 LD->getSrcValueOffset(), LD->getMemoryVT(),
3417 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003418}
3419
Dan Gohman475871a2008-07-27 21:46:04 +00003420SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3421 SDValue Ptr, const Value *SV, int SVOffset,
3422 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003423 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003424
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003425 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3426 Alignment = getMVTAlignment(VT);
3427
Evan Chengad071e12006-10-05 22:57:11 +00003428 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003429 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3430 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003431 FoldingSetNodeID ID;
3432 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003433 ID.AddInteger(ISD::UNINDEXED);
3434 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003435 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003436 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003437 void *IP = 0;
3438 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003439 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003440 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003441 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3442 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003443 CSEMap.InsertNode(N, IP);
3444 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003445 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003446}
3447
Dan Gohman475871a2008-07-27 21:46:04 +00003448SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3449 SDValue Ptr, const Value *SV,
3450 int SVOffset, MVT SVT,
3451 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003452 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003453
3454 if (VT == SVT)
3455 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003456
Duncan Sands8e4eb092008-06-08 20:54:56 +00003457 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003458 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003459 "Can't do FP-INT conversion!");
3460
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003461 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3462 Alignment = getMVTAlignment(VT);
3463
Evan Cheng8b2794a2006-10-13 21:14:26 +00003464 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003465 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3466 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003467 FoldingSetNodeID ID;
3468 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003469 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003470 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003471 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003472 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003473 void *IP = 0;
3474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003475 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003476 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003477 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3478 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003479 CSEMap.InsertNode(N, IP);
3480 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003481 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003482}
3483
Dan Gohman475871a2008-07-27 21:46:04 +00003484SDValue
3485SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3486 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003487 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3488 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3489 "Store is already a indexed store!");
3490 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003492 FoldingSetNodeID ID;
3493 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3494 ID.AddInteger(AM);
3495 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003496 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003497 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003498 void *IP = 0;
3499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003500 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003501 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003502 new (N) StoreSDNode(Ops, VTs, AM,
3503 ST->isTruncatingStore(), ST->getMemoryVT(),
3504 ST->getSrcValue(), ST->getSrcValueOffset(),
3505 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003506 CSEMap.InsertNode(N, IP);
3507 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003508 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003509}
3510
Dan Gohman475871a2008-07-27 21:46:04 +00003511SDValue SelectionDAG::getVAArg(MVT VT,
3512 SDValue Chain, SDValue Ptr,
3513 SDValue SV) {
3514 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003515 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003516}
3517
Dan Gohman475871a2008-07-27 21:46:04 +00003518SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3519 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003520 switch (NumOps) {
3521 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003522 case 1: return getNode(Opcode, VT, Ops[0]);
3523 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3524 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003525 default: break;
3526 }
3527
Dan Gohman475871a2008-07-27 21:46:04 +00003528 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003529 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3531 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003532}
3533
Dan Gohman475871a2008-07-27 21:46:04 +00003534SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3535 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003536 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003537 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003538 case 1: return getNode(Opcode, VT, Ops[0]);
3539 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3540 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003541 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003542 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003543
Chris Lattneref847df2005-04-09 03:27:28 +00003544 switch (Opcode) {
3545 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003546 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003547 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003548 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3549 "LHS and RHS of condition must have same type!");
3550 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3551 "True and False arms of SelectCC must have same type!");
3552 assert(Ops[2].getValueType() == VT &&
3553 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003554 break;
3555 }
3556 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003557 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003558 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3559 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003560 break;
3561 }
Chris Lattneref847df2005-04-09 03:27:28 +00003562 }
3563
Chris Lattner385328c2005-05-14 07:42:29 +00003564 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003565 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003566 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003567 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003568 FoldingSetNodeID ID;
3569 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003570 void *IP = 0;
3571 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003572 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003573 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003574 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003575 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003576 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003577 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003578 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003579 }
Chris Lattneref847df2005-04-09 03:27:28 +00003580 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003581#ifndef NDEBUG
3582 VerifyNode(N);
3583#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003584 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003585}
3586
Dan Gohman475871a2008-07-27 21:46:04 +00003587SDValue SelectionDAG::getNode(unsigned Opcode,
3588 const std::vector<MVT> &ResultTys,
3589 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003590 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3591 Ops, NumOps);
3592}
3593
Dan Gohman475871a2008-07-27 21:46:04 +00003594SDValue SelectionDAG::getNode(unsigned Opcode,
3595 const MVT *VTs, unsigned NumVTs,
3596 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003597 if (NumVTs == 1)
3598 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003599 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3600}
3601
Dan Gohman475871a2008-07-27 21:46:04 +00003602SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3603 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003604 if (VTList.NumVTs == 1)
3605 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003606
Chris Lattner5f056bf2005-07-10 01:55:33 +00003607 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003608 // FIXME: figure out how to safely handle things like
3609 // int foo(int x) { return 1 << (x & 255); }
3610 // int bar() { return foo(256); }
3611#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003612 case ISD::SRA_PARTS:
3613 case ISD::SRL_PARTS:
3614 case ISD::SHL_PARTS:
3615 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003616 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003617 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3618 else if (N3.getOpcode() == ISD::AND)
3619 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3620 // If the and is only masking out bits that cannot effect the shift,
3621 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003622 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003623 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3624 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3625 }
3626 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003627#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003628 }
Chris Lattner89c34632005-05-14 06:20:26 +00003629
Chris Lattner43247a12005-08-25 19:12:10 +00003630 // Memoize the node unless it returns a flag.
3631 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003632 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003633 FoldingSetNodeID ID;
3634 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003635 void *IP = 0;
3636 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003637 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003638 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003639 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003640 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3641 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003642 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003643 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3644 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003645 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003646 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3647 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003648 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003649 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3650 }
Chris Lattnera5682852006-08-07 23:03:03 +00003651 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003652 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003653 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003654 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003655 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3656 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003657 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003658 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3659 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003660 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003661 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3662 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003663 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003664 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3665 }
Chris Lattner43247a12005-08-25 19:12:10 +00003666 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003667 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003668#ifndef NDEBUG
3669 VerifyNode(N);
3670#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003671 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003672}
3673
Dan Gohman475871a2008-07-27 21:46:04 +00003674SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003675 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003676}
3677
Dan Gohman475871a2008-07-27 21:46:04 +00003678SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3679 SDValue N1) {
3680 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003681 return getNode(Opcode, VTList, Ops, 1);
3682}
3683
Dan Gohman475871a2008-07-27 21:46:04 +00003684SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3685 SDValue N1, SDValue N2) {
3686 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003687 return getNode(Opcode, VTList, Ops, 2);
3688}
3689
Dan Gohman475871a2008-07-27 21:46:04 +00003690SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3691 SDValue N1, SDValue N2, SDValue N3) {
3692 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003693 return getNode(Opcode, VTList, Ops, 3);
3694}
3695
Dan Gohman475871a2008-07-27 21:46:04 +00003696SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3697 SDValue N1, SDValue N2, SDValue N3,
3698 SDValue N4) {
3699 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003700 return getNode(Opcode, VTList, Ops, 4);
3701}
3702
Dan Gohman475871a2008-07-27 21:46:04 +00003703SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3704 SDValue N1, SDValue N2, SDValue N3,
3705 SDValue N4, SDValue N5) {
3706 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003707 return getNode(Opcode, VTList, Ops, 5);
3708}
3709
Duncan Sands83ec4b62008-06-06 12:08:01 +00003710SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003711 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003712}
3713
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003715 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3716 E = VTList.rend(); I != E; ++I)
3717 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3718 return *I;
3719
3720 MVT *Array = Allocator.Allocate<MVT>(2);
3721 Array[0] = VT1;
3722 Array[1] = VT2;
3723 SDVTList Result = makeVTList(Array, 2);
3724 VTList.push_back(Result);
3725 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003726}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003727
3728SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3729 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3730 E = VTList.rend(); I != E; ++I)
3731 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3732 I->VTs[2] == VT3)
3733 return *I;
3734
3735 MVT *Array = Allocator.Allocate<MVT>(3);
3736 Array[0] = VT1;
3737 Array[1] = VT2;
3738 Array[2] = VT3;
3739 SDVTList Result = makeVTList(Array, 3);
3740 VTList.push_back(Result);
3741 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003742}
3743
Duncan Sands83ec4b62008-06-06 12:08:01 +00003744SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003745 switch (NumVTs) {
3746 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003747 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003748 case 2: return getVTList(VTs[0], VTs[1]);
3749 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3750 default: break;
3751 }
3752
Dan Gohmane8be6c62008-07-17 19:10:17 +00003753 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3754 E = VTList.rend(); I != E; ++I) {
3755 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3756 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003757
3758 bool NoMatch = false;
3759 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003760 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003761 NoMatch = true;
3762 break;
3763 }
3764 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003765 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003766 }
3767
Dan Gohmane8be6c62008-07-17 19:10:17 +00003768 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3769 std::copy(VTs, VTs+NumVTs, Array);
3770 SDVTList Result = makeVTList(Array, NumVTs);
3771 VTList.push_back(Result);
3772 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003773}
3774
3775
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003776/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3777/// specified operands. If the resultant node already exists in the DAG,
3778/// this does not modify the specified node, instead it returns the node that
3779/// already exists. If the resultant node does not exist in the DAG, the
3780/// input node is returned. As a degenerate case, if you specify the same
3781/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003782SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003783 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003784 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3785
3786 // Check to see if there is no change.
3787 if (Op == N->getOperand(0)) return InN;
3788
3789 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003790 void *InsertPos = 0;
3791 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003792 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003793
Dan Gohman79acd2b2008-07-21 22:38:59 +00003794 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003795 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003796 if (!RemoveNodeFromCSEMaps(N))
3797 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003798
3799 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003800 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003801 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003802 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003803 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003804
3805 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003806 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003807 return InN;
3808}
3809
Dan Gohman475871a2008-07-27 21:46:04 +00003810SDValue SelectionDAG::
3811UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003812 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003813 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3814
3815 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003816 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3817 return InN; // No operands changed, just return the input node.
3818
3819 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003820 void *InsertPos = 0;
3821 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003822 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003823
Dan Gohman79acd2b2008-07-21 22:38:59 +00003824 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003825 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003826 if (!RemoveNodeFromCSEMaps(N))
3827 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003828
3829 // Now we update the operands.
3830 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003831 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003832 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003833 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003834 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003835 }
3836 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003837 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003838 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003839 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003840 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003841 }
3842
3843 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003844 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003845 return InN;
3846}
3847
Dan Gohman475871a2008-07-27 21:46:04 +00003848SDValue SelectionDAG::
3849UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3850 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003851 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003852}
3853
Dan Gohman475871a2008-07-27 21:46:04 +00003854SDValue SelectionDAG::
3855UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3856 SDValue Op3, SDValue Op4) {
3857 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003858 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003859}
3860
Dan Gohman475871a2008-07-27 21:46:04 +00003861SDValue SelectionDAG::
3862UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3863 SDValue Op3, SDValue Op4, SDValue Op5) {
3864 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003865 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003866}
3867
Dan Gohman475871a2008-07-27 21:46:04 +00003868SDValue SelectionDAG::
3869UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003870 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003871 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003872 "Update with wrong number of operands");
3873
3874 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003875 bool AnyChange = false;
3876 for (unsigned i = 0; i != NumOps; ++i) {
3877 if (Ops[i] != N->getOperand(i)) {
3878 AnyChange = true;
3879 break;
3880 }
3881 }
3882
3883 // No operands changed, just return the input node.
3884 if (!AnyChange) return InN;
3885
3886 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003887 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003888 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003889 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003890
Dan Gohman7ceda162008-05-02 00:05:03 +00003891 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003892 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003893 if (!RemoveNodeFromCSEMaps(N))
3894 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003895
3896 // Now we update the operands.
3897 for (unsigned i = 0; i != NumOps; ++i) {
3898 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003899 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003900 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003901 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003902 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003903 }
3904 }
3905
3906 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003907 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003908 return InN;
3909}
3910
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003911/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003912/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003913void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003914 // Unlike the code in MorphNodeTo that does this, we don't need to
3915 // watch for dead nodes here.
3916 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3917 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3918
3919 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003920}
Chris Lattner149c58c2005-08-16 18:17:10 +00003921
Dan Gohmane8be6c62008-07-17 19:10:17 +00003922/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3923/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003924///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003925SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003926 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003927 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003928 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003929}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003930
Dan Gohmane8be6c62008-07-17 19:10:17 +00003931SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003932 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003933 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003934 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003935 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003936}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003937
Dan Gohmane8be6c62008-07-17 19:10:17 +00003938SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003939 MVT VT, SDValue Op1,
3940 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003941 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003942 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003943 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003944}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003945
Dan Gohmane8be6c62008-07-17 19:10:17 +00003946SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003947 MVT VT, SDValue Op1,
3948 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003949 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003952}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003953
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003955 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003956 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003957 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003958 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003959}
3960
Dan Gohmane8be6c62008-07-17 19:10:17 +00003961SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003962 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003963 unsigned NumOps) {
3964 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003965 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003966}
3967
Dan Gohmane8be6c62008-07-17 19:10:17 +00003968SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003969 MVT VT1, MVT VT2) {
3970 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003971 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003972}
3973
Dan Gohmane8be6c62008-07-17 19:10:17 +00003974SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003975 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003976 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003977 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003978 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003979}
3980
Dan Gohmane8be6c62008-07-17 19:10:17 +00003981SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003982 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003983 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003984 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003985 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003986 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003987}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003988
Dan Gohmane8be6c62008-07-17 19:10:17 +00003989SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003990 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003991 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003992 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003993 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003994 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003995}
3996
Dan Gohmane8be6c62008-07-17 19:10:17 +00003997SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003998 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003999 SDValue Op1, SDValue Op2,
4000 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004001 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004002 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004003 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004004}
4005
Dan Gohmane8be6c62008-07-17 19:10:17 +00004006SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004007 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004008 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004009 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4010}
4011
4012SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4013 MVT VT) {
4014 SDVTList VTs = getVTList(VT);
4015 return MorphNodeTo(N, Opc, VTs, 0, 0);
4016}
4017
4018SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004019 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004020 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004021 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004022 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4023}
4024
4025SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004026 MVT VT, SDValue Op1,
4027 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004028 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004029 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004030 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4031}
4032
4033SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004034 MVT VT, SDValue Op1,
4035 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004036 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004037 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4039}
4040
4041SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004042 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004043 unsigned NumOps) {
4044 SDVTList VTs = getVTList(VT);
4045 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4046}
4047
4048SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004049 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004050 unsigned NumOps) {
4051 SDVTList VTs = getVTList(VT1, VT2);
4052 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4053}
4054
4055SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4056 MVT VT1, MVT VT2) {
4057 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004058 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004059}
4060
4061SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4062 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004063 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064 SDVTList VTs = getVTList(VT1, VT2, VT3);
4065 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4066}
4067
4068SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4069 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004070 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004071 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004072 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004073 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4074}
4075
4076SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4077 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004078 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004079 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004080 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004081 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4082}
4083
4084SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4085 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004086 SDValue Op1, SDValue Op2,
4087 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004088 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004089 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004090 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4091}
4092
4093/// MorphNodeTo - These *mutate* the specified node to have the specified
4094/// return type, opcode, and operands.
4095///
4096/// Note that MorphNodeTo returns the resultant node. If there is already a
4097/// node of the specified opcode and operands, it returns that node instead of
4098/// the current one.
4099///
4100/// Using MorphNodeTo is faster than creating a new node and swapping it in
4101/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004102/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004103/// the node's users.
4104///
4105SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004107 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004108 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004109 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004110 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4111 FoldingSetNodeID ID;
4112 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4113 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4114 return ON;
4115 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004116
Dan Gohman095cc292008-09-13 01:54:27 +00004117 if (!RemoveNodeFromCSEMaps(N))
4118 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004119
Dan Gohmane8be6c62008-07-17 19:10:17 +00004120 // Start the morphing.
4121 N->NodeType = Opc;
4122 N->ValueList = VTs.VTs;
4123 N->NumValues = VTs.NumVTs;
4124
4125 // Clear the operands list, updating used nodes to remove this from their
4126 // use list. Keep track of any operands that become dead as a result.
4127 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4128 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4129 I != E; ++I) {
4130 SDNode *Used = I->getVal();
4131 Used->removeUser(std::distance(B, I), N);
4132 if (Used->use_empty())
4133 DeadNodeSet.insert(Used);
4134 }
4135
4136 // If NumOps is larger than the # of operands we currently have, reallocate
4137 // the operand list.
4138 if (NumOps > N->NumOperands) {
4139 if (N->OperandsNeedDelete)
4140 delete[] N->OperandList;
4141 if (N->isMachineOpcode()) {
4142 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004143 // remainder of the current SelectionDAG iteration, so we can allocate
4144 // the operands directly out of a pool with no recycling metadata.
4145 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004146 N->OperandsNeedDelete = false;
4147 } else {
4148 N->OperandList = new SDUse[NumOps];
4149 N->OperandsNeedDelete = true;
4150 }
4151 }
4152
4153 // Assign the new operands.
4154 N->NumOperands = NumOps;
4155 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4156 N->OperandList[i] = Ops[i];
4157 N->OperandList[i].setUser(N);
4158 SDNode *ToUse = N->OperandList[i].getVal();
4159 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004160 }
4161
4162 // Delete any nodes that are still dead after adding the uses for the
4163 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004164 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004165 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4166 E = DeadNodeSet.end(); I != E; ++I)
4167 if ((*I)->use_empty())
4168 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004169 RemoveDeadNodes(DeadNodes);
4170
Dan Gohmane8be6c62008-07-17 19:10:17 +00004171 if (IP)
4172 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004173 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004174}
4175
Chris Lattner0fb094f2005-11-19 01:44:53 +00004176
Evan Cheng6ae46c42006-02-09 07:15:23 +00004177/// getTargetNode - These are used for target selectors to create a new node
4178/// with specified return type(s), target opcode, and operands.
4179///
4180/// Note that getTargetNode returns the resultant node. If there is already a
4181/// node of the specified opcode and operands, it returns that node instead of
4182/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004184 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004185}
Dan Gohman475871a2008-07-27 21:46:04 +00004186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004188}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004189SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004190 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004191 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004192}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 SDValue Op1, SDValue Op2,
4195 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004196 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004197}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004199 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004200 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004201}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4203 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004204 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004205 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004206}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004207SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004208 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004209 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004211}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004213 MVT VT2, SDValue Op1,
4214 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004215 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004216 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004217 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004218}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004219SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004220 MVT VT2, SDValue Op1,
4221 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004222 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004223 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004224 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004225}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004226SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004227 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004228 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004229 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004230}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004233 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004234 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004235 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004236}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004238 SDValue Op1, SDValue Op2,
4239 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004241 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004242 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004243}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004244SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004245 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004246 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004247 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004248}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4250 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004251 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004252 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004253 VTList.push_back(VT1);
4254 VTList.push_back(VT2);
4255 VTList.push_back(VT3);
4256 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004257 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004258 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004259}
Evan Cheng39305cf2007-10-05 01:10:49 +00004260SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004261 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004262 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004263 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004264 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004265 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004266}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004267
Evan Cheng08b11732008-03-22 01:55:50 +00004268/// getNodeIfExists - Get the specified node if it's already available, or
4269/// else return NULL.
4270SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004271 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004272 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4273 FoldingSetNodeID ID;
4274 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4275 void *IP = 0;
4276 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4277 return E;
4278 }
4279 return NULL;
4280}
4281
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004282
Evan Cheng99157a02006-08-07 22:13:29 +00004283/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004284/// This can cause recursive merging of nodes in the DAG.
4285///
Chris Lattner11d049c2008-02-03 03:35:22 +00004286/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004287///
Dan Gohman475871a2008-07-27 21:46:04 +00004288void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004289 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004290 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004291 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004292 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004293 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004294
Chris Lattner8b8749f2005-08-17 19:00:20 +00004295 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004296 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004297 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004298
Chris Lattner8b8749f2005-08-17 19:00:20 +00004299 // This node is about to morph, remove its old self from the CSE maps.
4300 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004301 int operandNum = 0;
4302 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4303 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004304 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004305 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004306 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004307 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004309 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004310
4311 // Now that we have modified U, add it back to the CSE maps. If it already
4312 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004313 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004314 ReplaceAllUsesWith(U, Existing, UpdateListener);
4315 // U is now dead. Inform the listener if it exists and delete it.
4316 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004317 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004318 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004319 } else {
4320 // If the node doesn't already exist, we updated it. Inform a listener if
4321 // it exists.
4322 if (UpdateListener)
4323 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004324 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004325 }
4326}
4327
4328/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4329/// This can cause recursive merging of nodes in the DAG.
4330///
4331/// This version assumes From/To have matching types and numbers of result
4332/// values.
4333///
Chris Lattner1e111c72005-09-07 05:37:01 +00004334void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004335 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004336 assert(From->getVTList().VTs == To->getVTList().VTs &&
4337 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004338 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004339
4340 // Handle the trivial case.
4341 if (From == To)
4342 return;
4343
Chris Lattner8b52f212005-08-26 18:36:28 +00004344 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004345 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004346 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004347
Chris Lattner8b52f212005-08-26 18:36:28 +00004348 // This node is about to morph, remove its old self from the CSE maps.
4349 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004350 int operandNum = 0;
4351 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4352 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004353 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004354 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004355 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004356 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004357 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004358
Chris Lattner8b8749f2005-08-17 19:00:20 +00004359 // Now that we have modified U, add it back to the CSE maps. If it already
4360 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004361 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004362 ReplaceAllUsesWith(U, Existing, UpdateListener);
4363 // U is now dead. Inform the listener if it exists and delete it.
4364 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004365 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004366 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004367 } else {
4368 // If the node doesn't already exist, we updated it. Inform a listener if
4369 // it exists.
4370 if (UpdateListener)
4371 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004372 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004373 }
4374}
4375
Chris Lattner8b52f212005-08-26 18:36:28 +00004376/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4377/// This can cause recursive merging of nodes in the DAG.
4378///
4379/// This version can replace From with any result values. To must match the
4380/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004381void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004382 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004383 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004384 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004385 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004386
4387 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004388 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004389 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004390
Chris Lattner7b2880c2005-08-24 22:44:39 +00004391 // This node is about to morph, remove its old self from the CSE maps.
4392 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004393 int operandNum = 0;
4394 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4395 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004396 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004397 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004398 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004399 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004400 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004401 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004402 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004403
Chris Lattner7b2880c2005-08-24 22:44:39 +00004404 // Now that we have modified U, add it back to the CSE maps. If it already
4405 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004406 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004407 ReplaceAllUsesWith(U, Existing, UpdateListener);
4408 // U is now dead. Inform the listener if it exists and delete it.
4409 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004410 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004411 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004412 } else {
4413 // If the node doesn't already exist, we updated it. Inform a listener if
4414 // it exists.
4415 if (UpdateListener)
4416 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004417 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004418 }
4419}
4420
Chris Lattner012f2412006-02-17 21:58:01 +00004421/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004422/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004423/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004424void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004425 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004426 // Handle the really simple, really trivial case efficiently.
4427 if (From == To) return;
4428
Chris Lattner012f2412006-02-17 21:58:01 +00004429 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004430 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004431 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004432 return;
4433 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004434
Gabor Greifba36cb52008-08-28 21:40:38 +00004435 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004436 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004437 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004438
4439 while (!Users.empty()) {
4440 // We know that this user uses some value of From. If it is the right
4441 // value, update it.
4442 SDNode *User = Users.back();
4443 Users.pop_back();
4444
Chris Lattner01d029b2007-10-15 06:10:22 +00004445 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004446 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004447 for (; Op != E; ++Op)
4448 if (*Op == From) break;
4449
4450 // If there are no matches, the user must use some other result of From.
4451 if (Op == E) continue;
4452
4453 // Okay, we know this user needs to be updated. Remove its old self
4454 // from the CSE maps.
4455 RemoveNodeFromCSEMaps(User);
4456
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004457 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004458 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004459 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004460 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004461 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004462 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004463 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004464 }
4465 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004466
4467 // Now that we have modified User, add it back to the CSE maps. If it
4468 // already exists there, recursively merge the results together.
4469 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004470 if (!Existing) {
4471 if (UpdateListener) UpdateListener->NodeUpdated(User);
4472 continue; // Continue on to next user.
4473 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004474
4475 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004476 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004477 // recursive merging of other unrelated nodes down the line.
4478 ReplaceAllUsesWith(User, Existing, UpdateListener);
4479
4480 // User is now dead. Notify a listener if present.
4481 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4482 DeleteNodeNotInCSEMaps(User);
4483 }
4484}
4485
4486/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004487/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004488/// appear in both the From and To list. The Deleted vector is
4489/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004490void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4491 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004492 unsigned Num,
4493 DAGUpdateListener *UpdateListener){
4494 // Handle the simple, trivial case efficiently.
4495 if (Num == 1)
4496 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4497
4498 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4499 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004500 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4501 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004502 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004503
4504 while (!Users.empty()) {
4505 // We know that this user uses some value of From. If it is the right
4506 // value, update it.
4507 SDNode *User = Users.back().first;
4508 unsigned i = Users.back().second;
4509 Users.pop_back();
4510
4511 // Scan for an operand that matches From.
4512 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4513 for (; Op != E; ++Op)
4514 if (*Op == From[i]) break;
4515
4516 // If there are no matches, the user must use some other result of From.
4517 if (Op == E) continue;
4518
4519 // Okay, we know this user needs to be updated. Remove its old self
4520 // from the CSE maps.
4521 RemoveNodeFromCSEMaps(User);
4522
4523 // Update all operands that match "From" in case there are multiple uses.
4524 for (; Op != E; ++Op) {
4525 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004526 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004527 *Op = To[i];
4528 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004529 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004530 }
4531 }
4532
4533 // Now that we have modified User, add it back to the CSE maps. If it
4534 // already exists there, recursively merge the results together.
4535 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4536 if (!Existing) {
4537 if (UpdateListener) UpdateListener->NodeUpdated(User);
4538 continue; // Continue on to next user.
4539 }
4540
4541 // If there was already an existing matching node, use ReplaceAllUsesWith
4542 // to replace the dead one with the existing one. This can cause
4543 // recursive merging of other unrelated nodes down the line.
4544 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004545
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004546 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004547 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004548 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004549 }
4550}
4551
Evan Chenge6f35d82006-08-01 08:20:41 +00004552/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004553/// based on their topological order. It returns the maximum id and a vector
4554/// of the SDNodes* in assigned order by reference.
4555unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004556 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004557 std::vector<SDNode*> Sources;
4558
Evan Chenge6f35d82006-08-01 08:20:41 +00004559 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4560 SDNode *N = I;
4561 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004562 // Temporarily use the Node Id as scratch space for the degree count.
4563 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004564 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004565 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004566 }
4567
Evan Cheng99157a02006-08-07 22:13:29 +00004568 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004569 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004570 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004571 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004572 SDNode *N = Sources.back();
4573 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004574 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004575 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004576 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004577 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004578 unsigned Degree = P->getNodeId();
4579 --Degree;
4580 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004581 if (Degree == 0)
4582 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004583 }
4584 }
4585
Evan Chengc384d6c2006-08-02 22:00:34 +00004586 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004587}
4588
4589
Evan Cheng091cba12006-07-27 06:39:06 +00004590
Jim Laskey58b968b2005-08-17 20:08:02 +00004591//===----------------------------------------------------------------------===//
4592// SDNode Class
4593//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004594
Chris Lattner917d2c92006-07-19 00:00:37 +00004595// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004596void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004597void UnarySDNode::ANCHOR() {}
4598void BinarySDNode::ANCHOR() {}
4599void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004600void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004601void ConstantSDNode::ANCHOR() {}
4602void ConstantFPSDNode::ANCHOR() {}
4603void GlobalAddressSDNode::ANCHOR() {}
4604void FrameIndexSDNode::ANCHOR() {}
4605void JumpTableSDNode::ANCHOR() {}
4606void ConstantPoolSDNode::ANCHOR() {}
4607void BasicBlockSDNode::ANCHOR() {}
4608void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004609void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004610void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004611void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004612void LabelSDNode::ANCHOR() {}
Bill Wendling056292f2008-09-16 21:48:12 +00004613void ExternalSymbolSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004614void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004615void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004616void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004617void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004618void LoadSDNode::ANCHOR() {}
4619void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004620void AtomicSDNode::ANCHOR() {}
Dan Gohman095cc292008-09-13 01:54:27 +00004621void CallSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004622
Chris Lattner48b85922007-02-04 02:41:42 +00004623HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004624 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004625}
4626
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004627GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004628 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004629 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004630 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004631 // Thread Local
4632 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4633 // Non Thread Local
4634 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4635 getSDVTList(VT)), Offset(o) {
4636 TheGlobal = const_cast<GlobalValue*>(GA);
4637}
Chris Lattner48b85922007-02-04 02:41:42 +00004638
Dan Gohman1ea58a52008-07-09 22:08:04 +00004639MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004640 const Value *srcValue, int SVO,
4641 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004642 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004643 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004644
4645 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4646 assert(getAlignment() == alignment && "Alignment representation error!");
4647 assert(isVolatile() == vol && "Volatile representation error!");
4648}
4649
Dan Gohman36b5c132008-04-07 19:35:22 +00004650/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004651/// reference performed by this memory reference.
4652MachineMemOperand MemSDNode::getMemOperand() const {
4653 int Flags;
4654 if (isa<LoadSDNode>(this))
4655 Flags = MachineMemOperand::MOLoad;
4656 else if (isa<StoreSDNode>(this))
4657 Flags = MachineMemOperand::MOStore;
4658 else {
4659 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4660 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4661 }
4662
4663 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004664 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4665
Dan Gohman1ea58a52008-07-09 22:08:04 +00004666 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004667 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004668 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004669 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004670 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4671 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004672 else
4673 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4674 Size, getAlignment());
4675}
4676
Jim Laskey583bd472006-10-27 23:46:08 +00004677/// Profile - Gather unique data for the node.
4678///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004679void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004680 AddNodeIDNode(ID, this);
4681}
4682
Chris Lattnera3255112005-11-08 23:30:28 +00004683/// getValueTypeList - Return a pointer to the specified value type.
4684///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004685const MVT *SDNode::getValueTypeList(MVT VT) {
4686 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004687 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004688 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004689 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004690 static MVT VTs[MVT::LAST_VALUETYPE];
4691 VTs[VT.getSimpleVT()] = VT;
4692 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004693 }
Chris Lattnera3255112005-11-08 23:30:28 +00004694}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004695
Chris Lattner5c884562005-01-12 18:37:47 +00004696/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4697/// indicated value. This method ignores uses of other values defined by this
4698/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004699bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004700 assert(Value < getNumValues() && "Bad value!");
4701
Roman Levensteindc1adac2008-04-07 10:06:32 +00004702 // TODO: Only iterate over uses of a given value of the node
4703 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004704 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004705 if (NUses == 0)
4706 return false;
4707 --NUses;
4708 }
Chris Lattner5c884562005-01-12 18:37:47 +00004709 }
4710
4711 // Found exactly the right number of uses?
4712 return NUses == 0;
4713}
4714
4715
Evan Cheng33d55952007-08-02 05:29:38 +00004716/// hasAnyUseOfValue - Return true if there are any use of the indicated
4717/// value. This method ignores uses of other values defined by this operation.
4718bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4719 assert(Value < getNumValues() && "Bad value!");
4720
Dan Gohman1373c1c2008-07-09 22:39:01 +00004721 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004722 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004723 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004724
4725 return false;
4726}
4727
4728
Dan Gohman2a629952008-07-27 18:06:42 +00004729/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004730///
Dan Gohman2a629952008-07-27 18:06:42 +00004731bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004732 bool Seen = false;
4733 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004734 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004735 if (User == this)
4736 Seen = true;
4737 else
4738 return false;
4739 }
4740
4741 return Seen;
4742}
4743
Evan Chenge6e97e62006-11-03 07:31:32 +00004744/// isOperand - Return true if this node is an operand of N.
4745///
Dan Gohman475871a2008-07-27 21:46:04 +00004746bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004747 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4748 if (*this == N->getOperand(i))
4749 return true;
4750 return false;
4751}
4752
Evan Cheng917be682008-03-04 00:41:45 +00004753bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004754 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004755 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004756 return true;
4757 return false;
4758}
Evan Cheng4ee62112006-02-05 06:29:23 +00004759
Chris Lattner572dee72008-01-16 05:49:24 +00004760/// reachesChainWithoutSideEffects - Return true if this operand (which must
4761/// be a chain) reaches the specified operand without crossing any
4762/// side-effecting instructions. In practice, this looks through token
4763/// factors and non-volatile loads. In order to remain efficient, this only
4764/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004765bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004766 unsigned Depth) const {
4767 if (*this == Dest) return true;
4768
4769 // Don't search too deeply, we just want to be able to see through
4770 // TokenFactor's etc.
4771 if (Depth == 0) return false;
4772
4773 // If this is a token factor, all inputs to the TF happen in parallel. If any
4774 // of the operands of the TF reach dest, then we can do the xform.
4775 if (getOpcode() == ISD::TokenFactor) {
4776 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4777 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4778 return true;
4779 return false;
4780 }
4781
4782 // Loads don't have side effects, look through them.
4783 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4784 if (!Ld->isVolatile())
4785 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4786 }
4787 return false;
4788}
4789
4790
Evan Chengc5fc57d2006-11-03 03:05:24 +00004791static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004792 SmallPtrSet<SDNode *, 32> &Visited) {
4793 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004794 return;
4795
4796 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004797 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004798 if (Op == P) {
4799 found = true;
4800 return;
4801 }
4802 findPredecessor(Op, P, found, Visited);
4803 }
4804}
4805
Evan Cheng917be682008-03-04 00:41:45 +00004806/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004807/// is either an operand of N or it can be reached by recursively traversing
4808/// up the operands.
4809/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004810bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004811 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004812 bool found = false;
4813 findPredecessor(N, this, found, Visited);
4814 return found;
4815}
4816
Evan Chengc5484282006-10-04 00:56:09 +00004817uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4818 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004819 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004820}
4821
Reid Spencer577cc322007-04-01 07:32:19 +00004822std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004823 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004824 default:
4825 if (getOpcode() < ISD::BUILTIN_OP_END)
4826 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004827 if (isMachineOpcode()) {
4828 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004829 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004830 if (getMachineOpcode() < TII->getNumOpcodes())
4831 return TII->get(getMachineOpcode()).getName();
4832 return "<<Unknown Machine Node>>";
4833 }
4834 if (G) {
4835 TargetLowering &TLI = G->getTargetLoweringInfo();
4836 const char *Name = TLI.getTargetNodeName(getOpcode());
4837 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004838 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004839 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004840 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004841
Dan Gohmane8be6c62008-07-17 19:10:17 +00004842#ifndef NDEBUG
4843 case ISD::DELETED_NODE:
4844 return "<<Deleted Node!>>";
4845#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004846 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004847 case ISD::MEMBARRIER: return "MemBarrier";
Dale Johannesene00a8a22008-08-28 02:44:49 +00004848 case ISD::ATOMIC_CMP_SWAP_8: return "AtomicCmpSwap8";
4849 case ISD::ATOMIC_SWAP_8: return "AtomicSwap8";
4850 case ISD::ATOMIC_LOAD_ADD_8: return "AtomicLoadAdd8";
4851 case ISD::ATOMIC_LOAD_SUB_8: return "AtomicLoadSub8";
4852 case ISD::ATOMIC_LOAD_AND_8: return "AtomicLoadAnd8";
4853 case ISD::ATOMIC_LOAD_OR_8: return "AtomicLoadOr8";
4854 case ISD::ATOMIC_LOAD_XOR_8: return "AtomicLoadXor8";
4855 case ISD::ATOMIC_LOAD_NAND_8: return "AtomicLoadNand8";
4856 case ISD::ATOMIC_LOAD_MIN_8: return "AtomicLoadMin8";
4857 case ISD::ATOMIC_LOAD_MAX_8: return "AtomicLoadMax8";
4858 case ISD::ATOMIC_LOAD_UMIN_8: return "AtomicLoadUMin8";
4859 case ISD::ATOMIC_LOAD_UMAX_8: return "AtomicLoadUMax8";
4860 case ISD::ATOMIC_CMP_SWAP_16: return "AtomicCmpSwap16";
4861 case ISD::ATOMIC_SWAP_16: return "AtomicSwap16";
4862 case ISD::ATOMIC_LOAD_ADD_16: return "AtomicLoadAdd16";
4863 case ISD::ATOMIC_LOAD_SUB_16: return "AtomicLoadSub16";
4864 case ISD::ATOMIC_LOAD_AND_16: return "AtomicLoadAnd16";
4865 case ISD::ATOMIC_LOAD_OR_16: return "AtomicLoadOr16";
4866 case ISD::ATOMIC_LOAD_XOR_16: return "AtomicLoadXor16";
4867 case ISD::ATOMIC_LOAD_NAND_16: return "AtomicLoadNand16";
4868 case ISD::ATOMIC_LOAD_MIN_16: return "AtomicLoadMin16";
4869 case ISD::ATOMIC_LOAD_MAX_16: return "AtomicLoadMax16";
4870 case ISD::ATOMIC_LOAD_UMIN_16: return "AtomicLoadUMin16";
4871 case ISD::ATOMIC_LOAD_UMAX_16: return "AtomicLoadUMax16";
4872 case ISD::ATOMIC_CMP_SWAP_32: return "AtomicCmpSwap32";
4873 case ISD::ATOMIC_SWAP_32: return "AtomicSwap32";
4874 case ISD::ATOMIC_LOAD_ADD_32: return "AtomicLoadAdd32";
4875 case ISD::ATOMIC_LOAD_SUB_32: return "AtomicLoadSub32";
4876 case ISD::ATOMIC_LOAD_AND_32: return "AtomicLoadAnd32";
4877 case ISD::ATOMIC_LOAD_OR_32: return "AtomicLoadOr32";
4878 case ISD::ATOMIC_LOAD_XOR_32: return "AtomicLoadXor32";
4879 case ISD::ATOMIC_LOAD_NAND_32: return "AtomicLoadNand32";
4880 case ISD::ATOMIC_LOAD_MIN_32: return "AtomicLoadMin32";
4881 case ISD::ATOMIC_LOAD_MAX_32: return "AtomicLoadMax32";
4882 case ISD::ATOMIC_LOAD_UMIN_32: return "AtomicLoadUMin32";
4883 case ISD::ATOMIC_LOAD_UMAX_32: return "AtomicLoadUMax32";
4884 case ISD::ATOMIC_CMP_SWAP_64: return "AtomicCmpSwap64";
4885 case ISD::ATOMIC_SWAP_64: return "AtomicSwap64";
4886 case ISD::ATOMIC_LOAD_ADD_64: return "AtomicLoadAdd64";
4887 case ISD::ATOMIC_LOAD_SUB_64: return "AtomicLoadSub64";
4888 case ISD::ATOMIC_LOAD_AND_64: return "AtomicLoadAnd64";
4889 case ISD::ATOMIC_LOAD_OR_64: return "AtomicLoadOr64";
4890 case ISD::ATOMIC_LOAD_XOR_64: return "AtomicLoadXor64";
4891 case ISD::ATOMIC_LOAD_NAND_64: return "AtomicLoadNand64";
4892 case ISD::ATOMIC_LOAD_MIN_64: return "AtomicLoadMin64";
4893 case ISD::ATOMIC_LOAD_MAX_64: return "AtomicLoadMax64";
4894 case ISD::ATOMIC_LOAD_UMIN_64: return "AtomicLoadUMin64";
4895 case ISD::ATOMIC_LOAD_UMAX_64: return "AtomicLoadUMax64";
Andrew Lenharth95762122005-03-31 21:24:06 +00004896 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004897 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004898 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004899 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004900 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004901 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004902 case ISD::AssertSext: return "AssertSext";
4903 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004904
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004905 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004906 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004907 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004908 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004909
4910 case ISD::Constant: return "Constant";
4911 case ISD::ConstantFP: return "ConstantFP";
4912 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004913 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004914 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004915 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004916 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00004917 case ISD::RETURNADDR: return "RETURNADDR";
4918 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004919 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004920 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00004921 case ISD::EHSELECTION: return "EHSELECTION";
4922 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004923 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00004924 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004925 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004926 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00004927 return Intrinsic::getName((Intrinsic::ID)IID);
4928 }
4929 case ISD::INTRINSIC_VOID:
4930 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004931 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004932 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004933 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004934
Chris Lattnerb2827b02006-03-19 00:52:58 +00004935 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004936 case ISD::TargetConstant: return "TargetConstant";
4937 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004938 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004939 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004940 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004941 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004942 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00004943 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004944
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004945 case ISD::CopyToReg: return "CopyToReg";
4946 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004947 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004948 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004949 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004950 case ISD::DBG_LABEL: return "dbg_label";
4951 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004952 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004953 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004954 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004955 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004956
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004957 // Unary operators
4958 case ISD::FABS: return "fabs";
4959 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004960 case ISD::FSQRT: return "fsqrt";
4961 case ISD::FSIN: return "fsin";
4962 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004963 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004964 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004965 case ISD::FTRUNC: return "ftrunc";
4966 case ISD::FFLOOR: return "ffloor";
4967 case ISD::FCEIL: return "fceil";
4968 case ISD::FRINT: return "frint";
4969 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004970
4971 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004972 case ISD::ADD: return "add";
4973 case ISD::SUB: return "sub";
4974 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004975 case ISD::MULHU: return "mulhu";
4976 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004977 case ISD::SDIV: return "sdiv";
4978 case ISD::UDIV: return "udiv";
4979 case ISD::SREM: return "srem";
4980 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004981 case ISD::SMUL_LOHI: return "smul_lohi";
4982 case ISD::UMUL_LOHI: return "umul_lohi";
4983 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00004984 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004985 case ISD::AND: return "and";
4986 case ISD::OR: return "or";
4987 case ISD::XOR: return "xor";
4988 case ISD::SHL: return "shl";
4989 case ISD::SRA: return "sra";
4990 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004991 case ISD::ROTL: return "rotl";
4992 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004993 case ISD::FADD: return "fadd";
4994 case ISD::FSUB: return "fsub";
4995 case ISD::FMUL: return "fmul";
4996 case ISD::FDIV: return "fdiv";
4997 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004998 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004999 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005000
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005001 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005002 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005003 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005004 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005005 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005006 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005007 case ISD::CONCAT_VECTORS: return "concat_vectors";
5008 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005009 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005010 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005011 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005012 case ISD::ADDC: return "addc";
5013 case ISD::ADDE: return "adde";
5014 case ISD::SUBC: return "subc";
5015 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005016 case ISD::SHL_PARTS: return "shl_parts";
5017 case ISD::SRA_PARTS: return "sra_parts";
5018 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005019
5020 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5021 case ISD::INSERT_SUBREG: return "insert_subreg";
5022
Chris Lattner7f644642005-04-28 21:44:03 +00005023 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005024 case ISD::SIGN_EXTEND: return "sign_extend";
5025 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005026 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005027 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005028 case ISD::TRUNCATE: return "truncate";
5029 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005030 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005031 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005032 case ISD::FP_EXTEND: return "fp_extend";
5033
5034 case ISD::SINT_TO_FP: return "sint_to_fp";
5035 case ISD::UINT_TO_FP: return "uint_to_fp";
5036 case ISD::FP_TO_SINT: return "fp_to_sint";
5037 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005038 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005039
5040 // Control flow instructions
5041 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005042 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005043 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005044 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005045 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005046 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005047 case ISD::CALLSEQ_START: return "callseq_start";
5048 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005049
5050 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005051 case ISD::LOAD: return "load";
5052 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005053 case ISD::VAARG: return "vaarg";
5054 case ISD::VACOPY: return "vacopy";
5055 case ISD::VAEND: return "vaend";
5056 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005057 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005058 case ISD::EXTRACT_ELEMENT: return "extract_element";
5059 case ISD::BUILD_PAIR: return "build_pair";
5060 case ISD::STACKSAVE: return "stacksave";
5061 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005062 case ISD::TRAP: return "trap";
5063
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005064 // Bit manipulation
5065 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005066 case ISD::CTPOP: return "ctpop";
5067 case ISD::CTTZ: return "cttz";
5068 case ISD::CTLZ: return "ctlz";
5069
Chris Lattner36ce6912005-11-29 06:21:05 +00005070 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005071 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005072 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005073
Duncan Sands36397f52007-07-27 12:58:54 +00005074 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005075 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005076
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005077 case ISD::CONDCODE:
5078 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005079 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005080 case ISD::SETOEQ: return "setoeq";
5081 case ISD::SETOGT: return "setogt";
5082 case ISD::SETOGE: return "setoge";
5083 case ISD::SETOLT: return "setolt";
5084 case ISD::SETOLE: return "setole";
5085 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005086
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005087 case ISD::SETO: return "seto";
5088 case ISD::SETUO: return "setuo";
5089 case ISD::SETUEQ: return "setue";
5090 case ISD::SETUGT: return "setugt";
5091 case ISD::SETUGE: return "setuge";
5092 case ISD::SETULT: return "setult";
5093 case ISD::SETULE: return "setule";
5094 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005095
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005096 case ISD::SETEQ: return "seteq";
5097 case ISD::SETGT: return "setgt";
5098 case ISD::SETGE: return "setge";
5099 case ISD::SETLT: return "setlt";
5100 case ISD::SETLE: return "setle";
5101 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005102 }
5103 }
5104}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005105
Evan Cheng144d8f02006-11-09 17:55:04 +00005106const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005107 switch (AM) {
5108 default:
5109 return "";
5110 case ISD::PRE_INC:
5111 return "<pre-inc>";
5112 case ISD::PRE_DEC:
5113 return "<pre-dec>";
5114 case ISD::POST_INC:
5115 return "<post-inc>";
5116 case ISD::POST_DEC:
5117 return "<post-dec>";
5118 }
5119}
5120
Duncan Sands276dcbd2008-03-21 09:14:45 +00005121std::string ISD::ArgFlagsTy::getArgFlagsString() {
5122 std::string S = "< ";
5123
5124 if (isZExt())
5125 S += "zext ";
5126 if (isSExt())
5127 S += "sext ";
5128 if (isInReg())
5129 S += "inreg ";
5130 if (isSRet())
5131 S += "sret ";
5132 if (isByVal())
5133 S += "byval ";
5134 if (isNest())
5135 S += "nest ";
5136 if (getByValAlign())
5137 S += "byval-align:" + utostr(getByValAlign()) + " ";
5138 if (getOrigAlign())
5139 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5140 if (getByValSize())
5141 S += "byval-size:" + utostr(getByValSize()) + " ";
5142 return S + ">";
5143}
5144
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005145void SDNode::dump() const { dump(0); }
5146void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005147 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005148 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005149}
5150
5151void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5152 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005153
5154 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005155 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005156 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005157 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005158 else
Chris Lattner944fac72008-08-23 22:23:09 +00005159 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005160 }
Chris Lattner944fac72008-08-23 22:23:09 +00005161 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005162
Chris Lattner944fac72008-08-23 22:23:09 +00005163 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005164 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005165 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005166 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005167 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005168 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005169 }
5170
Evan Chengce254432007-12-11 02:08:35 +00005171 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005172 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005173 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005174 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005175 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005176 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005177 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005178 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005179 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005180 }
Chris Lattner944fac72008-08-23 22:23:09 +00005181 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005182 }
5183
Chris Lattnerc3aae252005-01-07 07:46:32 +00005184 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005185 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005186 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005187 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005188 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005189 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005190 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005191 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005192 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005193 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005194 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005195 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005196 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005197 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005198 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005199 OS << '<';
5200 WriteAsOperand(OS, GADN->getGlobal());
5201 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005202 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005203 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005204 else
Chris Lattner944fac72008-08-23 22:23:09 +00005205 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005206 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005207 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005208 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005209 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005210 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005211 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005212 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005213 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005214 else
Chris Lattner944fac72008-08-23 22:23:09 +00005215 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005216 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005217 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005218 else
Chris Lattner944fac72008-08-23 22:23:09 +00005219 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005220 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005221 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005222 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5223 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005224 OS << LBB->getName() << " ";
5225 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005226 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005227 if (G && R->getReg() &&
5228 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005229 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005230 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005231 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005232 }
Bill Wendling056292f2008-09-16 21:48:12 +00005233 } else if (const ExternalSymbolSDNode *ES =
5234 dyn_cast<ExternalSymbolSDNode>(this)) {
5235 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005236 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5237 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005238 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005239 else
Chris Lattner944fac72008-08-23 22:23:09 +00005240 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005241 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5242 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005243 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005244 else
Chris Lattner944fac72008-08-23 22:23:09 +00005245 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005246 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005247 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005248 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005249 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005250 }
5251 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005252 const Value *SrcValue = LD->getSrcValue();
5253 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005254 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005255 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005256 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005257 else
Chris Lattner944fac72008-08-23 22:23:09 +00005258 OS << "null";
5259 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005260
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005261 bool doExt = true;
5262 switch (LD->getExtensionType()) {
5263 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005264 case ISD::EXTLOAD: OS << " <anyext "; break;
5265 case ISD::SEXTLOAD: OS << " <sext "; break;
5266 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005267 }
5268 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005269 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005270
Evan Cheng144d8f02006-11-09 17:55:04 +00005271 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005272 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005273 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005274 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005275 OS << " <volatile>";
5276 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005277 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005278 const Value *SrcValue = ST->getSrcValue();
5279 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005280 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005281 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005282 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005283 else
Chris Lattner944fac72008-08-23 22:23:09 +00005284 OS << "null";
5285 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005286
5287 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005288 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005289
5290 const char *AM = getIndexedModeName(ST->getAddressingMode());
5291 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005292 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005293 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005294 OS << " <volatile>";
5295 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005296 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5297 const Value *SrcValue = AT->getSrcValue();
5298 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005299 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005300 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005301 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005302 else
Chris Lattner944fac72008-08-23 22:23:09 +00005303 OS << "null";
5304 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005305 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005306 OS << " <volatile>";
5307 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005308 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005309}
5310
Chris Lattnerde202b32005-11-09 23:47:37 +00005311static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005312 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005313 if (N->getOperand(i).getNode()->hasOneUse())
5314 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005315 else
Bill Wendling832171c2006-12-07 20:04:42 +00005316 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005317 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005318
Chris Lattnerea946cd2005-01-09 20:38:33 +00005319
Bill Wendling832171c2006-12-07 20:04:42 +00005320 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005321 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005322}
5323
Chris Lattnerc3aae252005-01-07 07:46:32 +00005324void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005325 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005326
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005327 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5328 I != E; ++I) {
5329 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005330 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005331 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005332 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005333
Gabor Greifba36cb52008-08-28 21:40:38 +00005334 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005335
Bill Wendling832171c2006-12-07 20:04:42 +00005336 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005337}
5338
Evan Chengd6594ae2006-09-12 21:00:35 +00005339const Type *ConstantPoolSDNode::getType() const {
5340 if (isMachineConstantPoolEntry())
5341 return Val.MachineCPVal->getType();
5342 return Val.ConstVal->getType();
5343}