blob: cc1acf47db756cd51bf14ce1956595f73f0b9d92 [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"
Chris Lattner0561b3f2005-08-02 19:26:06 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000033#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000034#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000036#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000037#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000038#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000039#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner0b3e5252006-08-15 19:11:05 +000042/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands83ec4b62008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattnerf8dc0612008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Jim Laskey58b968b2005-08-17 20:08:02 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000072}
73
Duncan Sands83ec4b62008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000077
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000113 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Evan Chengefec7512008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Chengbb81d972008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000195/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
200 if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::DBG_LABEL)
202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000326 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
338 ID.AddPointer(Ops->getSDOperand().Val);
339 ID.AddInteger(Ops->getSDOperand().ResNo);
340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345 const SDOperand *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Roman Levenstein9cac5252008-04-16 16:15:27 +0000351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
353/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
355 AddNodeIDOpcode(ID, N->getOpcode());
356 // Add the return value info.
357 AddNodeIDValueTypes(ID, N->getVTList());
358 // Add the operand info.
359 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
360
361 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 switch (N->getOpcode()) {
363 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000364 case ISD::ARG_FLAGS:
365 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
366 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetConstant:
368 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000369 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 break;
371 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000372 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000373 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000377 case ISD::GlobalAddress:
378 case ISD::TargetGlobalTLSAddress:
379 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
381 ID.AddPointer(GA->getGlobal());
382 ID.AddInteger(GA->getOffset());
383 break;
384 }
385 case ISD::BasicBlock:
386 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
387 break;
388 case ISD::Register:
389 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
390 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000391 case ISD::DBG_STOPPOINT: {
392 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
393 ID.AddInteger(DSP->getLine());
394 ID.AddInteger(DSP->getColumn());
395 ID.AddPointer(DSP->getCompileUnit());
396 break;
397 }
Dan Gohman69de1932008-02-06 22:27:42 +0000398 case ISD::SRCVALUE:
399 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
400 break;
401 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000402 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000403 ID.AddPointer(MO.getValue());
404 ID.AddInteger(MO.getFlags());
405 ID.AddInteger(MO.getOffset());
406 ID.AddInteger(MO.getSize());
407 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000408 break;
409 }
410 case ISD::FrameIndex:
411 case ISD::TargetFrameIndex:
412 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
413 break;
414 case ISD::JumpTable:
415 case ISD::TargetJumpTable:
416 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
417 break;
418 case ISD::ConstantPool:
419 case ISD::TargetConstantPool: {
420 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
421 ID.AddInteger(CP->getAlignment());
422 ID.AddInteger(CP->getOffset());
423 if (CP->isMachineConstantPoolEntry())
424 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
425 else
426 ID.AddPointer(CP->getConstVal());
427 break;
428 }
429 case ISD::LOAD: {
430 LoadSDNode *LD = cast<LoadSDNode>(N);
431 ID.AddInteger(LD->getAddressingMode());
432 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 ID.AddInteger(LD->getAlignment());
435 ID.AddInteger(LD->isVolatile());
436 break;
437 }
438 case ISD::STORE: {
439 StoreSDNode *ST = cast<StoreSDNode>(N);
440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 ID.AddInteger(ST->getAlignment());
444 ID.AddInteger(ST->isVolatile());
445 break;
446 }
Mon P Wang28873102008-06-25 08:15:39 +0000447 case ISD::ATOMIC_CMP_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_SWAP:
450 case ISD::ATOMIC_LOAD_SUB:
451 case ISD::ATOMIC_LOAD_AND:
452 case ISD::ATOMIC_LOAD_OR:
453 case ISD::ATOMIC_LOAD_XOR:
454 case ISD::ATOMIC_LOAD_NAND:
455 case ISD::ATOMIC_LOAD_MIN:
456 case ISD::ATOMIC_LOAD_MAX:
457 case ISD::ATOMIC_LOAD_UMIN:
458 case ISD::ATOMIC_LOAD_UMAX: {
459 AtomicSDNode *AT = cast<AtomicSDNode>(N);
460 ID.AddInteger(AT->getAlignment());
461 ID.AddInteger(AT->isVolatile());
462 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000463 }
Mon P Wang28873102008-06-25 08:15:39 +0000464 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000465}
466
467//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000468// SelectionDAG Class
469//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000470
Dan Gohman0e5f1302008-07-07 23:02:41 +0000471inline alist_traits<SDNode, LargestSDNode>::AllocatorType &
472SelectionDAG::getAllocator() {
473 return AllNodes.getTraits().Allocator;
474}
475
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000476/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000477/// SelectionDAG.
478void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000479 // Create a dummy node (which is not added to allnodes), that adds a reference
480 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000481 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000482
Chris Lattner190a4182006-08-04 17:45:20 +0000483 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000484
Chris Lattner190a4182006-08-04 17:45:20 +0000485 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000486 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000487 if (I->use_empty())
488 DeadNodes.push_back(I);
489
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000490 RemoveDeadNodes(DeadNodes);
491
492 // If the root changed (e.g. it was a dead load, update the root).
493 setRoot(Dummy.getValue());
494}
495
496/// RemoveDeadNodes - This method deletes the unreachable nodes in the
497/// given list, and any nodes that become unreachable as a result.
498void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
499 DAGUpdateListener *UpdateListener) {
500
Chris Lattner190a4182006-08-04 17:45:20 +0000501 // Process the worklist, deleting the nodes and adding their uses to the
502 // worklist.
503 while (!DeadNodes.empty()) {
504 SDNode *N = DeadNodes.back();
505 DeadNodes.pop_back();
506
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000507 if (UpdateListener)
508 UpdateListener->NodeDeleted(N, 0);
509
Chris Lattner190a4182006-08-04 17:45:20 +0000510 // Take the node out of the appropriate CSE map.
511 RemoveNodeFromCSEMaps(N);
512
513 // Next, brutally remove the operand list. This is safe to do, as there are
514 // no cycles in the graph.
515 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000516 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000517 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000518
519 // Now that we removed this operand, see if there are no uses of it left.
520 if (Operand->use_empty())
521 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000522 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000523 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000524 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000525 }
Chris Lattner190a4182006-08-04 17:45:20 +0000526 N->OperandList = 0;
527 N->NumOperands = 0;
528
529 // Finally, remove N itself.
530 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000531 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532}
533
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000535 SmallVector<SDNode*, 16> DeadNodes;
536 DeadNodes.push_back(N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000537 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000538}
539
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000540void SelectionDAG::DeleteNode(SDNode *N) {
541 assert(N->use_empty() && "Cannot delete a node that is not dead!");
542
543 // First take this out of the appropriate CSE map.
544 RemoveNodeFromCSEMaps(N);
545
Chris Lattner1e111c72005-09-07 05:37:01 +0000546 // Finally, remove uses due to operands of this node, remove from the
547 // AllNodes list, and delete the node.
548 DeleteNodeNotInCSEMaps(N);
549}
550
551void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
552
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000553 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000554 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000555 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000556 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000557 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000558 }
Chris Lattner65113b22005-11-08 22:07:03 +0000559 N->OperandList = 0;
560 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000561
Dan Gohman0e5f1302008-07-07 23:02:41 +0000562 AllNodes.erase(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000563}
564
Chris Lattner149c58c2005-08-16 18:17:10 +0000565/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
566/// correspond to it. This is useful when we're about to delete or repurpose
567/// the node. We don't want future request for structurally identical nodes
568/// to return N anymore.
569void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000571 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000572 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000573 case ISD::CONDCODE:
574 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
575 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000576 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000577 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
578 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000579 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000580 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000582 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000583 Erased =
584 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000585 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000586 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 MVT VT = cast<VTSDNode>(N)->getVT();
588 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000589 Erased = ExtendedValueTypeNodes.erase(VT);
590 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000591 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
592 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000593 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000594 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000595 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000597 // Remove it from the CSE Map.
598 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000599 break;
600 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000601#ifndef NDEBUG
602 // Verify that the node was actually in one of the CSE maps, unless it has a
603 // flag result (which cannot be CSE'd) or is one of the special cases that are
604 // not subject to CSE.
605 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000606 !N->isTargetOpcode() &&
607 N->getOpcode() != ISD::DBG_LABEL &&
608 N->getOpcode() != ISD::DBG_STOPPOINT &&
609 N->getOpcode() != ISD::EH_LABEL &&
610 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000611 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000612 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000613 assert(0 && "Node is not in map!");
614 }
615#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000616}
617
Chris Lattner8b8749f2005-08-17 19:00:20 +0000618/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
619/// has been taken out and modified in some way. If the specified node already
620/// exists in the CSE maps, do not modify the maps, but return the existing node
621/// instead. If it doesn't exist, add it and return null.
622///
623SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
624 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000625
626 if (N->getValueType(0) == MVT::Flag)
627 return 0; // Never CSE anything that produces a flag.
628
629 switch (N->getOpcode()) {
630 default: break;
631 case ISD::HANDLENODE:
632 case ISD::DBG_LABEL:
633 case ISD::DBG_STOPPOINT:
634 case ISD::EH_LABEL:
635 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000636 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000637 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000638
Chris Lattner9f8cc692005-12-19 22:21:21 +0000639 // Check that remaining values produced are not flags.
640 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
641 if (N->getValueType(i) == MVT::Flag)
642 return 0; // Never CSE anything that produces a flag.
643
Chris Lattnera5682852006-08-07 23:03:03 +0000644 SDNode *New = CSEMap.GetOrInsertNode(N);
645 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000646 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000647}
648
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
650/// were replaced with those specified. If this node is never memoized,
651/// return null, otherwise return a pointer to the slot it would take. If a
652/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000653SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
654 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000655 if (N->getValueType(0) == MVT::Flag)
656 return 0; // Never CSE anything that produces a flag.
657
658 switch (N->getOpcode()) {
659 default: break;
660 case ISD::HANDLENODE:
661 case ISD::DBG_LABEL:
662 case ISD::DBG_STOPPOINT:
663 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000664 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000665 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000666
667 // Check that remaining values produced are not flags.
668 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
669 if (N->getValueType(i) == MVT::Flag)
670 return 0; // Never CSE anything that produces a flag.
671
Chris Lattner63e3f142007-02-04 07:28:00 +0000672 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000673 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000674 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000675 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000676}
677
678/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
679/// were replaced with those specified. If this node is never memoized,
680/// return null, otherwise return a pointer to the slot it would take. If a
681/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000682SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
683 SDOperand Op1, SDOperand Op2,
684 void *&InsertPos) {
685 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000686
687 // Check that remaining values produced are not flags.
688 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
689 if (N->getValueType(i) == MVT::Flag)
690 return 0; // Never CSE anything that produces a flag.
691
Chris Lattner63e3f142007-02-04 07:28:00 +0000692 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000693 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000694 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000695 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
696}
697
698
699/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
700/// were replaced with those specified. If this node is never memoized,
701/// return null, otherwise return a pointer to the slot it would take. If a
702/// node already exists with these operands, the slot will be non-null.
703SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000704 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000705 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000706 if (N->getValueType(0) == MVT::Flag)
707 return 0; // Never CSE anything that produces a flag.
708
709 switch (N->getOpcode()) {
710 default: break;
711 case ISD::HANDLENODE:
712 case ISD::DBG_LABEL:
713 case ISD::DBG_STOPPOINT:
714 case ISD::EH_LABEL:
715 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000716 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000717 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000718
719 // Check that remaining values produced are not flags.
720 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
721 if (N->getValueType(i) == MVT::Flag)
722 return 0; // Never CSE anything that produces a flag.
723
Jim Laskey583bd472006-10-27 23:46:08 +0000724 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000725 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000726
Evan Cheng9629aba2006-10-11 01:47:58 +0000727 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
728 ID.AddInteger(LD->getAddressingMode());
729 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000730 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000731 ID.AddInteger(LD->getAlignment());
732 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000733 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
734 ID.AddInteger(ST->getAddressingMode());
735 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000736 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000737 ID.AddInteger(ST->getAlignment());
738 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000739 }
Jim Laskey583bd472006-10-27 23:46:08 +0000740
Chris Lattnera5682852006-08-07 23:03:03 +0000741 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000742}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000743
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000744/// getMVTAlignment - Compute the default alignment value for the
745/// given type.
746///
747unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
748 const Type *Ty = VT == MVT::iPTR ?
749 PointerType::get(Type::Int8Ty, 0) :
750 VT.getTypeForMVT();
751
752 return TLI.getTargetData()->getABITypeAlignment(Ty);
753}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000754
Chris Lattner78ec3112003-08-11 14:57:33 +0000755SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000756 while (!AllNodes.empty()) {
757 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000758 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000759 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000760 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000761 }
Chris Lattner65113b22005-11-08 22:07:03 +0000762 N->OperandList = 0;
763 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000764 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000765 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000766}
767
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000769 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000770 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000771 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000772 return getNode(ISD::AND, Op.getValueType(), Op,
773 getConstant(Imm, Op.getValueType()));
774}
775
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000777 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000779}
780
Duncan Sands83ec4b62008-06-06 12:08:01 +0000781SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
782 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000783
Evan Cheng0ff39b32008-06-30 07:31:25 +0000784 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000785 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000786 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000787
788 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000789 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000790 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000791 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000792 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000793 SDNode *N = NULL;
794 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000795 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000796 return SDOperand(N, 0);
797 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000798 N = getAllocator().Allocate<ConstantSDNode>();
799 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000800 CSEMap.InsertNode(N, IP);
801 AllNodes.push_back(N);
802 }
803
804 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000806 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000807 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000808 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
809 }
810 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000811}
812
Chris Lattner0bd48932008-01-17 07:00:52 +0000813SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
814 return getConstant(Val, TLI.getPointerTy(), isTarget);
815}
816
817
Duncan Sands83ec4b62008-06-06 12:08:01 +0000818SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
819 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000820
Duncan Sands83ec4b62008-06-06 12:08:01 +0000821 MVT EltVT =
822 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823
Chris Lattnerd8658612005-02-17 20:17:32 +0000824 // Do the map lookup using the actual bit pattern for the floating point
825 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
826 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000827 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000828 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000829 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000830 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000831 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000832 SDNode *N = NULL;
833 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000834 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000835 return SDOperand(N, 0);
836 if (!N) {
Dan Gohman0e5f1302008-07-07 23:02:41 +0000837 N = getAllocator().Allocate<ConstantFPSDNode>();
838 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000839 CSEMap.InsertNode(N, IP);
840 AllNodes.push_back(N);
841 }
842
Dan Gohman7f321562007-06-25 16:23:39 +0000843 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000844 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000845 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000846 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000847 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
848 }
849 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000850}
851
Duncan Sands83ec4b62008-06-06 12:08:01 +0000852SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
853 MVT EltVT =
854 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000855 if (EltVT==MVT::f32)
856 return getConstantFP(APFloat((float)Val), VT, isTarget);
857 else
858 return getConstantFP(APFloat(Val), VT, isTarget);
859}
860
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000862 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000863 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000864 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000865
866 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
867 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000868 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000869 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
870 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
871 }
872
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000873 if (GVar && GVar->isThreadLocal())
874 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
875 else
876 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000877
Jim Laskey583bd472006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000879 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000880 ID.AddPointer(GV);
881 ID.AddInteger(Offset);
882 void *IP = 0;
883 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
884 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000885 SDNode *N = getAllocator().Allocate<GlobalAddressSDNode>();
886 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000887 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000888 AllNodes.push_back(N);
889 return SDOperand(N, 0);
890}
891
Duncan Sands83ec4b62008-06-06 12:08:01 +0000892SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000893 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000894 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000895 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000896 ID.AddInteger(FI);
897 void *IP = 0;
898 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
899 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000900 SDNode *N = getAllocator().Allocate<FrameIndexSDNode>();
901 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000902 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000903 AllNodes.push_back(N);
904 return SDOperand(N, 0);
905}
906
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000908 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000909 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000910 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000911 ID.AddInteger(JTI);
912 void *IP = 0;
913 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
914 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000915 SDNode *N = getAllocator().Allocate<JumpTableSDNode>();
916 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000917 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000918 AllNodes.push_back(N);
919 return SDOperand(N, 0);
920}
921
Duncan Sands83ec4b62008-06-06 12:08:01 +0000922SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000923 unsigned Alignment, int Offset,
924 bool isTarget) {
925 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000926 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000927 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000928 ID.AddInteger(Alignment);
929 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000930 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000931 void *IP = 0;
932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
933 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000934 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
935 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000936 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000937 AllNodes.push_back(N);
938 return SDOperand(N, 0);
939}
940
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941
Duncan Sands83ec4b62008-06-06 12:08:01 +0000942SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000943 unsigned Alignment, int Offset,
944 bool isTarget) {
945 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000946 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000947 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000948 ID.AddInteger(Alignment);
949 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000950 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000951 void *IP = 0;
952 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
953 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000954 SDNode *N = getAllocator().Allocate<ConstantPoolSDNode>();
955 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000956 CSEMap.InsertNode(N, IP);
957 AllNodes.push_back(N);
958 return SDOperand(N, 0);
959}
960
961
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000963 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000964 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000965 ID.AddPointer(MBB);
966 void *IP = 0;
967 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
968 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000969 SDNode *N = getAllocator().Allocate<BasicBlockSDNode>();
970 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000971 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 AllNodes.push_back(N);
973 return SDOperand(N, 0);
974}
975
Duncan Sands276dcbd2008-03-21 09:14:45 +0000976SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
977 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000978 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000979 ID.AddInteger(Flags.getRawBits());
980 void *IP = 0;
981 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
982 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000983 SDNode *N = getAllocator().Allocate<ARG_FLAGSSDNode>();
984 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000985 CSEMap.InsertNode(N, IP);
986 AllNodes.push_back(N);
987 return SDOperand(N, 0);
988}
989
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990SDOperand SelectionDAG::getValueType(MVT VT) {
991 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
992 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000993
Duncan Sands83ec4b62008-06-06 12:08:01 +0000994 SDNode *&N = VT.isExtended() ?
995 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000996
997 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +0000998 N = getAllocator().Allocate<VTSDNode>();
999 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001000 AllNodes.push_back(N);
1001 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001002}
1003
Duncan Sands83ec4b62008-06-06 12:08:01 +00001004SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 SDNode *&N = ExternalSymbols[Sym];
1006 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001007 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1008 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001009 AllNodes.push_back(N);
1010 return SDOperand(N, 0);
1011}
1012
Duncan Sands83ec4b62008-06-06 12:08:01 +00001013SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001014 SDNode *&N = TargetExternalSymbols[Sym];
1015 if (N) return SDOperand(N, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001016 N = getAllocator().Allocate<ExternalSymbolSDNode>();
1017 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 AllNodes.push_back(N);
1019 return SDOperand(N, 0);
1020}
1021
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001022SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
1023 if ((unsigned)Cond >= CondCodeNodes.size())
1024 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025
Chris Lattner079a27a2005-08-09 20:40:02 +00001026 if (CondCodeNodes[Cond] == 0) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001027 CondCodeSDNode *N = getAllocator().Allocate<CondCodeSDNode>();
1028 new (N) CondCodeSDNode(Cond);
1029 CondCodeNodes[Cond] = N;
1030 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001031 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001032 return SDOperand(CondCodeNodes[Cond], 0);
1033}
1034
Duncan Sands83ec4b62008-06-06 12:08:01 +00001035SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001036 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001037 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001038 ID.AddInteger(RegNo);
1039 void *IP = 0;
1040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1041 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001042 SDNode *N = getAllocator().Allocate<RegisterSDNode>();
1043 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001044 CSEMap.InsertNode(N, IP);
1045 AllNodes.push_back(N);
1046 return SDOperand(N, 0);
1047}
1048
Dan Gohman7f460202008-06-30 20:59:49 +00001049SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1050 unsigned Line, unsigned Col,
1051 const CompileUnitDesc *CU) {
Dan Gohman0e5f1302008-07-07 23:02:41 +00001052 SDNode *N = getAllocator().Allocate<DbgStopPointSDNode>();
1053 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001054 AllNodes.push_back(N);
1055 return SDOperand(N, 0);
1056}
1057
Dan Gohman44066042008-07-01 00:05:16 +00001058SDOperand SelectionDAG::getLabel(unsigned Opcode,
1059 SDOperand Root,
1060 unsigned LabelID) {
1061 FoldingSetNodeID ID;
1062 SDOperand Ops[] = { Root };
1063 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1064 ID.AddInteger(LabelID);
1065 void *IP = 0;
1066 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1067 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001068 SDNode *N = getAllocator().Allocate<LabelSDNode>();
1069 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001070 CSEMap.InsertNode(N, IP);
1071 AllNodes.push_back(N);
1072 return SDOperand(N, 0);
1073}
1074
Dan Gohman69de1932008-02-06 22:27:42 +00001075SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001076 assert((!V || isa<PointerType>(V->getType())) &&
1077 "SrcValue is not a pointer?");
1078
Jim Laskey583bd472006-10-27 23:46:08 +00001079 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001080 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001081 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001082
Chris Lattner61b09412006-08-11 21:01:22 +00001083 void *IP = 0;
1084 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1085 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001086
Dan Gohman0e5f1302008-07-07 23:02:41 +00001087 SDNode *N = getAllocator().Allocate<SrcValueSDNode>();
1088 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001089 CSEMap.InsertNode(N, IP);
1090 AllNodes.push_back(N);
1091 return SDOperand(N, 0);
1092}
1093
Dan Gohman36b5c132008-04-07 19:35:22 +00001094SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001095 const Value *v = MO.getValue();
1096 assert((!v || isa<PointerType>(v->getType())) &&
1097 "SrcValue is not a pointer?");
1098
1099 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001100 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001101 ID.AddPointer(v);
1102 ID.AddInteger(MO.getFlags());
1103 ID.AddInteger(MO.getOffset());
1104 ID.AddInteger(MO.getSize());
1105 ID.AddInteger(MO.getAlignment());
1106
1107 void *IP = 0;
1108 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1109 return SDOperand(E, 0);
1110
Dan Gohman0e5f1302008-07-07 23:02:41 +00001111 SDNode *N = getAllocator().Allocate<MemOperandSDNode>();
1112 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001113 CSEMap.InsertNode(N, IP);
1114 AllNodes.push_back(N);
1115 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001116}
1117
Chris Lattner37ce9df2007-10-15 17:47:20 +00001118/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1119/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001120SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001121 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001122 unsigned ByteSize = VT.getSizeInBits()/8;
1123 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001124 unsigned StackAlign =
1125 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1126
Chris Lattner37ce9df2007-10-15 17:47:20 +00001127 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1128 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1129}
1130
Duncan Sands83ec4b62008-06-06 12:08:01 +00001131SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001132 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 // These setcc operations always fold.
1134 switch (Cond) {
1135 default: break;
1136 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001137 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001139 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001140
1141 case ISD::SETOEQ:
1142 case ISD::SETOGT:
1143 case ISD::SETOGE:
1144 case ISD::SETOLT:
1145 case ISD::SETOLE:
1146 case ISD::SETONE:
1147 case ISD::SETO:
1148 case ISD::SETUO:
1149 case ISD::SETUEQ:
1150 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001151 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001152 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001154
Chris Lattner67255a12005-04-07 18:14:58 +00001155 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001156 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001157 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001158 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001159
Chris Lattnerc3aae252005-01-07 07:46:32 +00001160 switch (Cond) {
1161 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001162 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1163 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001164 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1165 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1166 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1167 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1168 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1169 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1170 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1171 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001173 }
Chris Lattner67255a12005-04-07 18:14:58 +00001174 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001175 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001177 // No compile time operations on this type yet.
1178 if (N1C->getValueType(0) == MVT::ppcf128)
1179 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001180
1181 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001182 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001183 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001184 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1185 return getNode(ISD::UNDEF, VT);
1186 // fall through
1187 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1188 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1189 return getNode(ISD::UNDEF, VT);
1190 // fall through
1191 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001192 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001193 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1194 return getNode(ISD::UNDEF, VT);
1195 // fall through
1196 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1197 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1198 return getNode(ISD::UNDEF, VT);
1199 // fall through
1200 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1201 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1202 return getNode(ISD::UNDEF, VT);
1203 // fall through
1204 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001205 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001206 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1207 return getNode(ISD::UNDEF, VT);
1208 // fall through
1209 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001210 R==APFloat::cmpEqual, VT);
1211 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1212 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1213 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1214 R==APFloat::cmpEqual, VT);
1215 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1216 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1217 R==APFloat::cmpLessThan, VT);
1218 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1219 R==APFloat::cmpUnordered, VT);
1220 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1221 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001222 }
1223 } else {
1224 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001225 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001227 }
1228
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001229 // Could not fold it.
1230 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231}
1232
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001233/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1234/// use this predicate to simplify operations downstream.
1235bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1236 unsigned BitWidth = Op.getValueSizeInBits();
1237 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1238}
1239
Dan Gohmanea859be2007-06-22 14:59:07 +00001240/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1241/// this predicate to simplify operations downstream. Mask is known to be zero
1242/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001243bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001244 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001245 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001246 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1247 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1248 return (KnownZero & Mask) == Mask;
1249}
1250
1251/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1252/// known to be either zero or one and return them in the KnownZero/KnownOne
1253/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1254/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001255void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001256 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001257 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001258 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001259 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001260 "Mask size mismatches value type size!");
1261
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001262 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001263 if (Depth == 6 || Mask == 0)
1264 return; // Limit search depth.
1265
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001266 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001267
1268 switch (Op.getOpcode()) {
1269 case ISD::Constant:
1270 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001271 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001272 KnownZero = ~KnownOne & Mask;
1273 return;
1274 case ISD::AND:
1275 // If either the LHS or the RHS are Zero, the result is zero.
1276 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001277 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1278 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001279 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1280 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1281
1282 // Output known-1 bits are only known if set in both the LHS & RHS.
1283 KnownOne &= KnownOne2;
1284 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1285 KnownZero |= KnownZero2;
1286 return;
1287 case ISD::OR:
1288 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001289 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1290 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1292 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1293
1294 // Output known-0 bits are only known if clear in both the LHS & RHS.
1295 KnownZero &= KnownZero2;
1296 // Output known-1 are known to be set if set in either the LHS | RHS.
1297 KnownOne |= KnownOne2;
1298 return;
1299 case ISD::XOR: {
1300 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1301 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1302 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1303 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1304
1305 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001306 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001307 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1308 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1309 KnownZero = KnownZeroOut;
1310 return;
1311 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001312 case ISD::MUL: {
1313 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1314 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1315 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1318
1319 // If low bits are zero in either operand, output low known-0 bits.
1320 // Also compute a conserative estimate for high known-0 bits.
1321 // More trickiness is possible, but this is sufficient for the
1322 // interesting case of alignment computation.
1323 KnownOne.clear();
1324 unsigned TrailZ = KnownZero.countTrailingOnes() +
1325 KnownZero2.countTrailingOnes();
1326 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001327 KnownZero2.countLeadingOnes(),
1328 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001329
1330 TrailZ = std::min(TrailZ, BitWidth);
1331 LeadZ = std::min(LeadZ, BitWidth);
1332 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1333 APInt::getHighBitsSet(BitWidth, LeadZ);
1334 KnownZero &= Mask;
1335 return;
1336 }
1337 case ISD::UDIV: {
1338 // For the purposes of computing leading zeros we can conservatively
1339 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001340 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001341 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1342 ComputeMaskedBits(Op.getOperand(0),
1343 AllOnes, KnownZero2, KnownOne2, Depth+1);
1344 unsigned LeadZ = KnownZero2.countLeadingOnes();
1345
1346 KnownOne2.clear();
1347 KnownZero2.clear();
1348 ComputeMaskedBits(Op.getOperand(1),
1349 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001350 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1351 if (RHSUnknownLeadingOnes != BitWidth)
1352 LeadZ = std::min(BitWidth,
1353 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001354
1355 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1356 return;
1357 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001358 case ISD::SELECT:
1359 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1360 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1361 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1362 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1363
1364 // Only known if known in both the LHS and RHS.
1365 KnownOne &= KnownOne2;
1366 KnownZero &= KnownZero2;
1367 return;
1368 case ISD::SELECT_CC:
1369 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1370 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1371 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1372 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1373
1374 // Only known if known in both the LHS and RHS.
1375 KnownOne &= KnownOne2;
1376 KnownZero &= KnownZero2;
1377 return;
1378 case ISD::SETCC:
1379 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001380 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1381 BitWidth > 1)
1382 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001383 return;
1384 case ISD::SHL:
1385 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1386 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001387 unsigned ShAmt = SA->getValue();
1388
1389 // If the shift count is an invalid immediate, don't do anything.
1390 if (ShAmt >= BitWidth)
1391 return;
1392
1393 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001394 KnownZero, KnownOne, Depth+1);
1395 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001396 KnownZero <<= ShAmt;
1397 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001398 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001399 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001400 }
1401 return;
1402 case ISD::SRL:
1403 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1404 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001405 unsigned ShAmt = SA->getValue();
1406
Dan Gohmand4cf9922008-02-26 18:50:50 +00001407 // If the shift count is an invalid immediate, don't do anything.
1408 if (ShAmt >= BitWidth)
1409 return;
1410
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001411 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001412 KnownZero, KnownOne, Depth+1);
1413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001414 KnownZero = KnownZero.lshr(ShAmt);
1415 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001416
Dan Gohman72d2fd52008-02-13 22:43:25 +00001417 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 KnownZero |= HighBits; // High bits known zero.
1419 }
1420 return;
1421 case ISD::SRA:
1422 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001423 unsigned ShAmt = SA->getValue();
1424
Dan Gohmand4cf9922008-02-26 18:50:50 +00001425 // If the shift count is an invalid immediate, don't do anything.
1426 if (ShAmt >= BitWidth)
1427 return;
1428
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001429 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 // If any of the demanded bits are produced by the sign extension, we also
1431 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001432 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1433 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001434 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001435
1436 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1437 Depth+1);
1438 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001439 KnownZero = KnownZero.lshr(ShAmt);
1440 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001441
1442 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001443 APInt SignBit = APInt::getSignBit(BitWidth);
1444 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001445
Dan Gohmanca93a432008-02-20 16:30:17 +00001446 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001447 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001448 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001449 KnownOne |= HighBits; // New bits are known one.
1450 }
1451 }
1452 return;
1453 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001454 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1455 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001456
1457 // Sign extension. Compute the demanded bits in the result that are not
1458 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001459 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001460
Dan Gohman977a76f2008-02-13 22:28:48 +00001461 APInt InSignBit = APInt::getSignBit(EBits);
1462 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001463
1464 // If the sign extended bits are demanded, we know that the sign
1465 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001466 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001467 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001468 InputDemandedBits |= InSignBit;
1469
1470 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1471 KnownZero, KnownOne, Depth+1);
1472 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1473
1474 // If the sign bit of the input is known set or clear, then we know the
1475 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001476 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001477 KnownZero |= NewBits;
1478 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001479 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001480 KnownOne |= NewBits;
1481 KnownZero &= ~NewBits;
1482 } else { // Input sign bit unknown
1483 KnownZero &= ~NewBits;
1484 KnownOne &= ~NewBits;
1485 }
1486 return;
1487 }
1488 case ISD::CTTZ:
1489 case ISD::CTLZ:
1490 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001491 unsigned LowBits = Log2_32(BitWidth)+1;
1492 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001493 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001494 return;
1495 }
1496 case ISD::LOAD: {
1497 if (ISD::isZEXTLoad(Op.Val)) {
1498 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001499 MVT VT = LD->getMemoryVT();
1500 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001501 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001502 }
1503 return;
1504 }
1505 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001506 MVT InVT = Op.getOperand(0).getValueType();
1507 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001508 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1509 APInt InMask = Mask;
1510 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001511 KnownZero.trunc(InBits);
1512 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001513 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001514 KnownZero.zext(BitWidth);
1515 KnownOne.zext(BitWidth);
1516 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001517 return;
1518 }
1519 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001520 MVT InVT = Op.getOperand(0).getValueType();
1521 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001522 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001523 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1524 APInt InMask = Mask;
1525 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001526
1527 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001528 // bit is demanded. Temporarily set this bit in the mask for our callee.
1529 if (NewBits.getBoolValue())
1530 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001531
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001532 KnownZero.trunc(InBits);
1533 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001534 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1535
1536 // Note if the sign bit is known to be zero or one.
1537 bool SignBitKnownZero = KnownZero.isNegative();
1538 bool SignBitKnownOne = KnownOne.isNegative();
1539 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1540 "Sign bit can't be known to be both zero and one!");
1541
1542 // If the sign bit wasn't actually demanded by our caller, we don't
1543 // want it set in the KnownZero and KnownOne result values. Reset the
1544 // mask and reapply it to the result values.
1545 InMask = Mask;
1546 InMask.trunc(InBits);
1547 KnownZero &= InMask;
1548 KnownOne &= InMask;
1549
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001550 KnownZero.zext(BitWidth);
1551 KnownOne.zext(BitWidth);
1552
Dan Gohman977a76f2008-02-13 22:28:48 +00001553 // If the sign bit is known zero or one, the top bits match.
1554 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001556 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001557 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001558 return;
1559 }
1560 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001561 MVT InVT = Op.getOperand(0).getValueType();
1562 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001563 APInt InMask = Mask;
1564 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001565 KnownZero.trunc(InBits);
1566 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001567 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001568 KnownZero.zext(BitWidth);
1569 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 return;
1571 }
1572 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001573 MVT InVT = Op.getOperand(0).getValueType();
1574 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001575 APInt InMask = Mask;
1576 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001577 KnownZero.zext(InBits);
1578 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001579 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001580 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001581 KnownZero.trunc(BitWidth);
1582 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001583 break;
1584 }
1585 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1587 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1589 KnownOne, Depth+1);
1590 KnownZero |= (~InMask) & Mask;
1591 return;
1592 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001593 case ISD::FGETSIGN:
1594 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001595 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001596 return;
1597
Dan Gohman23e8b712008-04-28 17:02:21 +00001598 case ISD::SUB: {
1599 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1600 // We know that the top bits of C-X are clear if X contains less bits
1601 // than C (i.e. no wrap-around can happen). For example, 20-X is
1602 // positive if we can prove that X is >= 0 and < 16.
1603 if (CLHS->getAPIntValue().isNonNegative()) {
1604 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1605 // NLZ can't be BitWidth with no sign bit
1606 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1607 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1608 Depth+1);
1609
1610 // If all of the MaskV bits are known to be zero, then we know the
1611 // output top bits are zero, because we now know that the output is
1612 // from [0-C].
1613 if ((KnownZero2 & MaskV) == MaskV) {
1614 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1615 // Top bits known zero.
1616 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1617 }
1618 }
1619 }
1620 }
1621 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001623 // Output known-0 bits are known if clear or set in both the low clear bits
1624 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1625 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001626 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1627 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1628 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1629 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1630
1631 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1632 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1633 KnownZeroOut = std::min(KnownZeroOut,
1634 KnownZero2.countTrailingOnes());
1635
1636 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 return;
1638 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001639 case ISD::SREM:
1640 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001641 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001642 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001643 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001644 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1645 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001646
Dan Gohman23e8b712008-04-28 17:02:21 +00001647 // The sign of a remainder is equal to the sign of the first
1648 // operand (zero being positive).
1649 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1650 KnownZero2 |= ~LowBits;
1651 else if (KnownOne2[BitWidth-1])
1652 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001653
Dan Gohman23e8b712008-04-28 17:02:21 +00001654 KnownZero |= KnownZero2 & Mask;
1655 KnownOne |= KnownOne2 & Mask;
1656
1657 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001658 }
1659 }
1660 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001661 case ISD::UREM: {
1662 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001663 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001664 if (RA.isPowerOf2()) {
1665 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001666 APInt Mask2 = LowBits & Mask;
1667 KnownZero |= ~LowBits & Mask;
1668 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1669 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1670 break;
1671 }
1672 }
1673
1674 // Since the result is less than or equal to either operand, any leading
1675 // zero bits in either operand must also exist in the result.
1676 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1677 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1678 Depth+1);
1679 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1680 Depth+1);
1681
1682 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1683 KnownZero2.countLeadingOnes());
1684 KnownOne.clear();
1685 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1686 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001687 }
1688 default:
1689 // Allow the target to implement this method for its nodes.
1690 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1691 case ISD::INTRINSIC_WO_CHAIN:
1692 case ISD::INTRINSIC_W_CHAIN:
1693 case ISD::INTRINSIC_VOID:
1694 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1695 }
1696 return;
1697 }
1698}
1699
1700/// ComputeNumSignBits - Return the number of times the sign bit of the
1701/// register is replicated into the other bits. We know that at least 1 bit
1702/// is always equal to the sign bit (itself), but other cases can give us
1703/// information. For example, immediately after an "SRA X, 2", we know that
1704/// the top 3 bits are all equal to each other, so we return 3.
1705unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001706 MVT VT = Op.getValueType();
1707 assert(VT.isInteger() && "Invalid VT!");
1708 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001709 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001710 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001711
1712 if (Depth == 6)
1713 return 1; // Limit search depth.
1714
1715 switch (Op.getOpcode()) {
1716 default: break;
1717 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001718 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001719 return VTBits-Tmp+1;
1720 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001722 return VTBits-Tmp;
1723
1724 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001725 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1726 // If negative, return # leading ones.
1727 if (Val.isNegative())
1728 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001729
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001730 // Return # leading zeros.
1731 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001732 }
1733
1734 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001735 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001736 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1737
1738 case ISD::SIGN_EXTEND_INREG:
1739 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 Tmp = VTBits-Tmp+1;
1742
1743 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1744 return std::max(Tmp, Tmp2);
1745
1746 case ISD::SRA:
1747 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1748 // SRA X, C -> adds C sign bits.
1749 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1750 Tmp += C->getValue();
1751 if (Tmp > VTBits) Tmp = VTBits;
1752 }
1753 return Tmp;
1754 case ISD::SHL:
1755 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1756 // shl destroys sign bits.
1757 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1758 if (C->getValue() >= VTBits || // Bad shift.
1759 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1760 return Tmp - C->getValue();
1761 }
1762 break;
1763 case ISD::AND:
1764 case ISD::OR:
1765 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001766 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001767 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001768 if (Tmp != 1) {
1769 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1770 FirstAnswer = std::min(Tmp, Tmp2);
1771 // We computed what we know about the sign bits as our first
1772 // answer. Now proceed to the generic code that uses
1773 // ComputeMaskedBits, and pick whichever answer is better.
1774 }
1775 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001776
1777 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001778 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001779 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001780 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001781 return std::min(Tmp, Tmp2);
1782
1783 case ISD::SETCC:
1784 // If setcc returns 0/-1, all bits are sign bits.
1785 if (TLI.getSetCCResultContents() ==
1786 TargetLowering::ZeroOrNegativeOneSetCCResult)
1787 return VTBits;
1788 break;
1789 case ISD::ROTL:
1790 case ISD::ROTR:
1791 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1792 unsigned RotAmt = C->getValue() & (VTBits-1);
1793
1794 // Handle rotate right by N like a rotate left by 32-N.
1795 if (Op.getOpcode() == ISD::ROTR)
1796 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1797
1798 // If we aren't rotating out all of the known-in sign bits, return the
1799 // number that are left. This handles rotl(sext(x), 1) for example.
1800 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1801 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1802 }
1803 break;
1804 case ISD::ADD:
1805 // Add can have at most one carry bit. Thus we know that the output
1806 // is, at worst, one more bit than the inputs.
1807 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1808 if (Tmp == 1) return 1; // Early out.
1809
1810 // Special case decrementing a value (ADD X, -1):
1811 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1812 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001813 APInt KnownZero, KnownOne;
1814 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1816
1817 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1818 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001819 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001820 return VTBits;
1821
1822 // If we are subtracting one from a positive number, there is no carry
1823 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001824 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001825 return Tmp;
1826 }
1827
1828 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1829 if (Tmp2 == 1) return 1;
1830 return std::min(Tmp, Tmp2)-1;
1831 break;
1832
1833 case ISD::SUB:
1834 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1835 if (Tmp2 == 1) return 1;
1836
1837 // Handle NEG.
1838 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001839 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001840 APInt KnownZero, KnownOne;
1841 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001842 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1843 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1844 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001845 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001846 return VTBits;
1847
1848 // If the input is known to be positive (the sign bit is known clear),
1849 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001850 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 return Tmp2;
1852
1853 // Otherwise, we treat this like a SUB.
1854 }
1855
1856 // Sub can have at most one carry bit. Thus we know that the output
1857 // is, at worst, one more bit than the inputs.
1858 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1859 if (Tmp == 1) return 1; // Early out.
1860 return std::min(Tmp, Tmp2)-1;
1861 break;
1862 case ISD::TRUNCATE:
1863 // FIXME: it's tricky to do anything useful for this, but it is an important
1864 // case for targets like X86.
1865 break;
1866 }
1867
1868 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1869 if (Op.getOpcode() == ISD::LOAD) {
1870 LoadSDNode *LD = cast<LoadSDNode>(Op);
1871 unsigned ExtType = LD->getExtensionType();
1872 switch (ExtType) {
1873 default: break;
1874 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001875 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001876 return VTBits-Tmp+1;
1877 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001878 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001879 return VTBits-Tmp;
1880 }
1881 }
1882
1883 // Allow the target to implement this method for its nodes.
1884 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1885 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1886 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1887 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1888 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001889 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001890 }
1891
1892 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1893 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001894 APInt KnownZero, KnownOne;
1895 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001896 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1897
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001898 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001900 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 Mask = KnownOne;
1902 } else {
1903 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001904 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001905 }
1906
1907 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1908 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001909 Mask = ~Mask;
1910 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 // Return # leading zeros. We use 'min' here in case Val was zero before
1912 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001913 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001914}
1915
Chris Lattner51dabfb2006-10-14 00:41:01 +00001916
Evan Chenga844bde2008-02-02 04:07:54 +00001917bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1918 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1919 if (!GA) return false;
1920 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1921 if (!GV) return false;
1922 MachineModuleInfo *MMI = getMachineModuleInfo();
1923 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1924}
1925
1926
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001927/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1928/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001929SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001930 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001931 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001932 SDOperand Idx = PermMask.getOperand(i);
1933 if (Idx.getOpcode() == ISD::UNDEF)
1934 return getNode(ISD::UNDEF, VT.getVectorElementType());
1935 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001936 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001937 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1938 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001939
1940 if (V.getOpcode() == ISD::BIT_CONVERT) {
1941 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001942 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001943 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001944 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001945 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001946 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001947 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001948 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001949 return V.getOperand(Index);
1950 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1951 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001952 return SDOperand();
1953}
1954
1955
Chris Lattnerc3aae252005-01-07 07:46:32 +00001956/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001957///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001958SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001959 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001960 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001961 void *IP = 0;
1962 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1963 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001964 SDNode *N = getAllocator().Allocate<SDNode>();
1965 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001966 CSEMap.InsertNode(N, IP);
1967
1968 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001969 return SDOperand(N, 0);
1970}
1971
Duncan Sands83ec4b62008-06-06 12:08:01 +00001972SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001973 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001974 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001975 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001976 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001977 switch (Opcode) {
1978 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001979 case ISD::SIGN_EXTEND:
1980 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001981 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001982 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001983 case ISD::TRUNCATE:
1984 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001985 case ISD::UINT_TO_FP:
1986 case ISD::SINT_TO_FP: {
1987 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001988 // No compile time operations on this type.
1989 if (VT==MVT::ppcf128)
1990 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001991 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1992 (void)apf.convertFromAPInt(Val,
1993 Opcode==ISD::SINT_TO_FP,
1994 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001995 return getConstantFP(apf, VT);
1996 }
Chris Lattner94683772005-12-23 05:30:37 +00001997 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001998 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001999 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002000 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002001 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002002 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002003 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002004 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002005 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002006 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002007 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002008 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002009 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002010 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002011 }
2012 }
2013
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002014 // Constant fold unary operations with a floating point constant operand.
2015 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2016 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002017 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002018 switch (Opcode) {
2019 case ISD::FNEG:
2020 V.changeSign();
2021 return getConstantFP(V, VT);
2022 case ISD::FABS:
2023 V.clearSign();
2024 return getConstantFP(V, VT);
2025 case ISD::FP_ROUND:
2026 case ISD::FP_EXTEND:
2027 // This can return overflow, underflow, or inexact; we don't care.
2028 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002029 (void)V.convert(*MVTToAPFloatSemantics(VT),
2030 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002031 return getConstantFP(V, VT);
2032 case ISD::FP_TO_SINT:
2033 case ISD::FP_TO_UINT: {
2034 integerPart x;
2035 assert(integerPartWidth >= 64);
2036 // FIXME need to be more flexible about rounding mode.
2037 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2038 Opcode==ISD::FP_TO_SINT,
2039 APFloat::rmTowardZero);
2040 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2041 break;
2042 return getConstant(x, VT);
2043 }
2044 case ISD::BIT_CONVERT:
2045 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2046 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2047 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2048 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002049 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002050 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002051 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002052 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002053
2054 unsigned OpOpcode = Operand.Val->getOpcode();
2055 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002056 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002057 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002058 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002059 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002060 assert(VT.isFloatingPoint() &&
2061 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002062 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002063 if (Operand.getOpcode() == ISD::UNDEF)
2064 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002065 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002066 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002067 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002068 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002069 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002070 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002071 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002072 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2073 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2074 break;
2075 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002076 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002077 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002078 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002079 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002080 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002081 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002082 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002083 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002084 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002086 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002087 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002088 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002089 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002090 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2091 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2092 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2093 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002094 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002095 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002096 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002097 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002098 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002099 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002100 if (OpOpcode == ISD::TRUNCATE)
2101 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002102 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2103 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002104 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002105 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002106 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002107 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002108 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2109 else
2110 return Operand.Val->getOperand(0);
2111 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 break;
Chris Lattner35481892005-12-23 00:16:34 +00002113 case ISD::BIT_CONVERT:
2114 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002115 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002116 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002117 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002118 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2119 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002120 if (OpOpcode == ISD::UNDEF)
2121 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002122 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002123 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002124 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2125 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002126 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002127 if (OpOpcode == ISD::UNDEF)
2128 return getNode(ISD::UNDEF, VT);
2129 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2130 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2131 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2132 Operand.getConstantOperandVal(1) == 0 &&
2133 Operand.getOperand(0).getValueType() == VT)
2134 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002135 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002136 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002137 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2138 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002139 Operand.Val->getOperand(0));
2140 if (OpOpcode == ISD::FNEG) // --X -> X
2141 return Operand.Val->getOperand(0);
2142 break;
2143 case ISD::FABS:
2144 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2145 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2146 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002147 }
2148
Chris Lattner43247a12005-08-25 19:12:10 +00002149 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002150 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002151 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002152 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002153 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002154 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002155 void *IP = 0;
2156 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2157 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002158 N = getAllocator().Allocate<UnarySDNode>();
2159 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002160 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002161 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002162 N = getAllocator().Allocate<UnarySDNode>();
2163 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002164 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002165 AllNodes.push_back(N);
2166 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002167}
2168
Chris Lattner36019aa2005-04-18 03:48:41 +00002169
2170
Duncan Sands83ec4b62008-06-06 12:08:01 +00002171SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002172 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2174 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002175 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002176 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002177 case ISD::TokenFactor:
2178 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2179 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002180 // Fold trivial token factors.
2181 if (N1.getOpcode() == ISD::EntryToken) return N2;
2182 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002183 break;
Chris Lattner76365122005-01-16 02:23:22 +00002184 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002185 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002186 N1.getValueType() == VT && "Binary operator types must match!");
2187 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2188 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002189 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002190 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002191 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2192 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002193 break;
Chris Lattner76365122005-01-16 02:23:22 +00002194 case ISD::OR:
2195 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002196 case ISD::ADD:
2197 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002198 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002199 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002200 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2201 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002202 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002203 return N1;
2204 break;
Chris Lattner76365122005-01-16 02:23:22 +00002205 case ISD::UDIV:
2206 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002207 case ISD::MULHU:
2208 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002210 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002211 case ISD::MUL:
2212 case ISD::SDIV:
2213 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002214 case ISD::FADD:
2215 case ISD::FSUB:
2216 case ISD::FMUL:
2217 case ISD::FDIV:
2218 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002219 assert(N1.getValueType() == N2.getValueType() &&
2220 N1.getValueType() == VT && "Binary operator types must match!");
2221 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002222 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2223 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 N1.getValueType().isFloatingPoint() &&
2225 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002226 "Invalid FCOPYSIGN!");
2227 break;
Chris Lattner76365122005-01-16 02:23:22 +00002228 case ISD::SHL:
2229 case ISD::SRA:
2230 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002231 case ISD::ROTL:
2232 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002233 assert(VT == N1.getValueType() &&
2234 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002236 "Shifts only work on integers");
2237
2238 // Always fold shifts of i1 values so the code generator doesn't need to
2239 // handle them. Since we know the size of the shift has to be less than the
2240 // size of the value, the shift/rotate count is guaranteed to be zero.
2241 if (VT == MVT::i1)
2242 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002243 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002244 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002245 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002246 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002247 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002248 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002249 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002250 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002251 break;
2252 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002253 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002254 assert(VT.isFloatingPoint() &&
2255 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002256 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002257 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002258 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002259 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002260 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002261 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002262 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002263 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002264 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002265 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002266 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002267 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002268 break;
2269 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002270 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002271 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002272 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002273 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002274 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002275 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002276 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002277
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002278 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002279 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002280 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002281 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002282 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002283 return getConstant(Val, VT);
2284 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002285 break;
2286 }
2287 case ISD::EXTRACT_VECTOR_ELT:
2288 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2289
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002290 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2291 if (N1.getOpcode() == ISD::UNDEF)
2292 return getNode(ISD::UNDEF, VT);
2293
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002294 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2295 // expanding copies of large vectors from registers.
2296 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2297 N1.getNumOperands() > 0) {
2298 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002299 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002300 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2301 N1.getOperand(N2C->getValue() / Factor),
2302 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2303 }
2304
2305 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2306 // expanding large vector constants.
2307 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2308 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002309
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002310 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2311 // operations are lowered to scalars.
2312 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2313 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2314 if (IEC == N2C)
2315 return N1.getOperand(1);
2316 else
2317 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2318 }
2319 break;
2320 case ISD::EXTRACT_ELEMENT:
2321 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002322 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2323 (N1.getValueType().isInteger() == VT.isInteger()) &&
2324 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002325
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002326 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2327 // 64-bit integers into 32-bit parts. Instead of building the extract of
2328 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2329 if (N1.getOpcode() == ISD::BUILD_PAIR)
2330 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002331
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002332 // EXTRACT_ELEMENT of a constant int is also very common.
2333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002334 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002335 unsigned Shift = ElementSize * N2C->getValue();
2336 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2337 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002338 }
2339 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002340 case ISD::EXTRACT_SUBVECTOR:
2341 if (N1.getValueType() == VT) // Trivial extraction.
2342 return N1;
2343 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002344 }
2345
2346 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002347 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002348 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002349 switch (Opcode) {
2350 case ISD::ADD: return getConstant(C1 + C2, VT);
2351 case ISD::SUB: return getConstant(C1 - C2, VT);
2352 case ISD::MUL: return getConstant(C1 * C2, VT);
2353 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002354 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002355 break;
2356 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002357 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002358 break;
2359 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002360 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002361 break;
2362 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002363 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364 break;
2365 case ISD::AND : return getConstant(C1 & C2, VT);
2366 case ISD::OR : return getConstant(C1 | C2, VT);
2367 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002368 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002369 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2370 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2371 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2372 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 default: break;
2374 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002375 } else { // Cannonicalize constant to RHS if commutative
2376 if (isCommutativeBinOp(Opcode)) {
2377 std::swap(N1C, N2C);
2378 std::swap(N1, N2);
2379 }
2380 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002381 }
2382
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002383 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2385 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002386 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002387 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2388 // Cannonicalize constant to RHS if commutative
2389 std::swap(N1CFP, N2CFP);
2390 std::swap(N1, N2);
2391 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002392 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2393 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002394 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002395 case ISD::FADD:
2396 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002397 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002398 return getConstantFP(V1, VT);
2399 break;
2400 case ISD::FSUB:
2401 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2402 if (s!=APFloat::opInvalidOp)
2403 return getConstantFP(V1, VT);
2404 break;
2405 case ISD::FMUL:
2406 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2407 if (s!=APFloat::opInvalidOp)
2408 return getConstantFP(V1, VT);
2409 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002410 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002411 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2412 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2413 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002414 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002415 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002416 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2417 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2418 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002419 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002420 case ISD::FCOPYSIGN:
2421 V1.copySign(V2);
2422 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002423 default: break;
2424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002425 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002426 }
Chris Lattner62b57722006-04-20 05:39:12 +00002427
2428 // Canonicalize an UNDEF to the RHS, even over a constant.
2429 if (N1.getOpcode() == ISD::UNDEF) {
2430 if (isCommutativeBinOp(Opcode)) {
2431 std::swap(N1, N2);
2432 } else {
2433 switch (Opcode) {
2434 case ISD::FP_ROUND_INREG:
2435 case ISD::SIGN_EXTEND_INREG:
2436 case ISD::SUB:
2437 case ISD::FSUB:
2438 case ISD::FDIV:
2439 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002440 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002441 return N1; // fold op(undef, arg2) -> undef
2442 case ISD::UDIV:
2443 case ISD::SDIV:
2444 case ISD::UREM:
2445 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002446 case ISD::SRL:
2447 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002448 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002449 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2450 // For vectors, we can't easily build an all zero vector, just return
2451 // the LHS.
2452 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002453 }
2454 }
2455 }
2456
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002457 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002458 if (N2.getOpcode() == ISD::UNDEF) {
2459 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002460 case ISD::XOR:
2461 if (N1.getOpcode() == ISD::UNDEF)
2462 // Handle undef ^ undef -> 0 special case. This is a common
2463 // idiom (misuse).
2464 return getConstant(0, VT);
2465 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002466 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002467 case ISD::ADDC:
2468 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002469 case ISD::SUB:
2470 case ISD::FADD:
2471 case ISD::FSUB:
2472 case ISD::FMUL:
2473 case ISD::FDIV:
2474 case ISD::FREM:
2475 case ISD::UDIV:
2476 case ISD::SDIV:
2477 case ISD::UREM:
2478 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002479 return N2; // fold op(arg1, undef) -> undef
2480 case ISD::MUL:
2481 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002482 case ISD::SRL:
2483 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002484 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002485 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2486 // For vectors, we can't easily build an all zero vector, just return
2487 // the LHS.
2488 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002489 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002490 if (!VT.isVector())
2491 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002492 // For vectors, we can't easily build an all one vector, just return
2493 // the LHS.
2494 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002495 case ISD::SRA:
2496 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002497 }
2498 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002499
Chris Lattner27e9b412005-05-11 18:57:39 +00002500 // Memoize this node if possible.
2501 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002502 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002503 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002504 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002505 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002506 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002507 void *IP = 0;
2508 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2509 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002510 N = getAllocator().Allocate<BinarySDNode>();
2511 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002512 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002513 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002514 N = getAllocator().Allocate<BinarySDNode>();
2515 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002516 }
2517
Chris Lattnerc3aae252005-01-07 07:46:32 +00002518 AllNodes.push_back(N);
2519 return SDOperand(N, 0);
2520}
2521
Duncan Sands83ec4b62008-06-06 12:08:01 +00002522SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002523 SDOperand N1, SDOperand N2, SDOperand N3) {
2524 // Perform various simplifications.
2525 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2526 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002527 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002528 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002529 // Use FoldSetCC to simplify SETCC's.
2530 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002531 if (Simp.Val) return Simp;
2532 break;
2533 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002534 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002535 if (N1C) {
2536 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002537 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002538 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002539 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002540 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002541
2542 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002543 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002544 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002545 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002546 if (N2C->getValue()) // Unconditional branch
2547 return getNode(ISD::BR, MVT::Other, N1, N3);
2548 else
2549 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002550 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002551 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002552 case ISD::VECTOR_SHUFFLE:
2553 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002555 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002556 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002557 "Illegal VECTOR_SHUFFLE node!");
2558 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002559 case ISD::BIT_CONVERT:
2560 // Fold bit_convert nodes from a type to themselves.
2561 if (N1.getValueType() == VT)
2562 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002563 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002564 }
2565
Chris Lattner43247a12005-08-25 19:12:10 +00002566 // Memoize node if it doesn't produce a flag.
2567 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002568 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002569 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002570 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002571 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002572 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002573 void *IP = 0;
2574 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2575 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00002576 N = getAllocator().Allocate<TernarySDNode>();
2577 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002578 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002579 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00002580 N = getAllocator().Allocate<TernarySDNode>();
2581 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002582 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002583 AllNodes.push_back(N);
2584 return SDOperand(N, 0);
2585}
2586
Duncan Sands83ec4b62008-06-06 12:08:01 +00002587SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002588 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002589 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002590 SDOperand Ops[] = { N1, N2, N3, N4 };
2591 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002592}
2593
Duncan Sands83ec4b62008-06-06 12:08:01 +00002594SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002595 SDOperand N1, SDOperand N2, SDOperand N3,
2596 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002597 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2598 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002599}
2600
Dan Gohman707e0182008-04-12 04:36:06 +00002601/// getMemsetValue - Vectorized representation of the memset value
2602/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2604 unsigned NumBits = VT.isVector() ?
2605 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002606 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002607 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002608 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002609 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002610 Val = (Val << Shift) | Val;
2611 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002612 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002614 return DAG.getConstant(Val, VT);
2615 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002616 }
Evan Chengf0df0312008-05-15 08:39:06 +00002617
2618 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2619 unsigned Shift = 8;
2620 for (unsigned i = NumBits; i > 8; i >>= 1) {
2621 Value = DAG.getNode(ISD::OR, VT,
2622 DAG.getNode(ISD::SHL, VT, Value,
2623 DAG.getConstant(Shift, MVT::i8)), Value);
2624 Shift <<= 1;
2625 }
2626
2627 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002628}
2629
Dan Gohman707e0182008-04-12 04:36:06 +00002630/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2631/// used when a memcpy is turned into a memset when the source is a constant
2632/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002633static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002634 const TargetLowering &TLI,
2635 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002636 // Handle vector with all elements zero.
2637 if (Str.empty()) {
2638 if (VT.isInteger())
2639 return DAG.getConstant(0, VT);
2640 unsigned NumElts = VT.getVectorNumElements();
2641 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2642 return DAG.getNode(ISD::BIT_CONVERT, VT,
2643 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2644 }
2645
Duncan Sands83ec4b62008-06-06 12:08:01 +00002646 assert(!VT.isVector() && "Can't handle vector type here!");
2647 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002648 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002649 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002650 if (TLI.isLittleEndian())
2651 Offset = Offset + MSB - 1;
2652 for (unsigned i = 0; i != MSB; ++i) {
2653 Val = (Val << 8) | (unsigned char)Str[Offset];
2654 Offset += TLI.isLittleEndian() ? -1 : 1;
2655 }
2656 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002657}
2658
Dan Gohman707e0182008-04-12 04:36:06 +00002659/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002660///
Dan Gohman707e0182008-04-12 04:36:06 +00002661static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2662 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002663 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002664 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2665}
2666
Evan Chengf0df0312008-05-15 08:39:06 +00002667/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2668///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002669static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002670 unsigned SrcDelta = 0;
2671 GlobalAddressSDNode *G = NULL;
2672 if (Src.getOpcode() == ISD::GlobalAddress)
2673 G = cast<GlobalAddressSDNode>(Src);
2674 else if (Src.getOpcode() == ISD::ADD &&
2675 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2676 Src.getOperand(1).getOpcode() == ISD::Constant) {
2677 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2678 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2679 }
2680 if (!G)
2681 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002682
Evan Chengf0df0312008-05-15 08:39:06 +00002683 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002684 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2685 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002686
Evan Chengf0df0312008-05-15 08:39:06 +00002687 return false;
2688}
Dan Gohman707e0182008-04-12 04:36:06 +00002689
Evan Chengf0df0312008-05-15 08:39:06 +00002690/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2691/// to replace the memset / memcpy is below the threshold. It also returns the
2692/// types of the sequence of memory ops to perform memset / memcpy.
2693static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002694bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002695 SDOperand Dst, SDOperand Src,
2696 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002697 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002698 SelectionDAG &DAG,
2699 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002700 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002701 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002702 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002703 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002704 if (VT != MVT::iAny) {
2705 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002707 // If source is a string constant, this will require an unaligned load.
2708 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2709 if (Dst.getOpcode() != ISD::FrameIndex) {
2710 // Can't change destination alignment. It requires a unaligned store.
2711 if (AllowUnalign)
2712 VT = MVT::iAny;
2713 } else {
2714 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2715 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2716 if (MFI->isFixedObjectIndex(FI)) {
2717 // Can't change destination alignment. It requires a unaligned store.
2718 if (AllowUnalign)
2719 VT = MVT::iAny;
2720 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002721 // Give the stack frame object a larger alignment if needed.
2722 if (MFI->getObjectAlignment(FI) < NewAlign)
2723 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002724 Align = NewAlign;
2725 }
2726 }
2727 }
2728 }
2729
2730 if (VT == MVT::iAny) {
2731 if (AllowUnalign) {
2732 VT = MVT::i64;
2733 } else {
2734 switch (Align & 7) {
2735 case 0: VT = MVT::i64; break;
2736 case 4: VT = MVT::i32; break;
2737 case 2: VT = MVT::i16; break;
2738 default: VT = MVT::i8; break;
2739 }
2740 }
2741
Duncan Sands83ec4b62008-06-06 12:08:01 +00002742 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002743 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002744 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2745 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002746
Duncan Sands8e4eb092008-06-08 20:54:56 +00002747 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002748 VT = LVT;
2749 }
Dan Gohman707e0182008-04-12 04:36:06 +00002750
2751 unsigned NumMemOps = 0;
2752 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002753 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002754 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002755 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002756 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002757 VT = MVT::i64;
2758 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002759 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2760 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002761 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002763 VTSize >>= 1;
2764 }
Dan Gohman707e0182008-04-12 04:36:06 +00002765 }
Dan Gohman707e0182008-04-12 04:36:06 +00002766
2767 if (++NumMemOps > Limit)
2768 return false;
2769 MemOps.push_back(VT);
2770 Size -= VTSize;
2771 }
2772
2773 return true;
2774}
2775
2776static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2777 SDOperand Chain, SDOperand Dst,
2778 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002779 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002780 const Value *DstSV, uint64_t DstSVOff,
2781 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002782 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2783
Dan Gohman21323f32008-05-29 19:42:22 +00002784 // Expand memcpy to a series of load and store ops if the size operand falls
2785 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002786 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002787 uint64_t Limit = -1;
2788 if (!AlwaysInline)
2789 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002790 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002791 std::string Str;
2792 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002793 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002794 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002795 return SDOperand();
2796
Dan Gohman707e0182008-04-12 04:36:06 +00002797
Evan Cheng0ff39b32008-06-30 07:31:25 +00002798 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002799 SmallVector<SDOperand, 8> OutChains;
2800 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002801 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002802 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002803 MVT VT = MemOps[i];
2804 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002805 SDOperand Value, Store;
2806
Evan Cheng0ff39b32008-06-30 07:31:25 +00002807 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002808 // It's unlikely a store of a vector immediate can be done in a single
2809 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002810 // We also handle store a vector with all zero's.
2811 // FIXME: Handle other cases where store of vector immediate is done in
2812 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002813 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002814 Store = DAG.getStore(Chain, Value,
2815 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002816 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002817 } else {
2818 Value = DAG.getLoad(VT, Chain,
2819 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002820 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002821 Store = DAG.getStore(Chain, Value,
2822 getMemBasePlusOffset(Dst, DstOff, DAG),
2823 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002824 }
2825 OutChains.push_back(Store);
2826 SrcOff += VTSize;
2827 DstOff += VTSize;
2828 }
2829
2830 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2831 &OutChains[0], OutChains.size());
2832}
2833
Dan Gohman21323f32008-05-29 19:42:22 +00002834static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2835 SDOperand Chain, SDOperand Dst,
2836 SDOperand Src, uint64_t Size,
2837 unsigned Align, bool AlwaysInline,
2838 const Value *DstSV, uint64_t DstSVOff,
2839 const Value *SrcSV, uint64_t SrcSVOff){
2840 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2841
2842 // Expand memmove to a series of load and store ops if the size operand falls
2843 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002844 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002845 uint64_t Limit = -1;
2846 if (!AlwaysInline)
2847 Limit = TLI.getMaxStoresPerMemmove();
2848 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002849 std::string Str;
2850 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002851 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002852 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002853 return SDOperand();
2854
Dan Gohman21323f32008-05-29 19:42:22 +00002855 uint64_t SrcOff = 0, DstOff = 0;
2856
2857 SmallVector<SDOperand, 8> LoadValues;
2858 SmallVector<SDOperand, 8> LoadChains;
2859 SmallVector<SDOperand, 8> OutChains;
2860 unsigned NumMemOps = MemOps.size();
2861 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 MVT VT = MemOps[i];
2863 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002864 SDOperand Value, Store;
2865
2866 Value = DAG.getLoad(VT, Chain,
2867 getMemBasePlusOffset(Src, SrcOff, DAG),
2868 SrcSV, SrcSVOff + SrcOff, false, Align);
2869 LoadValues.push_back(Value);
2870 LoadChains.push_back(Value.getValue(1));
2871 SrcOff += VTSize;
2872 }
2873 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2874 &LoadChains[0], LoadChains.size());
2875 OutChains.clear();
2876 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002877 MVT VT = MemOps[i];
2878 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002879 SDOperand Value, Store;
2880
2881 Store = DAG.getStore(Chain, LoadValues[i],
2882 getMemBasePlusOffset(Dst, DstOff, DAG),
2883 DstSV, DstSVOff + DstOff, false, DstAlign);
2884 OutChains.push_back(Store);
2885 DstOff += VTSize;
2886 }
2887
2888 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2889 &OutChains[0], OutChains.size());
2890}
2891
Dan Gohman707e0182008-04-12 04:36:06 +00002892static SDOperand getMemsetStores(SelectionDAG &DAG,
2893 SDOperand Chain, SDOperand Dst,
2894 SDOperand Src, uint64_t Size,
2895 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002896 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002897 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2898
2899 // Expand memset to a series of load/store ops if the size operand
2900 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002901 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002902 std::string Str;
2903 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002904 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002905 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002906 return SDOperand();
2907
2908 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002909 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002910
2911 unsigned NumMemOps = MemOps.size();
2912 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 MVT VT = MemOps[i];
2914 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002915 SDOperand Value = getMemsetValue(Src, VT, DAG);
2916 SDOperand Store = DAG.getStore(Chain, Value,
2917 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002918 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002919 OutChains.push_back(Store);
2920 DstOff += VTSize;
2921 }
2922
2923 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2924 &OutChains[0], OutChains.size());
2925}
2926
2927SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002928 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002929 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002930 const Value *DstSV, uint64_t DstSVOff,
2931 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002932
2933 // Check to see if we should lower the memcpy to loads and stores first.
2934 // For cases within the target-specified limits, this is the best choice.
2935 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2936 if (ConstantSize) {
2937 // Memcpy with size zero? Just return the original chain.
2938 if (ConstantSize->isNullValue())
2939 return Chain;
2940
2941 SDOperand Result =
2942 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002943 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002944 if (Result.Val)
2945 return Result;
2946 }
2947
2948 // Then check to see if we should lower the memcpy with target-specific
2949 // code. If the target chooses to do this, this is the next best.
2950 SDOperand Result =
2951 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2952 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002953 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002954 if (Result.Val)
2955 return Result;
2956
2957 // If we really need inline code and the target declined to provide it,
2958 // use a (potentially long) sequence of loads and stores.
2959 if (AlwaysInline) {
2960 assert(ConstantSize && "AlwaysInline requires a constant size!");
2961 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2962 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002963 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002964 }
2965
2966 // Emit a library call.
2967 TargetLowering::ArgListTy Args;
2968 TargetLowering::ArgListEntry Entry;
2969 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2970 Entry.Node = Dst; Args.push_back(Entry);
2971 Entry.Node = Src; Args.push_back(Entry);
2972 Entry.Node = Size; Args.push_back(Entry);
2973 std::pair<SDOperand,SDOperand> CallResult =
2974 TLI.LowerCallTo(Chain, Type::VoidTy,
2975 false, false, false, CallingConv::C, false,
2976 getExternalSymbol("memcpy", TLI.getPointerTy()),
2977 Args, *this);
2978 return CallResult.second;
2979}
2980
2981SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2982 SDOperand Src, SDOperand Size,
2983 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002984 const Value *DstSV, uint64_t DstSVOff,
2985 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002986
Dan Gohman21323f32008-05-29 19:42:22 +00002987 // Check to see if we should lower the memmove to loads and stores first.
2988 // For cases within the target-specified limits, this is the best choice.
2989 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2990 if (ConstantSize) {
2991 // Memmove with size zero? Just return the original chain.
2992 if (ConstantSize->isNullValue())
2993 return Chain;
2994
2995 SDOperand Result =
2996 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2997 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2998 if (Result.Val)
2999 return Result;
3000 }
Dan Gohman707e0182008-04-12 04:36:06 +00003001
3002 // Then check to see if we should lower the memmove with target-specific
3003 // code. If the target chooses to do this, this is the next best.
3004 SDOperand Result =
3005 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003006 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003007 if (Result.Val)
3008 return Result;
3009
3010 // Emit a library call.
3011 TargetLowering::ArgListTy Args;
3012 TargetLowering::ArgListEntry Entry;
3013 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3014 Entry.Node = Dst; Args.push_back(Entry);
3015 Entry.Node = Src; Args.push_back(Entry);
3016 Entry.Node = Size; Args.push_back(Entry);
3017 std::pair<SDOperand,SDOperand> CallResult =
3018 TLI.LowerCallTo(Chain, Type::VoidTy,
3019 false, false, false, CallingConv::C, false,
3020 getExternalSymbol("memmove", TLI.getPointerTy()),
3021 Args, *this);
3022 return CallResult.second;
3023}
3024
3025SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
3026 SDOperand Src, SDOperand Size,
3027 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003028 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003029
3030 // Check to see if we should lower the memset to stores first.
3031 // For cases within the target-specified limits, this is the best choice.
3032 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3033 if (ConstantSize) {
3034 // Memset with size zero? Just return the original chain.
3035 if (ConstantSize->isNullValue())
3036 return Chain;
3037
3038 SDOperand Result =
3039 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003040 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003041 if (Result.Val)
3042 return Result;
3043 }
3044
3045 // Then check to see if we should lower the memset with target-specific
3046 // code. If the target chooses to do this, this is the next best.
3047 SDOperand Result =
3048 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003049 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003050 if (Result.Val)
3051 return Result;
3052
3053 // Emit a library call.
3054 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3055 TargetLowering::ArgListTy Args;
3056 TargetLowering::ArgListEntry Entry;
3057 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3058 Args.push_back(Entry);
3059 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003060 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003061 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3062 else
3063 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3064 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3065 Args.push_back(Entry);
3066 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3067 Args.push_back(Entry);
3068 std::pair<SDOperand,SDOperand> CallResult =
3069 TLI.LowerCallTo(Chain, Type::VoidTy,
3070 false, false, false, CallingConv::C, false,
3071 getExternalSymbol("memset", TLI.getPointerTy()),
3072 Args, *this);
3073 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003074}
3075
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003076SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003077 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003078 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003079 unsigned Alignment) {
3080 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003081 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003082
3083 MVT VT = Cmp.getValueType();
3084
3085 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3086 Alignment = getMVTAlignment(VT);
3087
3088 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003089 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003090 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003091 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003092 void* IP = 0;
3093 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3094 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003095 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3096 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003097 CSEMap.InsertNode(N, IP);
3098 AllNodes.push_back(N);
3099 return SDOperand(N, 0);
3100}
3101
3102SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003103 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003104 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003105 unsigned Alignment) {
3106 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003107 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3108 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003109 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003110 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3111 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003112 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003113
3114 MVT VT = Val.getValueType();
3115
3116 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3117 Alignment = getMVTAlignment(VT);
3118
3119 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003120 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003121 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003122 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003123 void* IP = 0;
3124 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3125 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003126 SDNode* N = getAllocator().Allocate<AtomicSDNode>();
3127 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003128 CSEMap.InsertNode(N, IP);
3129 AllNodes.push_back(N);
3130 return SDOperand(N, 0);
3131}
3132
Duncan Sands4bdcb612008-07-02 17:40:58 +00003133/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3134/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003135SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003136 bool Simplify) {
3137 if (Simplify && NumOps == 1)
3138 return Ops[0];
3139
3140 SmallVector<MVT, 4> VTs;
3141 VTs.reserve(NumOps);
3142 for (unsigned i = 0; i < NumOps; ++i)
3143 VTs.push_back(Ops[i].getValueType());
3144 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3145}
3146
Duncan Sands14ea39c2008-03-27 20:23:40 +00003147SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003148SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003150 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003152 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003153 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3154 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003155
Duncan Sands14ea39c2008-03-27 20:23:40 +00003156 if (VT == EVT) {
3157 ExtType = ISD::NON_EXTLOAD;
3158 } else if (ExtType == ISD::NON_EXTLOAD) {
3159 assert(VT == EVT && "Non-extending load from different memory type!");
3160 } else {
3161 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003162 if (VT.isVector())
3163 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003164 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003165 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003166 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003168 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003169 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003170 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003171 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003172
3173 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003174 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003175 "Unindexed load with an offset!");
3176
3177 SDVTList VTs = Indexed ?
3178 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3179 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003180 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003181 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003182 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003183 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003184 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003185 ID.AddInteger(Alignment);
3186 ID.AddInteger(isVolatile);
3187 void *IP = 0;
3188 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3189 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003190 SDNode *N = getAllocator().Allocate<LoadSDNode>();
3191 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3192 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003193 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003194 AllNodes.push_back(N);
3195 return SDOperand(N, 0);
3196}
3197
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003199 SDOperand Chain, SDOperand Ptr,
3200 const Value *SV, int SVOffset,
3201 bool isVolatile, unsigned Alignment) {
3202 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003203 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3204 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003205}
3206
Duncan Sands83ec4b62008-06-06 12:08:01 +00003207SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003208 SDOperand Chain, SDOperand Ptr,
3209 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003211 bool isVolatile, unsigned Alignment) {
3212 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003213 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3214 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003215}
3216
Evan Cheng144d8f02006-11-09 17:55:04 +00003217SDOperand
3218SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3219 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003220 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003221 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3222 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003223 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3224 LD->getChain(), Base, Offset, LD->getSrcValue(),
3225 LD->getSrcValueOffset(), LD->getMemoryVT(),
3226 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003227}
3228
Jeff Cohend41b30d2006-11-05 19:31:28 +00003229SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003230 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003231 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003232 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003233
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003234 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3235 Alignment = getMVTAlignment(VT);
3236
Evan Chengad071e12006-10-05 22:57:11 +00003237 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003238 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003239 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003240 FoldingSetNodeID ID;
3241 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003242 ID.AddInteger(ISD::UNINDEXED);
3243 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003244 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003245 ID.AddInteger(Alignment);
3246 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003247 void *IP = 0;
3248 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3249 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003250 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3251 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3252 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003253 CSEMap.InsertNode(N, IP);
3254 AllNodes.push_back(N);
3255 return SDOperand(N, 0);
3256}
3257
Jeff Cohend41b30d2006-11-05 19:31:28 +00003258SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003259 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003260 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003261 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003262 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003263
3264 if (VT == SVT)
3265 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003266
Duncan Sands8e4eb092008-06-08 20:54:56 +00003267 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003268 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003269 "Can't do FP-INT conversion!");
3270
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003271 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3272 Alignment = getMVTAlignment(VT);
3273
Evan Cheng8b2794a2006-10-13 21:14:26 +00003274 SDVTList VTs = getVTList(MVT::Other);
3275 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003276 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003277 FoldingSetNodeID ID;
3278 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003279 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003280 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003281 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003282 ID.AddInteger(Alignment);
3283 ID.AddInteger(isVolatile);
3284 void *IP = 0;
3285 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3286 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003287 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3288 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3289 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003290 CSEMap.InsertNode(N, IP);
3291 AllNodes.push_back(N);
3292 return SDOperand(N, 0);
3293}
3294
Evan Cheng144d8f02006-11-09 17:55:04 +00003295SDOperand
3296SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3297 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003298 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3299 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3300 "Store is already a indexed store!");
3301 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3302 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3303 FoldingSetNodeID ID;
3304 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3305 ID.AddInteger(AM);
3306 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003307 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003308 ID.AddInteger(ST->getAlignment());
3309 ID.AddInteger(ST->isVolatile());
3310 void *IP = 0;
3311 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3312 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003313 SDNode *N = getAllocator().Allocate<StoreSDNode>();
3314 new (N) StoreSDNode(Ops, VTs, AM,
3315 ST->isTruncatingStore(), ST->getMemoryVT(),
3316 ST->getSrcValue(), ST->getSrcValueOffset(),
3317 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003318 CSEMap.InsertNode(N, IP);
3319 AllNodes.push_back(N);
3320 return SDOperand(N, 0);
3321}
3322
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003324 SDOperand Chain, SDOperand Ptr,
3325 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003326 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003327 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003328}
3329
Duncan Sands83ec4b62008-06-06 12:08:01 +00003330SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003331 const SDUse *Ops, unsigned NumOps) {
3332 switch (NumOps) {
3333 case 0: return getNode(Opcode, VT);
3334 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3335 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3336 Ops[1].getSDOperand());
3337 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3338 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3339 default: break;
3340 }
3341
3342 // Copy from an SDUse array into an SDOperand array for use with
3343 // the regular getNode logic.
3344 SmallVector<SDOperand, 8> NewOps;
3345 NewOps.reserve(NumOps);
3346 for (unsigned i = 0; i != NumOps; ++i)
3347 NewOps.push_back(Ops[i].getSDOperand());
3348 return getNode(Opcode, VT, Ops, NumOps);
3349}
3350
3351SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3352 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003353 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003354 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003355 case 1: return getNode(Opcode, VT, Ops[0]);
3356 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3357 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003358 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003359 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003360
Chris Lattneref847df2005-04-09 03:27:28 +00003361 switch (Opcode) {
3362 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003363 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003364 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003365 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3366 "LHS and RHS of condition must have same type!");
3367 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3368 "True and False arms of SelectCC must have same type!");
3369 assert(Ops[2].getValueType() == VT &&
3370 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003371 break;
3372 }
3373 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003374 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003375 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3376 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003377 break;
3378 }
Chris Lattneref847df2005-04-09 03:27:28 +00003379 }
3380
Chris Lattner385328c2005-05-14 07:42:29 +00003381 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003382 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003383 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003384 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003385 FoldingSetNodeID ID;
3386 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003387 void *IP = 0;
3388 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3389 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003390 N = getAllocator().Allocate<SDNode>();
3391 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003392 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003393 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003394 N = getAllocator().Allocate<SDNode>();
3395 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003396 }
Chris Lattneref847df2005-04-09 03:27:28 +00003397 AllNodes.push_back(N);
3398 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003399}
3400
Chris Lattner89c34632005-05-14 06:20:26 +00003401SDOperand SelectionDAG::getNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00003402 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003403 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003404 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3405 Ops, NumOps);
3406}
3407
3408SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003410 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003411 if (NumVTs == 1)
3412 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003413 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3414}
3415
3416SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003417 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003418 if (VTList.NumVTs == 1)
3419 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003420
Chris Lattner5f056bf2005-07-10 01:55:33 +00003421 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003422 // FIXME: figure out how to safely handle things like
3423 // int foo(int x) { return 1 << (x & 255); }
3424 // int bar() { return foo(256); }
3425#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003426 case ISD::SRA_PARTS:
3427 case ISD::SRL_PARTS:
3428 case ISD::SHL_PARTS:
3429 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003430 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003431 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3432 else if (N3.getOpcode() == ISD::AND)
3433 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3434 // If the and is only masking out bits that cannot effect the shift,
3435 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003436 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003437 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3438 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3439 }
3440 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003441#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003442 }
Chris Lattner89c34632005-05-14 06:20:26 +00003443
Chris Lattner43247a12005-08-25 19:12:10 +00003444 // Memoize the node unless it returns a flag.
3445 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003446 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003447 FoldingSetNodeID ID;
3448 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003449 void *IP = 0;
3450 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3451 return SDOperand(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003452 if (NumOps == 1) {
3453 N = getAllocator().Allocate<UnarySDNode>();
3454 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3455 } else if (NumOps == 2) {
3456 N = getAllocator().Allocate<BinarySDNode>();
3457 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3458 } else if (NumOps == 3) {
3459 N = getAllocator().Allocate<TernarySDNode>();
3460 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3461 } else {
3462 N = getAllocator().Allocate<SDNode>();
3463 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3464 }
Chris Lattnera5682852006-08-07 23:03:03 +00003465 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003466 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003467 if (NumOps == 1) {
3468 N = getAllocator().Allocate<UnarySDNode>();
3469 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3470 } else if (NumOps == 2) {
3471 N = getAllocator().Allocate<BinarySDNode>();
3472 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3473 } else if (NumOps == 3) {
3474 N = getAllocator().Allocate<TernarySDNode>();
3475 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3476 } else {
3477 N = getAllocator().Allocate<SDNode>();
3478 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3479 }
Chris Lattner43247a12005-08-25 19:12:10 +00003480 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003481 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003482 return SDOperand(N, 0);
3483}
3484
Dan Gohman08ce9762007-10-08 15:49:58 +00003485SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003486 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003487}
3488
3489SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3490 SDOperand N1) {
3491 SDOperand Ops[] = { N1 };
3492 return getNode(Opcode, VTList, Ops, 1);
3493}
3494
3495SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3496 SDOperand N1, SDOperand N2) {
3497 SDOperand Ops[] = { N1, N2 };
3498 return getNode(Opcode, VTList, Ops, 2);
3499}
3500
3501SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3502 SDOperand N1, SDOperand N2, SDOperand N3) {
3503 SDOperand Ops[] = { N1, N2, N3 };
3504 return getNode(Opcode, VTList, Ops, 3);
3505}
3506
3507SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3508 SDOperand N1, SDOperand N2, SDOperand N3,
3509 SDOperand N4) {
3510 SDOperand Ops[] = { N1, N2, N3, N4 };
3511 return getNode(Opcode, VTList, Ops, 4);
3512}
3513
3514SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3515 SDOperand N1, SDOperand N2, SDOperand N3,
3516 SDOperand N4, SDOperand N5) {
3517 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3518 return getNode(Opcode, VTList, Ops, 5);
3519}
3520
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003522 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003523}
3524
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3526 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003527 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003528 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003529 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003530 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003532 V.push_back(VT1);
3533 V.push_back(VT2);
3534 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003535 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003536}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3538 MVT VT3) {
3539 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003540 E = VTList.end(); I != E; ++I) {
3541 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3542 (*I)[2] == VT3)
3543 return makeVTList(&(*I)[0], 3);
3544 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003545 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003546 V.push_back(VT1);
3547 V.push_back(VT2);
3548 V.push_back(VT3);
3549 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003550 return makeVTList(&(*VTList.begin())[0], 3);
3551}
3552
Duncan Sands83ec4b62008-06-06 12:08:01 +00003553SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003554 switch (NumVTs) {
3555 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003556 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003557 case 2: return getVTList(VTs[0], VTs[1]);
3558 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3559 default: break;
3560 }
3561
Duncan Sands83ec4b62008-06-06 12:08:01 +00003562 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003563 E = VTList.end(); I != E; ++I) {
3564 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3565
3566 bool NoMatch = false;
3567 for (unsigned i = 2; i != NumVTs; ++i)
3568 if (VTs[i] != (*I)[i]) {
3569 NoMatch = true;
3570 break;
3571 }
3572 if (!NoMatch)
3573 return makeVTList(&*I->begin(), NumVTs);
3574 }
3575
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003577 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003578}
3579
3580
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003581/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3582/// specified operands. If the resultant node already exists in the DAG,
3583/// this does not modify the specified node, instead it returns the node that
3584/// already exists. If the resultant node does not exist in the DAG, the
3585/// input node is returned. As a degenerate case, if you specify the same
3586/// input operands as the node already has, the input node is returned.
3587SDOperand SelectionDAG::
3588UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3589 SDNode *N = InN.Val;
3590 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3591
3592 // Check to see if there is no change.
3593 if (Op == N->getOperand(0)) return InN;
3594
3595 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003596 void *InsertPos = 0;
3597 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3598 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003599
3600 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003601 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003602 RemoveNodeFromCSEMaps(N);
3603
3604 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003605 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003606 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003607 N->OperandList[0].setUser(N);
3608 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003609
3610 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003611 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003612 return InN;
3613}
3614
3615SDOperand SelectionDAG::
3616UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3617 SDNode *N = InN.Val;
3618 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3619
3620 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003621 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3622 return InN; // No operands changed, just return the input node.
3623
3624 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003625 void *InsertPos = 0;
3626 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3627 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003628
3629 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003630 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003631 RemoveNodeFromCSEMaps(N);
3632
3633 // Now we update the operands.
3634 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003635 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003636 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003637 N->OperandList[0].setUser(N);
3638 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003639 }
3640 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003641 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003642 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003643 N->OperandList[1].setUser(N);
3644 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003645 }
3646
3647 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003648 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003649 return InN;
3650}
3651
3652SDOperand SelectionDAG::
3653UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003654 SDOperand Ops[] = { Op1, Op2, Op3 };
3655 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003656}
3657
3658SDOperand SelectionDAG::
3659UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3660 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003661 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3662 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003663}
3664
3665SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003666UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3667 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003668 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3669 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003670}
3671
Chris Lattner809ec112006-01-28 10:09:25 +00003672SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003673UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003674 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003675 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003676 "Update with wrong number of operands");
3677
3678 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003679 bool AnyChange = false;
3680 for (unsigned i = 0; i != NumOps; ++i) {
3681 if (Ops[i] != N->getOperand(i)) {
3682 AnyChange = true;
3683 break;
3684 }
3685 }
3686
3687 // No operands changed, just return the input node.
3688 if (!AnyChange) return InN;
3689
3690 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003691 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003692 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003693 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003694
Dan Gohman7ceda162008-05-02 00:05:03 +00003695 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003696 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003697 RemoveNodeFromCSEMaps(N);
3698
3699 // Now we update the operands.
3700 for (unsigned i = 0; i != NumOps; ++i) {
3701 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003702 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003703 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003704 N->OperandList[i].setUser(N);
3705 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003706 }
3707 }
3708
3709 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003710 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003711 return InN;
3712}
3713
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003714/// MorphNodeTo - This frees the operands of the current node, resets the
3715/// opcode, types, and operands to the specified value. This should only be
3716/// used by the SelectionDAG class.
3717void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003718 const SDOperand *Ops, unsigned NumOps,
3719 SmallVectorImpl<SDNode *> &DeadNodes) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003720 NodeType = Opc;
3721 ValueList = L.VTs;
3722 NumValues = L.NumVTs;
3723
3724 // Clear the operands list, updating used nodes to remove this from their
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003725 // use list. Keep track of any operands that become dead as a result.
3726 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3727 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) {
3728 SDNode *N = I->getVal();
3729 N->removeUser(std::distance(op_begin(), I), this);
3730 if (N->use_empty())
3731 DeadNodeSet.insert(N);
3732 }
3733
Chris Lattner63e3f142007-02-04 07:28:00 +00003734 // If NumOps is larger than the # of operands we currently have, reallocate
3735 // the operand list.
3736 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003737 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003738 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003739 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003740 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003741 OperandsNeedDelete = true;
3742 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003743
3744 // Assign the new operands.
3745 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003746
3747 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3748 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003749 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003750 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003751 N->addUser(i, this);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003752 DeadNodeSet.erase(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003753 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003754
3755 // Clean up any nodes that are still dead after adding the uses for the
3756 // new operands.
3757 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
3758 E = DeadNodeSet.end(); I != E; ++I)
3759 DeadNodes.push_back(*I);
3760}
3761
3762/// DropOperands - Release the operands and set this node to have
3763/// zero operands. This should only be used by HandleSDNode to clear
3764/// its operand list.
3765void SDNode::DropOperands() {
3766 assert(NodeType == ISD::HANDLENODE &&
3767 "DropOperands is for HANDLENODE only!");
3768
3769 // Unlike the code in MorphNodeTo that does this, we don't need to
3770 // watch for dead nodes here.
3771 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3772 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3773
3774 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003775}
Chris Lattner149c58c2005-08-16 18:17:10 +00003776
3777/// SelectNodeTo - These are used for target selectors to *mutate* the
3778/// specified node to have the specified return type, Target opcode, and
3779/// operands. Note that target opcodes are stored as
3780/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003781///
3782/// Note that SelectNodeTo returns the resultant node. If there is already a
3783/// node of the specified opcode and operands, it returns that node instead of
3784/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003785SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003786 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003787 SDVTList VTs = getVTList(VT);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003788 return SelectNodeTo(N, TargetOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003789}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003790
Evan Cheng95514ba2006-08-26 08:00:10 +00003791SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003792 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003793 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003794 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003795 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003796}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003797
Evan Cheng95514ba2006-08-26 08:00:10 +00003798SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003799 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003800 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003801 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003802 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003803 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003804}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003805
Evan Cheng95514ba2006-08-26 08:00:10 +00003806SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003808 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003809 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003810 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003811 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003812}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003813
Evan Cheng95514ba2006-08-26 08:00:10 +00003814SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003815 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003816 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003817 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003818 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3819}
3820
3821SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003822 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003823 unsigned NumOps) {
3824 SDVTList VTs = getVTList(VT1, VT2);
3825 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3826}
3827
3828SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3829 MVT VT1, MVT VT2) {
3830 SDVTList VTs = getVTList(VT1, VT2);
3831 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3832}
3833
3834SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003835 MVT VT1, MVT VT2, MVT VT3,
3836 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003837 SDVTList VTs = getVTList(VT1, VT2, VT3);
3838 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3839}
3840
3841SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3842 MVT VT1, MVT VT2,
3843 SDOperand Op1) {
3844 SDVTList VTs = getVTList(VT1, VT2);
3845 SDOperand Ops[] = { Op1 };
3846 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003847}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003848
Evan Cheng95514ba2006-08-26 08:00:10 +00003849SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003850 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003851 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003852 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003853 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003854 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003855}
3856
Evan Cheng95514ba2006-08-26 08:00:10 +00003857SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003858 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003859 SDOperand Op1, SDOperand Op2,
3860 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003861 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003862 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003863 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3864}
3865
3866SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003867 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003868 unsigned NumOps) {
3869 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003870 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003871 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003872 void *IP = 0;
3873 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003874 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003875
Chris Lattner0fb094f2005-11-19 01:44:53 +00003876 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003877
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003878 SmallVector<SDNode *, 16> DeadNodes;
3879 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps, DeadNodes);
3880 RemoveDeadNodes(DeadNodes);
3881
Chris Lattnera5682852006-08-07 23:03:03 +00003882 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003883 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003884}
3885
Chris Lattner0fb094f2005-11-19 01:44:53 +00003886
Evan Cheng6ae46c42006-02-09 07:15:23 +00003887/// getTargetNode - These are used for target selectors to create a new node
3888/// with specified return type(s), target opcode, and operands.
3889///
3890/// Note that getTargetNode returns the resultant node. If there is already a
3891/// node of the specified opcode and operands, it returns that node instead of
3892/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003893SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003894 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3895}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003896SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003897 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3898}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003900 SDOperand Op1, SDOperand Op2) {
3901 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3902}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003904 SDOperand Op1, SDOperand Op2,
3905 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003906 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3907}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003908SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003909 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003910 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003911}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003912SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3913 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003914 SDOperand Op;
3915 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3916}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003917SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3918 MVT VT2, SDOperand Op1) {
3919 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003920 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003921}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003922SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3923 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003924 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003925 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003926 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003927 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003928}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003929SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3930 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003931 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003933 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003934 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003935}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003936SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003937 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003938 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003939 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003940}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003941SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003942 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003943 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003944 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003945 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003946}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003948 SDOperand Op1, SDOperand Op2,
3949 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003950 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003951 SDOperand Ops[] = { Op1, Op2, Op3 };
3952 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3953}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003954SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003955 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003956 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003957 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003958}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003959SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3960 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003961 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003962 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003963 VTList.push_back(VT1);
3964 VTList.push_back(VT2);
3965 VTList.push_back(VT3);
3966 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003967 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003968 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3969}
Evan Cheng39305cf2007-10-05 01:10:49 +00003970SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00003971 const std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003972 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003973 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003974 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3975 Ops, NumOps).Val;
3976}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003977
Evan Cheng08b11732008-03-22 01:55:50 +00003978/// getNodeIfExists - Get the specified node if it's already available, or
3979/// else return NULL.
3980SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003981 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003982 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3983 FoldingSetNodeID ID;
3984 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3985 void *IP = 0;
3986 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3987 return E;
3988 }
3989 return NULL;
3990}
3991
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003992
Evan Cheng99157a02006-08-07 22:13:29 +00003993/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003994/// This can cause recursive merging of nodes in the DAG.
3995///
Chris Lattner11d049c2008-02-03 03:35:22 +00003996/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003997///
Chris Lattner11d049c2008-02-03 03:35:22 +00003998void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003999 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004000 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004001 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004002 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004003 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004004
Chris Lattner8b8749f2005-08-17 19:00:20 +00004005 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004006 SDNode::use_iterator UI = From->use_begin();
4007 SDNode *U = UI->getUser();
4008
Chris Lattner8b8749f2005-08-17 19:00:20 +00004009 // This node is about to morph, remove its old self from the CSE maps.
4010 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004011 int operandNum = 0;
4012 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4013 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004014 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004015 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004016 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004017 I->setUser(U);
4018 To.Val->addUser(operandNum, U);
4019 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004020
4021 // Now that we have modified U, add it back to the CSE maps. If it already
4022 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004023 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004024 ReplaceAllUsesWith(U, Existing, UpdateListener);
4025 // U is now dead. Inform the listener if it exists and delete it.
4026 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004027 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004028 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004029 } else {
4030 // If the node doesn't already exist, we updated it. Inform a listener if
4031 // it exists.
4032 if (UpdateListener)
4033 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004034 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004035 }
4036}
4037
4038/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4039/// This can cause recursive merging of nodes in the DAG.
4040///
4041/// This version assumes From/To have matching types and numbers of result
4042/// values.
4043///
Chris Lattner1e111c72005-09-07 05:37:01 +00004044void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004045 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00004046 assert(From != To && "Cannot replace uses of with self");
4047 assert(From->getNumValues() == To->getNumValues() &&
4048 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004049 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004050 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
4051 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004052
4053 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004054 SDNode::use_iterator UI = From->use_begin();
4055 SDNode *U = UI->getUser();
4056
Chris Lattner8b52f212005-08-26 18:36:28 +00004057 // This node is about to morph, remove its old self from the CSE maps.
4058 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004059 int operandNum = 0;
4060 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4061 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004062 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004063 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004064 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004065 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004066 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004067
Chris Lattner8b8749f2005-08-17 19:00:20 +00004068 // Now that we have modified U, add it back to the CSE maps. If it already
4069 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004070 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004071 ReplaceAllUsesWith(U, Existing, UpdateListener);
4072 // U is now dead. Inform the listener if it exists and delete it.
4073 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004074 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004075 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004076 } else {
4077 // If the node doesn't already exist, we updated it. Inform a listener if
4078 // it exists.
4079 if (UpdateListener)
4080 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004081 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004082 }
4083}
4084
Chris Lattner8b52f212005-08-26 18:36:28 +00004085/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4086/// This can cause recursive merging of nodes in the DAG.
4087///
4088/// This version can replace From with any result values. To must match the
4089/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004090void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004091 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004092 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004093 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004094 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004095
4096 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004097 SDNode::use_iterator UI = From->use_begin();
4098 SDNode *U = UI->getUser();
4099
Chris Lattner7b2880c2005-08-24 22:44:39 +00004100 // This node is about to morph, remove its old self from the CSE maps.
4101 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004102 int operandNum = 0;
4103 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4104 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004105 if (I->getVal() == From) {
4106 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004107 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004108 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004109 I->setUser(U);
4110 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004111 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004112
Chris Lattner7b2880c2005-08-24 22:44:39 +00004113 // Now that we have modified U, add it back to the CSE maps. If it already
4114 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004115 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004116 ReplaceAllUsesWith(U, Existing, UpdateListener);
4117 // U is now dead. Inform the listener if it exists and delete it.
4118 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004119 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004120 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004121 } else {
4122 // If the node doesn't already exist, we updated it. Inform a listener if
4123 // it exists.
4124 if (UpdateListener)
4125 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004126 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004127 }
4128}
4129
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004130namespace {
4131 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4132 /// any deleted nodes from the set passed into its constructor and recursively
4133 /// notifies another update listener if specified.
4134 class ChainedSetUpdaterListener :
4135 public SelectionDAG::DAGUpdateListener {
4136 SmallSetVector<SDNode*, 16> &Set;
4137 SelectionDAG::DAGUpdateListener *Chain;
4138 public:
4139 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4140 SelectionDAG::DAGUpdateListener *chain)
4141 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004142
Duncan Sandsedfcf592008-06-11 11:42:12 +00004143 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004144 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004145 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004146 }
4147 virtual void NodeUpdated(SDNode *N) {
4148 if (Chain) Chain->NodeUpdated(N);
4149 }
4150 };
4151}
4152
Chris Lattner012f2412006-02-17 21:58:01 +00004153/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4154/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004155/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004156void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004157 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004158 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004159
Chris Lattner012f2412006-02-17 21:58:01 +00004160 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004161 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004162 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004163 return;
4164 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004165
4166 if (From.use_empty()) return;
4167
Chris Lattnercf5640b2007-02-04 00:14:31 +00004168 // Get all of the users of From.Val. We want these in a nice,
4169 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004170 SmallSetVector<SDNode*, 16> Users;
4171 for (SDNode::use_iterator UI = From.Val->use_begin(),
4172 E = From.Val->use_end(); UI != E; ++UI) {
4173 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004174 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004175 }
Chris Lattner012f2412006-02-17 21:58:01 +00004176
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004177 // When one of the recursive merges deletes nodes from the graph, we need to
4178 // make sure that UpdateListener is notified *and* that the node is removed
4179 // from Users if present. CSUL does this.
4180 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004181
Chris Lattner012f2412006-02-17 21:58:01 +00004182 while (!Users.empty()) {
4183 // We know that this user uses some value of From. If it is the right
4184 // value, update it.
4185 SDNode *User = Users.back();
4186 Users.pop_back();
4187
Chris Lattner01d029b2007-10-15 06:10:22 +00004188 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004189 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004190 for (; Op != E; ++Op)
4191 if (*Op == From) break;
4192
4193 // If there are no matches, the user must use some other result of From.
4194 if (Op == E) continue;
4195
4196 // Okay, we know this user needs to be updated. Remove its old self
4197 // from the CSE maps.
4198 RemoveNodeFromCSEMaps(User);
4199
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004200 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004201 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004202 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004203 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004204 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004205 Op->setUser(User);
4206 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004207 }
4208 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004209
4210 // Now that we have modified User, add it back to the CSE maps. If it
4211 // already exists there, recursively merge the results together.
4212 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004213 if (!Existing) {
4214 if (UpdateListener) UpdateListener->NodeUpdated(User);
4215 continue; // Continue on to next user.
4216 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004217
4218 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004219 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004220 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004221 // can cause deletion of nodes that used the old value. To handle this, we
4222 // use CSUL to remove them from the Users set.
4223 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004224
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004225 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004226 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004227 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004228 }
4229}
4230
Evan Chenge6f35d82006-08-01 08:20:41 +00004231/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4232/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004233unsigned SelectionDAG::AssignNodeIds() {
4234 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004235 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4236 SDNode *N = I;
4237 N->setNodeId(Id++);
4238 }
4239 return Id;
4240}
4241
Evan Chenge6f35d82006-08-01 08:20:41 +00004242/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004243/// based on their topological order. It returns the maximum id and a vector
4244/// of the SDNodes* in assigned order by reference.
4245unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004246 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004247 std::vector<unsigned> InDegree(DAGSize);
4248 std::vector<SDNode*> Sources;
4249
4250 // Use a two pass approach to avoid using a std::map which is slow.
4251 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004252 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4253 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004254 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004255 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004256 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004257 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004258 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004259 }
4260
Evan Cheng99157a02006-08-07 22:13:29 +00004261 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004262 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004263 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004264 SDNode *N = Sources.back();
4265 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004266 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004267 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004268 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004269 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004270 if (Degree == 0)
4271 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004272 }
4273 }
4274
Evan Chengc384d6c2006-08-02 22:00:34 +00004275 // Second pass, assign the actual topological order as node ids.
4276 Id = 0;
4277 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4278 TI != TE; ++TI)
4279 (*TI)->setNodeId(Id++);
4280
4281 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004282}
4283
4284
Evan Cheng091cba12006-07-27 06:39:06 +00004285
Jim Laskey58b968b2005-08-17 20:08:02 +00004286//===----------------------------------------------------------------------===//
4287// SDNode Class
4288//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004289
Chris Lattner917d2c92006-07-19 00:00:37 +00004290// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004291void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004292void UnarySDNode::ANCHOR() {}
4293void BinarySDNode::ANCHOR() {}
4294void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004295void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004296void ConstantSDNode::ANCHOR() {}
4297void ConstantFPSDNode::ANCHOR() {}
4298void GlobalAddressSDNode::ANCHOR() {}
4299void FrameIndexSDNode::ANCHOR() {}
4300void JumpTableSDNode::ANCHOR() {}
4301void ConstantPoolSDNode::ANCHOR() {}
4302void BasicBlockSDNode::ANCHOR() {}
4303void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004304void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004305void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004306void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004307void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004308void ExternalSymbolSDNode::ANCHOR() {}
4309void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004310void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004311void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004312void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004313void LoadSDNode::ANCHOR() {}
4314void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004315void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004316
Chris Lattner48b85922007-02-04 02:41:42 +00004317HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004318 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004319}
4320
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004321GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004322 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004323 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004324 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004325 // Thread Local
4326 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4327 // Non Thread Local
4328 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4329 getSDVTList(VT)), Offset(o) {
4330 TheGlobal = const_cast<GlobalValue*>(GA);
4331}
Chris Lattner48b85922007-02-04 02:41:42 +00004332
Dan Gohman1ea58a52008-07-09 22:08:04 +00004333MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004334 const Value *srcValue, int SVO,
4335 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004336 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohman492f2762008-07-09 21:23:02 +00004337 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4338
4339 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4340 assert(getAlignment() == alignment && "Alignment representation error!");
4341 assert(isVolatile() == vol && "Volatile representation error!");
4342}
4343
Dan Gohman36b5c132008-04-07 19:35:22 +00004344/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004345/// reference performed by this memory reference.
4346MachineMemOperand MemSDNode::getMemOperand() const {
4347 int Flags;
4348 if (isa<LoadSDNode>(this))
4349 Flags = MachineMemOperand::MOLoad;
4350 else if (isa<StoreSDNode>(this))
4351 Flags = MachineMemOperand::MOStore;
4352 else {
4353 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4354 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4355 }
4356
4357 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004358 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4359
Dan Gohman1ea58a52008-07-09 22:08:04 +00004360 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004361 const FrameIndexSDNode *FI =
4362 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4363 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004364 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4365 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004366 else
4367 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4368 Size, getAlignment());
4369}
4370
Jim Laskey583bd472006-10-27 23:46:08 +00004371/// Profile - Gather unique data for the node.
4372///
4373void SDNode::Profile(FoldingSetNodeID &ID) {
4374 AddNodeIDNode(ID, this);
4375}
4376
Chris Lattnera3255112005-11-08 23:30:28 +00004377/// getValueTypeList - Return a pointer to the specified value type.
4378///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004379const MVT *SDNode::getValueTypeList(MVT VT) {
4380 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004381 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004382 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004383 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004384 static MVT VTs[MVT::LAST_VALUETYPE];
4385 VTs[VT.getSimpleVT()] = VT;
4386 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004387 }
Chris Lattnera3255112005-11-08 23:30:28 +00004388}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004389
Chris Lattner5c884562005-01-12 18:37:47 +00004390/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4391/// indicated value. This method ignores uses of other values defined by this
4392/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004393bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004394 assert(Value < getNumValues() && "Bad value!");
4395
Roman Levensteindc1adac2008-04-07 10:06:32 +00004396 // TODO: Only iterate over uses of a given value of the node
4397 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmanb9c33c32008-07-09 23:03:14 +00004398 if (UI->getSDOperand().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004399 if (NUses == 0)
4400 return false;
4401 --NUses;
4402 }
Chris Lattner5c884562005-01-12 18:37:47 +00004403 }
4404
4405 // Found exactly the right number of uses?
4406 return NUses == 0;
4407}
4408
4409
Evan Cheng33d55952007-08-02 05:29:38 +00004410/// hasAnyUseOfValue - Return true if there are any use of the indicated
4411/// value. This method ignores uses of other values defined by this operation.
4412bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4413 assert(Value < getNumValues() && "Bad value!");
4414
Dan Gohman1373c1c2008-07-09 22:39:01 +00004415 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmanb9c33c32008-07-09 23:03:14 +00004416 if (UI->getSDOperand().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004417 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004418
4419 return false;
4420}
4421
4422
Evan Cheng917be682008-03-04 00:41:45 +00004423/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004424///
Evan Cheng917be682008-03-04 00:41:45 +00004425bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004426 bool Seen = false;
4427 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004428 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004429 if (User == this)
4430 Seen = true;
4431 else
4432 return false;
4433 }
4434
4435 return Seen;
4436}
4437
Evan Chenge6e97e62006-11-03 07:31:32 +00004438/// isOperand - Return true if this node is an operand of N.
4439///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004440bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004441 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4442 if (*this == N->getOperand(i))
4443 return true;
4444 return false;
4445}
4446
Evan Cheng917be682008-03-04 00:41:45 +00004447bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004448 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004449 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004450 return true;
4451 return false;
4452}
Evan Cheng4ee62112006-02-05 06:29:23 +00004453
Chris Lattner572dee72008-01-16 05:49:24 +00004454/// reachesChainWithoutSideEffects - Return true if this operand (which must
4455/// be a chain) reaches the specified operand without crossing any
4456/// side-effecting instructions. In practice, this looks through token
4457/// factors and non-volatile loads. In order to remain efficient, this only
4458/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004459bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004460 unsigned Depth) const {
4461 if (*this == Dest) return true;
4462
4463 // Don't search too deeply, we just want to be able to see through
4464 // TokenFactor's etc.
4465 if (Depth == 0) return false;
4466
4467 // If this is a token factor, all inputs to the TF happen in parallel. If any
4468 // of the operands of the TF reach dest, then we can do the xform.
4469 if (getOpcode() == ISD::TokenFactor) {
4470 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4471 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4472 return true;
4473 return false;
4474 }
4475
4476 // Loads don't have side effects, look through them.
4477 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4478 if (!Ld->isVolatile())
4479 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4480 }
4481 return false;
4482}
4483
4484
Evan Chengc5fc57d2006-11-03 03:05:24 +00004485static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004486 SmallPtrSet<SDNode *, 32> &Visited) {
4487 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004488 return;
4489
4490 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4491 SDNode *Op = N->getOperand(i).Val;
4492 if (Op == P) {
4493 found = true;
4494 return;
4495 }
4496 findPredecessor(Op, P, found, Visited);
4497 }
4498}
4499
Evan Cheng917be682008-03-04 00:41:45 +00004500/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004501/// is either an operand of N or it can be reached by recursively traversing
4502/// up the operands.
4503/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004504bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004505 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004506 bool found = false;
4507 findPredecessor(N, this, found, Visited);
4508 return found;
4509}
4510
Evan Chengc5484282006-10-04 00:56:09 +00004511uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4512 assert(Num < NumOperands && "Invalid child # of SDNode!");
4513 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4514}
4515
Reid Spencer577cc322007-04-01 07:32:19 +00004516std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004517 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004518 default:
4519 if (getOpcode() < ISD::BUILTIN_OP_END)
4520 return "<<Unknown DAG Node>>";
4521 else {
Evan Cheng72261582005-12-20 06:22:03 +00004522 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004523 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004524 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004525 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004526
Evan Cheng72261582005-12-20 06:22:03 +00004527 TargetLowering &TLI = G->getTargetLoweringInfo();
4528 const char *Name =
4529 TLI.getTargetNodeName(getOpcode());
4530 if (Name) return Name;
4531 }
4532
4533 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004534 }
4535
Evan Cheng27b7db52008-03-08 00:58:38 +00004536 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004537 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004538 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4539 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4540 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004541 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4542 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4543 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004544 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004545 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4546 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4547 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4548 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4549 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004550 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004551 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004552 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004553 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004554 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004555 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004556 case ISD::AssertSext: return "AssertSext";
4557 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004558
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004559 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004560 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004561 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004562 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004563
4564 case ISD::Constant: return "Constant";
4565 case ISD::ConstantFP: return "ConstantFP";
4566 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004567 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004568 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004569 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004570 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004571 case ISD::RETURNADDR: return "RETURNADDR";
4572 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004573 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004574 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4575 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004576 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004577 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004578 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004579 case ISD::INTRINSIC_WO_CHAIN: {
4580 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4581 return Intrinsic::getName((Intrinsic::ID)IID);
4582 }
4583 case ISD::INTRINSIC_VOID:
4584 case ISD::INTRINSIC_W_CHAIN: {
4585 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004586 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004587 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004588
Chris Lattnerb2827b02006-03-19 00:52:58 +00004589 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004590 case ISD::TargetConstant: return "TargetConstant";
4591 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004592 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004593 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004594 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004595 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004596 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004597 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004598
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004599 case ISD::CopyToReg: return "CopyToReg";
4600 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004601 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004602 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004603 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004604 case ISD::DBG_LABEL: return "dbg_label";
4605 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004606 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004607 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004608 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004609 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004610
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004611 // Unary operators
4612 case ISD::FABS: return "fabs";
4613 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004614 case ISD::FSQRT: return "fsqrt";
4615 case ISD::FSIN: return "fsin";
4616 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004617 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004618 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004619
4620 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004621 case ISD::ADD: return "add";
4622 case ISD::SUB: return "sub";
4623 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004624 case ISD::MULHU: return "mulhu";
4625 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004626 case ISD::SDIV: return "sdiv";
4627 case ISD::UDIV: return "udiv";
4628 case ISD::SREM: return "srem";
4629 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004630 case ISD::SMUL_LOHI: return "smul_lohi";
4631 case ISD::UMUL_LOHI: return "umul_lohi";
4632 case ISD::SDIVREM: return "sdivrem";
4633 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004634 case ISD::AND: return "and";
4635 case ISD::OR: return "or";
4636 case ISD::XOR: return "xor";
4637 case ISD::SHL: return "shl";
4638 case ISD::SRA: return "sra";
4639 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004640 case ISD::ROTL: return "rotl";
4641 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004642 case ISD::FADD: return "fadd";
4643 case ISD::FSUB: return "fsub";
4644 case ISD::FMUL: return "fmul";
4645 case ISD::FDIV: return "fdiv";
4646 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004647 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004648 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004649
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004650 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004651 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004652 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004653 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004654 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004655 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004656 case ISD::CONCAT_VECTORS: return "concat_vectors";
4657 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004658 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004659 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004660 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004661 case ISD::ADDC: return "addc";
4662 case ISD::ADDE: return "adde";
4663 case ISD::SUBC: return "subc";
4664 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004665 case ISD::SHL_PARTS: return "shl_parts";
4666 case ISD::SRA_PARTS: return "sra_parts";
4667 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004668
4669 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4670 case ISD::INSERT_SUBREG: return "insert_subreg";
4671
Chris Lattner7f644642005-04-28 21:44:03 +00004672 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004673 case ISD::SIGN_EXTEND: return "sign_extend";
4674 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004675 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004676 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004677 case ISD::TRUNCATE: return "truncate";
4678 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004679 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004680 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004681 case ISD::FP_EXTEND: return "fp_extend";
4682
4683 case ISD::SINT_TO_FP: return "sint_to_fp";
4684 case ISD::UINT_TO_FP: return "uint_to_fp";
4685 case ISD::FP_TO_SINT: return "fp_to_sint";
4686 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004687 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004688
4689 // Control flow instructions
4690 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004691 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004692 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004693 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004694 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004695 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004696 case ISD::CALLSEQ_START: return "callseq_start";
4697 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004698
4699 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004700 case ISD::LOAD: return "load";
4701 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004702 case ISD::VAARG: return "vaarg";
4703 case ISD::VACOPY: return "vacopy";
4704 case ISD::VAEND: return "vaend";
4705 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004706 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004707 case ISD::EXTRACT_ELEMENT: return "extract_element";
4708 case ISD::BUILD_PAIR: return "build_pair";
4709 case ISD::STACKSAVE: return "stacksave";
4710 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004711 case ISD::TRAP: return "trap";
4712
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004713 // Bit manipulation
4714 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004715 case ISD::CTPOP: return "ctpop";
4716 case ISD::CTTZ: return "cttz";
4717 case ISD::CTLZ: return "ctlz";
4718
Chris Lattner36ce6912005-11-29 06:21:05 +00004719 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004720 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004721 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004722
Duncan Sands36397f52007-07-27 12:58:54 +00004723 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004724 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004725
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004726 case ISD::CONDCODE:
4727 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004728 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004729 case ISD::SETOEQ: return "setoeq";
4730 case ISD::SETOGT: return "setogt";
4731 case ISD::SETOGE: return "setoge";
4732 case ISD::SETOLT: return "setolt";
4733 case ISD::SETOLE: return "setole";
4734 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004735
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004736 case ISD::SETO: return "seto";
4737 case ISD::SETUO: return "setuo";
4738 case ISD::SETUEQ: return "setue";
4739 case ISD::SETUGT: return "setugt";
4740 case ISD::SETUGE: return "setuge";
4741 case ISD::SETULT: return "setult";
4742 case ISD::SETULE: return "setule";
4743 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004744
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004745 case ISD::SETEQ: return "seteq";
4746 case ISD::SETGT: return "setgt";
4747 case ISD::SETGE: return "setge";
4748 case ISD::SETLT: return "setlt";
4749 case ISD::SETLE: return "setle";
4750 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004751 }
4752 }
4753}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004754
Evan Cheng144d8f02006-11-09 17:55:04 +00004755const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004756 switch (AM) {
4757 default:
4758 return "";
4759 case ISD::PRE_INC:
4760 return "<pre-inc>";
4761 case ISD::PRE_DEC:
4762 return "<pre-dec>";
4763 case ISD::POST_INC:
4764 return "<post-inc>";
4765 case ISD::POST_DEC:
4766 return "<post-dec>";
4767 }
4768}
4769
Duncan Sands276dcbd2008-03-21 09:14:45 +00004770std::string ISD::ArgFlagsTy::getArgFlagsString() {
4771 std::string S = "< ";
4772
4773 if (isZExt())
4774 S += "zext ";
4775 if (isSExt())
4776 S += "sext ";
4777 if (isInReg())
4778 S += "inreg ";
4779 if (isSRet())
4780 S += "sret ";
4781 if (isByVal())
4782 S += "byval ";
4783 if (isNest())
4784 S += "nest ";
4785 if (getByValAlign())
4786 S += "byval-align:" + utostr(getByValAlign()) + " ";
4787 if (getOrigAlign())
4788 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4789 if (getByValSize())
4790 S += "byval-size:" + utostr(getByValSize()) + " ";
4791 return S + ">";
4792}
4793
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004794void SDNode::dump() const { dump(0); }
4795void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004796 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004797
4798 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004799 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004800 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004801 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004802 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004803 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004804 }
Bill Wendling832171c2006-12-07 20:04:42 +00004805 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004806
Bill Wendling832171c2006-12-07 20:04:42 +00004807 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004808 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004809 if (i) cerr << ", ";
4810 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004811 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004812 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004813 }
4814
Evan Chengce254432007-12-11 02:08:35 +00004815 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4816 SDNode *Mask = getOperand(2).Val;
4817 cerr << "<";
4818 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4819 if (i) cerr << ",";
4820 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4821 cerr << "u";
4822 else
4823 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4824 }
4825 cerr << ">";
4826 }
4827
Chris Lattnerc3aae252005-01-07 07:46:32 +00004828 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Duncan Sandse1d97b12008-07-10 11:23:14 +00004829 cerr << "<" << CSDN->getAPIntValue().toStringUnsigned() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004830 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004831 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4832 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4833 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4834 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4835 else {
4836 cerr << "<APFloat(";
4837 CSDN->getValueAPF().convertToAPInt().dump();
4838 cerr << ")>";
4839 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004840 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004841 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004842 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004843 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004844 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004845 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004846 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004847 else
Bill Wendling832171c2006-12-07 20:04:42 +00004848 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004849 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004850 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004851 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004852 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004853 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004854 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004855 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004856 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004857 else
Bill Wendling832171c2006-12-07 20:04:42 +00004858 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004859 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004860 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004861 else
Bill Wendling832171c2006-12-07 20:04:42 +00004862 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004863 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004864 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004865 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4866 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004867 cerr << LBB->getName() << " ";
4868 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004869 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004870 if (G && R->getReg() &&
4871 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004872 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004873 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004874 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004875 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004876 } else if (const ExternalSymbolSDNode *ES =
4877 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004878 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004879 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4880 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004881 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004882 else
Dan Gohman69de1932008-02-06 22:27:42 +00004883 cerr << "<null>";
4884 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4885 if (M->MO.getValue())
4886 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4887 else
4888 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004889 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4890 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004891 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004892 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004893 }
4894 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004895 const Value *SrcValue = LD->getSrcValue();
4896 int SrcOffset = LD->getSrcValueOffset();
4897 cerr << " <";
4898 if (SrcValue)
4899 cerr << SrcValue;
4900 else
4901 cerr << "null";
4902 cerr << ":" << SrcOffset << ">";
4903
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004904 bool doExt = true;
4905 switch (LD->getExtensionType()) {
4906 default: doExt = false; break;
4907 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004908 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004909 break;
4910 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004911 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004912 break;
4913 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004914 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004915 break;
4916 }
4917 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004918 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004919
Evan Cheng144d8f02006-11-09 17:55:04 +00004920 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004921 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004922 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004923 if (LD->isVolatile())
4924 cerr << " <volatile>";
4925 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004926 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004927 const Value *SrcValue = ST->getSrcValue();
4928 int SrcOffset = ST->getSrcValueOffset();
4929 cerr << " <";
4930 if (SrcValue)
4931 cerr << SrcValue;
4932 else
4933 cerr << "null";
4934 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004935
4936 if (ST->isTruncatingStore())
4937 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004938 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004939
4940 const char *AM = getIndexedModeName(ST->getAddressingMode());
4941 if (*AM)
4942 cerr << " " << AM;
4943 if (ST->isVolatile())
4944 cerr << " <volatile>";
4945 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004946 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4947 const Value *SrcValue = AT->getSrcValue();
4948 int SrcOffset = AT->getSrcValueOffset();
4949 cerr << " <";
4950 if (SrcValue)
4951 cerr << SrcValue;
4952 else
4953 cerr << "null";
4954 cerr << ":" << SrcOffset << ">";
4955 if (AT->isVolatile())
4956 cerr << " <volatile>";
4957 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004958 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004959}
4960
Chris Lattnerde202b32005-11-09 23:47:37 +00004961static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004962 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4963 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004964 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004965 else
Bill Wendling832171c2006-12-07 20:04:42 +00004966 cerr << "\n" << std::string(indent+2, ' ')
4967 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004968
Chris Lattnerea946cd2005-01-09 20:38:33 +00004969
Bill Wendling832171c2006-12-07 20:04:42 +00004970 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004971 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004972}
4973
Chris Lattnerc3aae252005-01-07 07:46:32 +00004974void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004975 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004976 std::vector<const SDNode*> Nodes;
4977 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4978 I != E; ++I)
4979 Nodes.push_back(I);
4980
Chris Lattner49d24712005-01-09 20:26:36 +00004981 std::sort(Nodes.begin(), Nodes.end());
4982
4983 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004984 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004985 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004986 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004987
Jim Laskey26f7fa72006-10-17 19:33:52 +00004988 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004989
Bill Wendling832171c2006-12-07 20:04:42 +00004990 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004991}
4992
Evan Chengd6594ae2006-09-12 21:00:35 +00004993const Type *ConstantPoolSDNode::getType() const {
4994 if (isMachineConstantPoolEntry())
4995 return Val.MachineCPVal->getType();
4996 return Val.ConstVal->getType();
4997}