blob: 921d7b060b9e62097e2daf583f39e15278966ba2 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000032#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000034#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000035#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000037#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000038#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000039#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000040#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattner0b3e5252006-08-15 19:11:05 +000043/// makeVTList - Return an instance of the SDVTList struct initialized with the
44/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000045static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000046 SDVTList Res = {VTs, NumVTs};
47 return Res;
48}
49
Duncan Sands83ec4b62008-06-06 12:08:01 +000050static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
51 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000052 default: assert(0 && "Unknown FP format");
53 case MVT::f32: return &APFloat::IEEEsingle;
54 case MVT::f64: return &APFloat::IEEEdouble;
55 case MVT::f80: return &APFloat::x87DoubleExtended;
56 case MVT::f128: return &APFloat::IEEEquad;
57 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
58 }
59}
60
Chris Lattnerf8dc0612008-02-03 06:49:24 +000061SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000071bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000072 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000073}
74
Duncan Sands83ec4b62008-06-06 12:08:01 +000075bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000076 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000077 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000078
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000079 // PPC long double cannot be converted to any other type.
80 if (VT == MVT::ppcf128 ||
81 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000082 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +000099 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000140 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000196/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000197bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000199 if (N->getOpcode() == ISD::DBG_LABEL)
200 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000201 if (N->isMachineOpcode() &&
202 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000203 return true;
204 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000205}
206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
208/// when given the operation for (X op Y).
209ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
210 // To perform this operation, we just need to swap the L and G bits of the
211 // operation.
212 unsigned OldL = (Operation >> 2) & 1;
213 unsigned OldG = (Operation >> 1) & 1;
214 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
215 (OldL << 1) | // New G bit
216 (OldG << 2)); // New L bit.
217}
218
219/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
220/// 'op' is a valid SetCC operation.
221ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
222 unsigned Operation = Op;
223 if (isInteger)
224 Operation ^= 7; // Flip L, G, E bits, but not U.
225 else
226 Operation ^= 15; // Flip all of the condition bits.
227 if (Operation > ISD::SETTRUE2)
228 Operation &= ~8; // Don't let N and U bits get set.
229 return ISD::CondCode(Operation);
230}
231
232
233/// isSignedOp - For an integer comparison, return 1 if the comparison is a
234/// signed operation and 2 if the result is an unsigned comparison. Return zero
235/// if the operation does not depend on the sign of the input (setne and seteq).
236static int isSignedOp(ISD::CondCode Opcode) {
237 switch (Opcode) {
238 default: assert(0 && "Illegal integer setcc operation!");
239 case ISD::SETEQ:
240 case ISD::SETNE: return 0;
241 case ISD::SETLT:
242 case ISD::SETLE:
243 case ISD::SETGT:
244 case ISD::SETGE: return 1;
245 case ISD::SETULT:
246 case ISD::SETULE:
247 case ISD::SETUGT:
248 case ISD::SETUGE: return 2;
249 }
250}
251
252/// getSetCCOrOperation - Return the result of a logical OR between different
253/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
254/// returns SETCC_INVALID if it is not possible to represent the resultant
255/// comparison.
256ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
257 bool isInteger) {
258 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
259 // Cannot fold a signed integer setcc with an unsigned integer setcc.
260 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 // If the N and U bits get set then the resultant comparison DOES suddenly
265 // care about orderedness, and is true when ordered.
266 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000267 Op &= ~16; // Clear the U bit if the N bit is set.
268
269 // Canonicalize illegal integer setcc's.
270 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
271 Op = ISD::SETNE;
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Op);
274}
275
276/// getSetCCAndOperation - Return the result of a logical AND between different
277/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
278/// function returns zero if it is not possible to represent the resultant
279/// comparison.
280ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
281 bool isInteger) {
282 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
283 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285
286 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000287 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
288
289 // Canonicalize illegal integer setcc's.
290 if (isInteger) {
291 switch (Result) {
292 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000293 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000294 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
296 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
297 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 }
299 }
300
301 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302}
303
Chris Lattnerb48da392005-01-23 04:39:44 +0000304const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000305 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000306}
307
Jim Laskey58b968b2005-08-17 20:08:02 +0000308//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000309// SDNode Profile Support
310//===----------------------------------------------------------------------===//
311
Jim Laskeydef69b92006-10-27 23:52:51 +0000312/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
313///
Jim Laskey583bd472006-10-27 23:46:08 +0000314static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
315 ID.AddInteger(OpC);
316}
317
318/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
319/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000320static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000321 ID.AddPointer(VTList.VTs);
322}
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000327 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000328 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000329 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000330 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 }
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
336static void AddNodeIDOperands(FoldingSetNodeID &ID,
337 const SDUse *Ops, unsigned NumOps) {
338 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000339 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000341 }
342}
343
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID,
345 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000346 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000347 AddNodeIDOpcode(ID, OpC);
348 AddNodeIDValueTypes(ID, VTList);
349 AddNodeIDOperands(ID, OpList, N);
350}
351
Roman Levenstein9cac5252008-04-16 16:15:27 +0000352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
354/// data.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, N->getOpcode());
357 // Add the return value info.
358 AddNodeIDValueTypes(ID, N->getVTList());
359 // Add the operand info.
360 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
361
362 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 switch (N->getOpcode()) {
364 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 case ISD::ARG_FLAGS:
366 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
367 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetConstant:
369 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000370 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 break;
372 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000374 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000378 case ISD::GlobalAddress:
379 case ISD::TargetGlobalTLSAddress:
380 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000381 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 ID.AddPointer(GA->getGlobal());
383 ID.AddInteger(GA->getOffset());
384 break;
385 }
386 case ISD::BasicBlock:
387 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
388 break;
389 case ISD::Register:
390 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
391 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000392 case ISD::DBG_STOPPOINT: {
393 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
394 ID.AddInteger(DSP->getLine());
395 ID.AddInteger(DSP->getColumn());
396 ID.AddPointer(DSP->getCompileUnit());
397 break;
398 }
Dan Gohman69de1932008-02-06 22:27:42 +0000399 case ISD::SRCVALUE:
400 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
401 break;
402 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000403 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000404 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000405 break;
406 }
407 case ISD::FrameIndex:
408 case ISD::TargetFrameIndex:
409 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
410 break;
411 case ISD::JumpTable:
412 case ISD::TargetJumpTable:
413 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
414 break;
415 case ISD::ConstantPool:
416 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000417 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 ID.AddInteger(CP->getAlignment());
419 ID.AddInteger(CP->getOffset());
420 if (CP->isMachineConstantPoolEntry())
421 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
422 else
423 ID.AddPointer(CP->getConstVal());
424 break;
425 }
426 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000427 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAddressingMode());
429 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000430 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 break;
433 }
434 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 ID.AddInteger(ST->getAddressingMode());
437 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Dale Johannesene00a8a22008-08-28 02:44:49 +0000442 case ISD::ATOMIC_CMP_SWAP_8:
443 case ISD::ATOMIC_SWAP_8:
444 case ISD::ATOMIC_LOAD_ADD_8:
445 case ISD::ATOMIC_LOAD_SUB_8:
446 case ISD::ATOMIC_LOAD_AND_8:
447 case ISD::ATOMIC_LOAD_OR_8:
448 case ISD::ATOMIC_LOAD_XOR_8:
449 case ISD::ATOMIC_LOAD_NAND_8:
450 case ISD::ATOMIC_LOAD_MIN_8:
451 case ISD::ATOMIC_LOAD_MAX_8:
452 case ISD::ATOMIC_LOAD_UMIN_8:
453 case ISD::ATOMIC_LOAD_UMAX_8:
454 case ISD::ATOMIC_CMP_SWAP_16:
455 case ISD::ATOMIC_SWAP_16:
456 case ISD::ATOMIC_LOAD_ADD_16:
457 case ISD::ATOMIC_LOAD_SUB_16:
458 case ISD::ATOMIC_LOAD_AND_16:
459 case ISD::ATOMIC_LOAD_OR_16:
460 case ISD::ATOMIC_LOAD_XOR_16:
461 case ISD::ATOMIC_LOAD_NAND_16:
462 case ISD::ATOMIC_LOAD_MIN_16:
463 case ISD::ATOMIC_LOAD_MAX_16:
464 case ISD::ATOMIC_LOAD_UMIN_16:
465 case ISD::ATOMIC_LOAD_UMAX_16:
466 case ISD::ATOMIC_CMP_SWAP_32:
467 case ISD::ATOMIC_SWAP_32:
468 case ISD::ATOMIC_LOAD_ADD_32:
469 case ISD::ATOMIC_LOAD_SUB_32:
470 case ISD::ATOMIC_LOAD_AND_32:
471 case ISD::ATOMIC_LOAD_OR_32:
472 case ISD::ATOMIC_LOAD_XOR_32:
473 case ISD::ATOMIC_LOAD_NAND_32:
474 case ISD::ATOMIC_LOAD_MIN_32:
475 case ISD::ATOMIC_LOAD_MAX_32:
476 case ISD::ATOMIC_LOAD_UMIN_32:
477 case ISD::ATOMIC_LOAD_UMAX_32:
478 case ISD::ATOMIC_CMP_SWAP_64:
479 case ISD::ATOMIC_SWAP_64:
480 case ISD::ATOMIC_LOAD_ADD_64:
481 case ISD::ATOMIC_LOAD_SUB_64:
482 case ISD::ATOMIC_LOAD_AND_64:
483 case ISD::ATOMIC_LOAD_OR_64:
484 case ISD::ATOMIC_LOAD_XOR_64:
485 case ISD::ATOMIC_LOAD_NAND_64:
486 case ISD::ATOMIC_LOAD_MIN_64:
487 case ISD::ATOMIC_LOAD_MAX_64:
488 case ISD::ATOMIC_LOAD_UMIN_64:
489 case ISD::ATOMIC_LOAD_UMAX_64: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000490 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
491 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000492 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000493 }
Mon P Wang28873102008-06-25 08:15:39 +0000494 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000495}
496
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000497/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
498/// the CSE map that carries both alignment and volatility information.
499///
500static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
501 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
502}
503
Jim Laskey583bd472006-10-27 23:46:08 +0000504//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000505// SelectionDAG Class
506//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000507
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000509/// SelectionDAG.
510void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000511 // Create a dummy node (which is not added to allnodes), that adds a reference
512 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000513 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000514
Chris Lattner190a4182006-08-04 17:45:20 +0000515 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000516
Chris Lattner190a4182006-08-04 17:45:20 +0000517 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000518 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000519 if (I->use_empty())
520 DeadNodes.push_back(I);
521
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000522 RemoveDeadNodes(DeadNodes);
523
524 // If the root changed (e.g. it was a dead load, update the root).
525 setRoot(Dummy.getValue());
526}
527
528/// RemoveDeadNodes - This method deletes the unreachable nodes in the
529/// given list, and any nodes that become unreachable as a result.
530void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
531 DAGUpdateListener *UpdateListener) {
532
Chris Lattner190a4182006-08-04 17:45:20 +0000533 // Process the worklist, deleting the nodes and adding their uses to the
534 // worklist.
535 while (!DeadNodes.empty()) {
536 SDNode *N = DeadNodes.back();
537 DeadNodes.pop_back();
538
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000539 if (UpdateListener)
540 UpdateListener->NodeDeleted(N, 0);
541
Chris Lattner190a4182006-08-04 17:45:20 +0000542 // Take the node out of the appropriate CSE map.
543 RemoveNodeFromCSEMaps(N);
544
545 // Next, brutally remove the operand list. This is safe to do, as there are
546 // no cycles in the graph.
547 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000548 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000549 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000550
551 // Now that we removed this operand, see if there are no uses of it left.
552 if (Operand->use_empty())
553 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000554 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000555 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000556 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000557 }
Chris Lattner190a4182006-08-04 17:45:20 +0000558 N->OperandList = 0;
559 N->NumOperands = 0;
560
561 // Finally, remove N itself.
Dan Gohman11467282008-08-26 01:44:34 +0000562 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerf469cb62005-11-08 18:52:27 +0000563 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564}
565
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000566void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000567 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000568 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000569}
570
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000571void SelectionDAG::DeleteNode(SDNode *N) {
572 assert(N->use_empty() && "Cannot delete a node that is not dead!");
573
574 // First take this out of the appropriate CSE map.
575 RemoveNodeFromCSEMaps(N);
576
Chris Lattner1e111c72005-09-07 05:37:01 +0000577 // Finally, remove uses due to operands of this node, remove from the
578 // AllNodes list, and delete the node.
579 DeleteNodeNotInCSEMaps(N);
580}
581
582void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
583
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000584 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000585 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000586 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf350b272008-08-23 02:25:05 +0000587 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000588 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000589
Dan Gohman11467282008-08-26 01:44:34 +0000590 assert(N != AllNodes.begin());
591 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000592}
593
Chris Lattner149c58c2005-08-16 18:17:10 +0000594/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
595/// correspond to it. This is useful when we're about to delete or repurpose
596/// the node. We don't want future request for structurally identical nodes
597/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000598bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000599 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000600 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000601 case ISD::EntryToken:
602 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000603 return false;
604 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000605 case ISD::CONDCODE:
606 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
607 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000608 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000609 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
610 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000612 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000613 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000614 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000615 Erased =
616 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000617 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000618 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000619 MVT VT = cast<VTSDNode>(N)->getVT();
620 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000621 Erased = ExtendedValueTypeNodes.erase(VT);
622 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000623 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
624 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000625 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000626 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000627 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000628 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000629 // Remove it from the CSE Map.
630 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000631 break;
632 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000633#ifndef NDEBUG
634 // Verify that the node was actually in one of the CSE maps, unless it has a
635 // flag result (which cannot be CSE'd) or is one of the special cases that are
636 // not subject to CSE.
637 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Dan Gohman095cc292008-09-13 01:54:27 +0000638 !N->isMachineOpcode() &&
639 N->getOpcode() != ISD::CALL &&
Evan Cheng71e86852008-07-08 20:06:39 +0000640 N->getOpcode() != ISD::DBG_LABEL &&
641 N->getOpcode() != ISD::DBG_STOPPOINT &&
642 N->getOpcode() != ISD::EH_LABEL &&
643 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000644 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000645 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000646 assert(0 && "Node is not in map!");
647 }
648#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000649 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650}
651
Chris Lattner8b8749f2005-08-17 19:00:20 +0000652/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
653/// has been taken out and modified in some way. If the specified node already
654/// exists in the CSE maps, do not modify the maps, but return the existing node
655/// instead. If it doesn't exist, add it and return null.
656///
657SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
658 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000659
660 if (N->getValueType(0) == MVT::Flag)
661 return 0; // Never CSE anything that produces a flag.
662
663 switch (N->getOpcode()) {
664 default: break;
Dan Gohman095cc292008-09-13 01:54:27 +0000665 case ISD::CALL:
Evan Cheng71e86852008-07-08 20:06:39 +0000666 case ISD::HANDLENODE:
667 case ISD::DBG_LABEL:
668 case ISD::DBG_STOPPOINT:
669 case ISD::EH_LABEL:
670 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000671 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000672 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000673
Chris Lattner9f8cc692005-12-19 22:21:21 +0000674 // Check that remaining values produced are not flags.
675 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
676 if (N->getValueType(i) == MVT::Flag)
677 return 0; // Never CSE anything that produces a flag.
678
Chris Lattnera5682852006-08-07 23:03:03 +0000679 SDNode *New = CSEMap.GetOrInsertNode(N);
680 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000681 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000682}
683
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000684/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
685/// were replaced with those specified. If this node is never memoized,
686/// return null, otherwise return a pointer to the slot it would take. If a
687/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000688SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000689 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000690 if (N->getValueType(0) == MVT::Flag)
691 return 0; // Never CSE anything that produces a flag.
692
693 switch (N->getOpcode()) {
694 default: break;
695 case ISD::HANDLENODE:
696 case ISD::DBG_LABEL:
697 case ISD::DBG_STOPPOINT:
698 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000700 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000701
702 // Check that remaining values produced are not flags.
703 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
704 if (N->getValueType(i) == MVT::Flag)
705 return 0; // Never CSE anything that produces a flag.
706
Dan Gohman475871a2008-07-27 21:46:04 +0000707 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000708 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000709 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000710 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000711}
712
713/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
714/// were replaced with those specified. If this node is never memoized,
715/// return null, otherwise return a pointer to the slot it would take. If a
716/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000717SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000718 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000719 void *&InsertPos) {
720 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000721
722 // Check that remaining values produced are not flags.
723 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
724 if (N->getValueType(i) == MVT::Flag)
725 return 0; // Never CSE anything that produces a flag.
726
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000728 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000729 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000730 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
731}
732
733
734/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
735/// were replaced with those specified. If this node is never memoized,
736/// return null, otherwise return a pointer to the slot it would take. If a
737/// node already exists with these operands, the slot will be non-null.
738SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000739 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000740 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000741 if (N->getValueType(0) == MVT::Flag)
742 return 0; // Never CSE anything that produces a flag.
743
744 switch (N->getOpcode()) {
745 default: break;
746 case ISD::HANDLENODE:
747 case ISD::DBG_LABEL:
748 case ISD::DBG_STOPPOINT:
749 case ISD::EH_LABEL:
750 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000751 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000752 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000753
754 // Check that remaining values produced are not flags.
755 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
756 if (N->getValueType(i) == MVT::Flag)
757 return 0; // Never CSE anything that produces a flag.
758
Jim Laskey583bd472006-10-27 23:46:08 +0000759 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000760 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000761
Evan Cheng9629aba2006-10-11 01:47:58 +0000762 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
763 ID.AddInteger(LD->getAddressingMode());
764 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000765 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000766 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000767 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
768 ID.AddInteger(ST->getAddressingMode());
769 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000770 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000771 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000772 }
Jim Laskey583bd472006-10-27 23:46:08 +0000773
Chris Lattnera5682852006-08-07 23:03:03 +0000774 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000775}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000776
Duncan Sandsd038e042008-07-21 10:20:31 +0000777/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
778void SelectionDAG::VerifyNode(SDNode *N) {
779 switch (N->getOpcode()) {
780 default:
781 break;
782 case ISD::BUILD_VECTOR: {
783 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
784 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
785 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
786 "Wrong number of BUILD_VECTOR operands!");
787 MVT EltVT = N->getValueType(0).getVectorElementType();
788 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000789 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000790 "Wrong BUILD_VECTOR operand type!");
791 break;
792 }
793 }
794}
795
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000796/// getMVTAlignment - Compute the default alignment value for the
797/// given type.
798///
799unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
800 const Type *Ty = VT == MVT::iPTR ?
801 PointerType::get(Type::Int8Ty, 0) :
802 VT.getTypeForMVT();
803
804 return TLI.getTargetData()->getABITypeAlignment(Ty);
805}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000806
Dan Gohman7c3234c2008-08-27 23:52:12 +0000807SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
808 : TLI(tli), FLI(fli),
809 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000810 Root(getEntryNode()) {
811 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000812}
813
Dan Gohman7c3234c2008-08-27 23:52:12 +0000814void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi) {
815 MF = &mf;
816 MMI = mmi;
817}
818
Chris Lattner78ec3112003-08-11 14:57:33 +0000819SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000820 allnodes_clear();
821}
822
823void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000824 assert(&*AllNodes.begin() == &EntryNode);
825 AllNodes.remove(AllNodes.begin());
Chris Lattnerde202b32005-11-09 23:47:37 +0000826 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000827 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000828 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000829 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000830 delete [] N->OperandList;
Dan Gohman11467282008-08-26 01:44:34 +0000831 NodeAllocator.Deallocate(N);
Chris Lattner65113b22005-11-08 22:07:03 +0000832 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000833}
834
Dan Gohman7c3234c2008-08-27 23:52:12 +0000835void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000836 allnodes_clear();
837 OperandAllocator.Reset();
838 CSEMap.clear();
839
840 ExtendedValueTypeNodes.clear();
841 ExternalSymbols.clear();
842 TargetExternalSymbols.clear();
843 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
844 static_cast<CondCodeSDNode*>(0));
845 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
846 static_cast<SDNode*>(0));
847
848 EntryNode.Uses = 0;
849 AllNodes.push_back(&EntryNode);
850 Root = getEntryNode();
851}
852
Dan Gohman475871a2008-07-27 21:46:04 +0000853SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000854 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000855 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000856 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000857 return getNode(ISD::AND, Op.getValueType(), Op,
858 getConstant(Imm, Op.getValueType()));
859}
860
Dan Gohman475871a2008-07-27 21:46:04 +0000861SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000862 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000864}
865
Dan Gohman475871a2008-07-27 21:46:04 +0000866SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000867 return getConstant(*ConstantInt::get(Val), VT, isT);
868}
869
870SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000872
Evan Cheng0ff39b32008-06-30 07:31:25 +0000873 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000874 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000875 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000876
877 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000879 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000880 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000881 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000882 SDNode *N = NULL;
883 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000884 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000885 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000886 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000887 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000888 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000889 CSEMap.InsertNode(N, IP);
890 AllNodes.push_back(N);
891 }
892
Dan Gohman475871a2008-07-27 21:46:04 +0000893 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000896 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000897 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
898 }
899 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000900}
901
Dan Gohman475871a2008-07-27 21:46:04 +0000902SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000903 return getConstant(Val, TLI.getPointerTy(), isTarget);
904}
905
906
Dan Gohman475871a2008-07-27 21:46:04 +0000907SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000908 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
909}
910
911SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000913
Duncan Sands83ec4b62008-06-06 12:08:01 +0000914 MVT EltVT =
915 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916
Chris Lattnerd8658612005-02-17 20:17:32 +0000917 // Do the map lookup using the actual bit pattern for the floating point
918 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
919 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000920 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000921 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000922 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000923 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000924 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000925 SDNode *N = NULL;
926 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000927 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000928 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000929 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000930 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000931 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000932 CSEMap.InsertNode(N, IP);
933 AllNodes.push_back(N);
934 }
935
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000937 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000939 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000940 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
941 }
942 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943}
944
Dan Gohman475871a2008-07-27 21:46:04 +0000945SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 MVT EltVT =
947 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000948 if (EltVT==MVT::f32)
949 return getConstantFP(APFloat((float)Val), VT, isTarget);
950 else
951 return getConstantFP(APFloat(Val), VT, isTarget);
952}
953
Dan Gohman475871a2008-07-27 21:46:04 +0000954SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
955 MVT VT, int Offset,
956 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000957 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000958
959 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
960 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000961 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000962 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000963 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000964 }
965
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000966 if (GVar && GVar->isThreadLocal())
967 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
968 else
969 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000970
Jim Laskey583bd472006-10-27 23:46:08 +0000971 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000972 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000973 ID.AddPointer(GV);
974 ID.AddInteger(Offset);
975 void *IP = 0;
976 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000977 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000978 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000979 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000980 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000982 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983}
984
Dan Gohman475871a2008-07-27 21:46:04 +0000985SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000986 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000987 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000988 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000989 ID.AddInteger(FI);
990 void *IP = 0;
991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000992 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000993 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000994 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000995 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000996 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000997 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000998}
999
Dan Gohman475871a2008-07-27 21:46:04 +00001000SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001001 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001002 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001003 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001004 ID.AddInteger(JTI);
1005 void *IP = 0;
1006 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001007 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001008 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001009 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001010 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001011 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001012 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001013}
1014
Dan Gohman475871a2008-07-27 21:46:04 +00001015SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1016 unsigned Alignment, int Offset,
1017 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001018 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001019 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001020 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001021 ID.AddInteger(Alignment);
1022 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001023 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001024 void *IP = 0;
1025 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001026 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001027 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001028 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001029 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001030 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001031 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001032}
1033
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034
Dan Gohman475871a2008-07-27 21:46:04 +00001035SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1036 unsigned Alignment, int Offset,
1037 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001038 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001039 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001040 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001041 ID.AddInteger(Alignment);
1042 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001043 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001044 void *IP = 0;
1045 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001046 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001047 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001048 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001049 CSEMap.InsertNode(N, IP);
1050 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001051 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001052}
1053
1054
Dan Gohman475871a2008-07-27 21:46:04 +00001055SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001056 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001057 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001058 ID.AddPointer(MBB);
1059 void *IP = 0;
1060 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001061 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001062 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001063 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001064 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001066 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067}
1068
Dan Gohman475871a2008-07-27 21:46:04 +00001069SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001070 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001071 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001072 ID.AddInteger(Flags.getRawBits());
1073 void *IP = 0;
1074 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001075 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001076 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001077 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001078 CSEMap.InsertNode(N, IP);
1079 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001080 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001081}
1082
Dan Gohman475871a2008-07-27 21:46:04 +00001083SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1085 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001086
Duncan Sands83ec4b62008-06-06 12:08:01 +00001087 SDNode *&N = VT.isExtended() ?
1088 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001089
Dan Gohman475871a2008-07-27 21:46:04 +00001090 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001091 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001092 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001093 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001094 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001095}
1096
Dan Gohman475871a2008-07-27 21:46:04 +00001097SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001099 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001100 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001101 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001102 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001103 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001104}
1105
Dan Gohman475871a2008-07-27 21:46:04 +00001106SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001107 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001108 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001109 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001110 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001112 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113}
1114
Dan Gohman475871a2008-07-27 21:46:04 +00001115SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001116 if ((unsigned)Cond >= CondCodeNodes.size())
1117 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118
Chris Lattner079a27a2005-08-09 20:40:02 +00001119 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001120 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001121 new (N) CondCodeSDNode(Cond);
1122 CondCodeNodes[Cond] = N;
1123 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001124 }
Dan Gohman475871a2008-07-27 21:46:04 +00001125 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001126}
1127
Dan Gohman475871a2008-07-27 21:46:04 +00001128SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001129 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001130 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001131 ID.AddInteger(RegNo);
1132 void *IP = 0;
1133 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001134 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001135 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001136 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001137 CSEMap.InsertNode(N, IP);
1138 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001139 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001140}
1141
Dan Gohman475871a2008-07-27 21:46:04 +00001142SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001143 unsigned Line, unsigned Col,
1144 const CompileUnitDesc *CU) {
1145 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001146 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001147 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001148 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001149}
1150
Dan Gohman475871a2008-07-27 21:46:04 +00001151SDValue SelectionDAG::getLabel(unsigned Opcode,
1152 SDValue Root,
1153 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001154 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001155 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001156 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1157 ID.AddInteger(LabelID);
1158 void *IP = 0;
1159 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001160 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001161 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001162 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001163 CSEMap.InsertNode(N, IP);
1164 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001165 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001166}
1167
Dan Gohman475871a2008-07-27 21:46:04 +00001168SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001169 assert((!V || isa<PointerType>(V->getType())) &&
1170 "SrcValue is not a pointer?");
1171
Jim Laskey583bd472006-10-27 23:46:08 +00001172 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001173 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001174 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001175
Chris Lattner61b09412006-08-11 21:01:22 +00001176 void *IP = 0;
1177 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001178 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001179
Dan Gohmanfed90b62008-07-28 21:51:04 +00001180 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001181 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001182 CSEMap.InsertNode(N, IP);
1183 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001184 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001185}
1186
Dan Gohman475871a2008-07-27 21:46:04 +00001187SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001188 const Value *v = MO.getValue();
1189 assert((!v || isa<PointerType>(v->getType())) &&
1190 "SrcValue is not a pointer?");
1191
1192 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001193 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001194 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001195
1196 void *IP = 0;
1197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001198 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001199
Dan Gohmanfed90b62008-07-28 21:51:04 +00001200 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001201 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001202 CSEMap.InsertNode(N, IP);
1203 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001204 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001205}
1206
Chris Lattner37ce9df2007-10-15 17:47:20 +00001207/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1208/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001209SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001210 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001211 unsigned ByteSize = VT.getSizeInBits()/8;
1212 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001213 unsigned StackAlign =
1214 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1215
Chris Lattner37ce9df2007-10-15 17:47:20 +00001216 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1217 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1218}
1219
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1221 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001222 // These setcc operations always fold.
1223 switch (Cond) {
1224 default: break;
1225 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001226 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001228 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001229
1230 case ISD::SETOEQ:
1231 case ISD::SETOGT:
1232 case ISD::SETOGE:
1233 case ISD::SETOLT:
1234 case ISD::SETOLE:
1235 case ISD::SETONE:
1236 case ISD::SETO:
1237 case ISD::SETUO:
1238 case ISD::SETUEQ:
1239 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001240 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001241 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001243
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001245 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001246 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001247 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001248
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 switch (Cond) {
1250 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001251 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1252 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001253 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1254 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1255 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1256 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1257 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1258 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1259 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1260 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001262 }
Chris Lattner67255a12005-04-07 18:14:58 +00001263 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001264 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1265 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001266 // No compile time operations on this type yet.
1267 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001268 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001269
1270 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001271 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001272 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001273 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1274 return getNode(ISD::UNDEF, VT);
1275 // fall through
1276 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1277 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1278 return getNode(ISD::UNDEF, VT);
1279 // fall through
1280 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001281 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001282 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1283 return getNode(ISD::UNDEF, VT);
1284 // fall through
1285 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1286 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1287 return getNode(ISD::UNDEF, VT);
1288 // fall through
1289 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1290 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1291 return getNode(ISD::UNDEF, VT);
1292 // fall through
1293 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001294 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001295 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1296 return getNode(ISD::UNDEF, VT);
1297 // fall through
1298 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001299 R==APFloat::cmpEqual, VT);
1300 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1301 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1302 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1303 R==APFloat::cmpEqual, VT);
1304 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1305 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1306 R==APFloat::cmpLessThan, VT);
1307 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1308 R==APFloat::cmpUnordered, VT);
1309 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1310 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 }
1312 } else {
1313 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001314 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001316 }
1317
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001318 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001319 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320}
1321
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001322/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1323/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001324bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001325 unsigned BitWidth = Op.getValueSizeInBits();
1326 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1327}
1328
Dan Gohmanea859be2007-06-22 14:59:07 +00001329/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1330/// this predicate to simplify operations downstream. Mask is known to be zero
1331/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001332bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001334 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001335 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1336 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1337 return (KnownZero & Mask) == Mask;
1338}
1339
1340/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1341/// known to be either zero or one and return them in the KnownZero/KnownOne
1342/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1343/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001344void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001345 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001346 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001347 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001348 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001349 "Mask size mismatches value type size!");
1350
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001351 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001352 if (Depth == 6 || Mask == 0)
1353 return; // Limit search depth.
1354
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001355 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001356
1357 switch (Op.getOpcode()) {
1358 case ISD::Constant:
1359 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001360 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001361 KnownZero = ~KnownOne & Mask;
1362 return;
1363 case ISD::AND:
1364 // If either the LHS or the RHS are Zero, the result is zero.
1365 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001366 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1367 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001368 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1369 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1370
1371 // Output known-1 bits are only known if set in both the LHS & RHS.
1372 KnownOne &= KnownOne2;
1373 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1374 KnownZero |= KnownZero2;
1375 return;
1376 case ISD::OR:
1377 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001378 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1379 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001380 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1381 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1382
1383 // Output known-0 bits are only known if clear in both the LHS & RHS.
1384 KnownZero &= KnownZero2;
1385 // Output known-1 are known to be set if set in either the LHS | RHS.
1386 KnownOne |= KnownOne2;
1387 return;
1388 case ISD::XOR: {
1389 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1390 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1391 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1392 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1393
1394 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001395 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001396 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1397 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1398 KnownZero = KnownZeroOut;
1399 return;
1400 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001401 case ISD::MUL: {
1402 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1403 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1404 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1406 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1407
1408 // If low bits are zero in either operand, output low known-0 bits.
1409 // Also compute a conserative estimate for high known-0 bits.
1410 // More trickiness is possible, but this is sufficient for the
1411 // interesting case of alignment computation.
1412 KnownOne.clear();
1413 unsigned TrailZ = KnownZero.countTrailingOnes() +
1414 KnownZero2.countTrailingOnes();
1415 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001416 KnownZero2.countLeadingOnes(),
1417 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001418
1419 TrailZ = std::min(TrailZ, BitWidth);
1420 LeadZ = std::min(LeadZ, BitWidth);
1421 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1422 APInt::getHighBitsSet(BitWidth, LeadZ);
1423 KnownZero &= Mask;
1424 return;
1425 }
1426 case ISD::UDIV: {
1427 // For the purposes of computing leading zeros we can conservatively
1428 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001429 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001430 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1431 ComputeMaskedBits(Op.getOperand(0),
1432 AllOnes, KnownZero2, KnownOne2, Depth+1);
1433 unsigned LeadZ = KnownZero2.countLeadingOnes();
1434
1435 KnownOne2.clear();
1436 KnownZero2.clear();
1437 ComputeMaskedBits(Op.getOperand(1),
1438 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001439 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1440 if (RHSUnknownLeadingOnes != BitWidth)
1441 LeadZ = std::min(BitWidth,
1442 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001443
1444 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1445 return;
1446 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001447 case ISD::SELECT:
1448 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1449 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1450 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1451 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1452
1453 // Only known if known in both the LHS and RHS.
1454 KnownOne &= KnownOne2;
1455 KnownZero &= KnownZero2;
1456 return;
1457 case ISD::SELECT_CC:
1458 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1459 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1460 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1461 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1462
1463 // Only known if known in both the LHS and RHS.
1464 KnownOne &= KnownOne2;
1465 KnownZero &= KnownZero2;
1466 return;
1467 case ISD::SETCC:
1468 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001469 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1470 BitWidth > 1)
1471 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001472 return;
1473 case ISD::SHL:
1474 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1475 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001476 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001477
1478 // If the shift count is an invalid immediate, don't do anything.
1479 if (ShAmt >= BitWidth)
1480 return;
1481
1482 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001483 KnownZero, KnownOne, Depth+1);
1484 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001485 KnownZero <<= ShAmt;
1486 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001487 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001488 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001489 }
1490 return;
1491 case ISD::SRL:
1492 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1493 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001494 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001495
Dan Gohmand4cf9922008-02-26 18:50:50 +00001496 // If the shift count is an invalid immediate, don't do anything.
1497 if (ShAmt >= BitWidth)
1498 return;
1499
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001500 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001501 KnownZero, KnownOne, Depth+1);
1502 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001503 KnownZero = KnownZero.lshr(ShAmt);
1504 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001505
Dan Gohman72d2fd52008-02-13 22:43:25 +00001506 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001507 KnownZero |= HighBits; // High bits known zero.
1508 }
1509 return;
1510 case ISD::SRA:
1511 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001512 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001513
Dan Gohmand4cf9922008-02-26 18:50:50 +00001514 // If the shift count is an invalid immediate, don't do anything.
1515 if (ShAmt >= BitWidth)
1516 return;
1517
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001518 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001519 // If any of the demanded bits are produced by the sign extension, we also
1520 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001521 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1522 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001523 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001524
1525 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1526 Depth+1);
1527 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001528 KnownZero = KnownZero.lshr(ShAmt);
1529 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001530
1531 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001532 APInt SignBit = APInt::getSignBit(BitWidth);
1533 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001534
Dan Gohmanca93a432008-02-20 16:30:17 +00001535 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001536 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001537 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001538 KnownOne |= HighBits; // New bits are known one.
1539 }
1540 }
1541 return;
1542 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001543 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1544 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001545
1546 // Sign extension. Compute the demanded bits in the result that are not
1547 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001548 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001549
Dan Gohman977a76f2008-02-13 22:28:48 +00001550 APInt InSignBit = APInt::getSignBit(EBits);
1551 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001552
1553 // If the sign extended bits are demanded, we know that the sign
1554 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001555 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001556 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001557 InputDemandedBits |= InSignBit;
1558
1559 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1560 KnownZero, KnownOne, Depth+1);
1561 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1562
1563 // If the sign bit of the input is known set or clear, then we know the
1564 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001565 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001566 KnownZero |= NewBits;
1567 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001568 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001569 KnownOne |= NewBits;
1570 KnownZero &= ~NewBits;
1571 } else { // Input sign bit unknown
1572 KnownZero &= ~NewBits;
1573 KnownOne &= ~NewBits;
1574 }
1575 return;
1576 }
1577 case ISD::CTTZ:
1578 case ISD::CTLZ:
1579 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001580 unsigned LowBits = Log2_32(BitWidth)+1;
1581 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001582 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001583 return;
1584 }
1585 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001587 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001588 MVT VT = LD->getMemoryVT();
1589 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001590 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 }
1592 return;
1593 }
1594 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001595 MVT InVT = Op.getOperand(0).getValueType();
1596 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001597 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1598 APInt InMask = Mask;
1599 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001600 KnownZero.trunc(InBits);
1601 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001602 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001603 KnownZero.zext(BitWidth);
1604 KnownOne.zext(BitWidth);
1605 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001606 return;
1607 }
1608 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001609 MVT InVT = Op.getOperand(0).getValueType();
1610 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001611 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001612 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1613 APInt InMask = Mask;
1614 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001615
1616 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001617 // bit is demanded. Temporarily set this bit in the mask for our callee.
1618 if (NewBits.getBoolValue())
1619 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001620
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001621 KnownZero.trunc(InBits);
1622 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001623 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1624
1625 // Note if the sign bit is known to be zero or one.
1626 bool SignBitKnownZero = KnownZero.isNegative();
1627 bool SignBitKnownOne = KnownOne.isNegative();
1628 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1629 "Sign bit can't be known to be both zero and one!");
1630
1631 // If the sign bit wasn't actually demanded by our caller, we don't
1632 // want it set in the KnownZero and KnownOne result values. Reset the
1633 // mask and reapply it to the result values.
1634 InMask = Mask;
1635 InMask.trunc(InBits);
1636 KnownZero &= InMask;
1637 KnownOne &= InMask;
1638
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001639 KnownZero.zext(BitWidth);
1640 KnownOne.zext(BitWidth);
1641
Dan Gohman977a76f2008-02-13 22:28:48 +00001642 // If the sign bit is known zero or one, the top bits match.
1643 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001645 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001646 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001647 return;
1648 }
1649 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001650 MVT InVT = Op.getOperand(0).getValueType();
1651 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001652 APInt InMask = Mask;
1653 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001654 KnownZero.trunc(InBits);
1655 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001656 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001657 KnownZero.zext(BitWidth);
1658 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001659 return;
1660 }
1661 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001662 MVT InVT = Op.getOperand(0).getValueType();
1663 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001664 APInt InMask = Mask;
1665 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001666 KnownZero.zext(InBits);
1667 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001668 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001670 KnownZero.trunc(BitWidth);
1671 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001672 break;
1673 }
1674 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001675 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1676 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001677 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1678 KnownOne, Depth+1);
1679 KnownZero |= (~InMask) & Mask;
1680 return;
1681 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001682 case ISD::FGETSIGN:
1683 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001684 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001685 return;
1686
Dan Gohman23e8b712008-04-28 17:02:21 +00001687 case ISD::SUB: {
1688 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1689 // We know that the top bits of C-X are clear if X contains less bits
1690 // than C (i.e. no wrap-around can happen). For example, 20-X is
1691 // positive if we can prove that X is >= 0 and < 16.
1692 if (CLHS->getAPIntValue().isNonNegative()) {
1693 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1694 // NLZ can't be BitWidth with no sign bit
1695 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1696 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1697 Depth+1);
1698
1699 // If all of the MaskV bits are known to be zero, then we know the
1700 // output top bits are zero, because we now know that the output is
1701 // from [0-C].
1702 if ((KnownZero2 & MaskV) == MaskV) {
1703 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1704 // Top bits known zero.
1705 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1706 }
1707 }
1708 }
1709 }
1710 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001711 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001712 // Output known-0 bits are known if clear or set in both the low clear bits
1713 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1714 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001715 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1716 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1717 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1718 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1719
1720 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1721 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1722 KnownZeroOut = std::min(KnownZeroOut,
1723 KnownZero2.countTrailingOnes());
1724
1725 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 return;
1727 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001728 case ISD::SREM:
1729 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001730 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001731 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001732 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001733 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1734 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001735
Dan Gohmana60832b2008-08-13 23:12:35 +00001736 // If the sign bit of the first operand is zero, the sign bit of
1737 // the result is zero. If the first operand has no one bits below
1738 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001739 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1740 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001741
Dan Gohman23e8b712008-04-28 17:02:21 +00001742 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001743
1744 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 }
1746 }
1747 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001748 case ISD::UREM: {
1749 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001750 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001751 if (RA.isPowerOf2()) {
1752 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001753 APInt Mask2 = LowBits & Mask;
1754 KnownZero |= ~LowBits & Mask;
1755 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1756 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1757 break;
1758 }
1759 }
1760
1761 // Since the result is less than or equal to either operand, any leading
1762 // zero bits in either operand must also exist in the result.
1763 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1764 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1765 Depth+1);
1766 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1767 Depth+1);
1768
1769 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1770 KnownZero2.countLeadingOnes());
1771 KnownOne.clear();
1772 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1773 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001774 }
1775 default:
1776 // Allow the target to implement this method for its nodes.
1777 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1778 case ISD::INTRINSIC_WO_CHAIN:
1779 case ISD::INTRINSIC_W_CHAIN:
1780 case ISD::INTRINSIC_VOID:
1781 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1782 }
1783 return;
1784 }
1785}
1786
1787/// ComputeNumSignBits - Return the number of times the sign bit of the
1788/// register is replicated into the other bits. We know that at least 1 bit
1789/// is always equal to the sign bit (itself), but other cases can give us
1790/// information. For example, immediately after an "SRA X, 2", we know that
1791/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001792unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001793 MVT VT = Op.getValueType();
1794 assert(VT.isInteger() && "Invalid VT!");
1795 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001796 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001797 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001798
1799 if (Depth == 6)
1800 return 1; // Limit search depth.
1801
1802 switch (Op.getOpcode()) {
1803 default: break;
1804 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001805 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001806 return VTBits-Tmp+1;
1807 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001808 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001809 return VTBits-Tmp;
1810
1811 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001812 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1813 // If negative, return # leading ones.
1814 if (Val.isNegative())
1815 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001816
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001817 // Return # leading zeros.
1818 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 }
1820
1821 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001822 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001823 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1824
1825 case ISD::SIGN_EXTEND_INREG:
1826 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001827 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001828 Tmp = VTBits-Tmp+1;
1829
1830 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1831 return std::max(Tmp, Tmp2);
1832
1833 case ISD::SRA:
1834 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1835 // SRA X, C -> adds C sign bits.
1836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001837 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 if (Tmp > VTBits) Tmp = VTBits;
1839 }
1840 return Tmp;
1841 case ISD::SHL:
1842 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1843 // shl destroys sign bits.
1844 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001845 if (C->getZExtValue() >= VTBits || // Bad shift.
1846 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1847 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001848 }
1849 break;
1850 case ISD::AND:
1851 case ISD::OR:
1852 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001853 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001854 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001855 if (Tmp != 1) {
1856 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1857 FirstAnswer = std::min(Tmp, Tmp2);
1858 // We computed what we know about the sign bits as our first
1859 // answer. Now proceed to the generic code that uses
1860 // ComputeMaskedBits, and pick whichever answer is better.
1861 }
1862 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001863
1864 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001865 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001867 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001868 return std::min(Tmp, Tmp2);
1869
1870 case ISD::SETCC:
1871 // If setcc returns 0/-1, all bits are sign bits.
1872 if (TLI.getSetCCResultContents() ==
1873 TargetLowering::ZeroOrNegativeOneSetCCResult)
1874 return VTBits;
1875 break;
1876 case ISD::ROTL:
1877 case ISD::ROTR:
1878 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001879 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001880
1881 // Handle rotate right by N like a rotate left by 32-N.
1882 if (Op.getOpcode() == ISD::ROTR)
1883 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1884
1885 // If we aren't rotating out all of the known-in sign bits, return the
1886 // number that are left. This handles rotl(sext(x), 1) for example.
1887 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1888 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1889 }
1890 break;
1891 case ISD::ADD:
1892 // Add can have at most one carry bit. Thus we know that the output
1893 // is, at worst, one more bit than the inputs.
1894 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1895 if (Tmp == 1) return 1; // Early out.
1896
1897 // Special case decrementing a value (ADD X, -1):
1898 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1899 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001900 APInt KnownZero, KnownOne;
1901 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001902 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1903
1904 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1905 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001906 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001907 return VTBits;
1908
1909 // If we are subtracting one from a positive number, there is no carry
1910 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001911 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001912 return Tmp;
1913 }
1914
1915 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1916 if (Tmp2 == 1) return 1;
1917 return std::min(Tmp, Tmp2)-1;
1918 break;
1919
1920 case ISD::SUB:
1921 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1922 if (Tmp2 == 1) return 1;
1923
1924 // Handle NEG.
1925 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001926 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001927 APInt KnownZero, KnownOne;
1928 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001929 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1930 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1931 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001932 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 return VTBits;
1934
1935 // If the input is known to be positive (the sign bit is known clear),
1936 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001937 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001938 return Tmp2;
1939
1940 // Otherwise, we treat this like a SUB.
1941 }
1942
1943 // Sub can have at most one carry bit. Thus we know that the output
1944 // is, at worst, one more bit than the inputs.
1945 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1946 if (Tmp == 1) return 1; // Early out.
1947 return std::min(Tmp, Tmp2)-1;
1948 break;
1949 case ISD::TRUNCATE:
1950 // FIXME: it's tricky to do anything useful for this, but it is an important
1951 // case for targets like X86.
1952 break;
1953 }
1954
1955 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1956 if (Op.getOpcode() == ISD::LOAD) {
1957 LoadSDNode *LD = cast<LoadSDNode>(Op);
1958 unsigned ExtType = LD->getExtensionType();
1959 switch (ExtType) {
1960 default: break;
1961 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 return VTBits-Tmp+1;
1964 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001965 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001966 return VTBits-Tmp;
1967 }
1968 }
1969
1970 // Allow the target to implement this method for its nodes.
1971 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1972 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1973 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1974 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1975 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001976 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001977 }
1978
1979 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1980 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001981 APInt KnownZero, KnownOne;
1982 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1984
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001985 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001986 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001987 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001988 Mask = KnownOne;
1989 } else {
1990 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001991 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001992 }
1993
1994 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1995 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001996 Mask = ~Mask;
1997 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001998 // Return # leading zeros. We use 'min' here in case Val was zero before
1999 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002000 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002001}
2002
Chris Lattner51dabfb2006-10-14 00:41:01 +00002003
Dan Gohman475871a2008-07-27 21:46:04 +00002004bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002005 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2006 if (!GA) return false;
2007 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2008 if (!GV) return false;
2009 MachineModuleInfo *MMI = getMachineModuleInfo();
2010 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
2011}
2012
2013
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002014/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2015/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002016SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002017 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002018 SDValue PermMask = N->getOperand(2);
2019 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002020 if (Idx.getOpcode() == ISD::UNDEF)
2021 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002022 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002023 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002025 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002026
2027 if (V.getOpcode() == ISD::BIT_CONVERT) {
2028 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002029 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002030 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002031 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002032 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002033 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002035 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002036 return V.getOperand(Index);
2037 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002038 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002039 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002040}
2041
2042
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002044///
Dan Gohman475871a2008-07-27 21:46:04 +00002045SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002046 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002047 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002048 void *IP = 0;
2049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002050 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002051 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002052 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002053 CSEMap.InsertNode(N, IP);
2054
2055 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002056#ifndef NDEBUG
2057 VerifyNode(N);
2058#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002059 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002060}
2061
Dan Gohman475871a2008-07-27 21:46:04 +00002062SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002063 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002065 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002066 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002067 switch (Opcode) {
2068 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002069 case ISD::SIGN_EXTEND:
2070 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002071 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002072 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002073 case ISD::TRUNCATE:
2074 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002075 case ISD::UINT_TO_FP:
2076 case ISD::SINT_TO_FP: {
2077 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002078 // No compile time operations on this type.
2079 if (VT==MVT::ppcf128)
2080 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002081 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2082 (void)apf.convertFromAPInt(Val,
2083 Opcode==ISD::SINT_TO_FP,
2084 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002085 return getConstantFP(apf, VT);
2086 }
Chris Lattner94683772005-12-23 05:30:37 +00002087 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002088 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002089 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002090 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002091 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002092 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002093 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002094 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002095 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002096 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002097 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002098 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002099 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002100 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002101 }
2102 }
2103
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002104 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002105 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002106 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002107 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002108 switch (Opcode) {
2109 case ISD::FNEG:
2110 V.changeSign();
2111 return getConstantFP(V, VT);
2112 case ISD::FABS:
2113 V.clearSign();
2114 return getConstantFP(V, VT);
2115 case ISD::FP_ROUND:
2116 case ISD::FP_EXTEND:
2117 // This can return overflow, underflow, or inexact; we don't care.
2118 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002119 (void)V.convert(*MVTToAPFloatSemantics(VT),
2120 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002121 return getConstantFP(V, VT);
2122 case ISD::FP_TO_SINT:
2123 case ISD::FP_TO_UINT: {
2124 integerPart x;
2125 assert(integerPartWidth >= 64);
2126 // FIXME need to be more flexible about rounding mode.
2127 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2128 Opcode==ISD::FP_TO_SINT,
2129 APFloat::rmTowardZero);
2130 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2131 break;
2132 return getConstant(x, VT);
2133 }
2134 case ISD::BIT_CONVERT:
2135 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2136 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2137 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2138 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002139 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002140 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002141 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002142 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143
Gabor Greifba36cb52008-08-28 21:40:38 +00002144 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002145 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002146 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002147 case ISD::CONCAT_VECTORS:
2148 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002149 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002150 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002151 assert(VT.isFloatingPoint() &&
2152 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002153 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002154 if (Operand.getOpcode() == ISD::UNDEF)
2155 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002156 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002157 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002158 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002159 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002160 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002161 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002162 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002164 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002165 break;
2166 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002167 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002168 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002169 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002170 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002171 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002172 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002173 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002174 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002175 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002176 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002177 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002178 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002179 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002180 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002181 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2182 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002183 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002184 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002185 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002186 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002187 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002188 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002189 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002190 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002191 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002193 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2194 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002195 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2197 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2198 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2199 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002200 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002201 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002202 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002203 break;
Chris Lattner35481892005-12-23 00:16:34 +00002204 case ISD::BIT_CONVERT:
2205 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002207 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002208 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002209 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2210 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002211 if (OpOpcode == ISD::UNDEF)
2212 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002213 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002214 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002215 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2216 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002217 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002218 if (OpOpcode == ISD::UNDEF)
2219 return getNode(ISD::UNDEF, VT);
2220 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2221 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2222 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2223 Operand.getConstantOperandVal(1) == 0 &&
2224 Operand.getOperand(0).getValueType() == VT)
2225 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002226 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002227 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002228 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002229 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2230 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002231 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002232 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002233 break;
2234 case ISD::FABS:
2235 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002237 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002238 }
2239
Chris Lattner43247a12005-08-25 19:12:10 +00002240 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002241 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002242 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002243 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002245 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002246 void *IP = 0;
2247 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002248 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002249 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002250 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002251 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002252 } else {
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 Lattner43247a12005-08-25 19:12:10 +00002255 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002256
Chris Lattnerc3aae252005-01-07 07:46:32 +00002257 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002258#ifndef NDEBUG
2259 VerifyNode(N);
2260#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002261 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002262}
2263
Dan Gohman475871a2008-07-27 21:46:04 +00002264SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2265 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002266 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2267 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002268 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002269 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002270 case ISD::TokenFactor:
2271 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2272 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002273 // Fold trivial token factors.
2274 if (N1.getOpcode() == ISD::EntryToken) return N2;
2275 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002276 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002277 case ISD::CONCAT_VECTORS:
2278 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2279 // one big BUILD_VECTOR.
2280 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2281 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2283 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002284 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2285 }
2286 break;
Chris Lattner76365122005-01-16 02:23:22 +00002287 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002288 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002289 N1.getValueType() == VT && "Binary operator types must match!");
2290 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2291 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002292 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002293 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002294 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2295 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002296 break;
Chris Lattner76365122005-01-16 02:23:22 +00002297 case ISD::OR:
2298 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002299 case ISD::ADD:
2300 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002301 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002302 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002303 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2304 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002305 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002306 return N1;
2307 break;
Chris Lattner76365122005-01-16 02:23:22 +00002308 case ISD::UDIV:
2309 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002310 case ISD::MULHU:
2311 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002312 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002313 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002314 case ISD::MUL:
2315 case ISD::SDIV:
2316 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002317 case ISD::FADD:
2318 case ISD::FSUB:
2319 case ISD::FMUL:
2320 case ISD::FDIV:
2321 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002322 assert(N1.getValueType() == N2.getValueType() &&
2323 N1.getValueType() == VT && "Binary operator types must match!");
2324 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002325 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2326 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002327 N1.getValueType().isFloatingPoint() &&
2328 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002329 "Invalid FCOPYSIGN!");
2330 break;
Chris Lattner76365122005-01-16 02:23:22 +00002331 case ISD::SHL:
2332 case ISD::SRA:
2333 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002334 case ISD::ROTL:
2335 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002336 assert(VT == N1.getValueType() &&
2337 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002338 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002339 "Shifts only work on integers");
2340
2341 // Always fold shifts of i1 values so the code generator doesn't need to
2342 // handle them. Since we know the size of the shift has to be less than the
2343 // size of the value, the shift/rotate count is guaranteed to be zero.
2344 if (VT == MVT::i1)
2345 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002346 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002347 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002348 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002349 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002350 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002351 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002352 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002353 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002354 break;
2355 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002356 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 assert(VT.isFloatingPoint() &&
2358 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002359 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002360 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002361 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002362 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002363 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002364 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002365 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002366 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002367 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002369 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002370 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002371 break;
2372 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002373 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002374 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002375 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002376 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002377 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002378 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002379 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002380
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002381 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002382 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002384 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002385 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002386 return getConstant(Val, VT);
2387 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002388 break;
2389 }
2390 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002391 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2392 if (N1.getOpcode() == ISD::UNDEF)
2393 return getNode(ISD::UNDEF, VT);
2394
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002395 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2396 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002397 if (N2C &&
2398 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002399 N1.getNumOperands() > 0) {
2400 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002401 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002402 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002403 N1.getOperand(N2C->getZExtValue() / Factor),
2404 getConstant(N2C->getZExtValue() % Factor,
2405 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002406 }
2407
2408 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2409 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002410 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002411 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002412
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002413 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2414 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002415 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2416 if (N1.getOperand(2) == N2)
2417 return N1.getOperand(1);
2418 else
2419 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2420 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002421 break;
2422 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002423 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002424 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2425 (N1.getValueType().isInteger() == VT.isInteger()) &&
2426 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002427
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002428 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2429 // 64-bit integers into 32-bit parts. Instead of building the extract of
2430 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2431 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002432 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002433
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002434 // EXTRACT_ELEMENT of a constant int is also very common.
2435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002436 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002437 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002438 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2439 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002440 }
2441 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002442 case ISD::EXTRACT_SUBVECTOR:
2443 if (N1.getValueType() == VT) // Trivial extraction.
2444 return N1;
2445 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002446 }
2447
2448 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002449 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002450 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002451 switch (Opcode) {
2452 case ISD::ADD: return getConstant(C1 + C2, VT);
2453 case ISD::SUB: return getConstant(C1 - C2, VT);
2454 case ISD::MUL: return getConstant(C1 * C2, VT);
2455 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002456 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002457 break;
2458 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002459 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460 break;
2461 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002462 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463 break;
2464 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002465 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466 break;
2467 case ISD::AND : return getConstant(C1 & C2, VT);
2468 case ISD::OR : return getConstant(C1 | C2, VT);
2469 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002470 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002471 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2472 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2473 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2474 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002475 default: break;
2476 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002477 } else { // Cannonicalize constant to RHS if commutative
2478 if (isCommutativeBinOp(Opcode)) {
2479 std::swap(N1C, N2C);
2480 std::swap(N1, N2);
2481 }
2482 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002483 }
2484
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002485 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002486 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2487 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002488 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002489 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2490 // Cannonicalize constant to RHS if commutative
2491 std::swap(N1CFP, N2CFP);
2492 std::swap(N1, N2);
2493 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002494 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2495 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002496 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002497 case ISD::FADD:
2498 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002499 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002500 return getConstantFP(V1, VT);
2501 break;
2502 case ISD::FSUB:
2503 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2504 if (s!=APFloat::opInvalidOp)
2505 return getConstantFP(V1, VT);
2506 break;
2507 case ISD::FMUL:
2508 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2509 if (s!=APFloat::opInvalidOp)
2510 return getConstantFP(V1, VT);
2511 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002512 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002513 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2514 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2515 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002516 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002517 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002518 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2519 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2520 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002521 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002522 case ISD::FCOPYSIGN:
2523 V1.copySign(V2);
2524 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002525 default: break;
2526 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002527 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002528 }
Chris Lattner62b57722006-04-20 05:39:12 +00002529
2530 // Canonicalize an UNDEF to the RHS, even over a constant.
2531 if (N1.getOpcode() == ISD::UNDEF) {
2532 if (isCommutativeBinOp(Opcode)) {
2533 std::swap(N1, N2);
2534 } else {
2535 switch (Opcode) {
2536 case ISD::FP_ROUND_INREG:
2537 case ISD::SIGN_EXTEND_INREG:
2538 case ISD::SUB:
2539 case ISD::FSUB:
2540 case ISD::FDIV:
2541 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002542 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002543 return N1; // fold op(undef, arg2) -> undef
2544 case ISD::UDIV:
2545 case ISD::SDIV:
2546 case ISD::UREM:
2547 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002548 case ISD::SRL:
2549 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002550 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002551 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2552 // For vectors, we can't easily build an all zero vector, just return
2553 // the LHS.
2554 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002555 }
2556 }
2557 }
2558
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002559 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002560 if (N2.getOpcode() == ISD::UNDEF) {
2561 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002562 case ISD::XOR:
2563 if (N1.getOpcode() == ISD::UNDEF)
2564 // Handle undef ^ undef -> 0 special case. This is a common
2565 // idiom (misuse).
2566 return getConstant(0, VT);
2567 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002568 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002569 case ISD::ADDC:
2570 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002571 case ISD::SUB:
2572 case ISD::FADD:
2573 case ISD::FSUB:
2574 case ISD::FMUL:
2575 case ISD::FDIV:
2576 case ISD::FREM:
2577 case ISD::UDIV:
2578 case ISD::SDIV:
2579 case ISD::UREM:
2580 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002581 return N2; // fold op(arg1, undef) -> undef
2582 case ISD::MUL:
2583 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002584 case ISD::SRL:
2585 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002586 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002587 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2588 // For vectors, we can't easily build an all zero vector, just return
2589 // the LHS.
2590 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002591 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002592 if (!VT.isVector())
2593 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002594 // For vectors, we can't easily build an all one vector, just return
2595 // the LHS.
2596 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002597 case ISD::SRA:
2598 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002599 }
2600 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002601
Chris Lattner27e9b412005-05-11 18:57:39 +00002602 // Memoize this node if possible.
2603 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002604 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002605 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002606 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002607 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002608 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002609 void *IP = 0;
2610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002611 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002612 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002613 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002614 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002615 } else {
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 Lattner27e9b412005-05-11 18:57:39 +00002618 }
2619
Chris Lattnerc3aae252005-01-07 07:46:32 +00002620 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002621#ifndef NDEBUG
2622 VerifyNode(N);
2623#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002624 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002625}
2626
Dan Gohman475871a2008-07-27 21:46:04 +00002627SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2628 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002629 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002630 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2631 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002632 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002633 case ISD::CONCAT_VECTORS:
2634 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2635 // one big BUILD_VECTOR.
2636 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2637 N2.getOpcode() == ISD::BUILD_VECTOR &&
2638 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002639 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2640 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2641 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002642 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2643 }
2644 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002645 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002646 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002647 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002648 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002649 break;
2650 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002651 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002652 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002653 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002654 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002655 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002656 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002657 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002658
2659 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002660 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002661 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002662 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002663 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002664 return getNode(ISD::BR, MVT::Other, N1, N3);
2665 else
2666 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002667 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002668 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002669 case ISD::VECTOR_SHUFFLE:
2670 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002671 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002672 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002674 "Illegal VECTOR_SHUFFLE node!");
2675 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002676 case ISD::BIT_CONVERT:
2677 // Fold bit_convert nodes from a type to themselves.
2678 if (N1.getValueType() == VT)
2679 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002680 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002681 }
2682
Chris Lattner43247a12005-08-25 19:12:10 +00002683 // Memoize node if it doesn't produce a flag.
2684 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002685 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002686 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002687 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002688 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002689 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002690 void *IP = 0;
2691 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002692 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002693 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002694 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002695 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002696 } else {
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 Lattner43247a12005-08-25 19:12:10 +00002699 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002700 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002701#ifndef NDEBUG
2702 VerifyNode(N);
2703#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002704 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002705}
2706
Dan Gohman475871a2008-07-27 21:46:04 +00002707SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2708 SDValue N1, SDValue N2, SDValue N3,
2709 SDValue N4) {
2710 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002711 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002712}
2713
Dan Gohman475871a2008-07-27 21:46:04 +00002714SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2715 SDValue N1, SDValue N2, SDValue N3,
2716 SDValue N4, SDValue N5) {
2717 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002718 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002719}
2720
Dan Gohman707e0182008-04-12 04:36:06 +00002721/// getMemsetValue - Vectorized representation of the memset value
2722/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002723static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002724 unsigned NumBits = VT.isVector() ?
2725 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002726 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002727 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002728 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002729 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002730 Val = (Val << Shift) | Val;
2731 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002732 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002733 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002734 return DAG.getConstant(Val, VT);
2735 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002736 }
Evan Chengf0df0312008-05-15 08:39:06 +00002737
2738 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2739 unsigned Shift = 8;
2740 for (unsigned i = NumBits; i > 8; i >>= 1) {
2741 Value = DAG.getNode(ISD::OR, VT,
2742 DAG.getNode(ISD::SHL, VT, Value,
2743 DAG.getConstant(Shift, MVT::i8)), Value);
2744 Shift <<= 1;
2745 }
2746
2747 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002748}
2749
Dan Gohman707e0182008-04-12 04:36:06 +00002750/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2751/// used when a memcpy is turned into a memset when the source is a constant
2752/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002753static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002754 const TargetLowering &TLI,
2755 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002756 // Handle vector with all elements zero.
2757 if (Str.empty()) {
2758 if (VT.isInteger())
2759 return DAG.getConstant(0, VT);
2760 unsigned NumElts = VT.getVectorNumElements();
2761 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2762 return DAG.getNode(ISD::BIT_CONVERT, VT,
2763 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2764 }
2765
Duncan Sands83ec4b62008-06-06 12:08:01 +00002766 assert(!VT.isVector() && "Can't handle vector type here!");
2767 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002768 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002769 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002770 if (TLI.isLittleEndian())
2771 Offset = Offset + MSB - 1;
2772 for (unsigned i = 0; i != MSB; ++i) {
2773 Val = (Val << 8) | (unsigned char)Str[Offset];
2774 Offset += TLI.isLittleEndian() ? -1 : 1;
2775 }
2776 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002777}
2778
Dan Gohman707e0182008-04-12 04:36:06 +00002779/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002780///
Dan Gohman475871a2008-07-27 21:46:04 +00002781static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002782 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002783 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002784 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2785}
2786
Evan Chengf0df0312008-05-15 08:39:06 +00002787/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2788///
Dan Gohman475871a2008-07-27 21:46:04 +00002789static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002790 unsigned SrcDelta = 0;
2791 GlobalAddressSDNode *G = NULL;
2792 if (Src.getOpcode() == ISD::GlobalAddress)
2793 G = cast<GlobalAddressSDNode>(Src);
2794 else if (Src.getOpcode() == ISD::ADD &&
2795 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2796 Src.getOperand(1).getOpcode() == ISD::Constant) {
2797 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002798 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002799 }
2800 if (!G)
2801 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002802
Evan Chengf0df0312008-05-15 08:39:06 +00002803 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002804 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2805 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002806
Evan Chengf0df0312008-05-15 08:39:06 +00002807 return false;
2808}
Dan Gohman707e0182008-04-12 04:36:06 +00002809
Evan Chengf0df0312008-05-15 08:39:06 +00002810/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2811/// to replace the memset / memcpy is below the threshold. It also returns the
2812/// types of the sequence of memory ops to perform memset / memcpy.
2813static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002815 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002816 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002817 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002818 SelectionDAG &DAG,
2819 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002820 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002821 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002822 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002823 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002824 if (VT != MVT::iAny) {
2825 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002826 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002827 // If source is a string constant, this will require an unaligned load.
2828 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2829 if (Dst.getOpcode() != ISD::FrameIndex) {
2830 // Can't change destination alignment. It requires a unaligned store.
2831 if (AllowUnalign)
2832 VT = MVT::iAny;
2833 } else {
2834 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2835 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2836 if (MFI->isFixedObjectIndex(FI)) {
2837 // Can't change destination alignment. It requires a unaligned store.
2838 if (AllowUnalign)
2839 VT = MVT::iAny;
2840 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002841 // Give the stack frame object a larger alignment if needed.
2842 if (MFI->getObjectAlignment(FI) < NewAlign)
2843 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002844 Align = NewAlign;
2845 }
2846 }
2847 }
2848 }
2849
2850 if (VT == MVT::iAny) {
2851 if (AllowUnalign) {
2852 VT = MVT::i64;
2853 } else {
2854 switch (Align & 7) {
2855 case 0: VT = MVT::i64; break;
2856 case 4: VT = MVT::i32; break;
2857 case 2: VT = MVT::i16; break;
2858 default: VT = MVT::i8; break;
2859 }
2860 }
2861
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002863 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002864 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2865 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002866
Duncan Sands8e4eb092008-06-08 20:54:56 +00002867 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002868 VT = LVT;
2869 }
Dan Gohman707e0182008-04-12 04:36:06 +00002870
2871 unsigned NumMemOps = 0;
2872 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002874 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002875 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002876 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002877 VT = MVT::i64;
2878 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002879 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2880 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002881 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002883 VTSize >>= 1;
2884 }
Dan Gohman707e0182008-04-12 04:36:06 +00002885 }
Dan Gohman707e0182008-04-12 04:36:06 +00002886
2887 if (++NumMemOps > Limit)
2888 return false;
2889 MemOps.push_back(VT);
2890 Size -= VTSize;
2891 }
2892
2893 return true;
2894}
2895
Dan Gohman475871a2008-07-27 21:46:04 +00002896static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2897 SDValue Chain, SDValue Dst,
2898 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002899 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002900 const Value *DstSV, uint64_t DstSVOff,
2901 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002902 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2903
Dan Gohman21323f32008-05-29 19:42:22 +00002904 // Expand memcpy to a series of load and store ops if the size operand falls
2905 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002906 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002907 uint64_t Limit = -1;
2908 if (!AlwaysInline)
2909 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002910 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002911 std::string Str;
2912 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002913 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002914 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002915 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002916
Dan Gohman707e0182008-04-12 04:36:06 +00002917
Evan Cheng0ff39b32008-06-30 07:31:25 +00002918 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002919 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002920 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002921 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002922 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002923 MVT VT = MemOps[i];
2924 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002925 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002926
Evan Cheng0ff39b32008-06-30 07:31:25 +00002927 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002928 // It's unlikely a store of a vector immediate can be done in a single
2929 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002930 // We also handle store a vector with all zero's.
2931 // FIXME: Handle other cases where store of vector immediate is done in
2932 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002933 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002934 Store = DAG.getStore(Chain, Value,
2935 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002936 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002937 } else {
2938 Value = DAG.getLoad(VT, Chain,
2939 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002940 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002941 Store = DAG.getStore(Chain, Value,
2942 getMemBasePlusOffset(Dst, DstOff, DAG),
2943 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002944 }
2945 OutChains.push_back(Store);
2946 SrcOff += VTSize;
2947 DstOff += VTSize;
2948 }
2949
2950 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2951 &OutChains[0], OutChains.size());
2952}
2953
Dan Gohman475871a2008-07-27 21:46:04 +00002954static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2955 SDValue Chain, SDValue Dst,
2956 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002957 unsigned Align, bool AlwaysInline,
2958 const Value *DstSV, uint64_t DstSVOff,
2959 const Value *SrcSV, uint64_t SrcSVOff){
2960 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2961
2962 // Expand memmove to a series of load and store ops if the size operand falls
2963 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002964 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002965 uint64_t Limit = -1;
2966 if (!AlwaysInline)
2967 Limit = TLI.getMaxStoresPerMemmove();
2968 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002969 std::string Str;
2970 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002971 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002972 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002973 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002974
Dan Gohman21323f32008-05-29 19:42:22 +00002975 uint64_t SrcOff = 0, DstOff = 0;
2976
Dan Gohman475871a2008-07-27 21:46:04 +00002977 SmallVector<SDValue, 8> LoadValues;
2978 SmallVector<SDValue, 8> LoadChains;
2979 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002980 unsigned NumMemOps = MemOps.size();
2981 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002982 MVT VT = MemOps[i];
2983 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002984 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002985
2986 Value = DAG.getLoad(VT, Chain,
2987 getMemBasePlusOffset(Src, SrcOff, DAG),
2988 SrcSV, SrcSVOff + SrcOff, false, Align);
2989 LoadValues.push_back(Value);
2990 LoadChains.push_back(Value.getValue(1));
2991 SrcOff += VTSize;
2992 }
2993 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2994 &LoadChains[0], LoadChains.size());
2995 OutChains.clear();
2996 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002997 MVT VT = MemOps[i];
2998 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002999 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003000
3001 Store = DAG.getStore(Chain, LoadValues[i],
3002 getMemBasePlusOffset(Dst, DstOff, DAG),
3003 DstSV, DstSVOff + DstOff, false, DstAlign);
3004 OutChains.push_back(Store);
3005 DstOff += VTSize;
3006 }
3007
3008 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3009 &OutChains[0], OutChains.size());
3010}
3011
Dan Gohman475871a2008-07-27 21:46:04 +00003012static SDValue getMemsetStores(SelectionDAG &DAG,
3013 SDValue Chain, SDValue Dst,
3014 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003015 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003016 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003017 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3018
3019 // Expand memset to a series of load/store ops if the size operand
3020 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003021 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003022 std::string Str;
3023 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003024 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003025 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003026 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003027
Dan Gohman475871a2008-07-27 21:46:04 +00003028 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003029 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003030
3031 unsigned NumMemOps = MemOps.size();
3032 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003033 MVT VT = MemOps[i];
3034 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003035 SDValue Value = getMemsetValue(Src, VT, DAG);
3036 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00003037 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003038 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003039 OutChains.push_back(Store);
3040 DstOff += VTSize;
3041 }
3042
3043 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3044 &OutChains[0], OutChains.size());
3045}
3046
Dan Gohman475871a2008-07-27 21:46:04 +00003047SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3048 SDValue Src, SDValue Size,
3049 unsigned Align, bool AlwaysInline,
3050 const Value *DstSV, uint64_t DstSVOff,
3051 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003052
3053 // Check to see if we should lower the memcpy to loads and stores first.
3054 // For cases within the target-specified limits, this is the best choice.
3055 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3056 if (ConstantSize) {
3057 // Memcpy with size zero? Just return the original chain.
3058 if (ConstantSize->isNullValue())
3059 return Chain;
3060
Dan Gohman475871a2008-07-27 21:46:04 +00003061 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003062 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3063 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003064 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003065 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003066 return Result;
3067 }
3068
3069 // Then check to see if we should lower the memcpy with target-specific
3070 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003071 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003072 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3073 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003074 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003075 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003076 return Result;
3077
3078 // If we really need inline code and the target declined to provide it,
3079 // use a (potentially long) sequence of loads and stores.
3080 if (AlwaysInline) {
3081 assert(ConstantSize && "AlwaysInline requires a constant size!");
3082 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003083 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003084 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003085 }
3086
3087 // Emit a library call.
3088 TargetLowering::ArgListTy Args;
3089 TargetLowering::ArgListEntry Entry;
3090 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3091 Entry.Node = Dst; Args.push_back(Entry);
3092 Entry.Node = Src; Args.push_back(Entry);
3093 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003094 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003095 TLI.LowerCallTo(Chain, Type::VoidTy,
3096 false, false, false, CallingConv::C, false,
3097 getExternalSymbol("memcpy", TLI.getPointerTy()),
3098 Args, *this);
3099 return CallResult.second;
3100}
3101
Dan Gohman475871a2008-07-27 21:46:04 +00003102SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3103 SDValue Src, SDValue Size,
3104 unsigned Align,
3105 const Value *DstSV, uint64_t DstSVOff,
3106 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003107
Dan Gohman21323f32008-05-29 19:42:22 +00003108 // Check to see if we should lower the memmove to loads and stores first.
3109 // For cases within the target-specified limits, this is the best choice.
3110 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3111 if (ConstantSize) {
3112 // Memmove with size zero? Just return the original chain.
3113 if (ConstantSize->isNullValue())
3114 return Chain;
3115
Dan Gohman475871a2008-07-27 21:46:04 +00003116 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003117 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3118 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003119 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003120 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003121 return Result;
3122 }
Dan Gohman707e0182008-04-12 04:36:06 +00003123
3124 // Then check to see if we should lower the memmove with target-specific
3125 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003126 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003127 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003128 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003129 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003130 return Result;
3131
3132 // Emit a library call.
3133 TargetLowering::ArgListTy Args;
3134 TargetLowering::ArgListEntry Entry;
3135 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3136 Entry.Node = Dst; Args.push_back(Entry);
3137 Entry.Node = Src; Args.push_back(Entry);
3138 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003139 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003140 TLI.LowerCallTo(Chain, Type::VoidTy,
3141 false, false, false, CallingConv::C, false,
3142 getExternalSymbol("memmove", TLI.getPointerTy()),
3143 Args, *this);
3144 return CallResult.second;
3145}
3146
Dan Gohman475871a2008-07-27 21:46:04 +00003147SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3148 SDValue Src, SDValue Size,
3149 unsigned Align,
3150 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003151
3152 // Check to see if we should lower the memset to stores first.
3153 // For cases within the target-specified limits, this is the best choice.
3154 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3155 if (ConstantSize) {
3156 // Memset with size zero? Just return the original chain.
3157 if (ConstantSize->isNullValue())
3158 return Chain;
3159
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003161 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3162 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003163 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003164 return Result;
3165 }
3166
3167 // Then check to see if we should lower the memset with target-specific
3168 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003169 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003170 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003171 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003172 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003173 return Result;
3174
3175 // Emit a library call.
3176 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3177 TargetLowering::ArgListTy Args;
3178 TargetLowering::ArgListEntry Entry;
3179 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3180 Args.push_back(Entry);
3181 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003182 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003183 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3184 else
3185 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3186 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3187 Args.push_back(Entry);
3188 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3189 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003190 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003191 TLI.LowerCallTo(Chain, Type::VoidTy,
3192 false, false, false, CallingConv::C, false,
3193 getExternalSymbol("memset", TLI.getPointerTy()),
3194 Args, *this);
3195 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003196}
3197
Dan Gohman475871a2008-07-27 21:46:04 +00003198SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3199 SDValue Ptr, SDValue Cmp,
3200 SDValue Swp, const Value* PtrVal,
3201 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003202 assert((Opcode == ISD::ATOMIC_CMP_SWAP_8 ||
3203 Opcode == ISD::ATOMIC_CMP_SWAP_16 ||
3204 Opcode == ISD::ATOMIC_CMP_SWAP_32 ||
3205 Opcode == ISD::ATOMIC_CMP_SWAP_64) && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003206 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003207
3208 MVT VT = Cmp.getValueType();
3209
3210 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3211 Alignment = getMVTAlignment(VT);
3212
3213 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003214 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003215 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003216 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003217 void* IP = 0;
3218 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003219 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003220 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003221 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003222 CSEMap.InsertNode(N, IP);
3223 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003224 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003225}
3226
Dan Gohman475871a2008-07-27 21:46:04 +00003227SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3228 SDValue Ptr, SDValue Val,
3229 const Value* PtrVal,
3230 unsigned Alignment) {
Dale Johannesene00a8a22008-08-28 02:44:49 +00003231 assert((Opcode == ISD::ATOMIC_LOAD_ADD_8 ||
3232 Opcode == ISD::ATOMIC_LOAD_SUB_8 ||
3233 Opcode == ISD::ATOMIC_LOAD_AND_8 ||
3234 Opcode == ISD::ATOMIC_LOAD_OR_8 ||
3235 Opcode == ISD::ATOMIC_LOAD_XOR_8 ||
3236 Opcode == ISD::ATOMIC_LOAD_NAND_8 ||
3237 Opcode == ISD::ATOMIC_LOAD_MIN_8 ||
3238 Opcode == ISD::ATOMIC_LOAD_MAX_8 ||
3239 Opcode == ISD::ATOMIC_LOAD_UMIN_8 ||
3240 Opcode == ISD::ATOMIC_LOAD_UMAX_8 ||
3241 Opcode == ISD::ATOMIC_SWAP_8 ||
3242 Opcode == ISD::ATOMIC_LOAD_ADD_16 ||
3243 Opcode == ISD::ATOMIC_LOAD_SUB_16 ||
3244 Opcode == ISD::ATOMIC_LOAD_AND_16 ||
3245 Opcode == ISD::ATOMIC_LOAD_OR_16 ||
3246 Opcode == ISD::ATOMIC_LOAD_XOR_16 ||
3247 Opcode == ISD::ATOMIC_LOAD_NAND_16 ||
3248 Opcode == ISD::ATOMIC_LOAD_MIN_16 ||
3249 Opcode == ISD::ATOMIC_LOAD_MAX_16 ||
3250 Opcode == ISD::ATOMIC_LOAD_UMIN_16 ||
3251 Opcode == ISD::ATOMIC_LOAD_UMAX_16 ||
3252 Opcode == ISD::ATOMIC_SWAP_16 ||
3253 Opcode == ISD::ATOMIC_LOAD_ADD_32 ||
3254 Opcode == ISD::ATOMIC_LOAD_SUB_32 ||
3255 Opcode == ISD::ATOMIC_LOAD_AND_32 ||
3256 Opcode == ISD::ATOMIC_LOAD_OR_32 ||
3257 Opcode == ISD::ATOMIC_LOAD_XOR_32 ||
3258 Opcode == ISD::ATOMIC_LOAD_NAND_32 ||
3259 Opcode == ISD::ATOMIC_LOAD_MIN_32 ||
3260 Opcode == ISD::ATOMIC_LOAD_MAX_32 ||
3261 Opcode == ISD::ATOMIC_LOAD_UMIN_32 ||
3262 Opcode == ISD::ATOMIC_LOAD_UMAX_32 ||
3263 Opcode == ISD::ATOMIC_SWAP_32 ||
3264 Opcode == ISD::ATOMIC_LOAD_ADD_64 ||
3265 Opcode == ISD::ATOMIC_LOAD_SUB_64 ||
3266 Opcode == ISD::ATOMIC_LOAD_AND_64 ||
3267 Opcode == ISD::ATOMIC_LOAD_OR_64 ||
3268 Opcode == ISD::ATOMIC_LOAD_XOR_64 ||
3269 Opcode == ISD::ATOMIC_LOAD_NAND_64 ||
3270 Opcode == ISD::ATOMIC_LOAD_MIN_64 ||
3271 Opcode == ISD::ATOMIC_LOAD_MAX_64 ||
3272 Opcode == ISD::ATOMIC_LOAD_UMIN_64 ||
3273 Opcode == ISD::ATOMIC_LOAD_UMAX_64 ||
3274 Opcode == ISD::ATOMIC_SWAP_64) && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003275
3276 MVT VT = Val.getValueType();
3277
3278 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3279 Alignment = getMVTAlignment(VT);
3280
3281 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003282 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003283 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003284 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003285 void* IP = 0;
3286 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003287 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003288 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003289 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003290 CSEMap.InsertNode(N, IP);
3291 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003292 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003293}
3294
Duncan Sands4bdcb612008-07-02 17:40:58 +00003295/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3296/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003297SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3298 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003299 if (Simplify && NumOps == 1)
3300 return Ops[0];
3301
3302 SmallVector<MVT, 4> VTs;
3303 VTs.reserve(NumOps);
3304 for (unsigned i = 0; i < NumOps; ++i)
3305 VTs.push_back(Ops[i].getValueType());
3306 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3307}
3308
Dan Gohman475871a2008-07-27 21:46:04 +00003309SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003310SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
3311 SDVTList VTs,
3312 const SDValue *Operands, unsigned NumOperands) {
3313 // Do not CSE calls. Note that in addition to being a compile-time
3314 // optimization (since attempting CSE of calls is unlikely to be
3315 // meaningful), we actually depend on this behavior. CallSDNode can
3316 // be mutated, which is only safe if calls are not CSE'd.
3317 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3318 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall,
3319 VTs, Operands, NumOperands);
3320 AllNodes.push_back(N);
3321 return SDValue(N, 0);
3322}
3323
3324SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003325SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003326 MVT VT, SDValue Chain,
3327 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003329 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003330 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3331 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003332
Duncan Sands14ea39c2008-03-27 20:23:40 +00003333 if (VT == EVT) {
3334 ExtType = ISD::NON_EXTLOAD;
3335 } else if (ExtType == ISD::NON_EXTLOAD) {
3336 assert(VT == EVT && "Non-extending load from different memory type!");
3337 } else {
3338 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003339 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003340 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3341 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003342 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003343 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003344 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003345 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003346 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003347 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003348 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003349 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003350
3351 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003352 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003353 "Unindexed load with an offset!");
3354
3355 SDVTList VTs = Indexed ?
3356 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003357 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003358 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003359 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003360 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003361 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003362 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003363 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003364 void *IP = 0;
3365 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003366 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003367 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003368 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3369 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003370 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003371 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003372 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003373}
3374
Dan Gohman475871a2008-07-27 21:46:04 +00003375SDValue SelectionDAG::getLoad(MVT VT,
3376 SDValue Chain, SDValue Ptr,
3377 const Value *SV, int SVOffset,
3378 bool isVolatile, unsigned Alignment) {
3379 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003380 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3381 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003382}
3383
Dan Gohman475871a2008-07-27 21:46:04 +00003384SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3385 SDValue Chain, SDValue Ptr,
3386 const Value *SV,
3387 int SVOffset, MVT EVT,
3388 bool isVolatile, unsigned Alignment) {
3389 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003390 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3391 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003392}
3393
Dan Gohman475871a2008-07-27 21:46:04 +00003394SDValue
3395SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3396 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003397 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003398 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3399 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003400 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3401 LD->getChain(), Base, Offset, LD->getSrcValue(),
3402 LD->getSrcValueOffset(), LD->getMemoryVT(),
3403 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003404}
3405
Dan Gohman475871a2008-07-27 21:46:04 +00003406SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3407 SDValue Ptr, const Value *SV, int SVOffset,
3408 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003410
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003411 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3412 Alignment = getMVTAlignment(VT);
3413
Evan Chengad071e12006-10-05 22:57:11 +00003414 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003415 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3416 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003417 FoldingSetNodeID ID;
3418 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003419 ID.AddInteger(ISD::UNINDEXED);
3420 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003421 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003422 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003423 void *IP = 0;
3424 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003425 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003426 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003427 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3428 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003429 CSEMap.InsertNode(N, IP);
3430 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003431 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003432}
3433
Dan Gohman475871a2008-07-27 21:46:04 +00003434SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3435 SDValue Ptr, const Value *SV,
3436 int SVOffset, MVT SVT,
3437 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003438 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003439
3440 if (VT == SVT)
3441 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003442
Duncan Sands8e4eb092008-06-08 20:54:56 +00003443 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003444 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003445 "Can't do FP-INT conversion!");
3446
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003447 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3448 Alignment = getMVTAlignment(VT);
3449
Evan Cheng8b2794a2006-10-13 21:14:26 +00003450 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3452 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003453 FoldingSetNodeID ID;
3454 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003455 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003456 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003457 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003458 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003459 void *IP = 0;
3460 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003461 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003462 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003463 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3464 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003465 CSEMap.InsertNode(N, IP);
3466 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003467 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003468}
3469
Dan Gohman475871a2008-07-27 21:46:04 +00003470SDValue
3471SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3472 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003473 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3474 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3475 "Store is already a indexed store!");
3476 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003477 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003478 FoldingSetNodeID ID;
3479 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3480 ID.AddInteger(AM);
3481 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003482 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003483 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003484 void *IP = 0;
3485 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003486 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003487 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003488 new (N) StoreSDNode(Ops, VTs, AM,
3489 ST->isTruncatingStore(), ST->getMemoryVT(),
3490 ST->getSrcValue(), ST->getSrcValueOffset(),
3491 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003492 CSEMap.InsertNode(N, IP);
3493 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003494 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003495}
3496
Dan Gohman475871a2008-07-27 21:46:04 +00003497SDValue SelectionDAG::getVAArg(MVT VT,
3498 SDValue Chain, SDValue Ptr,
3499 SDValue SV) {
3500 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003501 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003502}
3503
Dan Gohman475871a2008-07-27 21:46:04 +00003504SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3505 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003506 switch (NumOps) {
3507 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003508 case 1: return getNode(Opcode, VT, Ops[0]);
3509 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3510 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003511 default: break;
3512 }
3513
Dan Gohman475871a2008-07-27 21:46:04 +00003514 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003515 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003516 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3517 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003518}
3519
Dan Gohman475871a2008-07-27 21:46:04 +00003520SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3521 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003522 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003523 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003524 case 1: return getNode(Opcode, VT, Ops[0]);
3525 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3526 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003527 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003528 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003529
Chris Lattneref847df2005-04-09 03:27:28 +00003530 switch (Opcode) {
3531 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003532 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003533 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003534 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3535 "LHS and RHS of condition must have same type!");
3536 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3537 "True and False arms of SelectCC must have same type!");
3538 assert(Ops[2].getValueType() == VT &&
3539 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003540 break;
3541 }
3542 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003543 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003544 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3545 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003546 break;
3547 }
Chris Lattneref847df2005-04-09 03:27:28 +00003548 }
3549
Chris Lattner385328c2005-05-14 07:42:29 +00003550 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003551 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003552 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003553 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003554 FoldingSetNodeID ID;
3555 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003556 void *IP = 0;
3557 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003558 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003559 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003560 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003561 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003562 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003563 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003564 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003565 }
Chris Lattneref847df2005-04-09 03:27:28 +00003566 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003567#ifndef NDEBUG
3568 VerifyNode(N);
3569#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003570 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003571}
3572
Dan Gohman475871a2008-07-27 21:46:04 +00003573SDValue SelectionDAG::getNode(unsigned Opcode,
3574 const std::vector<MVT> &ResultTys,
3575 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003576 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3577 Ops, NumOps);
3578}
3579
Dan Gohman475871a2008-07-27 21:46:04 +00003580SDValue SelectionDAG::getNode(unsigned Opcode,
3581 const MVT *VTs, unsigned NumVTs,
3582 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003583 if (NumVTs == 1)
3584 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003585 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3586}
3587
Dan Gohman475871a2008-07-27 21:46:04 +00003588SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3589 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003590 if (VTList.NumVTs == 1)
3591 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003592
Chris Lattner5f056bf2005-07-10 01:55:33 +00003593 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003594 // FIXME: figure out how to safely handle things like
3595 // int foo(int x) { return 1 << (x & 255); }
3596 // int bar() { return foo(256); }
3597#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003598 case ISD::SRA_PARTS:
3599 case ISD::SRL_PARTS:
3600 case ISD::SHL_PARTS:
3601 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003602 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003603 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3604 else if (N3.getOpcode() == ISD::AND)
3605 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3606 // If the and is only masking out bits that cannot effect the shift,
3607 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003608 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003609 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3610 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3611 }
3612 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003613#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003614 }
Chris Lattner89c34632005-05-14 06:20:26 +00003615
Chris Lattner43247a12005-08-25 19:12:10 +00003616 // Memoize the node unless it returns a flag.
3617 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003618 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003619 FoldingSetNodeID ID;
3620 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003621 void *IP = 0;
3622 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003623 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003624 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003625 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003626 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3627 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003628 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003629 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3630 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003631 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003632 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3633 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003634 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003635 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3636 }
Chris Lattnera5682852006-08-07 23:03:03 +00003637 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003638 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003639 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003640 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003641 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3642 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003643 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003644 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3645 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003646 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003647 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3648 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003649 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003650 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3651 }
Chris Lattner43247a12005-08-25 19:12:10 +00003652 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003653 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003654#ifndef NDEBUG
3655 VerifyNode(N);
3656#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003657 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003658}
3659
Dan Gohman475871a2008-07-27 21:46:04 +00003660SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003661 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003662}
3663
Dan Gohman475871a2008-07-27 21:46:04 +00003664SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3665 SDValue N1) {
3666 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003667 return getNode(Opcode, VTList, Ops, 1);
3668}
3669
Dan Gohman475871a2008-07-27 21:46:04 +00003670SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3671 SDValue N1, SDValue N2) {
3672 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003673 return getNode(Opcode, VTList, Ops, 2);
3674}
3675
Dan Gohman475871a2008-07-27 21:46:04 +00003676SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3677 SDValue N1, SDValue N2, SDValue N3) {
3678 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003679 return getNode(Opcode, VTList, Ops, 3);
3680}
3681
Dan Gohman475871a2008-07-27 21:46:04 +00003682SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3683 SDValue N1, SDValue N2, SDValue N3,
3684 SDValue N4) {
3685 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003686 return getNode(Opcode, VTList, Ops, 4);
3687}
3688
Dan Gohman475871a2008-07-27 21:46:04 +00003689SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3690 SDValue N1, SDValue N2, SDValue N3,
3691 SDValue N4, SDValue N5) {
3692 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003693 return getNode(Opcode, VTList, Ops, 5);
3694}
3695
Duncan Sands83ec4b62008-06-06 12:08:01 +00003696SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003697 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003698}
3699
Duncan Sands83ec4b62008-06-06 12:08:01 +00003700SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003701 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3702 E = VTList.rend(); I != E; ++I)
3703 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3704 return *I;
3705
3706 MVT *Array = Allocator.Allocate<MVT>(2);
3707 Array[0] = VT1;
3708 Array[1] = VT2;
3709 SDVTList Result = makeVTList(Array, 2);
3710 VTList.push_back(Result);
3711 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003712}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003713
3714SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3715 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3716 E = VTList.rend(); I != E; ++I)
3717 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3718 I->VTs[2] == VT3)
3719 return *I;
3720
3721 MVT *Array = Allocator.Allocate<MVT>(3);
3722 Array[0] = VT1;
3723 Array[1] = VT2;
3724 Array[2] = VT3;
3725 SDVTList Result = makeVTList(Array, 3);
3726 VTList.push_back(Result);
3727 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003728}
3729
Duncan Sands83ec4b62008-06-06 12:08:01 +00003730SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003731 switch (NumVTs) {
3732 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003733 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003734 case 2: return getVTList(VTs[0], VTs[1]);
3735 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3736 default: break;
3737 }
3738
Dan Gohmane8be6c62008-07-17 19:10:17 +00003739 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3740 E = VTList.rend(); I != E; ++I) {
3741 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3742 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003743
3744 bool NoMatch = false;
3745 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003746 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003747 NoMatch = true;
3748 break;
3749 }
3750 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003751 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003752 }
3753
Dan Gohmane8be6c62008-07-17 19:10:17 +00003754 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3755 std::copy(VTs, VTs+NumVTs, Array);
3756 SDVTList Result = makeVTList(Array, NumVTs);
3757 VTList.push_back(Result);
3758 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003759}
3760
3761
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003762/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3763/// specified operands. If the resultant node already exists in the DAG,
3764/// this does not modify the specified node, instead it returns the node that
3765/// already exists. If the resultant node does not exist in the DAG, the
3766/// input node is returned. As a degenerate case, if you specify the same
3767/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003768SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003769 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003770 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3771
3772 // Check to see if there is no change.
3773 if (Op == N->getOperand(0)) return InN;
3774
3775 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003776 void *InsertPos = 0;
3777 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003778 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003779
Dan Gohman79acd2b2008-07-21 22:38:59 +00003780 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003781 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003782 if (!RemoveNodeFromCSEMaps(N))
3783 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003784
3785 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003786 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003787 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003788 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003789 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003790
3791 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003792 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003793 return InN;
3794}
3795
Dan Gohman475871a2008-07-27 21:46:04 +00003796SDValue SelectionDAG::
3797UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003798 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003799 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3800
3801 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003802 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3803 return InN; // No operands changed, just return the input node.
3804
3805 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003806 void *InsertPos = 0;
3807 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003808 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003809
Dan Gohman79acd2b2008-07-21 22:38:59 +00003810 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003811 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003812 if (!RemoveNodeFromCSEMaps(N))
3813 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003814
3815 // Now we update the operands.
3816 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003817 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003818 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003819 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003820 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003821 }
3822 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003823 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003824 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003825 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003826 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003827 }
3828
3829 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003830 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003831 return InN;
3832}
3833
Dan Gohman475871a2008-07-27 21:46:04 +00003834SDValue SelectionDAG::
3835UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3836 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003837 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003838}
3839
Dan Gohman475871a2008-07-27 21:46:04 +00003840SDValue SelectionDAG::
3841UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3842 SDValue Op3, SDValue Op4) {
3843 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003844 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003845}
3846
Dan Gohman475871a2008-07-27 21:46:04 +00003847SDValue SelectionDAG::
3848UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3849 SDValue Op3, SDValue Op4, SDValue Op5) {
3850 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003851 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003852}
3853
Dan Gohman475871a2008-07-27 21:46:04 +00003854SDValue SelectionDAG::
3855UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003856 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003857 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003858 "Update with wrong number of operands");
3859
3860 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003861 bool AnyChange = false;
3862 for (unsigned i = 0; i != NumOps; ++i) {
3863 if (Ops[i] != N->getOperand(i)) {
3864 AnyChange = true;
3865 break;
3866 }
3867 }
3868
3869 // No operands changed, just return the input node.
3870 if (!AnyChange) return InN;
3871
3872 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003873 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003874 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003875 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003876
Dan Gohman7ceda162008-05-02 00:05:03 +00003877 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003878 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003879 if (!RemoveNodeFromCSEMaps(N))
3880 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003881
3882 // Now we update the operands.
3883 for (unsigned i = 0; i != NumOps; ++i) {
3884 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003885 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003886 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003887 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003888 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003889 }
3890 }
3891
3892 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003893 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003894 return InN;
3895}
3896
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003897/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003898/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003899void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003900 // Unlike the code in MorphNodeTo that does this, we don't need to
3901 // watch for dead nodes here.
3902 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3903 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3904
3905 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003906}
Chris Lattner149c58c2005-08-16 18:17:10 +00003907
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3909/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003910///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003912 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003913 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003914 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003915}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003916
Dan Gohmane8be6c62008-07-17 19:10:17 +00003917SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003918 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003919 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003920 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003921 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003922}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003923
Dan Gohmane8be6c62008-07-17 19:10:17 +00003924SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003925 MVT VT, SDValue Op1,
3926 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003927 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003928 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003929 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003930}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003931
Dan Gohmane8be6c62008-07-17 19:10:17 +00003932SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003933 MVT VT, SDValue Op1,
3934 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003935 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003936 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003937 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003938}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003939
Dan Gohmane8be6c62008-07-17 19:10:17 +00003940SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003941 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003942 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003943 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003944 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003945}
3946
Dan Gohmane8be6c62008-07-17 19:10:17 +00003947SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003948 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003949 unsigned NumOps) {
3950 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003952}
3953
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003955 MVT VT1, MVT VT2) {
3956 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003957 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003958}
3959
Dan Gohmane8be6c62008-07-17 19:10:17 +00003960SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003961 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003962 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003963 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003964 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003965}
3966
Dan Gohmane8be6c62008-07-17 19:10:17 +00003967SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003968 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003969 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003970 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003971 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003972 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003973}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003974
Dan Gohmane8be6c62008-07-17 19:10:17 +00003975SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003976 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003977 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003978 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003979 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003980 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003981}
3982
Dan Gohmane8be6c62008-07-17 19:10:17 +00003983SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003984 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003985 SDValue Op1, SDValue Op2,
3986 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003987 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003988 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003989 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003990}
3991
Dan Gohmane8be6c62008-07-17 19:10:17 +00003992SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003993 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003994 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003995 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3996}
3997
3998SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3999 MVT VT) {
4000 SDVTList VTs = getVTList(VT);
4001 return MorphNodeTo(N, Opc, VTs, 0, 0);
4002}
4003
4004SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004005 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004006 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004007 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4009}
4010
4011SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004012 MVT VT, SDValue Op1,
4013 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004014 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004015 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004016 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4017}
4018
4019SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004020 MVT VT, SDValue Op1,
4021 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004022 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004023 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004024 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4025}
4026
4027SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004028 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004029 unsigned NumOps) {
4030 SDVTList VTs = getVTList(VT);
4031 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4032}
4033
4034SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004035 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004036 unsigned NumOps) {
4037 SDVTList VTs = getVTList(VT1, VT2);
4038 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4039}
4040
4041SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4042 MVT VT1, MVT VT2) {
4043 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004044 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004045}
4046
4047SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4048 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004049 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004050 SDVTList VTs = getVTList(VT1, VT2, VT3);
4051 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4052}
4053
4054SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4055 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004056 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004057 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004058 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004059 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4060}
4061
4062SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4063 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004064 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004065 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004066 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004067 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4068}
4069
4070SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4071 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004072 SDValue Op1, SDValue Op2,
4073 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004074 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004075 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004076 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4077}
4078
4079/// MorphNodeTo - These *mutate* the specified node to have the specified
4080/// return type, opcode, and operands.
4081///
4082/// Note that MorphNodeTo returns the resultant node. If there is already a
4083/// node of the specified opcode and operands, it returns that node instead of
4084/// the current one.
4085///
4086/// Using MorphNodeTo is faster than creating a new node and swapping it in
4087/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004088/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004089/// the node's users.
4090///
4091SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004092 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004093 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004094 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004095 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004096 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4097 FoldingSetNodeID ID;
4098 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4099 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4100 return ON;
4101 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004102
Dan Gohman095cc292008-09-13 01:54:27 +00004103 if (!RemoveNodeFromCSEMaps(N))
4104 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004105
Dan Gohmane8be6c62008-07-17 19:10:17 +00004106 // Start the morphing.
4107 N->NodeType = Opc;
4108 N->ValueList = VTs.VTs;
4109 N->NumValues = VTs.NumVTs;
4110
4111 // Clear the operands list, updating used nodes to remove this from their
4112 // use list. Keep track of any operands that become dead as a result.
4113 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4114 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4115 I != E; ++I) {
4116 SDNode *Used = I->getVal();
4117 Used->removeUser(std::distance(B, I), N);
4118 if (Used->use_empty())
4119 DeadNodeSet.insert(Used);
4120 }
4121
4122 // If NumOps is larger than the # of operands we currently have, reallocate
4123 // the operand list.
4124 if (NumOps > N->NumOperands) {
4125 if (N->OperandsNeedDelete)
4126 delete[] N->OperandList;
4127 if (N->isMachineOpcode()) {
4128 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004129 // remainder of the current SelectionDAG iteration, so we can allocate
4130 // the operands directly out of a pool with no recycling metadata.
4131 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004132 N->OperandsNeedDelete = false;
4133 } else {
4134 N->OperandList = new SDUse[NumOps];
4135 N->OperandsNeedDelete = true;
4136 }
4137 }
4138
4139 // Assign the new operands.
4140 N->NumOperands = NumOps;
4141 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4142 N->OperandList[i] = Ops[i];
4143 N->OperandList[i].setUser(N);
4144 SDNode *ToUse = N->OperandList[i].getVal();
4145 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004146 }
4147
4148 // Delete any nodes that are still dead after adding the uses for the
4149 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004150 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004151 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4152 E = DeadNodeSet.end(); I != E; ++I)
4153 if ((*I)->use_empty())
4154 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004155 RemoveDeadNodes(DeadNodes);
4156
Dan Gohmane8be6c62008-07-17 19:10:17 +00004157 if (IP)
4158 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004159 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004160}
4161
Chris Lattner0fb094f2005-11-19 01:44:53 +00004162
Evan Cheng6ae46c42006-02-09 07:15:23 +00004163/// getTargetNode - These are used for target selectors to create a new node
4164/// with specified return type(s), target opcode, and operands.
4165///
4166/// Note that getTargetNode returns the resultant node. If there is already a
4167/// node of the specified opcode and operands, it returns that node instead of
4168/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004169SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004170 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004171}
Dan Gohman475871a2008-07-27 21:46:04 +00004172SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004173 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004174}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004175SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004176 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004177 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004178}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004179SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004180 SDValue Op1, SDValue Op2,
4181 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004183}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004184SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004185 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004186 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004187}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004188SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4189 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004190 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004191 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004192}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004195 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004196 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004197}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004199 MVT VT2, SDValue Op1,
4200 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004201 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004202 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004203 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004204}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004205SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004206 MVT VT2, SDValue Op1,
4207 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004209 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004211}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004213 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004214 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004215 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004216}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004217SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004218 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004219 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004220 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004221 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004222}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004224 SDValue Op1, SDValue Op2,
4225 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004226 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004227 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004228 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004229}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004230SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004231 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004232 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004233 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004234}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004235SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4236 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004237 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004238 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004239 VTList.push_back(VT1);
4240 VTList.push_back(VT2);
4241 VTList.push_back(VT3);
4242 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004244 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004245}
Evan Cheng39305cf2007-10-05 01:10:49 +00004246SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004247 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004248 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004249 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004250 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004251 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004252}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004253
Evan Cheng08b11732008-03-22 01:55:50 +00004254/// getNodeIfExists - Get the specified node if it's already available, or
4255/// else return NULL.
4256SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004257 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004258 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4259 FoldingSetNodeID ID;
4260 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4261 void *IP = 0;
4262 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4263 return E;
4264 }
4265 return NULL;
4266}
4267
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004268
Evan Cheng99157a02006-08-07 22:13:29 +00004269/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004270/// This can cause recursive merging of nodes in the DAG.
4271///
Chris Lattner11d049c2008-02-03 03:35:22 +00004272/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004273///
Dan Gohman475871a2008-07-27 21:46:04 +00004274void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004275 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004276 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004277 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004278 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004279 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004280
Chris Lattner8b8749f2005-08-17 19:00:20 +00004281 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004282 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004283 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004284
Chris Lattner8b8749f2005-08-17 19:00:20 +00004285 // This node is about to morph, remove its old self from the CSE maps.
4286 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004287 int operandNum = 0;
4288 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4289 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004290 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004291 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004292 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004293 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004294 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004295 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004296
4297 // Now that we have modified U, add it back to the CSE maps. If it already
4298 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004299 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004300 ReplaceAllUsesWith(U, Existing, UpdateListener);
4301 // U is now dead. Inform the listener if it exists and delete it.
4302 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004303 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004304 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004305 } else {
4306 // If the node doesn't already exist, we updated it. Inform a listener if
4307 // it exists.
4308 if (UpdateListener)
4309 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004310 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004311 }
4312}
4313
4314/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4315/// This can cause recursive merging of nodes in the DAG.
4316///
4317/// This version assumes From/To have matching types and numbers of result
4318/// values.
4319///
Chris Lattner1e111c72005-09-07 05:37:01 +00004320void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004321 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004322 assert(From->getVTList().VTs == To->getVTList().VTs &&
4323 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004324 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004325
4326 // Handle the trivial case.
4327 if (From == To)
4328 return;
4329
Chris Lattner8b52f212005-08-26 18:36:28 +00004330 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004331 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004332 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004333
Chris Lattner8b52f212005-08-26 18:36:28 +00004334 // This node is about to morph, remove its old self from the CSE maps.
4335 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004336 int operandNum = 0;
4337 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4338 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004339 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004340 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004341 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004342 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004343 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004344
Chris Lattner8b8749f2005-08-17 19:00:20 +00004345 // Now that we have modified U, add it back to the CSE maps. If it already
4346 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004347 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004348 ReplaceAllUsesWith(U, Existing, UpdateListener);
4349 // U is now dead. Inform the listener if it exists and delete it.
4350 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004351 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004352 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004353 } else {
4354 // If the node doesn't already exist, we updated it. Inform a listener if
4355 // it exists.
4356 if (UpdateListener)
4357 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004358 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004359 }
4360}
4361
Chris Lattner8b52f212005-08-26 18:36:28 +00004362/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4363/// This can cause recursive merging of nodes in the DAG.
4364///
4365/// This version can replace From with any result values. To must match the
4366/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004367void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004368 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004369 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004370 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004371 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004372
4373 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004374 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004375 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004376
Chris Lattner7b2880c2005-08-24 22:44:39 +00004377 // This node is about to morph, remove its old self from the CSE maps.
4378 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004379 int operandNum = 0;
4380 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4381 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004382 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004383 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004384 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004385 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004386 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004387 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004388 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004389
Chris Lattner7b2880c2005-08-24 22:44:39 +00004390 // Now that we have modified U, add it back to the CSE maps. If it already
4391 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004392 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004393 ReplaceAllUsesWith(U, Existing, UpdateListener);
4394 // U is now dead. Inform the listener if it exists and delete it.
4395 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004396 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004397 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004398 } else {
4399 // If the node doesn't already exist, we updated it. Inform a listener if
4400 // it exists.
4401 if (UpdateListener)
4402 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004403 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004404 }
4405}
4406
Chris Lattner012f2412006-02-17 21:58:01 +00004407/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004408/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004409/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004410void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004411 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004412 // Handle the really simple, really trivial case efficiently.
4413 if (From == To) return;
4414
Chris Lattner012f2412006-02-17 21:58:01 +00004415 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004416 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004417 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004418 return;
4419 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004420
Gabor Greifba36cb52008-08-28 21:40:38 +00004421 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004422 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004423 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004424
4425 while (!Users.empty()) {
4426 // We know that this user uses some value of From. If it is the right
4427 // value, update it.
4428 SDNode *User = Users.back();
4429 Users.pop_back();
4430
Chris Lattner01d029b2007-10-15 06:10:22 +00004431 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004432 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004433 for (; Op != E; ++Op)
4434 if (*Op == From) break;
4435
4436 // If there are no matches, the user must use some other result of From.
4437 if (Op == E) continue;
4438
4439 // Okay, we know this user needs to be updated. Remove its old self
4440 // from the CSE maps.
4441 RemoveNodeFromCSEMaps(User);
4442
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004443 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004444 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004445 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004446 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004447 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004448 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004449 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004450 }
4451 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004452
4453 // Now that we have modified User, add it back to the CSE maps. If it
4454 // already exists there, recursively merge the results together.
4455 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004456 if (!Existing) {
4457 if (UpdateListener) UpdateListener->NodeUpdated(User);
4458 continue; // Continue on to next user.
4459 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004460
4461 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004462 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004463 // recursive merging of other unrelated nodes down the line.
4464 ReplaceAllUsesWith(User, Existing, UpdateListener);
4465
4466 // User is now dead. Notify a listener if present.
4467 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4468 DeleteNodeNotInCSEMaps(User);
4469 }
4470}
4471
4472/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004473/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004474/// appear in both the From and To list. The Deleted vector is
4475/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004476void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4477 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004478 unsigned Num,
4479 DAGUpdateListener *UpdateListener){
4480 // Handle the simple, trivial case efficiently.
4481 if (Num == 1)
4482 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4483
4484 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4485 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004486 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4487 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004488 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004489
4490 while (!Users.empty()) {
4491 // We know that this user uses some value of From. If it is the right
4492 // value, update it.
4493 SDNode *User = Users.back().first;
4494 unsigned i = Users.back().second;
4495 Users.pop_back();
4496
4497 // Scan for an operand that matches From.
4498 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4499 for (; Op != E; ++Op)
4500 if (*Op == From[i]) break;
4501
4502 // If there are no matches, the user must use some other result of From.
4503 if (Op == E) continue;
4504
4505 // Okay, we know this user needs to be updated. Remove its old self
4506 // from the CSE maps.
4507 RemoveNodeFromCSEMaps(User);
4508
4509 // Update all operands that match "From" in case there are multiple uses.
4510 for (; Op != E; ++Op) {
4511 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004512 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004513 *Op = To[i];
4514 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004515 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004516 }
4517 }
4518
4519 // Now that we have modified User, add it back to the CSE maps. If it
4520 // already exists there, recursively merge the results together.
4521 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4522 if (!Existing) {
4523 if (UpdateListener) UpdateListener->NodeUpdated(User);
4524 continue; // Continue on to next user.
4525 }
4526
4527 // If there was already an existing matching node, use ReplaceAllUsesWith
4528 // to replace the dead one with the existing one. This can cause
4529 // recursive merging of other unrelated nodes down the line.
4530 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004531
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004532 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004533 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004534 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004535 }
4536}
4537
Evan Chenge6f35d82006-08-01 08:20:41 +00004538/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004539/// based on their topological order. It returns the maximum id and a vector
4540/// of the SDNodes* in assigned order by reference.
4541unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004542 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004543 std::vector<SDNode*> Sources;
4544
Evan Chenge6f35d82006-08-01 08:20:41 +00004545 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4546 SDNode *N = I;
4547 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004548 // Temporarily use the Node Id as scratch space for the degree count.
4549 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004550 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004551 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004552 }
4553
Evan Cheng99157a02006-08-07 22:13:29 +00004554 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004555 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004556 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004557 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004558 SDNode *N = Sources.back();
4559 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004560 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004561 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004562 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004563 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004564 unsigned Degree = P->getNodeId();
4565 --Degree;
4566 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004567 if (Degree == 0)
4568 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004569 }
4570 }
4571
Evan Chengc384d6c2006-08-02 22:00:34 +00004572 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004573}
4574
4575
Evan Cheng091cba12006-07-27 06:39:06 +00004576
Jim Laskey58b968b2005-08-17 20:08:02 +00004577//===----------------------------------------------------------------------===//
4578// SDNode Class
4579//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004580
Chris Lattner917d2c92006-07-19 00:00:37 +00004581// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004582void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004583void UnarySDNode::ANCHOR() {}
4584void BinarySDNode::ANCHOR() {}
4585void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004586void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004587void ConstantSDNode::ANCHOR() {}
4588void ConstantFPSDNode::ANCHOR() {}
4589void GlobalAddressSDNode::ANCHOR() {}
4590void FrameIndexSDNode::ANCHOR() {}
4591void JumpTableSDNode::ANCHOR() {}
4592void ConstantPoolSDNode::ANCHOR() {}
4593void BasicBlockSDNode::ANCHOR() {}
4594void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004595void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004596void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004597void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004598void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004599void ExternalSymbolSDNode::ANCHOR() {}
4600void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004601void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004602void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004603void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004604void LoadSDNode::ANCHOR() {}
4605void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004606void AtomicSDNode::ANCHOR() {}
Dan Gohman095cc292008-09-13 01:54:27 +00004607void CallSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004608
Chris Lattner48b85922007-02-04 02:41:42 +00004609HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004610 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004611}
4612
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004613GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004614 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004615 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004616 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004617 // Thread Local
4618 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4619 // Non Thread Local
4620 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4621 getSDVTList(VT)), Offset(o) {
4622 TheGlobal = const_cast<GlobalValue*>(GA);
4623}
Chris Lattner48b85922007-02-04 02:41:42 +00004624
Dan Gohman1ea58a52008-07-09 22:08:04 +00004625MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004626 const Value *srcValue, int SVO,
4627 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004628 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004629 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004630
4631 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4632 assert(getAlignment() == alignment && "Alignment representation error!");
4633 assert(isVolatile() == vol && "Volatile representation error!");
4634}
4635
Dan Gohman36b5c132008-04-07 19:35:22 +00004636/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004637/// reference performed by this memory reference.
4638MachineMemOperand MemSDNode::getMemOperand() const {
4639 int Flags;
4640 if (isa<LoadSDNode>(this))
4641 Flags = MachineMemOperand::MOLoad;
4642 else if (isa<StoreSDNode>(this))
4643 Flags = MachineMemOperand::MOStore;
4644 else {
4645 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4646 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4647 }
4648
4649 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004650 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4651
Dan Gohman1ea58a52008-07-09 22:08:04 +00004652 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004653 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004654 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004655 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004656 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4657 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004658 else
4659 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4660 Size, getAlignment());
4661}
4662
Jim Laskey583bd472006-10-27 23:46:08 +00004663/// Profile - Gather unique data for the node.
4664///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004665void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004666 AddNodeIDNode(ID, this);
4667}
4668
Chris Lattnera3255112005-11-08 23:30:28 +00004669/// getValueTypeList - Return a pointer to the specified value type.
4670///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004671const MVT *SDNode::getValueTypeList(MVT VT) {
4672 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004673 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004674 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004675 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004676 static MVT VTs[MVT::LAST_VALUETYPE];
4677 VTs[VT.getSimpleVT()] = VT;
4678 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004679 }
Chris Lattnera3255112005-11-08 23:30:28 +00004680}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004681
Chris Lattner5c884562005-01-12 18:37:47 +00004682/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4683/// indicated value. This method ignores uses of other values defined by this
4684/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004685bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004686 assert(Value < getNumValues() && "Bad value!");
4687
Roman Levensteindc1adac2008-04-07 10:06:32 +00004688 // TODO: Only iterate over uses of a given value of the node
4689 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004690 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004691 if (NUses == 0)
4692 return false;
4693 --NUses;
4694 }
Chris Lattner5c884562005-01-12 18:37:47 +00004695 }
4696
4697 // Found exactly the right number of uses?
4698 return NUses == 0;
4699}
4700
4701
Evan Cheng33d55952007-08-02 05:29:38 +00004702/// hasAnyUseOfValue - Return true if there are any use of the indicated
4703/// value. This method ignores uses of other values defined by this operation.
4704bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4705 assert(Value < getNumValues() && "Bad value!");
4706
Dan Gohman1373c1c2008-07-09 22:39:01 +00004707 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004708 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004709 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004710
4711 return false;
4712}
4713
4714
Dan Gohman2a629952008-07-27 18:06:42 +00004715/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004716///
Dan Gohman2a629952008-07-27 18:06:42 +00004717bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004718 bool Seen = false;
4719 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004720 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004721 if (User == this)
4722 Seen = true;
4723 else
4724 return false;
4725 }
4726
4727 return Seen;
4728}
4729
Evan Chenge6e97e62006-11-03 07:31:32 +00004730/// isOperand - Return true if this node is an operand of N.
4731///
Dan Gohman475871a2008-07-27 21:46:04 +00004732bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004733 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4734 if (*this == N->getOperand(i))
4735 return true;
4736 return false;
4737}
4738
Evan Cheng917be682008-03-04 00:41:45 +00004739bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004740 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004741 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004742 return true;
4743 return false;
4744}
Evan Cheng4ee62112006-02-05 06:29:23 +00004745
Chris Lattner572dee72008-01-16 05:49:24 +00004746/// reachesChainWithoutSideEffects - Return true if this operand (which must
4747/// be a chain) reaches the specified operand without crossing any
4748/// side-effecting instructions. In practice, this looks through token
4749/// factors and non-volatile loads. In order to remain efficient, this only
4750/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004751bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004752 unsigned Depth) const {
4753 if (*this == Dest) return true;
4754
4755 // Don't search too deeply, we just want to be able to see through
4756 // TokenFactor's etc.
4757 if (Depth == 0) return false;
4758
4759 // If this is a token factor, all inputs to the TF happen in parallel. If any
4760 // of the operands of the TF reach dest, then we can do the xform.
4761 if (getOpcode() == ISD::TokenFactor) {
4762 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4763 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4764 return true;
4765 return false;
4766 }
4767
4768 // Loads don't have side effects, look through them.
4769 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4770 if (!Ld->isVolatile())
4771 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4772 }
4773 return false;
4774}
4775
4776
Evan Chengc5fc57d2006-11-03 03:05:24 +00004777static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004778 SmallPtrSet<SDNode *, 32> &Visited) {
4779 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004780 return;
4781
4782 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004783 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004784 if (Op == P) {
4785 found = true;
4786 return;
4787 }
4788 findPredecessor(Op, P, found, Visited);
4789 }
4790}
4791
Evan Cheng917be682008-03-04 00:41:45 +00004792/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004793/// is either an operand of N or it can be reached by recursively traversing
4794/// up the operands.
4795/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004796bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004797 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004798 bool found = false;
4799 findPredecessor(N, this, found, Visited);
4800 return found;
4801}
4802
Evan Chengc5484282006-10-04 00:56:09 +00004803uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4804 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004805 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004806}
4807
Reid Spencer577cc322007-04-01 07:32:19 +00004808std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004809 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004810 default:
4811 if (getOpcode() < ISD::BUILTIN_OP_END)
4812 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004813 if (isMachineOpcode()) {
4814 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004815 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004816 if (getMachineOpcode() < TII->getNumOpcodes())
4817 return TII->get(getMachineOpcode()).getName();
4818 return "<<Unknown Machine Node>>";
4819 }
4820 if (G) {
4821 TargetLowering &TLI = G->getTargetLoweringInfo();
4822 const char *Name = TLI.getTargetNodeName(getOpcode());
4823 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004824 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004825 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004826 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004827
Dan Gohmane8be6c62008-07-17 19:10:17 +00004828#ifndef NDEBUG
4829 case ISD::DELETED_NODE:
4830 return "<<Deleted Node!>>";
4831#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004832 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004833 case ISD::MEMBARRIER: return "MemBarrier";
Dale Johannesene00a8a22008-08-28 02:44:49 +00004834 case ISD::ATOMIC_CMP_SWAP_8: return "AtomicCmpSwap8";
4835 case ISD::ATOMIC_SWAP_8: return "AtomicSwap8";
4836 case ISD::ATOMIC_LOAD_ADD_8: return "AtomicLoadAdd8";
4837 case ISD::ATOMIC_LOAD_SUB_8: return "AtomicLoadSub8";
4838 case ISD::ATOMIC_LOAD_AND_8: return "AtomicLoadAnd8";
4839 case ISD::ATOMIC_LOAD_OR_8: return "AtomicLoadOr8";
4840 case ISD::ATOMIC_LOAD_XOR_8: return "AtomicLoadXor8";
4841 case ISD::ATOMIC_LOAD_NAND_8: return "AtomicLoadNand8";
4842 case ISD::ATOMIC_LOAD_MIN_8: return "AtomicLoadMin8";
4843 case ISD::ATOMIC_LOAD_MAX_8: return "AtomicLoadMax8";
4844 case ISD::ATOMIC_LOAD_UMIN_8: return "AtomicLoadUMin8";
4845 case ISD::ATOMIC_LOAD_UMAX_8: return "AtomicLoadUMax8";
4846 case ISD::ATOMIC_CMP_SWAP_16: return "AtomicCmpSwap16";
4847 case ISD::ATOMIC_SWAP_16: return "AtomicSwap16";
4848 case ISD::ATOMIC_LOAD_ADD_16: return "AtomicLoadAdd16";
4849 case ISD::ATOMIC_LOAD_SUB_16: return "AtomicLoadSub16";
4850 case ISD::ATOMIC_LOAD_AND_16: return "AtomicLoadAnd16";
4851 case ISD::ATOMIC_LOAD_OR_16: return "AtomicLoadOr16";
4852 case ISD::ATOMIC_LOAD_XOR_16: return "AtomicLoadXor16";
4853 case ISD::ATOMIC_LOAD_NAND_16: return "AtomicLoadNand16";
4854 case ISD::ATOMIC_LOAD_MIN_16: return "AtomicLoadMin16";
4855 case ISD::ATOMIC_LOAD_MAX_16: return "AtomicLoadMax16";
4856 case ISD::ATOMIC_LOAD_UMIN_16: return "AtomicLoadUMin16";
4857 case ISD::ATOMIC_LOAD_UMAX_16: return "AtomicLoadUMax16";
4858 case ISD::ATOMIC_CMP_SWAP_32: return "AtomicCmpSwap32";
4859 case ISD::ATOMIC_SWAP_32: return "AtomicSwap32";
4860 case ISD::ATOMIC_LOAD_ADD_32: return "AtomicLoadAdd32";
4861 case ISD::ATOMIC_LOAD_SUB_32: return "AtomicLoadSub32";
4862 case ISD::ATOMIC_LOAD_AND_32: return "AtomicLoadAnd32";
4863 case ISD::ATOMIC_LOAD_OR_32: return "AtomicLoadOr32";
4864 case ISD::ATOMIC_LOAD_XOR_32: return "AtomicLoadXor32";
4865 case ISD::ATOMIC_LOAD_NAND_32: return "AtomicLoadNand32";
4866 case ISD::ATOMIC_LOAD_MIN_32: return "AtomicLoadMin32";
4867 case ISD::ATOMIC_LOAD_MAX_32: return "AtomicLoadMax32";
4868 case ISD::ATOMIC_LOAD_UMIN_32: return "AtomicLoadUMin32";
4869 case ISD::ATOMIC_LOAD_UMAX_32: return "AtomicLoadUMax32";
4870 case ISD::ATOMIC_CMP_SWAP_64: return "AtomicCmpSwap64";
4871 case ISD::ATOMIC_SWAP_64: return "AtomicSwap64";
4872 case ISD::ATOMIC_LOAD_ADD_64: return "AtomicLoadAdd64";
4873 case ISD::ATOMIC_LOAD_SUB_64: return "AtomicLoadSub64";
4874 case ISD::ATOMIC_LOAD_AND_64: return "AtomicLoadAnd64";
4875 case ISD::ATOMIC_LOAD_OR_64: return "AtomicLoadOr64";
4876 case ISD::ATOMIC_LOAD_XOR_64: return "AtomicLoadXor64";
4877 case ISD::ATOMIC_LOAD_NAND_64: return "AtomicLoadNand64";
4878 case ISD::ATOMIC_LOAD_MIN_64: return "AtomicLoadMin64";
4879 case ISD::ATOMIC_LOAD_MAX_64: return "AtomicLoadMax64";
4880 case ISD::ATOMIC_LOAD_UMIN_64: return "AtomicLoadUMin64";
4881 case ISD::ATOMIC_LOAD_UMAX_64: return "AtomicLoadUMax64";
Andrew Lenharth95762122005-03-31 21:24:06 +00004882 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004883 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004884 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004885 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004886 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004887 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004888 case ISD::AssertSext: return "AssertSext";
4889 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004890
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004891 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004892 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004893 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004894 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004895
4896 case ISD::Constant: return "Constant";
4897 case ISD::ConstantFP: return "ConstantFP";
4898 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004899 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004900 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004901 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004902 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004903 case ISD::RETURNADDR: return "RETURNADDR";
4904 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004905 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004906 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4907 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004908 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004909 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004910 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004911 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004912 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00004913 return Intrinsic::getName((Intrinsic::ID)IID);
4914 }
4915 case ISD::INTRINSIC_VOID:
4916 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004917 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004918 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004919 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004920
Chris Lattnerb2827b02006-03-19 00:52:58 +00004921 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004922 case ISD::TargetConstant: return "TargetConstant";
4923 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004924 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004925 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004926 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004927 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004928 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004929 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004930
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004931 case ISD::CopyToReg: return "CopyToReg";
4932 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004933 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004934 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004935 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004936 case ISD::DBG_LABEL: return "dbg_label";
4937 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004938 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004939 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004940 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004941 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004942
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004943 // Unary operators
4944 case ISD::FABS: return "fabs";
4945 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004946 case ISD::FSQRT: return "fsqrt";
4947 case ISD::FSIN: return "fsin";
4948 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004949 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004950 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004951 case ISD::FTRUNC: return "ftrunc";
4952 case ISD::FFLOOR: return "ffloor";
4953 case ISD::FCEIL: return "fceil";
4954 case ISD::FRINT: return "frint";
4955 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004956
4957 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004958 case ISD::ADD: return "add";
4959 case ISD::SUB: return "sub";
4960 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004961 case ISD::MULHU: return "mulhu";
4962 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004963 case ISD::SDIV: return "sdiv";
4964 case ISD::UDIV: return "udiv";
4965 case ISD::SREM: return "srem";
4966 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004967 case ISD::SMUL_LOHI: return "smul_lohi";
4968 case ISD::UMUL_LOHI: return "umul_lohi";
4969 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00004970 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004971 case ISD::AND: return "and";
4972 case ISD::OR: return "or";
4973 case ISD::XOR: return "xor";
4974 case ISD::SHL: return "shl";
4975 case ISD::SRA: return "sra";
4976 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004977 case ISD::ROTL: return "rotl";
4978 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004979 case ISD::FADD: return "fadd";
4980 case ISD::FSUB: return "fsub";
4981 case ISD::FMUL: return "fmul";
4982 case ISD::FDIV: return "fdiv";
4983 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004984 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004985 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004986
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004987 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004988 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004989 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004990 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004991 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004992 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004993 case ISD::CONCAT_VECTORS: return "concat_vectors";
4994 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004995 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004996 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004997 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004998 case ISD::ADDC: return "addc";
4999 case ISD::ADDE: return "adde";
5000 case ISD::SUBC: return "subc";
5001 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005002 case ISD::SHL_PARTS: return "shl_parts";
5003 case ISD::SRA_PARTS: return "sra_parts";
5004 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005005
5006 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5007 case ISD::INSERT_SUBREG: return "insert_subreg";
5008
Chris Lattner7f644642005-04-28 21:44:03 +00005009 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005010 case ISD::SIGN_EXTEND: return "sign_extend";
5011 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005012 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005013 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005014 case ISD::TRUNCATE: return "truncate";
5015 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005016 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005017 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005018 case ISD::FP_EXTEND: return "fp_extend";
5019
5020 case ISD::SINT_TO_FP: return "sint_to_fp";
5021 case ISD::UINT_TO_FP: return "uint_to_fp";
5022 case ISD::FP_TO_SINT: return "fp_to_sint";
5023 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005024 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005025
5026 // Control flow instructions
5027 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005028 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005029 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005030 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005031 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005032 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005033 case ISD::CALLSEQ_START: return "callseq_start";
5034 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005035
5036 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005037 case ISD::LOAD: return "load";
5038 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005039 case ISD::VAARG: return "vaarg";
5040 case ISD::VACOPY: return "vacopy";
5041 case ISD::VAEND: return "vaend";
5042 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005043 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005044 case ISD::EXTRACT_ELEMENT: return "extract_element";
5045 case ISD::BUILD_PAIR: return "build_pair";
5046 case ISD::STACKSAVE: return "stacksave";
5047 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005048 case ISD::TRAP: return "trap";
5049
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005050 // Bit manipulation
5051 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005052 case ISD::CTPOP: return "ctpop";
5053 case ISD::CTTZ: return "cttz";
5054 case ISD::CTLZ: return "ctlz";
5055
Chris Lattner36ce6912005-11-29 06:21:05 +00005056 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005057 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005058 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005059
Duncan Sands36397f52007-07-27 12:58:54 +00005060 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005061 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005062
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005063 case ISD::CONDCODE:
5064 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005065 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005066 case ISD::SETOEQ: return "setoeq";
5067 case ISD::SETOGT: return "setogt";
5068 case ISD::SETOGE: return "setoge";
5069 case ISD::SETOLT: return "setolt";
5070 case ISD::SETOLE: return "setole";
5071 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005072
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005073 case ISD::SETO: return "seto";
5074 case ISD::SETUO: return "setuo";
5075 case ISD::SETUEQ: return "setue";
5076 case ISD::SETUGT: return "setugt";
5077 case ISD::SETUGE: return "setuge";
5078 case ISD::SETULT: return "setult";
5079 case ISD::SETULE: return "setule";
5080 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005081
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005082 case ISD::SETEQ: return "seteq";
5083 case ISD::SETGT: return "setgt";
5084 case ISD::SETGE: return "setge";
5085 case ISD::SETLT: return "setlt";
5086 case ISD::SETLE: return "setle";
5087 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005088 }
5089 }
5090}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005091
Evan Cheng144d8f02006-11-09 17:55:04 +00005092const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005093 switch (AM) {
5094 default:
5095 return "";
5096 case ISD::PRE_INC:
5097 return "<pre-inc>";
5098 case ISD::PRE_DEC:
5099 return "<pre-dec>";
5100 case ISD::POST_INC:
5101 return "<post-inc>";
5102 case ISD::POST_DEC:
5103 return "<post-dec>";
5104 }
5105}
5106
Duncan Sands276dcbd2008-03-21 09:14:45 +00005107std::string ISD::ArgFlagsTy::getArgFlagsString() {
5108 std::string S = "< ";
5109
5110 if (isZExt())
5111 S += "zext ";
5112 if (isSExt())
5113 S += "sext ";
5114 if (isInReg())
5115 S += "inreg ";
5116 if (isSRet())
5117 S += "sret ";
5118 if (isByVal())
5119 S += "byval ";
5120 if (isNest())
5121 S += "nest ";
5122 if (getByValAlign())
5123 S += "byval-align:" + utostr(getByValAlign()) + " ";
5124 if (getOrigAlign())
5125 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5126 if (getByValSize())
5127 S += "byval-size:" + utostr(getByValSize()) + " ";
5128 return S + ">";
5129}
5130
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005131void SDNode::dump() const { dump(0); }
5132void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005133 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005134 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005135}
5136
5137void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5138 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005139
5140 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005141 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005142 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005143 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005144 else
Chris Lattner944fac72008-08-23 22:23:09 +00005145 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005146 }
Chris Lattner944fac72008-08-23 22:23:09 +00005147 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005148
Chris Lattner944fac72008-08-23 22:23:09 +00005149 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005150 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005151 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005152 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005153 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005154 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005155 }
5156
Evan Chengce254432007-12-11 02:08:35 +00005157 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005158 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005159 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005160 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005161 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005162 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005163 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005164 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005165 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005166 }
Chris Lattner944fac72008-08-23 22:23:09 +00005167 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005168 }
5169
Chris Lattnerc3aae252005-01-07 07:46:32 +00005170 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005171 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005172 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005173 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005174 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005175 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005176 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005177 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005178 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005179 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005180 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005181 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005182 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005183 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005184 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005185 OS << '<';
5186 WriteAsOperand(OS, GADN->getGlobal());
5187 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005188 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005189 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005190 else
Chris Lattner944fac72008-08-23 22:23:09 +00005191 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005192 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005193 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005194 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005195 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005196 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005197 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005198 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005199 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005200 else
Chris Lattner944fac72008-08-23 22:23:09 +00005201 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005202 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005203 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005204 else
Chris Lattner944fac72008-08-23 22:23:09 +00005205 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005206 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005207 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005208 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5209 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005210 OS << LBB->getName() << " ";
5211 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005212 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005213 if (G && R->getReg() &&
5214 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005215 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005216 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005217 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005218 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005219 } else if (const ExternalSymbolSDNode *ES =
5220 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005221 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005222 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5223 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005224 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005225 else
Chris Lattner944fac72008-08-23 22:23:09 +00005226 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005227 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5228 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005229 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005230 else
Chris Lattner944fac72008-08-23 22:23:09 +00005231 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005232 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005233 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005234 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005235 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005236 }
5237 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005238 const Value *SrcValue = LD->getSrcValue();
5239 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005240 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005241 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005242 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005243 else
Chris Lattner944fac72008-08-23 22:23:09 +00005244 OS << "null";
5245 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005246
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005247 bool doExt = true;
5248 switch (LD->getExtensionType()) {
5249 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005250 case ISD::EXTLOAD: OS << " <anyext "; break;
5251 case ISD::SEXTLOAD: OS << " <sext "; break;
5252 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005253 }
5254 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005255 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005256
Evan Cheng144d8f02006-11-09 17:55:04 +00005257 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005258 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005259 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005260 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005261 OS << " <volatile>";
5262 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005263 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005264 const Value *SrcValue = ST->getSrcValue();
5265 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005266 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005267 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005268 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005269 else
Chris Lattner944fac72008-08-23 22:23:09 +00005270 OS << "null";
5271 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005272
5273 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005274 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005275
5276 const char *AM = getIndexedModeName(ST->getAddressingMode());
5277 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005278 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005279 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005280 OS << " <volatile>";
5281 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005282 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5283 const Value *SrcValue = AT->getSrcValue();
5284 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005285 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005286 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005287 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005288 else
Chris Lattner944fac72008-08-23 22:23:09 +00005289 OS << "null";
5290 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005291 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005292 OS << " <volatile>";
5293 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005294 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005295}
5296
Chris Lattnerde202b32005-11-09 23:47:37 +00005297static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005298 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005299 if (N->getOperand(i).getNode()->hasOneUse())
5300 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005301 else
Bill Wendling832171c2006-12-07 20:04:42 +00005302 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005303 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005304
Chris Lattnerea946cd2005-01-09 20:38:33 +00005305
Bill Wendling832171c2006-12-07 20:04:42 +00005306 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005307 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005308}
5309
Chris Lattnerc3aae252005-01-07 07:46:32 +00005310void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005311 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005312
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005313 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5314 I != E; ++I) {
5315 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005316 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005317 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005318 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005319
Gabor Greifba36cb52008-08-28 21:40:38 +00005320 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005321
Bill Wendling832171c2006-12-07 20:04:42 +00005322 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005323}
5324
Evan Chengd6594ae2006-09-12 21:00:35 +00005325const Type *ConstantPoolSDNode::getType() const {
5326 if (isMachineConstantPoolEntry())
5327 return Val.MachineCPVal->getType();
5328 return Val.ConstVal->getType();
5329}