blob: 80dda1f280e59a5f5519b185e5b05bd1a5509493 [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.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 SDValue 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.
Dan Gohman475871a2008-07-27 21:46:04 +0000154 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000155 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) {
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000187 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) {
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000200 if (N->isMachineOpcode() &&
201 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000202 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 Gohman475871a2008-07-27 21:46:04 +0000326 const SDValue *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) {
Dan Gohman475871a2008-07-27 21:46:04 +0000338 ID.AddPointer(Ops->getVal());
339 ID.AddInteger(Ops->getSDValue().ResNo);
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000345 const SDValue *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
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000471/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000472/// SelectionDAG.
473void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000474 // Create a dummy node (which is not added to allnodes), that adds a reference
475 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000476 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000477
Chris Lattner190a4182006-08-04 17:45:20 +0000478 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000479
Chris Lattner190a4182006-08-04 17:45:20 +0000480 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000481 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000482 if (I->use_empty())
483 DeadNodes.push_back(I);
484
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000485 RemoveDeadNodes(DeadNodes);
486
487 // If the root changed (e.g. it was a dead load, update the root).
488 setRoot(Dummy.getValue());
489}
490
491/// RemoveDeadNodes - This method deletes the unreachable nodes in the
492/// given list, and any nodes that become unreachable as a result.
493void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
494 DAGUpdateListener *UpdateListener) {
495
Chris Lattner190a4182006-08-04 17:45:20 +0000496 // Process the worklist, deleting the nodes and adding their uses to the
497 // worklist.
498 while (!DeadNodes.empty()) {
499 SDNode *N = DeadNodes.back();
500 DeadNodes.pop_back();
501
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000502 if (UpdateListener)
503 UpdateListener->NodeDeleted(N, 0);
504
Chris Lattner190a4182006-08-04 17:45:20 +0000505 // Take the node out of the appropriate CSE map.
506 RemoveNodeFromCSEMaps(N);
507
508 // Next, brutally remove the operand list. This is safe to do, as there are
509 // no cycles in the graph.
510 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000511 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000512 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000513
514 // Now that we removed this operand, see if there are no uses of it left.
515 if (Operand->use_empty())
516 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000517 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000518 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000519 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000520 }
Chris Lattner190a4182006-08-04 17:45:20 +0000521 N->OperandList = 0;
522 N->NumOperands = 0;
523
524 // Finally, remove N itself.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000525 AllNodes.remove(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000526 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000527}
528
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000529void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000530 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000531 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000532}
533
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000534void SelectionDAG::DeleteNode(SDNode *N) {
535 assert(N->use_empty() && "Cannot delete a node that is not dead!");
536
537 // First take this out of the appropriate CSE map.
538 RemoveNodeFromCSEMaps(N);
539
Chris Lattner1e111c72005-09-07 05:37:01 +0000540 // Finally, remove uses due to operands of this node, remove from the
541 // AllNodes list, and delete the node.
542 DeleteNodeNotInCSEMaps(N);
543}
544
545void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
546
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000547 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000548 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000549 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000550 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000551 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000552 }
Chris Lattner65113b22005-11-08 22:07:03 +0000553 N->OperandList = 0;
554 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000555
Dan Gohmanfed90b62008-07-28 21:51:04 +0000556 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000557}
558
Chris Lattner149c58c2005-08-16 18:17:10 +0000559/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
560/// correspond to it. This is useful when we're about to delete or repurpose
561/// the node. We don't want future request for structurally identical nodes
562/// to return N anymore.
563void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000564 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000565 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000566 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000567 case ISD::CONDCODE:
568 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
569 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000571 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
572 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000573 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000574 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000576 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000577 Erased =
578 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000579 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000580 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000581 MVT VT = cast<VTSDNode>(N)->getVT();
582 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000583 Erased = ExtendedValueTypeNodes.erase(VT);
584 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000585 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
586 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000587 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000588 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000589 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000590 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000591 // Remove it from the CSE Map.
592 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000593 break;
594 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000595#ifndef NDEBUG
596 // Verify that the node was actually in one of the CSE maps, unless it has a
597 // flag result (which cannot be CSE'd) or is one of the special cases that are
598 // not subject to CSE.
599 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000600 !N->isTargetOpcode() &&
601 N->getOpcode() != ISD::DBG_LABEL &&
602 N->getOpcode() != ISD::DBG_STOPPOINT &&
603 N->getOpcode() != ISD::EH_LABEL &&
604 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000605 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000606 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000607 assert(0 && "Node is not in map!");
608 }
609#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000610}
611
Chris Lattner8b8749f2005-08-17 19:00:20 +0000612/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
613/// has been taken out and modified in some way. If the specified node already
614/// exists in the CSE maps, do not modify the maps, but return the existing node
615/// instead. If it doesn't exist, add it and return null.
616///
617SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
618 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000619
620 if (N->getValueType(0) == MVT::Flag)
621 return 0; // Never CSE anything that produces a flag.
622
623 switch (N->getOpcode()) {
624 default: break;
625 case ISD::HANDLENODE:
626 case ISD::DBG_LABEL:
627 case ISD::DBG_STOPPOINT:
628 case ISD::EH_LABEL:
629 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000630 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000631 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000632
Chris Lattner9f8cc692005-12-19 22:21:21 +0000633 // Check that remaining values produced are not flags.
634 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
635 if (N->getValueType(i) == MVT::Flag)
636 return 0; // Never CSE anything that produces a flag.
637
Chris Lattnera5682852006-08-07 23:03:03 +0000638 SDNode *New = CSEMap.GetOrInsertNode(N);
639 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000640 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000641}
642
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000643/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
644/// were replaced with those specified. If this node is never memoized,
645/// return null, otherwise return a pointer to the slot it would take. If a
646/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000647SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000648 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000649 if (N->getValueType(0) == MVT::Flag)
650 return 0; // Never CSE anything that produces a flag.
651
652 switch (N->getOpcode()) {
653 default: break;
654 case ISD::HANDLENODE:
655 case ISD::DBG_LABEL:
656 case ISD::DBG_STOPPOINT:
657 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000658 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000659 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000660
661 // Check that remaining values produced are not flags.
662 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
663 if (N->getValueType(i) == MVT::Flag)
664 return 0; // Never CSE anything that produces a flag.
665
Dan Gohman475871a2008-07-27 21:46:04 +0000666 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000667 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000668 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000669 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000670}
671
672/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
673/// were replaced with those specified. If this node is never memoized,
674/// return null, otherwise return a pointer to the slot it would take. If a
675/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000676SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000677 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000678 void *&InsertPos) {
679 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000680
681 // Check that remaining values produced are not flags.
682 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
683 if (N->getValueType(i) == MVT::Flag)
684 return 0; // Never CSE anything that produces a flag.
685
Dan Gohman475871a2008-07-27 21:46:04 +0000686 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000687 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000688 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000689 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
690}
691
692
693/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
694/// were replaced with those specified. If this node is never memoized,
695/// return null, otherwise return a pointer to the slot it would take. If a
696/// node already exists with these operands, the slot will be non-null.
697SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000698 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000699 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000700 if (N->getValueType(0) == MVT::Flag)
701 return 0; // Never CSE anything that produces a flag.
702
703 switch (N->getOpcode()) {
704 default: break;
705 case ISD::HANDLENODE:
706 case ISD::DBG_LABEL:
707 case ISD::DBG_STOPPOINT:
708 case ISD::EH_LABEL:
709 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000710 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000711 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000712
713 // Check that remaining values produced are not flags.
714 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
715 if (N->getValueType(i) == MVT::Flag)
716 return 0; // Never CSE anything that produces a flag.
717
Jim Laskey583bd472006-10-27 23:46:08 +0000718 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000719 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000720
Evan Cheng9629aba2006-10-11 01:47:58 +0000721 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
722 ID.AddInteger(LD->getAddressingMode());
723 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000724 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000725 ID.AddInteger(LD->getAlignment());
726 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000727 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
728 ID.AddInteger(ST->getAddressingMode());
729 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000730 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000731 ID.AddInteger(ST->getAlignment());
732 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000733 }
Jim Laskey583bd472006-10-27 23:46:08 +0000734
Chris Lattnera5682852006-08-07 23:03:03 +0000735 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000736}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000737
Duncan Sandsd038e042008-07-21 10:20:31 +0000738/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
739void SelectionDAG::VerifyNode(SDNode *N) {
740 switch (N->getOpcode()) {
741 default:
742 break;
743 case ISD::BUILD_VECTOR: {
744 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
745 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
746 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
747 "Wrong number of BUILD_VECTOR operands!");
748 MVT EltVT = N->getValueType(0).getVectorElementType();
749 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000750 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000751 "Wrong BUILD_VECTOR operand type!");
752 break;
753 }
754 }
755}
756
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000757/// getMVTAlignment - Compute the default alignment value for the
758/// given type.
759///
760unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
761 const Type *Ty = VT == MVT::iPTR ?
762 PointerType::get(Type::Int8Ty, 0) :
763 VT.getTypeForMVT();
764
765 return TLI.getTargetData()->getABITypeAlignment(Ty);
766}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000767
Chris Lattner78ec3112003-08-11 14:57:33 +0000768SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000769 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000770 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000771 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000772 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000773 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000774 }
Chris Lattner65113b22005-11-08 22:07:03 +0000775 N->OperandList = 0;
776 N->NumOperands = 0;
Chris Lattner65113b22005-11-08 22:07:03 +0000777 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000778}
779
Dan Gohman475871a2008-07-27 21:46:04 +0000780SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000781 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000782 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000783 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000784 return getNode(ISD::AND, Op.getValueType(), Op,
785 getConstant(Imm, Op.getValueType()));
786}
787
Dan Gohman475871a2008-07-27 21:46:04 +0000788SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000789 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000790 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000791}
792
Dan Gohman475871a2008-07-27 21:46:04 +0000793SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000794 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000795
Evan Cheng0ff39b32008-06-30 07:31:25 +0000796 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000797 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000798 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000799
800 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000801 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000802 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000803 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000804 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000805 SDNode *N = NULL;
806 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000807 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000808 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000809 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000810 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000811 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000812 CSEMap.InsertNode(N, IP);
813 AllNodes.push_back(N);
814 }
815
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000819 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000820 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
821 }
822 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000823}
824
Dan Gohman475871a2008-07-27 21:46:04 +0000825SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000826 return getConstant(Val, TLI.getPointerTy(), isTarget);
827}
828
829
Dan Gohman475871a2008-07-27 21:46:04 +0000830SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000831 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000832
Duncan Sands83ec4b62008-06-06 12:08:01 +0000833 MVT EltVT =
834 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835
Chris Lattnerd8658612005-02-17 20:17:32 +0000836 // Do the map lookup using the actual bit pattern for the floating point
837 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
838 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000839 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000840 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000841 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000842 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000843 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000844 SDNode *N = NULL;
845 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000846 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000847 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000848 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000849 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000850 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000851 CSEMap.InsertNode(N, IP);
852 AllNodes.push_back(N);
853 }
854
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000856 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000857 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000859 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
860 }
861 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000862}
863
Dan Gohman475871a2008-07-27 21:46:04 +0000864SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000865 MVT EltVT =
866 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000867 if (EltVT==MVT::f32)
868 return getConstantFP(APFloat((float)Val), VT, isTarget);
869 else
870 return getConstantFP(APFloat(Val), VT, isTarget);
871}
872
Dan Gohman475871a2008-07-27 21:46:04 +0000873SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
874 MVT VT, int Offset,
875 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000876 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000877
878 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
879 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000880 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000881 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
882 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
883 }
884
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000885 if (GVar && GVar->isThreadLocal())
886 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
887 else
888 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000889
Jim Laskey583bd472006-10-27 23:46:08 +0000890 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000891 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000892 ID.AddPointer(GV);
893 ID.AddInteger(Offset);
894 void *IP = 0;
895 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000896 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000897 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000898 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000899 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000900 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000901 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000902}
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000905 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000906 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000907 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000908 ID.AddInteger(FI);
909 void *IP = 0;
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000911 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000912 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000913 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000914 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000915 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000916 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000917}
918
Dan Gohman475871a2008-07-27 21:46:04 +0000919SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000920 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000921 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000922 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000923 ID.AddInteger(JTI);
924 void *IP = 0;
925 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000926 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000927 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000928 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000929 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000930 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000931 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000932}
933
Dan Gohman475871a2008-07-27 21:46:04 +0000934SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
935 unsigned Alignment, int Offset,
936 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000937 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000938 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000939 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000940 ID.AddInteger(Alignment);
941 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000942 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000943 void *IP = 0;
944 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000945 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000946 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000947 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000948 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000949 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000950 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000951}
952
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953
Dan Gohman475871a2008-07-27 21:46:04 +0000954SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
955 unsigned Alignment, int Offset,
956 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000957 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000958 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000959 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000960 ID.AddInteger(Alignment);
961 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000962 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000963 void *IP = 0;
964 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000965 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000966 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000967 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000968 CSEMap.InsertNode(N, IP);
969 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000970 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000971}
972
973
Dan Gohman475871a2008-07-27 21:46:04 +0000974SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000975 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000976 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000977 ID.AddPointer(MBB);
978 void *IP = 0;
979 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000980 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000981 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000982 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000983 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000984 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000985 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986}
987
Dan Gohman475871a2008-07-27 21:46:04 +0000988SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +0000989 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000990 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000991 ID.AddInteger(Flags.getRawBits());
992 void *IP = 0;
993 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000994 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000995 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000996 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000997 CSEMap.InsertNode(N, IP);
998 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000999 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001000}
1001
Dan Gohman475871a2008-07-27 21:46:04 +00001002SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1004 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001005
Duncan Sands83ec4b62008-06-06 12:08:01 +00001006 SDNode *&N = VT.isExtended() ?
1007 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001008
Dan Gohman475871a2008-07-27 21:46:04 +00001009 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001010 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001011 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001012 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001013 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001014}
1015
Dan Gohman475871a2008-07-27 21:46:04 +00001016SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001018 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001019 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001020 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001021 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001022 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001023}
1024
Dan Gohman475871a2008-07-27 21:46:04 +00001025SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001026 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001027 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001028 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001029 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001031 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032}
1033
Dan Gohman475871a2008-07-27 21:46:04 +00001034SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001035 if ((unsigned)Cond >= CondCodeNodes.size())
1036 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001037
Chris Lattner079a27a2005-08-09 20:40:02 +00001038 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001039 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001040 new (N) CondCodeSDNode(Cond);
1041 CondCodeNodes[Cond] = N;
1042 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001043 }
Dan Gohman475871a2008-07-27 21:46:04 +00001044 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001045}
1046
Dan Gohman475871a2008-07-27 21:46:04 +00001047SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001048 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001049 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001050 ID.AddInteger(RegNo);
1051 void *IP = 0;
1052 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001053 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001054 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001055 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001056 CSEMap.InsertNode(N, IP);
1057 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001059}
1060
Dan Gohman475871a2008-07-27 21:46:04 +00001061SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001062 unsigned Line, unsigned Col,
1063 const CompileUnitDesc *CU) {
1064 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001065 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001066 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001067 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001068}
1069
Dan Gohman475871a2008-07-27 21:46:04 +00001070SDValue SelectionDAG::getLabel(unsigned Opcode,
1071 SDValue Root,
1072 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001073 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001074 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001075 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1076 ID.AddInteger(LabelID);
1077 void *IP = 0;
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001079 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001080 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001081 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001082 CSEMap.InsertNode(N, IP);
1083 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001084 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001085}
1086
Dan Gohman475871a2008-07-27 21:46:04 +00001087SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001088 assert((!V || isa<PointerType>(V->getType())) &&
1089 "SrcValue is not a pointer?");
1090
Jim Laskey583bd472006-10-27 23:46:08 +00001091 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001092 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001093 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001094
Chris Lattner61b09412006-08-11 21:01:22 +00001095 void *IP = 0;
1096 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001097 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001098
Dan Gohmanfed90b62008-07-28 21:51:04 +00001099 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001100 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001101 CSEMap.InsertNode(N, IP);
1102 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001103 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001104}
1105
Dan Gohman475871a2008-07-27 21:46:04 +00001106SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001107 const Value *v = MO.getValue();
1108 assert((!v || isa<PointerType>(v->getType())) &&
1109 "SrcValue is not a pointer?");
1110
1111 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001112 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001113 ID.AddPointer(v);
1114 ID.AddInteger(MO.getFlags());
1115 ID.AddInteger(MO.getOffset());
1116 ID.AddInteger(MO.getSize());
1117 ID.AddInteger(MO.getAlignment());
1118
1119 void *IP = 0;
1120 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001121 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001122
Dan Gohmanfed90b62008-07-28 21:51:04 +00001123 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001124 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001125 CSEMap.InsertNode(N, IP);
1126 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001127 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001128}
1129
Chris Lattner37ce9df2007-10-15 17:47:20 +00001130/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1131/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001133 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001134 unsigned ByteSize = VT.getSizeInBits()/8;
1135 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001136 unsigned StackAlign =
1137 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1138
Chris Lattner37ce9df2007-10-15 17:47:20 +00001139 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1140 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1141}
1142
Dan Gohman475871a2008-07-27 21:46:04 +00001143SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1144 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 // These setcc operations always fold.
1146 switch (Cond) {
1147 default: break;
1148 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001149 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001150 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001151 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001152
1153 case ISD::SETOEQ:
1154 case ISD::SETOGT:
1155 case ISD::SETOGE:
1156 case ISD::SETOLT:
1157 case ISD::SETOLE:
1158 case ISD::SETONE:
1159 case ISD::SETO:
1160 case ISD::SETUO:
1161 case ISD::SETUEQ:
1162 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001163 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001164 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001166
Chris Lattner67255a12005-04-07 18:14:58 +00001167 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001168 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001169 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001170 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001171
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 switch (Cond) {
1173 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001174 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1175 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001176 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1177 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1178 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1179 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1180 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1181 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1182 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1183 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
Chris Lattner67255a12005-04-07 18:14:58 +00001186 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001187 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001188 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001189 // No compile time operations on this type yet.
1190 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001191 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001192
1193 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001195 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001196 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1197 return getNode(ISD::UNDEF, VT);
1198 // fall through
1199 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1200 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1201 return getNode(ISD::UNDEF, VT);
1202 // fall through
1203 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001204 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001205 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1206 return getNode(ISD::UNDEF, VT);
1207 // fall through
1208 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1209 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1210 return getNode(ISD::UNDEF, VT);
1211 // fall through
1212 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1213 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1214 return getNode(ISD::UNDEF, VT);
1215 // fall through
1216 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001217 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001218 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1219 return getNode(ISD::UNDEF, VT);
1220 // fall through
1221 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001222 R==APFloat::cmpEqual, VT);
1223 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1224 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1225 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1226 R==APFloat::cmpEqual, VT);
1227 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1228 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1229 R==APFloat::cmpLessThan, VT);
1230 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1231 R==APFloat::cmpUnordered, VT);
1232 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1233 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 }
1235 } else {
1236 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001237 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001239 }
1240
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001241 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001242 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001243}
1244
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001245/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1246/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001247bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001248 unsigned BitWidth = Op.getValueSizeInBits();
1249 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1250}
1251
Dan Gohmanea859be2007-06-22 14:59:07 +00001252/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1253/// this predicate to simplify operations downstream. Mask is known to be zero
1254/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001255bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001256 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001257 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001258 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1259 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1260 return (KnownZero & Mask) == Mask;
1261}
1262
1263/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1264/// known to be either zero or one and return them in the KnownZero/KnownOne
1265/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1266/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001267void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001268 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001269 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001270 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001271 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001272 "Mask size mismatches value type size!");
1273
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001274 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001275 if (Depth == 6 || Mask == 0)
1276 return; // Limit search depth.
1277
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001278 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001279
1280 switch (Op.getOpcode()) {
1281 case ISD::Constant:
1282 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001283 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001284 KnownZero = ~KnownOne & Mask;
1285 return;
1286 case ISD::AND:
1287 // If either the LHS or the RHS are Zero, the result is zero.
1288 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001289 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
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-1 bits are only known if set in both the LHS & RHS.
1295 KnownOne &= KnownOne2;
1296 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1297 KnownZero |= KnownZero2;
1298 return;
1299 case ISD::OR:
1300 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001301 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1302 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001303 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1304 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1305
1306 // Output known-0 bits are only known if clear in both the LHS & RHS.
1307 KnownZero &= KnownZero2;
1308 // Output known-1 are known to be set if set in either the LHS | RHS.
1309 KnownOne |= KnownOne2;
1310 return;
1311 case ISD::XOR: {
1312 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1313 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1314 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1315 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1316
1317 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001318 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001319 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1320 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1321 KnownZero = KnownZeroOut;
1322 return;
1323 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001324 case ISD::MUL: {
1325 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1326 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1327 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1329 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1330
1331 // If low bits are zero in either operand, output low known-0 bits.
1332 // Also compute a conserative estimate for high known-0 bits.
1333 // More trickiness is possible, but this is sufficient for the
1334 // interesting case of alignment computation.
1335 KnownOne.clear();
1336 unsigned TrailZ = KnownZero.countTrailingOnes() +
1337 KnownZero2.countTrailingOnes();
1338 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001339 KnownZero2.countLeadingOnes(),
1340 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001341
1342 TrailZ = std::min(TrailZ, BitWidth);
1343 LeadZ = std::min(LeadZ, BitWidth);
1344 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1345 APInt::getHighBitsSet(BitWidth, LeadZ);
1346 KnownZero &= Mask;
1347 return;
1348 }
1349 case ISD::UDIV: {
1350 // For the purposes of computing leading zeros we can conservatively
1351 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001352 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001353 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1354 ComputeMaskedBits(Op.getOperand(0),
1355 AllOnes, KnownZero2, KnownOne2, Depth+1);
1356 unsigned LeadZ = KnownZero2.countLeadingOnes();
1357
1358 KnownOne2.clear();
1359 KnownZero2.clear();
1360 ComputeMaskedBits(Op.getOperand(1),
1361 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001362 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1363 if (RHSUnknownLeadingOnes != BitWidth)
1364 LeadZ = std::min(BitWidth,
1365 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001366
1367 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1368 return;
1369 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001370 case ISD::SELECT:
1371 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1372 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1373 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1374 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1375
1376 // Only known if known in both the LHS and RHS.
1377 KnownOne &= KnownOne2;
1378 KnownZero &= KnownZero2;
1379 return;
1380 case ISD::SELECT_CC:
1381 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1382 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1383 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1384 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1385
1386 // Only known if known in both the LHS and RHS.
1387 KnownOne &= KnownOne2;
1388 KnownZero &= KnownZero2;
1389 return;
1390 case ISD::SETCC:
1391 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001392 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1393 BitWidth > 1)
1394 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001395 return;
1396 case ISD::SHL:
1397 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1398 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001399 unsigned ShAmt = SA->getValue();
1400
1401 // If the shift count is an invalid immediate, don't do anything.
1402 if (ShAmt >= BitWidth)
1403 return;
1404
1405 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001406 KnownZero, KnownOne, Depth+1);
1407 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001408 KnownZero <<= ShAmt;
1409 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001410 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001411 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001412 }
1413 return;
1414 case ISD::SRL:
1415 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1416 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001417 unsigned ShAmt = SA->getValue();
1418
Dan Gohmand4cf9922008-02-26 18:50:50 +00001419 // If the shift count is an invalid immediate, don't do anything.
1420 if (ShAmt >= BitWidth)
1421 return;
1422
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001423 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001424 KnownZero, KnownOne, Depth+1);
1425 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001426 KnownZero = KnownZero.lshr(ShAmt);
1427 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001428
Dan Gohman72d2fd52008-02-13 22:43:25 +00001429 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 KnownZero |= HighBits; // High bits known zero.
1431 }
1432 return;
1433 case ISD::SRA:
1434 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001435 unsigned ShAmt = SA->getValue();
1436
Dan Gohmand4cf9922008-02-26 18:50:50 +00001437 // If the shift count is an invalid immediate, don't do anything.
1438 if (ShAmt >= BitWidth)
1439 return;
1440
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001441 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001442 // If any of the demanded bits are produced by the sign extension, we also
1443 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001444 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1445 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001446 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001447
1448 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1449 Depth+1);
1450 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001451 KnownZero = KnownZero.lshr(ShAmt);
1452 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001453
1454 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001455 APInt SignBit = APInt::getSignBit(BitWidth);
1456 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001457
Dan Gohmanca93a432008-02-20 16:30:17 +00001458 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001459 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001460 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001461 KnownOne |= HighBits; // New bits are known one.
1462 }
1463 }
1464 return;
1465 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001466 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1467 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001468
1469 // Sign extension. Compute the demanded bits in the result that are not
1470 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001471 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001472
Dan Gohman977a76f2008-02-13 22:28:48 +00001473 APInt InSignBit = APInt::getSignBit(EBits);
1474 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001475
1476 // If the sign extended bits are demanded, we know that the sign
1477 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001478 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001479 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001480 InputDemandedBits |= InSignBit;
1481
1482 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1483 KnownZero, KnownOne, Depth+1);
1484 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1485
1486 // If the sign bit of the input is known set or clear, then we know the
1487 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001488 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001489 KnownZero |= NewBits;
1490 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001491 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001492 KnownOne |= NewBits;
1493 KnownZero &= ~NewBits;
1494 } else { // Input sign bit unknown
1495 KnownZero &= ~NewBits;
1496 KnownOne &= ~NewBits;
1497 }
1498 return;
1499 }
1500 case ISD::CTTZ:
1501 case ISD::CTLZ:
1502 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001503 unsigned LowBits = Log2_32(BitWidth)+1;
1504 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001505 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001506 return;
1507 }
1508 case ISD::LOAD: {
1509 if (ISD::isZEXTLoad(Op.Val)) {
1510 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001511 MVT VT = LD->getMemoryVT();
1512 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001513 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 }
1515 return;
1516 }
1517 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001518 MVT InVT = Op.getOperand(0).getValueType();
1519 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001520 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1521 APInt InMask = Mask;
1522 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001523 KnownZero.trunc(InBits);
1524 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001525 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001526 KnownZero.zext(BitWidth);
1527 KnownOne.zext(BitWidth);
1528 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001529 return;
1530 }
1531 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 MVT InVT = Op.getOperand(0).getValueType();
1533 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001534 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001535 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1536 APInt InMask = Mask;
1537 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001538
1539 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001540 // bit is demanded. Temporarily set this bit in the mask for our callee.
1541 if (NewBits.getBoolValue())
1542 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001543
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001544 KnownZero.trunc(InBits);
1545 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001546 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1547
1548 // Note if the sign bit is known to be zero or one.
1549 bool SignBitKnownZero = KnownZero.isNegative();
1550 bool SignBitKnownOne = KnownOne.isNegative();
1551 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1552 "Sign bit can't be known to be both zero and one!");
1553
1554 // If the sign bit wasn't actually demanded by our caller, we don't
1555 // want it set in the KnownZero and KnownOne result values. Reset the
1556 // mask and reapply it to the result values.
1557 InMask = Mask;
1558 InMask.trunc(InBits);
1559 KnownZero &= InMask;
1560 KnownOne &= InMask;
1561
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001562 KnownZero.zext(BitWidth);
1563 KnownOne.zext(BitWidth);
1564
Dan Gohman977a76f2008-02-13 22:28:48 +00001565 // If the sign bit is known zero or one, the top bits match.
1566 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001567 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001568 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001569 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 return;
1571 }
1572 case ISD::ANY_EXTEND: {
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.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001577 KnownZero.trunc(InBits);
1578 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001579 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001580 KnownZero.zext(BitWidth);
1581 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001582 return;
1583 }
1584 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 MVT InVT = Op.getOperand(0).getValueType();
1586 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001587 APInt InMask = Mask;
1588 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001589 KnownZero.zext(InBits);
1590 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001591 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001592 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001593 KnownZero.trunc(BitWidth);
1594 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 break;
1596 }
1597 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001598 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1599 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001600 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1601 KnownOne, Depth+1);
1602 KnownZero |= (~InMask) & Mask;
1603 return;
1604 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001605 case ISD::FGETSIGN:
1606 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001607 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001608 return;
1609
Dan Gohman23e8b712008-04-28 17:02:21 +00001610 case ISD::SUB: {
1611 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1612 // We know that the top bits of C-X are clear if X contains less bits
1613 // than C (i.e. no wrap-around can happen). For example, 20-X is
1614 // positive if we can prove that X is >= 0 and < 16.
1615 if (CLHS->getAPIntValue().isNonNegative()) {
1616 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1617 // NLZ can't be BitWidth with no sign bit
1618 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1619 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1620 Depth+1);
1621
1622 // If all of the MaskV bits are known to be zero, then we know the
1623 // output top bits are zero, because we now know that the output is
1624 // from [0-C].
1625 if ((KnownZero2 & MaskV) == MaskV) {
1626 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1627 // Top bits known zero.
1628 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1629 }
1630 }
1631 }
1632 }
1633 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001634 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 // Output known-0 bits are known if clear or set in both the low clear bits
1636 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1637 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001638 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1639 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1640 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1641 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1642
1643 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1644 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1645 KnownZeroOut = std::min(KnownZeroOut,
1646 KnownZero2.countTrailingOnes());
1647
1648 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001649 return;
1650 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001651 case ISD::SREM:
1652 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001653 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001654 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001655 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001656 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1657 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001658
Dan Gohmana60832b2008-08-13 23:12:35 +00001659 // If the sign bit of the first operand is zero, the sign bit of
1660 // the result is zero. If the first operand has no one bits below
1661 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001662 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1663 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001664
Dan Gohman23e8b712008-04-28 17:02:21 +00001665 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001666
1667 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001668 }
1669 }
1670 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001671 case ISD::UREM: {
1672 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001673 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001674 if (RA.isPowerOf2()) {
1675 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001676 APInt Mask2 = LowBits & Mask;
1677 KnownZero |= ~LowBits & Mask;
1678 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1679 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1680 break;
1681 }
1682 }
1683
1684 // Since the result is less than or equal to either operand, any leading
1685 // zero bits in either operand must also exist in the result.
1686 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1687 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1688 Depth+1);
1689 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1690 Depth+1);
1691
1692 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1693 KnownZero2.countLeadingOnes());
1694 KnownOne.clear();
1695 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1696 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001697 }
1698 default:
1699 // Allow the target to implement this method for its nodes.
1700 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1701 case ISD::INTRINSIC_WO_CHAIN:
1702 case ISD::INTRINSIC_W_CHAIN:
1703 case ISD::INTRINSIC_VOID:
1704 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1705 }
1706 return;
1707 }
1708}
1709
1710/// ComputeNumSignBits - Return the number of times the sign bit of the
1711/// register is replicated into the other bits. We know that at least 1 bit
1712/// is always equal to the sign bit (itself), but other cases can give us
1713/// information. For example, immediately after an "SRA X, 2", we know that
1714/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001715unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 MVT VT = Op.getValueType();
1717 assert(VT.isInteger() && "Invalid VT!");
1718 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001719 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001720 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001721
1722 if (Depth == 6)
1723 return 1; // Limit search depth.
1724
1725 switch (Op.getOpcode()) {
1726 default: break;
1727 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001728 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001729 return VTBits-Tmp+1;
1730 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001731 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001732 return VTBits-Tmp;
1733
1734 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001735 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1736 // If negative, return # leading ones.
1737 if (Val.isNegative())
1738 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001739
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001740 // Return # leading zeros.
1741 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 }
1743
1744 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001745 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001746 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1747
1748 case ISD::SIGN_EXTEND_INREG:
1749 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001750 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001751 Tmp = VTBits-Tmp+1;
1752
1753 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1754 return std::max(Tmp, Tmp2);
1755
1756 case ISD::SRA:
1757 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1758 // SRA X, C -> adds C sign bits.
1759 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1760 Tmp += C->getValue();
1761 if (Tmp > VTBits) Tmp = VTBits;
1762 }
1763 return Tmp;
1764 case ISD::SHL:
1765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1766 // shl destroys sign bits.
1767 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1768 if (C->getValue() >= VTBits || // Bad shift.
1769 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1770 return Tmp - C->getValue();
1771 }
1772 break;
1773 case ISD::AND:
1774 case ISD::OR:
1775 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001776 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001778 if (Tmp != 1) {
1779 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1780 FirstAnswer = std::min(Tmp, Tmp2);
1781 // We computed what we know about the sign bits as our first
1782 // answer. Now proceed to the generic code that uses
1783 // ComputeMaskedBits, and pick whichever answer is better.
1784 }
1785 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001786
1787 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001788 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001789 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001790 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001791 return std::min(Tmp, Tmp2);
1792
1793 case ISD::SETCC:
1794 // If setcc returns 0/-1, all bits are sign bits.
1795 if (TLI.getSetCCResultContents() ==
1796 TargetLowering::ZeroOrNegativeOneSetCCResult)
1797 return VTBits;
1798 break;
1799 case ISD::ROTL:
1800 case ISD::ROTR:
1801 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1802 unsigned RotAmt = C->getValue() & (VTBits-1);
1803
1804 // Handle rotate right by N like a rotate left by 32-N.
1805 if (Op.getOpcode() == ISD::ROTR)
1806 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1807
1808 // If we aren't rotating out all of the known-in sign bits, return the
1809 // number that are left. This handles rotl(sext(x), 1) for example.
1810 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1811 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1812 }
1813 break;
1814 case ISD::ADD:
1815 // Add can have at most one carry bit. Thus we know that the output
1816 // is, at worst, one more bit than the inputs.
1817 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1818 if (Tmp == 1) return 1; // Early out.
1819
1820 // Special case decrementing a value (ADD X, -1):
1821 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1822 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001823 APInt KnownZero, KnownOne;
1824 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001825 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1826
1827 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1828 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001829 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001830 return VTBits;
1831
1832 // If we are subtracting one from a positive number, there is no carry
1833 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001834 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 return Tmp;
1836 }
1837
1838 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1839 if (Tmp2 == 1) return 1;
1840 return std::min(Tmp, Tmp2)-1;
1841 break;
1842
1843 case ISD::SUB:
1844 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1845 if (Tmp2 == 1) return 1;
1846
1847 // Handle NEG.
1848 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001849 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001850 APInt KnownZero, KnownOne;
1851 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1853 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1854 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001855 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001856 return VTBits;
1857
1858 // If the input is known to be positive (the sign bit is known clear),
1859 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001860 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 return Tmp2;
1862
1863 // Otherwise, we treat this like a SUB.
1864 }
1865
1866 // Sub can have at most one carry bit. Thus we know that the output
1867 // is, at worst, one more bit than the inputs.
1868 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1869 if (Tmp == 1) return 1; // Early out.
1870 return std::min(Tmp, Tmp2)-1;
1871 break;
1872 case ISD::TRUNCATE:
1873 // FIXME: it's tricky to do anything useful for this, but it is an important
1874 // case for targets like X86.
1875 break;
1876 }
1877
1878 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1879 if (Op.getOpcode() == ISD::LOAD) {
1880 LoadSDNode *LD = cast<LoadSDNode>(Op);
1881 unsigned ExtType = LD->getExtensionType();
1882 switch (ExtType) {
1883 default: break;
1884 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001885 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001886 return VTBits-Tmp+1;
1887 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001888 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 return VTBits-Tmp;
1890 }
1891 }
1892
1893 // Allow the target to implement this method for its nodes.
1894 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1895 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1896 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1897 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1898 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001899 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001900 }
1901
1902 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1903 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001904 APInt KnownZero, KnownOne;
1905 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001906 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1907
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001908 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001909 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001910 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 Mask = KnownOne;
1912 } else {
1913 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001914 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001915 }
1916
1917 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1918 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001919 Mask = ~Mask;
1920 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001921 // Return # leading zeros. We use 'min' here in case Val was zero before
1922 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001923 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001924}
1925
Chris Lattner51dabfb2006-10-14 00:41:01 +00001926
Dan Gohman475871a2008-07-27 21:46:04 +00001927bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001928 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1929 if (!GA) return false;
1930 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1931 if (!GV) return false;
1932 MachineModuleInfo *MMI = getMachineModuleInfo();
1933 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1934}
1935
1936
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001937/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1938/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00001939SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001940 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001941 SDValue PermMask = N->getOperand(2);
1942 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00001943 if (Idx.getOpcode() == ISD::UNDEF)
1944 return getNode(ISD::UNDEF, VT.getVectorElementType());
1945 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001946 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00001948 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001949
1950 if (V.getOpcode() == ISD::BIT_CONVERT) {
1951 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001952 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00001953 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001954 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001955 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001956 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001957 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001958 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001959 return V.getOperand(Index);
1960 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1961 return getShuffleScalarElt(V.Val, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00001962 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001963}
1964
1965
Chris Lattnerc3aae252005-01-07 07:46:32 +00001966/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001967///
Dan Gohman475871a2008-07-27 21:46:04 +00001968SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001969 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001970 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001971 void *IP = 0;
1972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001973 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001974 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001975 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001976 CSEMap.InsertNode(N, IP);
1977
1978 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00001979#ifndef NDEBUG
1980 VerifyNode(N);
1981#endif
Dan Gohman475871a2008-07-27 21:46:04 +00001982 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001983}
1984
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001986 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001987 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001988 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001990 switch (Opcode) {
1991 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001992 case ISD::SIGN_EXTEND:
1993 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001994 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001995 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001996 case ISD::TRUNCATE:
1997 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001998 case ISD::UINT_TO_FP:
1999 case ISD::SINT_TO_FP: {
2000 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002001 // No compile time operations on this type.
2002 if (VT==MVT::ppcf128)
2003 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002004 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2005 (void)apf.convertFromAPInt(Val,
2006 Opcode==ISD::SINT_TO_FP,
2007 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002008 return getConstantFP(apf, VT);
2009 }
Chris Lattner94683772005-12-23 05:30:37 +00002010 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002011 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002012 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002013 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002014 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002015 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002016 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002017 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002018 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002019 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002020 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002021 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002022 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002023 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002024 }
2025 }
2026
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002027 // Constant fold unary operations with a floating point constant operand.
2028 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2029 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002030 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002031 switch (Opcode) {
2032 case ISD::FNEG:
2033 V.changeSign();
2034 return getConstantFP(V, VT);
2035 case ISD::FABS:
2036 V.clearSign();
2037 return getConstantFP(V, VT);
2038 case ISD::FP_ROUND:
2039 case ISD::FP_EXTEND:
2040 // This can return overflow, underflow, or inexact; we don't care.
2041 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002042 (void)V.convert(*MVTToAPFloatSemantics(VT),
2043 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002044 return getConstantFP(V, VT);
2045 case ISD::FP_TO_SINT:
2046 case ISD::FP_TO_UINT: {
2047 integerPart x;
2048 assert(integerPartWidth >= 64);
2049 // FIXME need to be more flexible about rounding mode.
2050 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2051 Opcode==ISD::FP_TO_SINT,
2052 APFloat::rmTowardZero);
2053 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2054 break;
2055 return getConstant(x, VT);
2056 }
2057 case ISD::BIT_CONVERT:
2058 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2059 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2060 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2061 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002062 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002063 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002064 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002065 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002066
2067 unsigned OpOpcode = Operand.Val->getOpcode();
2068 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002069 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002070 case ISD::CONCAT_VECTORS:
2071 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002072 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002073 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002074 assert(VT.isFloatingPoint() &&
2075 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002076 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002077 if (Operand.getOpcode() == ISD::UNDEF)
2078 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002079 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002080 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002081 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002082 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002083 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002084 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002085 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002086 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2087 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2088 break;
2089 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002090 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002091 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002092 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002093 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002094 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002095 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002096 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002097 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002098 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002100 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002101 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002102 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002103 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002104 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2105 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2106 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2107 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002108 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002109 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002110 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002111 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002112 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002113 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002114 if (OpOpcode == ISD::TRUNCATE)
2115 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002116 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2117 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002118 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002119 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002120 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002121 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002122 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2123 else
2124 return Operand.Val->getOperand(0);
2125 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002126 break;
Chris Lattner35481892005-12-23 00:16:34 +00002127 case ISD::BIT_CONVERT:
2128 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002130 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002131 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002132 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2133 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002134 if (OpOpcode == ISD::UNDEF)
2135 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002136 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002137 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2139 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002140 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002141 if (OpOpcode == ISD::UNDEF)
2142 return getNode(ISD::UNDEF, VT);
2143 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2144 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2145 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2146 Operand.getConstantOperandVal(1) == 0 &&
2147 Operand.getOperand(0).getValueType() == VT)
2148 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002149 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002150 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002151 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2152 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002153 Operand.Val->getOperand(0));
2154 if (OpOpcode == ISD::FNEG) // --X -> X
2155 return Operand.Val->getOperand(0);
2156 break;
2157 case ISD::FABS:
2158 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2159 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2160 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002161 }
2162
Chris Lattner43247a12005-08-25 19:12:10 +00002163 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002164 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002165 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002166 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002167 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002168 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002169 void *IP = 0;
2170 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002171 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002172 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002173 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002174 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002175 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002176 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002177 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002178 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002179
Chris Lattnerc3aae252005-01-07 07:46:32 +00002180 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002181#ifndef NDEBUG
2182 VerifyNode(N);
2183#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002184 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002185}
2186
Dan Gohman475871a2008-07-27 21:46:04 +00002187SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2188 SDValue N1, SDValue N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002189 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2190 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002191 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002192 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002193 case ISD::TokenFactor:
2194 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2195 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002196 // Fold trivial token factors.
2197 if (N1.getOpcode() == ISD::EntryToken) return N2;
2198 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002199 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002200 case ISD::CONCAT_VECTORS:
2201 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2202 // one big BUILD_VECTOR.
2203 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2204 N2.getOpcode() == ISD::BUILD_VECTOR) {
2205 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2206 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2207 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2208 }
2209 break;
Chris Lattner76365122005-01-16 02:23:22 +00002210 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002211 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002212 N1.getValueType() == VT && "Binary operator types must match!");
2213 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2214 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002215 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002216 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002217 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2218 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002219 break;
Chris Lattner76365122005-01-16 02:23:22 +00002220 case ISD::OR:
2221 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002222 case ISD::ADD:
2223 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002225 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002226 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2227 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002228 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002229 return N1;
2230 break;
Chris Lattner76365122005-01-16 02:23:22 +00002231 case ISD::UDIV:
2232 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002233 case ISD::MULHU:
2234 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002236 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002237 case ISD::MUL:
2238 case ISD::SDIV:
2239 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002240 case ISD::FADD:
2241 case ISD::FSUB:
2242 case ISD::FMUL:
2243 case ISD::FDIV:
2244 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002245 assert(N1.getValueType() == N2.getValueType() &&
2246 N1.getValueType() == VT && "Binary operator types must match!");
2247 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002248 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2249 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002250 N1.getValueType().isFloatingPoint() &&
2251 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002252 "Invalid FCOPYSIGN!");
2253 break;
Chris Lattner76365122005-01-16 02:23:22 +00002254 case ISD::SHL:
2255 case ISD::SRA:
2256 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002257 case ISD::ROTL:
2258 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002259 assert(VT == N1.getValueType() &&
2260 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002262 "Shifts only work on integers");
2263
2264 // Always fold shifts of i1 values so the code generator doesn't need to
2265 // handle them. Since we know the size of the shift has to be less than the
2266 // size of the value, the shift/rotate count is guaranteed to be zero.
2267 if (VT == MVT::i1)
2268 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002269 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002270 case ISD::FP_ROUND_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 round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002273 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002274 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002275 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002276 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002277 break;
2278 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002279 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002280 assert(VT.isFloatingPoint() &&
2281 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002282 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002283 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002284 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002285 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002286 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002287 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002288 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002289 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002290 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002291 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002292 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002293 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002294 break;
2295 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002296 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002297 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002298 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002299 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002300 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002301 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002302 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002303
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002304 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002305 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002306 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002307 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002308 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002309 return getConstant(Val, VT);
2310 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002311 break;
2312 }
2313 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002314 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2315 if (N1.getOpcode() == ISD::UNDEF)
2316 return getNode(ISD::UNDEF, VT);
2317
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002318 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2319 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002320 if (N2C &&
2321 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002322 N1.getNumOperands() > 0) {
2323 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002324 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002325 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2326 N1.getOperand(N2C->getValue() / Factor),
2327 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2328 }
2329
2330 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2331 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002332 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002333 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002334
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002335 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2336 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002337 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2338 if (N1.getOperand(2) == N2)
2339 return N1.getOperand(1);
2340 else
2341 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2342 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002343 break;
2344 case ISD::EXTRACT_ELEMENT:
2345 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002346 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2347 (N1.getValueType().isInteger() == VT.isInteger()) &&
2348 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002349
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002350 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2351 // 64-bit integers into 32-bit parts. Instead of building the extract of
2352 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2353 if (N1.getOpcode() == ISD::BUILD_PAIR)
2354 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002355
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002356 // EXTRACT_ELEMENT of a constant int is also very common.
2357 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002358 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002359 unsigned Shift = ElementSize * N2C->getValue();
2360 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2361 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002362 }
2363 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002364 case ISD::EXTRACT_SUBVECTOR:
2365 if (N1.getValueType() == VT) // Trivial extraction.
2366 return N1;
2367 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002368 }
2369
2370 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002371 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002372 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 switch (Opcode) {
2374 case ISD::ADD: return getConstant(C1 + C2, VT);
2375 case ISD::SUB: return getConstant(C1 - C2, VT);
2376 case ISD::MUL: return getConstant(C1 * C2, VT);
2377 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002378 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002379 break;
2380 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002381 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002382 break;
2383 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002384 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385 break;
2386 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002387 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002388 break;
2389 case ISD::AND : return getConstant(C1 & C2, VT);
2390 case ISD::OR : return getConstant(C1 | C2, VT);
2391 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002392 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002393 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2394 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2395 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2396 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002397 default: break;
2398 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002399 } else { // Cannonicalize constant to RHS if commutative
2400 if (isCommutativeBinOp(Opcode)) {
2401 std::swap(N1C, N2C);
2402 std::swap(N1, N2);
2403 }
2404 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002405 }
2406
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002407 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2409 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002410 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002411 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2412 // Cannonicalize constant to RHS if commutative
2413 std::swap(N1CFP, N2CFP);
2414 std::swap(N1, N2);
2415 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002416 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2417 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002418 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002419 case ISD::FADD:
2420 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002421 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002422 return getConstantFP(V1, VT);
2423 break;
2424 case ISD::FSUB:
2425 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2426 if (s!=APFloat::opInvalidOp)
2427 return getConstantFP(V1, VT);
2428 break;
2429 case ISD::FMUL:
2430 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2431 if (s!=APFloat::opInvalidOp)
2432 return getConstantFP(V1, VT);
2433 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002434 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002435 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2436 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2437 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002438 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002439 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002440 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2441 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2442 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002443 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002444 case ISD::FCOPYSIGN:
2445 V1.copySign(V2);
2446 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002447 default: break;
2448 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002449 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002450 }
Chris Lattner62b57722006-04-20 05:39:12 +00002451
2452 // Canonicalize an UNDEF to the RHS, even over a constant.
2453 if (N1.getOpcode() == ISD::UNDEF) {
2454 if (isCommutativeBinOp(Opcode)) {
2455 std::swap(N1, N2);
2456 } else {
2457 switch (Opcode) {
2458 case ISD::FP_ROUND_INREG:
2459 case ISD::SIGN_EXTEND_INREG:
2460 case ISD::SUB:
2461 case ISD::FSUB:
2462 case ISD::FDIV:
2463 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002464 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002465 return N1; // fold op(undef, arg2) -> undef
2466 case ISD::UDIV:
2467 case ISD::SDIV:
2468 case ISD::UREM:
2469 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002470 case ISD::SRL:
2471 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002472 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002473 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2474 // For vectors, we can't easily build an all zero vector, just return
2475 // the LHS.
2476 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002477 }
2478 }
2479 }
2480
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002481 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002482 if (N2.getOpcode() == ISD::UNDEF) {
2483 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002484 case ISD::XOR:
2485 if (N1.getOpcode() == ISD::UNDEF)
2486 // Handle undef ^ undef -> 0 special case. This is a common
2487 // idiom (misuse).
2488 return getConstant(0, VT);
2489 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002490 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002491 case ISD::ADDC:
2492 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002493 case ISD::SUB:
2494 case ISD::FADD:
2495 case ISD::FSUB:
2496 case ISD::FMUL:
2497 case ISD::FDIV:
2498 case ISD::FREM:
2499 case ISD::UDIV:
2500 case ISD::SDIV:
2501 case ISD::UREM:
2502 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002503 return N2; // fold op(arg1, undef) -> undef
2504 case ISD::MUL:
2505 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002506 case ISD::SRL:
2507 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002508 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002509 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2510 // For vectors, we can't easily build an all zero vector, just return
2511 // the LHS.
2512 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002513 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002514 if (!VT.isVector())
2515 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002516 // For vectors, we can't easily build an all one vector, just return
2517 // the LHS.
2518 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002519 case ISD::SRA:
2520 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002521 }
2522 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002523
Chris Lattner27e9b412005-05-11 18:57:39 +00002524 // Memoize this node if possible.
2525 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002526 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002527 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002528 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002529 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002530 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002531 void *IP = 0;
2532 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002533 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002534 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002535 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002536 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002537 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002538 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002539 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002540 }
2541
Chris Lattnerc3aae252005-01-07 07:46:32 +00002542 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002543#ifndef NDEBUG
2544 VerifyNode(N);
2545#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002546 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002547}
2548
Dan Gohman475871a2008-07-27 21:46:04 +00002549SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2550 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002551 // Perform various simplifications.
2552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2553 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002554 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002555 case ISD::CONCAT_VECTORS:
2556 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2557 // one big BUILD_VECTOR.
2558 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2559 N2.getOpcode() == ISD::BUILD_VECTOR &&
2560 N3.getOpcode() == ISD::BUILD_VECTOR) {
2561 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2562 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2563 Elts.insert(Elts.end(), N3.Val->op_begin(), N3.Val->op_end());
2564 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2565 }
2566 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002567 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002568 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002569 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002570 if (Simp.Val) return Simp;
2571 break;
2572 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002574 if (N1C) {
2575 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002576 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002577 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002578 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002579 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002580
2581 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002582 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002583 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002584 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002585 if (N2C->getValue()) // Unconditional branch
2586 return getNode(ISD::BR, MVT::Other, N1, N3);
2587 else
2588 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002589 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002590 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002591 case ISD::VECTOR_SHUFFLE:
2592 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002593 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002594 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002595 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002596 "Illegal VECTOR_SHUFFLE node!");
2597 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002598 case ISD::BIT_CONVERT:
2599 // Fold bit_convert nodes from a type to themselves.
2600 if (N1.getValueType() == VT)
2601 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002602 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002603 }
2604
Chris Lattner43247a12005-08-25 19:12:10 +00002605 // Memoize node if it doesn't produce a flag.
2606 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002607 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002608 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002609 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002610 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002611 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002612 void *IP = 0;
2613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002614 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002615 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002616 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002617 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002618 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002619 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002620 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002621 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002622 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002623#ifndef NDEBUG
2624 VerifyNode(N);
2625#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002626 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002627}
2628
Dan Gohman475871a2008-07-27 21:46:04 +00002629SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2630 SDValue N1, SDValue N2, SDValue N3,
2631 SDValue N4) {
2632 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002633 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002634}
2635
Dan Gohman475871a2008-07-27 21:46:04 +00002636SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2637 SDValue N1, SDValue N2, SDValue N3,
2638 SDValue N4, SDValue N5) {
2639 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002640 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002641}
2642
Dan Gohman707e0182008-04-12 04:36:06 +00002643/// getMemsetValue - Vectorized representation of the memset value
2644/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002645static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002646 unsigned NumBits = VT.isVector() ?
2647 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002648 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002649 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002650 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002651 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002652 Val = (Val << Shift) | Val;
2653 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002654 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002655 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002656 return DAG.getConstant(Val, VT);
2657 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002658 }
Evan Chengf0df0312008-05-15 08:39:06 +00002659
2660 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2661 unsigned Shift = 8;
2662 for (unsigned i = NumBits; i > 8; i >>= 1) {
2663 Value = DAG.getNode(ISD::OR, VT,
2664 DAG.getNode(ISD::SHL, VT, Value,
2665 DAG.getConstant(Shift, MVT::i8)), Value);
2666 Shift <<= 1;
2667 }
2668
2669 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002670}
2671
Dan Gohman707e0182008-04-12 04:36:06 +00002672/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2673/// used when a memcpy is turned into a memset when the source is a constant
2674/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002675static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002676 const TargetLowering &TLI,
2677 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002678 // Handle vector with all elements zero.
2679 if (Str.empty()) {
2680 if (VT.isInteger())
2681 return DAG.getConstant(0, VT);
2682 unsigned NumElts = VT.getVectorNumElements();
2683 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2684 return DAG.getNode(ISD::BIT_CONVERT, VT,
2685 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2686 }
2687
Duncan Sands83ec4b62008-06-06 12:08:01 +00002688 assert(!VT.isVector() && "Can't handle vector type here!");
2689 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002690 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002691 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002692 if (TLI.isLittleEndian())
2693 Offset = Offset + MSB - 1;
2694 for (unsigned i = 0; i != MSB; ++i) {
2695 Val = (Val << 8) | (unsigned char)Str[Offset];
2696 Offset += TLI.isLittleEndian() ? -1 : 1;
2697 }
2698 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002699}
2700
Dan Gohman707e0182008-04-12 04:36:06 +00002701/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002702///
Dan Gohman475871a2008-07-27 21:46:04 +00002703static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002704 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002705 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002706 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2707}
2708
Evan Chengf0df0312008-05-15 08:39:06 +00002709/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2710///
Dan Gohman475871a2008-07-27 21:46:04 +00002711static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002712 unsigned SrcDelta = 0;
2713 GlobalAddressSDNode *G = NULL;
2714 if (Src.getOpcode() == ISD::GlobalAddress)
2715 G = cast<GlobalAddressSDNode>(Src);
2716 else if (Src.getOpcode() == ISD::ADD &&
2717 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2718 Src.getOperand(1).getOpcode() == ISD::Constant) {
2719 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2720 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2721 }
2722 if (!G)
2723 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002724
Evan Chengf0df0312008-05-15 08:39:06 +00002725 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002726 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2727 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002728
Evan Chengf0df0312008-05-15 08:39:06 +00002729 return false;
2730}
Dan Gohman707e0182008-04-12 04:36:06 +00002731
Evan Chengf0df0312008-05-15 08:39:06 +00002732/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2733/// to replace the memset / memcpy is below the threshold. It also returns the
2734/// types of the sequence of memory ops to perform memset / memcpy.
2735static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002736bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002738 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002739 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002740 SelectionDAG &DAG,
2741 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002742 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002743 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002744 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002746 if (VT != MVT::iAny) {
2747 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002748 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002749 // If source is a string constant, this will require an unaligned load.
2750 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2751 if (Dst.getOpcode() != ISD::FrameIndex) {
2752 // Can't change destination alignment. It requires a unaligned store.
2753 if (AllowUnalign)
2754 VT = MVT::iAny;
2755 } else {
2756 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2757 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2758 if (MFI->isFixedObjectIndex(FI)) {
2759 // Can't change destination alignment. It requires a unaligned store.
2760 if (AllowUnalign)
2761 VT = MVT::iAny;
2762 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002763 // Give the stack frame object a larger alignment if needed.
2764 if (MFI->getObjectAlignment(FI) < NewAlign)
2765 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002766 Align = NewAlign;
2767 }
2768 }
2769 }
2770 }
2771
2772 if (VT == MVT::iAny) {
2773 if (AllowUnalign) {
2774 VT = MVT::i64;
2775 } else {
2776 switch (Align & 7) {
2777 case 0: VT = MVT::i64; break;
2778 case 4: VT = MVT::i32; break;
2779 case 2: VT = MVT::i16; break;
2780 default: VT = MVT::i8; break;
2781 }
2782 }
2783
Duncan Sands83ec4b62008-06-06 12:08:01 +00002784 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002785 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002786 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2787 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002788
Duncan Sands8e4eb092008-06-08 20:54:56 +00002789 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002790 VT = LVT;
2791 }
Dan Gohman707e0182008-04-12 04:36:06 +00002792
2793 unsigned NumMemOps = 0;
2794 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002795 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002796 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002797 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002798 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002799 VT = MVT::i64;
2800 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002801 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2802 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002803 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002804 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002805 VTSize >>= 1;
2806 }
Dan Gohman707e0182008-04-12 04:36:06 +00002807 }
Dan Gohman707e0182008-04-12 04:36:06 +00002808
2809 if (++NumMemOps > Limit)
2810 return false;
2811 MemOps.push_back(VT);
2812 Size -= VTSize;
2813 }
2814
2815 return true;
2816}
2817
Dan Gohman475871a2008-07-27 21:46:04 +00002818static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2819 SDValue Chain, SDValue Dst,
2820 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002821 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002822 const Value *DstSV, uint64_t DstSVOff,
2823 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2825
Dan Gohman21323f32008-05-29 19:42:22 +00002826 // Expand memcpy to a series of load and store ops if the size operand falls
2827 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002829 uint64_t Limit = -1;
2830 if (!AlwaysInline)
2831 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002832 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002833 std::string Str;
2834 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002835 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002836 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002837 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002838
Dan Gohman707e0182008-04-12 04:36:06 +00002839
Evan Cheng0ff39b32008-06-30 07:31:25 +00002840 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002841 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002842 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002843 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002844 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002845 MVT VT = MemOps[i];
2846 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002847 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002848
Evan Cheng0ff39b32008-06-30 07:31:25 +00002849 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002850 // It's unlikely a store of a vector immediate can be done in a single
2851 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002852 // We also handle store a vector with all zero's.
2853 // FIXME: Handle other cases where store of vector immediate is done in
2854 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002855 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002856 Store = DAG.getStore(Chain, Value,
2857 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002858 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002859 } else {
2860 Value = DAG.getLoad(VT, Chain,
2861 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002862 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002863 Store = DAG.getStore(Chain, Value,
2864 getMemBasePlusOffset(Dst, DstOff, DAG),
2865 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002866 }
2867 OutChains.push_back(Store);
2868 SrcOff += VTSize;
2869 DstOff += VTSize;
2870 }
2871
2872 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2873 &OutChains[0], OutChains.size());
2874}
2875
Dan Gohman475871a2008-07-27 21:46:04 +00002876static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2877 SDValue Chain, SDValue Dst,
2878 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002879 unsigned Align, bool AlwaysInline,
2880 const Value *DstSV, uint64_t DstSVOff,
2881 const Value *SrcSV, uint64_t SrcSVOff){
2882 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2883
2884 // Expand memmove to a series of load and store ops if the size operand falls
2885 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002886 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002887 uint64_t Limit = -1;
2888 if (!AlwaysInline)
2889 Limit = TLI.getMaxStoresPerMemmove();
2890 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002891 std::string Str;
2892 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002893 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002894 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002895 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002896
Dan Gohman21323f32008-05-29 19:42:22 +00002897 uint64_t SrcOff = 0, DstOff = 0;
2898
Dan Gohman475871a2008-07-27 21:46:04 +00002899 SmallVector<SDValue, 8> LoadValues;
2900 SmallVector<SDValue, 8> LoadChains;
2901 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002902 unsigned NumMemOps = MemOps.size();
2903 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002904 MVT VT = MemOps[i];
2905 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002907
2908 Value = DAG.getLoad(VT, Chain,
2909 getMemBasePlusOffset(Src, SrcOff, DAG),
2910 SrcSV, SrcSVOff + SrcOff, false, Align);
2911 LoadValues.push_back(Value);
2912 LoadChains.push_back(Value.getValue(1));
2913 SrcOff += VTSize;
2914 }
2915 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2916 &LoadChains[0], LoadChains.size());
2917 OutChains.clear();
2918 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002919 MVT VT = MemOps[i];
2920 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002921 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002922
2923 Store = DAG.getStore(Chain, LoadValues[i],
2924 getMemBasePlusOffset(Dst, DstOff, DAG),
2925 DstSV, DstSVOff + DstOff, false, DstAlign);
2926 OutChains.push_back(Store);
2927 DstOff += VTSize;
2928 }
2929
2930 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2931 &OutChains[0], OutChains.size());
2932}
2933
Dan Gohman475871a2008-07-27 21:46:04 +00002934static SDValue getMemsetStores(SelectionDAG &DAG,
2935 SDValue Chain, SDValue Dst,
2936 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002937 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002938 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002939 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2940
2941 // Expand memset to a series of load/store ops if the size operand
2942 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002943 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002944 std::string Str;
2945 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002946 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002947 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002948 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002949
Dan Gohman475871a2008-07-27 21:46:04 +00002950 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002951 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002952
2953 unsigned NumMemOps = MemOps.size();
2954 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002955 MVT VT = MemOps[i];
2956 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002957 SDValue Value = getMemsetValue(Src, VT, DAG);
2958 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00002959 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002960 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002961 OutChains.push_back(Store);
2962 DstOff += VTSize;
2963 }
2964
2965 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2966 &OutChains[0], OutChains.size());
2967}
2968
Dan Gohman475871a2008-07-27 21:46:04 +00002969SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
2970 SDValue Src, SDValue Size,
2971 unsigned Align, bool AlwaysInline,
2972 const Value *DstSV, uint64_t DstSVOff,
2973 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002974
2975 // Check to see if we should lower the memcpy to loads and stores first.
2976 // For cases within the target-specified limits, this is the best choice.
2977 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2978 if (ConstantSize) {
2979 // Memcpy with size zero? Just return the original chain.
2980 if (ConstantSize->isNullValue())
2981 return Chain;
2982
Dan Gohman475871a2008-07-27 21:46:04 +00002983 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00002984 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002985 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002986 if (Result.Val)
2987 return Result;
2988 }
2989
2990 // Then check to see if we should lower the memcpy with target-specific
2991 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00002993 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2994 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002995 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002996 if (Result.Val)
2997 return Result;
2998
2999 // If we really need inline code and the target declined to provide it,
3000 // use a (potentially long) sequence of loads and stores.
3001 if (AlwaysInline) {
3002 assert(ConstantSize && "AlwaysInline requires a constant size!");
3003 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3004 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003005 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003006 }
3007
3008 // Emit a library call.
3009 TargetLowering::ArgListTy Args;
3010 TargetLowering::ArgListEntry Entry;
3011 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3012 Entry.Node = Dst; Args.push_back(Entry);
3013 Entry.Node = Src; Args.push_back(Entry);
3014 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003015 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003016 TLI.LowerCallTo(Chain, Type::VoidTy,
3017 false, false, false, CallingConv::C, false,
3018 getExternalSymbol("memcpy", TLI.getPointerTy()),
3019 Args, *this);
3020 return CallResult.second;
3021}
3022
Dan Gohman475871a2008-07-27 21:46:04 +00003023SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3024 SDValue Src, SDValue Size,
3025 unsigned Align,
3026 const Value *DstSV, uint64_t DstSVOff,
3027 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003028
Dan Gohman21323f32008-05-29 19:42:22 +00003029 // Check to see if we should lower the memmove to loads and stores first.
3030 // For cases within the target-specified limits, this is the best choice.
3031 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3032 if (ConstantSize) {
3033 // Memmove with size zero? Just return the original chain.
3034 if (ConstantSize->isNullValue())
3035 return Chain;
3036
Dan Gohman475871a2008-07-27 21:46:04 +00003037 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003038 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3039 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3040 if (Result.Val)
3041 return Result;
3042 }
Dan Gohman707e0182008-04-12 04:36:06 +00003043
3044 // Then check to see if we should lower the memmove with target-specific
3045 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003046 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003047 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003048 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003049 if (Result.Val)
3050 return Result;
3051
3052 // Emit a library call.
3053 TargetLowering::ArgListTy Args;
3054 TargetLowering::ArgListEntry Entry;
3055 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3056 Entry.Node = Dst; Args.push_back(Entry);
3057 Entry.Node = Src; Args.push_back(Entry);
3058 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003059 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003060 TLI.LowerCallTo(Chain, Type::VoidTy,
3061 false, false, false, CallingConv::C, false,
3062 getExternalSymbol("memmove", TLI.getPointerTy()),
3063 Args, *this);
3064 return CallResult.second;
3065}
3066
Dan Gohman475871a2008-07-27 21:46:04 +00003067SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3068 SDValue Src, SDValue Size,
3069 unsigned Align,
3070 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003071
3072 // Check to see if we should lower the memset to stores first.
3073 // For cases within the target-specified limits, this is the best choice.
3074 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3075 if (ConstantSize) {
3076 // Memset with size zero? Just return the original chain.
3077 if (ConstantSize->isNullValue())
3078 return Chain;
3079
Dan Gohman475871a2008-07-27 21:46:04 +00003080 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003081 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003082 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003083 if (Result.Val)
3084 return Result;
3085 }
3086
3087 // Then check to see if we should lower the memset with target-specific
3088 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003089 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003090 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003091 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003092 if (Result.Val)
3093 return Result;
3094
3095 // Emit a library call.
3096 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3097 TargetLowering::ArgListTy Args;
3098 TargetLowering::ArgListEntry Entry;
3099 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3100 Args.push_back(Entry);
3101 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003102 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003103 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3104 else
3105 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3106 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3107 Args.push_back(Entry);
3108 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3109 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003110 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003111 TLI.LowerCallTo(Chain, Type::VoidTy,
3112 false, false, false, CallingConv::C, false,
3113 getExternalSymbol("memset", TLI.getPointerTy()),
3114 Args, *this);
3115 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003116}
3117
Dan Gohman475871a2008-07-27 21:46:04 +00003118SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3119 SDValue Ptr, SDValue Cmp,
3120 SDValue Swp, const Value* PtrVal,
3121 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003122 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003123 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003124
3125 MVT VT = Cmp.getValueType();
3126
3127 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3128 Alignment = getMVTAlignment(VT);
3129
3130 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003131 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003132 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003133 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003134 void* IP = 0;
3135 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003136 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003137 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003138 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003139 CSEMap.InsertNode(N, IP);
3140 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003141 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003142}
3143
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3145 SDValue Ptr, SDValue Val,
3146 const Value* PtrVal,
3147 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003148 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003149 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3150 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003151 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003152 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3153 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003154 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003155
3156 MVT VT = Val.getValueType();
3157
3158 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3159 Alignment = getMVTAlignment(VT);
3160
3161 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003162 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003164 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003165 void* IP = 0;
3166 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003167 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003168 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003169 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003170 CSEMap.InsertNode(N, IP);
3171 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003172 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003173}
3174
Duncan Sands4bdcb612008-07-02 17:40:58 +00003175/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3176/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003177SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3178 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003179 if (Simplify && NumOps == 1)
3180 return Ops[0];
3181
3182 SmallVector<MVT, 4> VTs;
3183 VTs.reserve(NumOps);
3184 for (unsigned i = 0; i < NumOps; ++i)
3185 VTs.push_back(Ops[i].getValueType());
3186 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3187}
3188
Dan Gohman475871a2008-07-27 21:46:04 +00003189SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003190SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003191 MVT VT, SDValue Chain,
3192 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003193 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003194 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003195 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3196 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003197
Duncan Sands14ea39c2008-03-27 20:23:40 +00003198 if (VT == EVT) {
3199 ExtType = ISD::NON_EXTLOAD;
3200 } else if (ExtType == ISD::NON_EXTLOAD) {
3201 assert(VT == EVT && "Non-extending load from different memory type!");
3202 } else {
3203 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003204 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003205 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3206 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003207 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003208 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003209 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003211 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003212 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003213 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003214 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003215
3216 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003217 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003218 "Unindexed load with an offset!");
3219
3220 SDVTList VTs = Indexed ?
3221 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003222 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003223 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003224 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003225 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003226 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003227 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003228 ID.AddInteger(Alignment);
3229 ID.AddInteger(isVolatile);
3230 void *IP = 0;
3231 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003232 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003233 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003234 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3235 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003236 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003237 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003238 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003239}
3240
Dan Gohman475871a2008-07-27 21:46:04 +00003241SDValue SelectionDAG::getLoad(MVT VT,
3242 SDValue Chain, SDValue Ptr,
3243 const Value *SV, int SVOffset,
3244 bool isVolatile, unsigned Alignment) {
3245 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003246 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3247 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003248}
3249
Dan Gohman475871a2008-07-27 21:46:04 +00003250SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3251 SDValue Chain, SDValue Ptr,
3252 const Value *SV,
3253 int SVOffset, MVT EVT,
3254 bool isVolatile, unsigned Alignment) {
3255 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003256 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3257 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003258}
3259
Dan Gohman475871a2008-07-27 21:46:04 +00003260SDValue
3261SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3262 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003263 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003264 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3265 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003266 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3267 LD->getChain(), Base, Offset, LD->getSrcValue(),
3268 LD->getSrcValueOffset(), LD->getMemoryVT(),
3269 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003270}
3271
Dan Gohman475871a2008-07-27 21:46:04 +00003272SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3273 SDValue Ptr, const Value *SV, int SVOffset,
3274 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003275 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003276
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003277 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3278 Alignment = getMVTAlignment(VT);
3279
Evan Chengad071e12006-10-05 22:57:11 +00003280 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003281 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3282 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003283 FoldingSetNodeID ID;
3284 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003285 ID.AddInteger(ISD::UNINDEXED);
3286 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003287 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003288 ID.AddInteger(Alignment);
3289 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003290 void *IP = 0;
3291 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003292 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003293 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003294 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3295 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003296 CSEMap.InsertNode(N, IP);
3297 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003298 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003299}
3300
Dan Gohman475871a2008-07-27 21:46:04 +00003301SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3302 SDValue Ptr, const Value *SV,
3303 int SVOffset, MVT SVT,
3304 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003305 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003306
3307 if (VT == SVT)
3308 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003309
Duncan Sands8e4eb092008-06-08 20:54:56 +00003310 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003311 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003312 "Can't do FP-INT conversion!");
3313
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003314 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3315 Alignment = getMVTAlignment(VT);
3316
Evan Cheng8b2794a2006-10-13 21:14:26 +00003317 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3319 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003320 FoldingSetNodeID ID;
3321 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003322 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003323 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003324 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003325 ID.AddInteger(Alignment);
3326 ID.AddInteger(isVolatile);
3327 void *IP = 0;
3328 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003329 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003330 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003331 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3332 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003333 CSEMap.InsertNode(N, IP);
3334 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003335 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003336}
3337
Dan Gohman475871a2008-07-27 21:46:04 +00003338SDValue
3339SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3340 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003341 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3342 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3343 "Store is already a indexed store!");
3344 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003345 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003346 FoldingSetNodeID ID;
3347 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3348 ID.AddInteger(AM);
3349 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003350 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003351 ID.AddInteger(ST->getAlignment());
3352 ID.AddInteger(ST->isVolatile());
3353 void *IP = 0;
3354 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003355 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003356 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003357 new (N) StoreSDNode(Ops, VTs, AM,
3358 ST->isTruncatingStore(), ST->getMemoryVT(),
3359 ST->getSrcValue(), ST->getSrcValueOffset(),
3360 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003361 CSEMap.InsertNode(N, IP);
3362 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003363 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003364}
3365
Dan Gohman475871a2008-07-27 21:46:04 +00003366SDValue SelectionDAG::getVAArg(MVT VT,
3367 SDValue Chain, SDValue Ptr,
3368 SDValue SV) {
3369 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003370 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003371}
3372
Dan Gohman475871a2008-07-27 21:46:04 +00003373SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3374 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003375 switch (NumOps) {
3376 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003377 case 1: return getNode(Opcode, VT, Ops[0]);
3378 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3379 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003380 default: break;
3381 }
3382
Dan Gohman475871a2008-07-27 21:46:04 +00003383 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003384 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003385 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3386 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003387}
3388
Dan Gohman475871a2008-07-27 21:46:04 +00003389SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3390 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003391 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003392 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003393 case 1: return getNode(Opcode, VT, Ops[0]);
3394 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3395 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003396 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003397 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003398
Chris Lattneref847df2005-04-09 03:27:28 +00003399 switch (Opcode) {
3400 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003401 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003402 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003403 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3404 "LHS and RHS of condition must have same type!");
3405 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3406 "True and False arms of SelectCC must have same type!");
3407 assert(Ops[2].getValueType() == VT &&
3408 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003409 break;
3410 }
3411 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003412 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003413 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3414 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003415 break;
3416 }
Chris Lattneref847df2005-04-09 03:27:28 +00003417 }
3418
Chris Lattner385328c2005-05-14 07:42:29 +00003419 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003420 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003421 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003422 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003423 FoldingSetNodeID ID;
3424 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003425 void *IP = 0;
3426 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003427 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003428 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003429 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003430 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003431 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003432 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003433 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003434 }
Chris Lattneref847df2005-04-09 03:27:28 +00003435 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003436#ifndef NDEBUG
3437 VerifyNode(N);
3438#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003439 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003440}
3441
Dan Gohman475871a2008-07-27 21:46:04 +00003442SDValue SelectionDAG::getNode(unsigned Opcode,
3443 const std::vector<MVT> &ResultTys,
3444 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003445 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3446 Ops, NumOps);
3447}
3448
Dan Gohman475871a2008-07-27 21:46:04 +00003449SDValue SelectionDAG::getNode(unsigned Opcode,
3450 const MVT *VTs, unsigned NumVTs,
3451 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003452 if (NumVTs == 1)
3453 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003454 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3455}
3456
Dan Gohman475871a2008-07-27 21:46:04 +00003457SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3458 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003459 if (VTList.NumVTs == 1)
3460 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003461
Chris Lattner5f056bf2005-07-10 01:55:33 +00003462 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003463 // FIXME: figure out how to safely handle things like
3464 // int foo(int x) { return 1 << (x & 255); }
3465 // int bar() { return foo(256); }
3466#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003467 case ISD::SRA_PARTS:
3468 case ISD::SRL_PARTS:
3469 case ISD::SHL_PARTS:
3470 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003471 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003472 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3473 else if (N3.getOpcode() == ISD::AND)
3474 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3475 // If the and is only masking out bits that cannot effect the shift,
3476 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003477 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003478 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3479 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3480 }
3481 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003482#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003483 }
Chris Lattner89c34632005-05-14 06:20:26 +00003484
Chris Lattner43247a12005-08-25 19:12:10 +00003485 // Memoize the node unless it returns a flag.
3486 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003487 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003488 FoldingSetNodeID ID;
3489 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003490 void *IP = 0;
3491 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003492 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003493 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003494 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003495 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3496 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003497 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003498 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3499 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003500 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003501 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3502 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003503 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003504 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3505 }
Chris Lattnera5682852006-08-07 23:03:03 +00003506 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003507 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003508 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003509 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003510 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3511 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003512 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003513 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3514 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003515 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003516 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3517 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003518 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003519 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3520 }
Chris Lattner43247a12005-08-25 19:12:10 +00003521 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003522 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003523#ifndef NDEBUG
3524 VerifyNode(N);
3525#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003526 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003527}
3528
Dan Gohman475871a2008-07-27 21:46:04 +00003529SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003530 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003531}
3532
Dan Gohman475871a2008-07-27 21:46:04 +00003533SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3534 SDValue N1) {
3535 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003536 return getNode(Opcode, VTList, Ops, 1);
3537}
3538
Dan Gohman475871a2008-07-27 21:46:04 +00003539SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3540 SDValue N1, SDValue N2) {
3541 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003542 return getNode(Opcode, VTList, Ops, 2);
3543}
3544
Dan Gohman475871a2008-07-27 21:46:04 +00003545SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3546 SDValue N1, SDValue N2, SDValue N3) {
3547 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003548 return getNode(Opcode, VTList, Ops, 3);
3549}
3550
Dan Gohman475871a2008-07-27 21:46:04 +00003551SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3552 SDValue N1, SDValue N2, SDValue N3,
3553 SDValue N4) {
3554 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003555 return getNode(Opcode, VTList, Ops, 4);
3556}
3557
Dan Gohman475871a2008-07-27 21:46:04 +00003558SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3559 SDValue N1, SDValue N2, SDValue N3,
3560 SDValue N4, SDValue N5) {
3561 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003562 return getNode(Opcode, VTList, Ops, 5);
3563}
3564
Duncan Sands83ec4b62008-06-06 12:08:01 +00003565SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003566 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003567}
3568
Duncan Sands83ec4b62008-06-06 12:08:01 +00003569SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003570 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3571 E = VTList.rend(); I != E; ++I)
3572 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3573 return *I;
3574
3575 MVT *Array = Allocator.Allocate<MVT>(2);
3576 Array[0] = VT1;
3577 Array[1] = VT2;
3578 SDVTList Result = makeVTList(Array, 2);
3579 VTList.push_back(Result);
3580 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003581}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003582
3583SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3584 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3585 E = VTList.rend(); I != E; ++I)
3586 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3587 I->VTs[2] == VT3)
3588 return *I;
3589
3590 MVT *Array = Allocator.Allocate<MVT>(3);
3591 Array[0] = VT1;
3592 Array[1] = VT2;
3593 Array[2] = VT3;
3594 SDVTList Result = makeVTList(Array, 3);
3595 VTList.push_back(Result);
3596 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003597}
3598
Duncan Sands83ec4b62008-06-06 12:08:01 +00003599SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003600 switch (NumVTs) {
3601 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003602 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003603 case 2: return getVTList(VTs[0], VTs[1]);
3604 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3605 default: break;
3606 }
3607
Dan Gohmane8be6c62008-07-17 19:10:17 +00003608 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3609 E = VTList.rend(); I != E; ++I) {
3610 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3611 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003612
3613 bool NoMatch = false;
3614 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003615 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003616 NoMatch = true;
3617 break;
3618 }
3619 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003620 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003621 }
3622
Dan Gohmane8be6c62008-07-17 19:10:17 +00003623 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3624 std::copy(VTs, VTs+NumVTs, Array);
3625 SDVTList Result = makeVTList(Array, NumVTs);
3626 VTList.push_back(Result);
3627 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003628}
3629
3630
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003631/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3632/// specified operands. If the resultant node already exists in the DAG,
3633/// this does not modify the specified node, instead it returns the node that
3634/// already exists. If the resultant node does not exist in the DAG, the
3635/// input node is returned. As a degenerate case, if you specify the same
3636/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003637SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003638 SDNode *N = InN.Val;
3639 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3640
3641 // Check to see if there is no change.
3642 if (Op == N->getOperand(0)) return InN;
3643
3644 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003645 void *InsertPos = 0;
3646 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003647 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003648
Dan Gohman79acd2b2008-07-21 22:38:59 +00003649 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003650 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003651 RemoveNodeFromCSEMaps(N);
3652
3653 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003654 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003655 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003656 N->OperandList[0].setUser(N);
3657 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003658
3659 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003660 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003661 return InN;
3662}
3663
Dan Gohman475871a2008-07-27 21:46:04 +00003664SDValue SelectionDAG::
3665UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003666 SDNode *N = InN.Val;
3667 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3668
3669 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003670 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3671 return InN; // No operands changed, just return the input node.
3672
3673 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003674 void *InsertPos = 0;
3675 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003676 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677
Dan Gohman79acd2b2008-07-21 22:38:59 +00003678 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003679 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003680 RemoveNodeFromCSEMaps(N);
3681
3682 // Now we update the operands.
3683 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003684 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003685 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003686 N->OperandList[0].setUser(N);
3687 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003688 }
3689 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003690 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003691 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003692 N->OperandList[1].setUser(N);
3693 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003694 }
3695
3696 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003697 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003698 return InN;
3699}
3700
Dan Gohman475871a2008-07-27 21:46:04 +00003701SDValue SelectionDAG::
3702UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3703 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003704 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003705}
3706
Dan Gohman475871a2008-07-27 21:46:04 +00003707SDValue SelectionDAG::
3708UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3709 SDValue Op3, SDValue Op4) {
3710 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003711 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003712}
3713
Dan Gohman475871a2008-07-27 21:46:04 +00003714SDValue SelectionDAG::
3715UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3716 SDValue Op3, SDValue Op4, SDValue Op5) {
3717 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003718 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003719}
3720
Dan Gohman475871a2008-07-27 21:46:04 +00003721SDValue SelectionDAG::
3722UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003723 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003724 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003725 "Update with wrong number of operands");
3726
3727 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003728 bool AnyChange = false;
3729 for (unsigned i = 0; i != NumOps; ++i) {
3730 if (Ops[i] != N->getOperand(i)) {
3731 AnyChange = true;
3732 break;
3733 }
3734 }
3735
3736 // No operands changed, just return the input node.
3737 if (!AnyChange) return InN;
3738
3739 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003740 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003741 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003742 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003743
Dan Gohman7ceda162008-05-02 00:05:03 +00003744 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003745 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003746 RemoveNodeFromCSEMaps(N);
3747
3748 // Now we update the operands.
3749 for (unsigned i = 0; i != NumOps; ++i) {
3750 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003751 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003752 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003753 N->OperandList[i].setUser(N);
3754 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003755 }
3756 }
3757
3758 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003759 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003760 return InN;
3761}
3762
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003763/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003764/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003765void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003766 // Unlike the code in MorphNodeTo that does this, we don't need to
3767 // watch for dead nodes here.
3768 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3769 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3770
3771 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003772}
Chris Lattner149c58c2005-08-16 18:17:10 +00003773
Dan Gohmane8be6c62008-07-17 19:10:17 +00003774/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3775/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003776///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003777SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003778 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003779 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003780 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003781}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003782
Dan Gohmane8be6c62008-07-17 19:10:17 +00003783SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003784 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003785 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003786 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003787 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003788}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003789
Dan Gohmane8be6c62008-07-17 19:10:17 +00003790SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003791 MVT VT, SDValue Op1,
3792 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003793 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003794 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003795 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003796}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003797
Dan Gohmane8be6c62008-07-17 19:10:17 +00003798SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003799 MVT VT, SDValue Op1,
3800 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003801 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003802 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003803 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003804}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003805
Dan Gohmane8be6c62008-07-17 19:10:17 +00003806SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003807 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003808 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003809 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003810 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003811}
3812
Dan Gohmane8be6c62008-07-17 19:10:17 +00003813SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003814 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003815 unsigned NumOps) {
3816 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003817 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003818}
3819
Dan Gohmane8be6c62008-07-17 19:10:17 +00003820SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003821 MVT VT1, MVT VT2) {
3822 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003823 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003824}
3825
Dan Gohmane8be6c62008-07-17 19:10:17 +00003826SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003827 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003828 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003829 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003830 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003831}
3832
Dan Gohmane8be6c62008-07-17 19:10:17 +00003833SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003834 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003835 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003836 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003837 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003838 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003839}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003840
Dan Gohmane8be6c62008-07-17 19:10:17 +00003841SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003842 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003843 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003844 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003845 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003846 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003847}
3848
Dan Gohmane8be6c62008-07-17 19:10:17 +00003849SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003850 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003851 SDValue Op1, SDValue Op2,
3852 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003853 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003854 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003855 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003856}
3857
Dan Gohmane8be6c62008-07-17 19:10:17 +00003858SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003859 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003860 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003861 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3862}
3863
3864SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3865 MVT VT) {
3866 SDVTList VTs = getVTList(VT);
3867 return MorphNodeTo(N, Opc, VTs, 0, 0);
3868}
3869
3870SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003871 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003872 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003873 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003874 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3875}
3876
3877SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003878 MVT VT, SDValue Op1,
3879 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003880 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003881 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003882 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3883}
3884
3885SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003886 MVT VT, SDValue Op1,
3887 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003888 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003889 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003890 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3891}
3892
3893SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003894 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003895 unsigned NumOps) {
3896 SDVTList VTs = getVTList(VT);
3897 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3898}
3899
3900SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003901 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003902 unsigned NumOps) {
3903 SDVTList VTs = getVTList(VT1, VT2);
3904 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3905}
3906
3907SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3908 MVT VT1, MVT VT2) {
3909 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003911}
3912
3913SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3914 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003915 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003916 SDVTList VTs = getVTList(VT1, VT2, VT3);
3917 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3918}
3919
3920SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3921 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003922 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003923 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003924 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003925 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3926}
3927
3928SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3929 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003930 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003931 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003932 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003933 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3934}
3935
3936SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3937 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue Op1, SDValue Op2,
3939 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003940 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003941 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003942 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3943}
3944
3945/// MorphNodeTo - These *mutate* the specified node to have the specified
3946/// return type, opcode, and operands.
3947///
3948/// Note that MorphNodeTo returns the resultant node. If there is already a
3949/// node of the specified opcode and operands, it returns that node instead of
3950/// the current one.
3951///
3952/// Using MorphNodeTo is faster than creating a new node and swapping it in
3953/// with ReplaceAllUsesWith both because it often avoids allocating a new
3954/// node, and because it doesn't require CSE recalulation for any of
3955/// the node's users.
3956///
3957SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003958 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003959 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003960 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003961 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003962 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3963 FoldingSetNodeID ID;
3964 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3965 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3966 return ON;
3967 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003968
Chris Lattner0fb094f2005-11-19 01:44:53 +00003969 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003970
Dan Gohmane8be6c62008-07-17 19:10:17 +00003971 // Start the morphing.
3972 N->NodeType = Opc;
3973 N->ValueList = VTs.VTs;
3974 N->NumValues = VTs.NumVTs;
3975
3976 // Clear the operands list, updating used nodes to remove this from their
3977 // use list. Keep track of any operands that become dead as a result.
3978 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3979 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
3980 I != E; ++I) {
3981 SDNode *Used = I->getVal();
3982 Used->removeUser(std::distance(B, I), N);
3983 if (Used->use_empty())
3984 DeadNodeSet.insert(Used);
3985 }
3986
3987 // If NumOps is larger than the # of operands we currently have, reallocate
3988 // the operand list.
3989 if (NumOps > N->NumOperands) {
3990 if (N->OperandsNeedDelete)
3991 delete[] N->OperandList;
3992 if (N->isMachineOpcode()) {
3993 // We're creating a final node that will live unmorphed for the
3994 // remainder of this SelectionDAG's duration, so we can allocate the
3995 // operands directly out of the pool with no recycling metadata.
3996 N->OperandList = Allocator.Allocate<SDUse>(NumOps);
3997 N->OperandsNeedDelete = false;
3998 } else {
3999 N->OperandList = new SDUse[NumOps];
4000 N->OperandsNeedDelete = true;
4001 }
4002 }
4003
4004 // Assign the new operands.
4005 N->NumOperands = NumOps;
4006 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4007 N->OperandList[i] = Ops[i];
4008 N->OperandList[i].setUser(N);
4009 SDNode *ToUse = N->OperandList[i].getVal();
4010 ToUse->addUser(i, N);
4011 DeadNodeSet.erase(ToUse);
4012 }
4013
4014 // Delete any nodes that are still dead after adding the uses for the
4015 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004016 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004017 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4018 E = DeadNodeSet.end(); I != E; ++I)
4019 if ((*I)->use_empty())
4020 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004021 RemoveDeadNodes(DeadNodes);
4022
Dan Gohmane8be6c62008-07-17 19:10:17 +00004023 if (IP)
4024 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004025 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004026}
4027
Chris Lattner0fb094f2005-11-19 01:44:53 +00004028
Evan Cheng6ae46c42006-02-09 07:15:23 +00004029/// getTargetNode - These are used for target selectors to create a new node
4030/// with specified return type(s), target opcode, and operands.
4031///
4032/// Note that getTargetNode returns the resultant node. If there is already a
4033/// node of the specified opcode and operands, it returns that node instead of
4034/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004035SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004036 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004037}
Dan Gohman475871a2008-07-27 21:46:04 +00004038SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004039 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004040}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004042 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004043 return getNode(~Opcode, VT, Op1, Op2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004044}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004045SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004046 SDValue Op1, SDValue Op2,
4047 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004048 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004049}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004051 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004052 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004053}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004054SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4055 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004056 SDValue Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004057 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004058}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004060 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004061 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004062 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004063}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004064SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004065 MVT VT2, SDValue Op1,
4066 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004067 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004068 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004069 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004070}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004072 MVT VT2, SDValue Op1,
4073 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004074 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004075 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004076 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004077}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004078SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004079 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004080 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004081 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004082}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004084 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004086 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004087 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004088}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004090 SDValue Op1, SDValue Op2,
4091 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004092 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004094 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004095}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004096SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004097 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004098 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004099 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004100}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4102 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004103 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004104 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004105 VTList.push_back(VT1);
4106 VTList.push_back(VT2);
4107 VTList.push_back(VT3);
4108 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004109 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004110 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004111}
Evan Cheng39305cf2007-10-05 01:10:49 +00004112SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004113 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004114 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004116 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004117 Ops, NumOps).Val;
4118}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004119
Evan Cheng08b11732008-03-22 01:55:50 +00004120/// getNodeIfExists - Get the specified node if it's already available, or
4121/// else return NULL.
4122SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004123 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004124 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4125 FoldingSetNodeID ID;
4126 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4127 void *IP = 0;
4128 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4129 return E;
4130 }
4131 return NULL;
4132}
4133
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004134
Evan Cheng99157a02006-08-07 22:13:29 +00004135/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004136/// This can cause recursive merging of nodes in the DAG.
4137///
Chris Lattner11d049c2008-02-03 03:35:22 +00004138/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004139///
Dan Gohman475871a2008-07-27 21:46:04 +00004140void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004141 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004142 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004143 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004144 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004145 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004146
Chris Lattner8b8749f2005-08-17 19:00:20 +00004147 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004148 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004149 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004150
Chris Lattner8b8749f2005-08-17 19:00:20 +00004151 // This node is about to morph, remove its old self from the CSE maps.
4152 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004153 int operandNum = 0;
4154 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4155 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004156 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004157 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004158 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004159 I->setUser(U);
4160 To.Val->addUser(operandNum, U);
4161 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004162
4163 // Now that we have modified U, add it back to the CSE maps. If it already
4164 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004165 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004166 ReplaceAllUsesWith(U, Existing, UpdateListener);
4167 // U is now dead. Inform the listener if it exists and delete it.
4168 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004169 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004170 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004171 } else {
4172 // If the node doesn't already exist, we updated it. Inform a listener if
4173 // it exists.
4174 if (UpdateListener)
4175 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004176 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004177 }
4178}
4179
4180/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4181/// This can cause recursive merging of nodes in the DAG.
4182///
4183/// This version assumes From/To have matching types and numbers of result
4184/// values.
4185///
Chris Lattner1e111c72005-09-07 05:37:01 +00004186void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004187 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004188 assert(From->getVTList().VTs == To->getVTList().VTs &&
4189 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004190 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004191
4192 // Handle the trivial case.
4193 if (From == To)
4194 return;
4195
Chris Lattner8b52f212005-08-26 18:36:28 +00004196 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004197 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004198 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004199
Chris Lattner8b52f212005-08-26 18:36:28 +00004200 // This node is about to morph, remove its old self from the CSE maps.
4201 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004202 int operandNum = 0;
4203 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4204 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004205 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004206 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004207 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004208 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004209 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004210
Chris Lattner8b8749f2005-08-17 19:00:20 +00004211 // Now that we have modified U, add it back to the CSE maps. If it already
4212 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004213 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004214 ReplaceAllUsesWith(U, Existing, UpdateListener);
4215 // U is now dead. Inform the listener if it exists and delete it.
4216 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004217 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004218 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004219 } else {
4220 // If the node doesn't already exist, we updated it. Inform a listener if
4221 // it exists.
4222 if (UpdateListener)
4223 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004224 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004225 }
4226}
4227
Chris Lattner8b52f212005-08-26 18:36:28 +00004228/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4229/// This can cause recursive merging of nodes in the DAG.
4230///
4231/// This version can replace From with any result values. To must match the
4232/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004233void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004234 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004235 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004236 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004237 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004238
4239 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004240 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004241 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004242
Chris Lattner7b2880c2005-08-24 22:44:39 +00004243 // This node is about to morph, remove its old self from the CSE maps.
4244 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004245 int operandNum = 0;
4246 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4247 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004248 if (I->getVal() == From) {
Dan Gohman475871a2008-07-27 21:46:04 +00004249 const SDValue &ToOp = To[I->getSDValue().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004250 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004251 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004252 I->setUser(U);
4253 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004254 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004255
Chris Lattner7b2880c2005-08-24 22:44:39 +00004256 // Now that we have modified U, add it back to the CSE maps. If it already
4257 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004258 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004259 ReplaceAllUsesWith(U, Existing, UpdateListener);
4260 // U is now dead. Inform the listener if it exists and delete it.
4261 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004262 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004263 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004264 } else {
4265 // If the node doesn't already exist, we updated it. Inform a listener if
4266 // it exists.
4267 if (UpdateListener)
4268 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004269 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004270 }
4271}
4272
Chris Lattner012f2412006-02-17 21:58:01 +00004273/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4274/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004275/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004276void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004277 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004278 // Handle the really simple, really trivial case efficiently.
4279 if (From == To) return;
4280
Chris Lattner012f2412006-02-17 21:58:01 +00004281 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004282 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004283 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004284 return;
4285 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004286
Chris Lattnercf5640b2007-02-04 00:14:31 +00004287 // Get all of the users of From.Val. We want these in a nice,
4288 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004289 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004290
4291 while (!Users.empty()) {
4292 // We know that this user uses some value of From. If it is the right
4293 // value, update it.
4294 SDNode *User = Users.back();
4295 Users.pop_back();
4296
Chris Lattner01d029b2007-10-15 06:10:22 +00004297 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004298 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004299 for (; Op != E; ++Op)
4300 if (*Op == From) break;
4301
4302 // If there are no matches, the user must use some other result of From.
4303 if (Op == E) continue;
4304
4305 // Okay, we know this user needs to be updated. Remove its old self
4306 // from the CSE maps.
4307 RemoveNodeFromCSEMaps(User);
4308
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004309 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004310 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004311 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004312 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004313 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004314 Op->setUser(User);
4315 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004316 }
4317 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004318
4319 // Now that we have modified User, add it back to the CSE maps. If it
4320 // already exists there, recursively merge the results together.
4321 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004322 if (!Existing) {
4323 if (UpdateListener) UpdateListener->NodeUpdated(User);
4324 continue; // Continue on to next user.
4325 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004326
4327 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004328 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004329 // recursive merging of other unrelated nodes down the line.
4330 ReplaceAllUsesWith(User, Existing, UpdateListener);
4331
4332 // User is now dead. Notify a listener if present.
4333 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4334 DeleteNodeNotInCSEMaps(User);
4335 }
4336}
4337
4338/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4339/// uses of other values produced by From.Val alone. The same value may
4340/// appear in both the From and To list. The Deleted vector is
4341/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004342void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4343 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004344 unsigned Num,
4345 DAGUpdateListener *UpdateListener){
4346 // Handle the simple, trivial case efficiently.
4347 if (Num == 1)
4348 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4349
4350 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4351 for (unsigned i = 0; i != Num; ++i)
4352 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4353 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004354 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004355
4356 while (!Users.empty()) {
4357 // We know that this user uses some value of From. If it is the right
4358 // value, update it.
4359 SDNode *User = Users.back().first;
4360 unsigned i = Users.back().second;
4361 Users.pop_back();
4362
4363 // Scan for an operand that matches From.
4364 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4365 for (; Op != E; ++Op)
4366 if (*Op == From[i]) break;
4367
4368 // If there are no matches, the user must use some other result of From.
4369 if (Op == E) continue;
4370
4371 // Okay, we know this user needs to be updated. Remove its old self
4372 // from the CSE maps.
4373 RemoveNodeFromCSEMaps(User);
4374
4375 // Update all operands that match "From" in case there are multiple uses.
4376 for (; Op != E; ++Op) {
4377 if (*Op == From[i]) {
4378 From[i].Val->removeUser(Op-User->op_begin(), User);
4379 *Op = To[i];
4380 Op->setUser(User);
4381 To[i].Val->addUser(Op-User->op_begin(), User);
4382 }
4383 }
4384
4385 // Now that we have modified User, add it back to the CSE maps. If it
4386 // already exists there, recursively merge the results together.
4387 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4388 if (!Existing) {
4389 if (UpdateListener) UpdateListener->NodeUpdated(User);
4390 continue; // Continue on to next user.
4391 }
4392
4393 // If there was already an existing matching node, use ReplaceAllUsesWith
4394 // to replace the dead one with the existing one. This can cause
4395 // recursive merging of other unrelated nodes down the line.
4396 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004397
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004398 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004399 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004400 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004401 }
4402}
4403
Evan Chenge6f35d82006-08-01 08:20:41 +00004404/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004405/// based on their topological order. It returns the maximum id and a vector
4406/// of the SDNodes* in assigned order by reference.
4407unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004408 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004409 std::vector<unsigned> InDegree(DAGSize);
4410 std::vector<SDNode*> Sources;
4411
4412 // Use a two pass approach to avoid using a std::map which is slow.
4413 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004414 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4415 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004416 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004417 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004418 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004419 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004420 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004421 }
4422
Evan Cheng99157a02006-08-07 22:13:29 +00004423 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004424 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004425 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004426 SDNode *N = Sources.back();
4427 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004428 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004429 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004430 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004431 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004432 if (Degree == 0)
4433 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004434 }
4435 }
4436
Evan Chengc384d6c2006-08-02 22:00:34 +00004437 // Second pass, assign the actual topological order as node ids.
4438 Id = 0;
4439 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4440 TI != TE; ++TI)
4441 (*TI)->setNodeId(Id++);
4442
4443 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004444}
4445
4446
Evan Cheng091cba12006-07-27 06:39:06 +00004447
Jim Laskey58b968b2005-08-17 20:08:02 +00004448//===----------------------------------------------------------------------===//
4449// SDNode Class
4450//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004451
Chris Lattner917d2c92006-07-19 00:00:37 +00004452// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004453void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004454void UnarySDNode::ANCHOR() {}
4455void BinarySDNode::ANCHOR() {}
4456void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004457void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004458void ConstantSDNode::ANCHOR() {}
4459void ConstantFPSDNode::ANCHOR() {}
4460void GlobalAddressSDNode::ANCHOR() {}
4461void FrameIndexSDNode::ANCHOR() {}
4462void JumpTableSDNode::ANCHOR() {}
4463void ConstantPoolSDNode::ANCHOR() {}
4464void BasicBlockSDNode::ANCHOR() {}
4465void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004466void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004467void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004468void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004469void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004470void ExternalSymbolSDNode::ANCHOR() {}
4471void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004472void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004473void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004474void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004475void LoadSDNode::ANCHOR() {}
4476void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004477void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004478
Chris Lattner48b85922007-02-04 02:41:42 +00004479HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004480 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004481}
4482
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004483GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004484 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004485 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004486 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004487 // Thread Local
4488 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4489 // Non Thread Local
4490 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4491 getSDVTList(VT)), Offset(o) {
4492 TheGlobal = const_cast<GlobalValue*>(GA);
4493}
Chris Lattner48b85922007-02-04 02:41:42 +00004494
Dan Gohman1ea58a52008-07-09 22:08:04 +00004495MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004496 const Value *srcValue, int SVO,
4497 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004498 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohman492f2762008-07-09 21:23:02 +00004499 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4500
4501 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4502 assert(getAlignment() == alignment && "Alignment representation error!");
4503 assert(isVolatile() == vol && "Volatile representation error!");
4504}
4505
Dan Gohman36b5c132008-04-07 19:35:22 +00004506/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004507/// reference performed by this memory reference.
4508MachineMemOperand MemSDNode::getMemOperand() const {
4509 int Flags;
4510 if (isa<LoadSDNode>(this))
4511 Flags = MachineMemOperand::MOLoad;
4512 else if (isa<StoreSDNode>(this))
4513 Flags = MachineMemOperand::MOStore;
4514 else {
4515 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4516 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4517 }
4518
4519 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004520 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4521
Dan Gohman1ea58a52008-07-09 22:08:04 +00004522 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004523 const FrameIndexSDNode *FI =
4524 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4525 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004526 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4527 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004528 else
4529 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4530 Size, getAlignment());
4531}
4532
Jim Laskey583bd472006-10-27 23:46:08 +00004533/// Profile - Gather unique data for the node.
4534///
4535void SDNode::Profile(FoldingSetNodeID &ID) {
4536 AddNodeIDNode(ID, this);
4537}
4538
Chris Lattnera3255112005-11-08 23:30:28 +00004539/// getValueTypeList - Return a pointer to the specified value type.
4540///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004541const MVT *SDNode::getValueTypeList(MVT VT) {
4542 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004543 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004544 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004545 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004546 static MVT VTs[MVT::LAST_VALUETYPE];
4547 VTs[VT.getSimpleVT()] = VT;
4548 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004549 }
Chris Lattnera3255112005-11-08 23:30:28 +00004550}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004551
Chris Lattner5c884562005-01-12 18:37:47 +00004552/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4553/// indicated value. This method ignores uses of other values defined by this
4554/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004555bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004556 assert(Value < getNumValues() && "Bad value!");
4557
Roman Levensteindc1adac2008-04-07 10:06:32 +00004558 // TODO: Only iterate over uses of a given value of the node
4559 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman475871a2008-07-27 21:46:04 +00004560 if (UI.getUse().getSDValue().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004561 if (NUses == 0)
4562 return false;
4563 --NUses;
4564 }
Chris Lattner5c884562005-01-12 18:37:47 +00004565 }
4566
4567 // Found exactly the right number of uses?
4568 return NUses == 0;
4569}
4570
4571
Evan Cheng33d55952007-08-02 05:29:38 +00004572/// hasAnyUseOfValue - Return true if there are any use of the indicated
4573/// value. This method ignores uses of other values defined by this operation.
4574bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4575 assert(Value < getNumValues() && "Bad value!");
4576
Dan Gohman1373c1c2008-07-09 22:39:01 +00004577 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman475871a2008-07-27 21:46:04 +00004578 if (UI.getUse().getSDValue().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004579 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004580
4581 return false;
4582}
4583
4584
Dan Gohman2a629952008-07-27 18:06:42 +00004585/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004586///
Dan Gohman2a629952008-07-27 18:06:42 +00004587bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004588 bool Seen = false;
4589 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004590 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004591 if (User == this)
4592 Seen = true;
4593 else
4594 return false;
4595 }
4596
4597 return Seen;
4598}
4599
Evan Chenge6e97e62006-11-03 07:31:32 +00004600/// isOperand - Return true if this node is an operand of N.
4601///
Dan Gohman475871a2008-07-27 21:46:04 +00004602bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004603 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4604 if (*this == N->getOperand(i))
4605 return true;
4606 return false;
4607}
4608
Evan Cheng917be682008-03-04 00:41:45 +00004609bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004610 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004611 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004612 return true;
4613 return false;
4614}
Evan Cheng4ee62112006-02-05 06:29:23 +00004615
Chris Lattner572dee72008-01-16 05:49:24 +00004616/// reachesChainWithoutSideEffects - Return true if this operand (which must
4617/// be a chain) reaches the specified operand without crossing any
4618/// side-effecting instructions. In practice, this looks through token
4619/// factors and non-volatile loads. In order to remain efficient, this only
4620/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004621bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004622 unsigned Depth) const {
4623 if (*this == Dest) return true;
4624
4625 // Don't search too deeply, we just want to be able to see through
4626 // TokenFactor's etc.
4627 if (Depth == 0) return false;
4628
4629 // If this is a token factor, all inputs to the TF happen in parallel. If any
4630 // of the operands of the TF reach dest, then we can do the xform.
4631 if (getOpcode() == ISD::TokenFactor) {
4632 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4633 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4634 return true;
4635 return false;
4636 }
4637
4638 // Loads don't have side effects, look through them.
4639 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4640 if (!Ld->isVolatile())
4641 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4642 }
4643 return false;
4644}
4645
4646
Evan Chengc5fc57d2006-11-03 03:05:24 +00004647static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004648 SmallPtrSet<SDNode *, 32> &Visited) {
4649 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004650 return;
4651
4652 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4653 SDNode *Op = N->getOperand(i).Val;
4654 if (Op == P) {
4655 found = true;
4656 return;
4657 }
4658 findPredecessor(Op, P, found, Visited);
4659 }
4660}
4661
Evan Cheng917be682008-03-04 00:41:45 +00004662/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004663/// is either an operand of N or it can be reached by recursively traversing
4664/// up the operands.
4665/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004666bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004667 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004668 bool found = false;
4669 findPredecessor(N, this, found, Visited);
4670 return found;
4671}
4672
Evan Chengc5484282006-10-04 00:56:09 +00004673uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4674 assert(Num < NumOperands && "Invalid child # of SDNode!");
4675 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4676}
4677
Reid Spencer577cc322007-04-01 07:32:19 +00004678std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004679 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004680 default:
4681 if (getOpcode() < ISD::BUILTIN_OP_END)
4682 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004683 if (isMachineOpcode()) {
4684 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004685 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004686 if (getMachineOpcode() < TII->getNumOpcodes())
4687 return TII->get(getMachineOpcode()).getName();
4688 return "<<Unknown Machine Node>>";
4689 }
4690 if (G) {
4691 TargetLowering &TLI = G->getTargetLoweringInfo();
4692 const char *Name = TLI.getTargetNodeName(getOpcode());
4693 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004694 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004695 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004696 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004697
Dan Gohmane8be6c62008-07-17 19:10:17 +00004698#ifndef NDEBUG
4699 case ISD::DELETED_NODE:
4700 return "<<Deleted Node!>>";
4701#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004702 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004703 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004704 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4705 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4706 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004707 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4708 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4709 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004710 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004711 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4712 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4713 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4714 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4715 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004716 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004717 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004718 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004719 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004720 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004721 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004722 case ISD::AssertSext: return "AssertSext";
4723 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004724
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004725 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004726 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004727 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004728 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004729
4730 case ISD::Constant: return "Constant";
4731 case ISD::ConstantFP: return "ConstantFP";
4732 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004733 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004734 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004735 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004736 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004737 case ISD::RETURNADDR: return "RETURNADDR";
4738 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004739 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004740 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4741 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004742 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004743 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004744 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004745 case ISD::INTRINSIC_WO_CHAIN: {
4746 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4747 return Intrinsic::getName((Intrinsic::ID)IID);
4748 }
4749 case ISD::INTRINSIC_VOID:
4750 case ISD::INTRINSIC_W_CHAIN: {
4751 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004752 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004753 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004754
Chris Lattnerb2827b02006-03-19 00:52:58 +00004755 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004756 case ISD::TargetConstant: return "TargetConstant";
4757 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004758 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004759 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004760 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004761 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004762 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004763 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004764
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004765 case ISD::CopyToReg: return "CopyToReg";
4766 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004767 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004768 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004769 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004770 case ISD::DBG_LABEL: return "dbg_label";
4771 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004772 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004773 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004774 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004775 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004776
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004777 // Unary operators
4778 case ISD::FABS: return "fabs";
4779 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004780 case ISD::FSQRT: return "fsqrt";
4781 case ISD::FSIN: return "fsin";
4782 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004783 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004784 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004785
4786 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004787 case ISD::ADD: return "add";
4788 case ISD::SUB: return "sub";
4789 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004790 case ISD::MULHU: return "mulhu";
4791 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004792 case ISD::SDIV: return "sdiv";
4793 case ISD::UDIV: return "udiv";
4794 case ISD::SREM: return "srem";
4795 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004796 case ISD::SMUL_LOHI: return "smul_lohi";
4797 case ISD::UMUL_LOHI: return "umul_lohi";
4798 case ISD::SDIVREM: return "sdivrem";
4799 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004800 case ISD::AND: return "and";
4801 case ISD::OR: return "or";
4802 case ISD::XOR: return "xor";
4803 case ISD::SHL: return "shl";
4804 case ISD::SRA: return "sra";
4805 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004806 case ISD::ROTL: return "rotl";
4807 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004808 case ISD::FADD: return "fadd";
4809 case ISD::FSUB: return "fsub";
4810 case ISD::FMUL: return "fmul";
4811 case ISD::FDIV: return "fdiv";
4812 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004813 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004814 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004816 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004817 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004818 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004819 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004820 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004821 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004822 case ISD::CONCAT_VECTORS: return "concat_vectors";
4823 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004824 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004825 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004826 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004827 case ISD::ADDC: return "addc";
4828 case ISD::ADDE: return "adde";
4829 case ISD::SUBC: return "subc";
4830 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004831 case ISD::SHL_PARTS: return "shl_parts";
4832 case ISD::SRA_PARTS: return "sra_parts";
4833 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004834
4835 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4836 case ISD::INSERT_SUBREG: return "insert_subreg";
4837
Chris Lattner7f644642005-04-28 21:44:03 +00004838 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004839 case ISD::SIGN_EXTEND: return "sign_extend";
4840 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004841 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004842 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004843 case ISD::TRUNCATE: return "truncate";
4844 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004845 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004846 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004847 case ISD::FP_EXTEND: return "fp_extend";
4848
4849 case ISD::SINT_TO_FP: return "sint_to_fp";
4850 case ISD::UINT_TO_FP: return "uint_to_fp";
4851 case ISD::FP_TO_SINT: return "fp_to_sint";
4852 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004853 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004854
4855 // Control flow instructions
4856 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004857 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004858 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004859 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004860 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004861 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004862 case ISD::CALLSEQ_START: return "callseq_start";
4863 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004864
4865 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004866 case ISD::LOAD: return "load";
4867 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004868 case ISD::VAARG: return "vaarg";
4869 case ISD::VACOPY: return "vacopy";
4870 case ISD::VAEND: return "vaend";
4871 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004872 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004873 case ISD::EXTRACT_ELEMENT: return "extract_element";
4874 case ISD::BUILD_PAIR: return "build_pair";
4875 case ISD::STACKSAVE: return "stacksave";
4876 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004877 case ISD::TRAP: return "trap";
4878
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004879 // Bit manipulation
4880 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004881 case ISD::CTPOP: return "ctpop";
4882 case ISD::CTTZ: return "cttz";
4883 case ISD::CTLZ: return "ctlz";
4884
Chris Lattner36ce6912005-11-29 06:21:05 +00004885 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004886 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004887 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004888
Duncan Sands36397f52007-07-27 12:58:54 +00004889 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004890 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004891
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004892 case ISD::CONDCODE:
4893 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004894 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004895 case ISD::SETOEQ: return "setoeq";
4896 case ISD::SETOGT: return "setogt";
4897 case ISD::SETOGE: return "setoge";
4898 case ISD::SETOLT: return "setolt";
4899 case ISD::SETOLE: return "setole";
4900 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004901
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004902 case ISD::SETO: return "seto";
4903 case ISD::SETUO: return "setuo";
4904 case ISD::SETUEQ: return "setue";
4905 case ISD::SETUGT: return "setugt";
4906 case ISD::SETUGE: return "setuge";
4907 case ISD::SETULT: return "setult";
4908 case ISD::SETULE: return "setule";
4909 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004910
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004911 case ISD::SETEQ: return "seteq";
4912 case ISD::SETGT: return "setgt";
4913 case ISD::SETGE: return "setge";
4914 case ISD::SETLT: return "setlt";
4915 case ISD::SETLE: return "setle";
4916 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004917 }
4918 }
4919}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004920
Evan Cheng144d8f02006-11-09 17:55:04 +00004921const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004922 switch (AM) {
4923 default:
4924 return "";
4925 case ISD::PRE_INC:
4926 return "<pre-inc>";
4927 case ISD::PRE_DEC:
4928 return "<pre-dec>";
4929 case ISD::POST_INC:
4930 return "<post-inc>";
4931 case ISD::POST_DEC:
4932 return "<post-dec>";
4933 }
4934}
4935
Duncan Sands276dcbd2008-03-21 09:14:45 +00004936std::string ISD::ArgFlagsTy::getArgFlagsString() {
4937 std::string S = "< ";
4938
4939 if (isZExt())
4940 S += "zext ";
4941 if (isSExt())
4942 S += "sext ";
4943 if (isInReg())
4944 S += "inreg ";
4945 if (isSRet())
4946 S += "sret ";
4947 if (isByVal())
4948 S += "byval ";
4949 if (isNest())
4950 S += "nest ";
4951 if (getByValAlign())
4952 S += "byval-align:" + utostr(getByValAlign()) + " ";
4953 if (getOrigAlign())
4954 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4955 if (getByValSize())
4956 S += "byval-size:" + utostr(getByValSize()) + " ";
4957 return S + ">";
4958}
4959
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004960void SDNode::dump() const { dump(0); }
4961void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004962 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004963
4964 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004965 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004966 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004967 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004968 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004969 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004970 }
Bill Wendling832171c2006-12-07 20:04:42 +00004971 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004972
Bill Wendling832171c2006-12-07 20:04:42 +00004973 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004974 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004975 if (i) cerr << ", ";
4976 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004977 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004978 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004979 }
4980
Evan Chengce254432007-12-11 02:08:35 +00004981 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4982 SDNode *Mask = getOperand(2).Val;
4983 cerr << "<";
4984 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4985 if (i) cerr << ",";
4986 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4987 cerr << "u";
4988 else
4989 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4990 }
4991 cerr << ">";
4992 }
4993
Chris Lattnerc3aae252005-01-07 07:46:32 +00004994 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Duncan Sandse1d97b12008-07-10 11:23:14 +00004995 cerr << "<" << CSDN->getAPIntValue().toStringUnsigned() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004996 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004997 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4998 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4999 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
5000 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
5001 else {
5002 cerr << "<APFloat(";
5003 CSDN->getValueAPF().convertToAPInt().dump();
5004 cerr << ")>";
5005 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005006 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005007 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005008 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00005009 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00005010 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00005011 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005012 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005013 else
Bill Wendling832171c2006-12-07 20:04:42 +00005014 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005015 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005016 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005017 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005018 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005019 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005020 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005021 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00005022 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005023 else
Bill Wendling832171c2006-12-07 20:04:42 +00005024 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005025 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005026 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005027 else
Bill Wendling832171c2006-12-07 20:04:42 +00005028 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005029 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005030 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005031 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5032 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00005033 cerr << LBB->getName() << " ";
5034 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005035 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005036 if (G && R->getReg() &&
5037 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00005038 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005039 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00005040 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005041 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005042 } else if (const ExternalSymbolSDNode *ES =
5043 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005044 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005045 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5046 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00005047 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005048 else
Dan Gohman69de1932008-02-06 22:27:42 +00005049 cerr << "<null>";
5050 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5051 if (M->MO.getValue())
5052 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
5053 else
5054 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005055 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
5056 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005057 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005058 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005059 }
5060 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005061 const Value *SrcValue = LD->getSrcValue();
5062 int SrcOffset = LD->getSrcValueOffset();
5063 cerr << " <";
5064 if (SrcValue)
5065 cerr << SrcValue;
5066 else
5067 cerr << "null";
5068 cerr << ":" << SrcOffset << ">";
5069
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005070 bool doExt = true;
5071 switch (LD->getExtensionType()) {
5072 default: doExt = false; break;
5073 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005074 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005075 break;
5076 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005077 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005078 break;
5079 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005080 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005081 break;
5082 }
5083 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00005084 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005085
Evan Cheng144d8f02006-11-09 17:55:04 +00005086 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005087 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00005088 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005089 if (LD->isVolatile())
5090 cerr << " <volatile>";
5091 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005092 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005093 const Value *SrcValue = ST->getSrcValue();
5094 int SrcOffset = ST->getSrcValueOffset();
5095 cerr << " <";
5096 if (SrcValue)
5097 cerr << SrcValue;
5098 else
5099 cerr << "null";
5100 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005101
5102 if (ST->isTruncatingStore())
5103 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00005104 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005105
5106 const char *AM = getIndexedModeName(ST->getAddressingMode());
5107 if (*AM)
5108 cerr << " " << AM;
5109 if (ST->isVolatile())
5110 cerr << " <volatile>";
5111 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005112 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5113 const Value *SrcValue = AT->getSrcValue();
5114 int SrcOffset = AT->getSrcValueOffset();
5115 cerr << " <";
5116 if (SrcValue)
5117 cerr << SrcValue;
5118 else
5119 cerr << "null";
5120 cerr << ":" << SrcOffset << ">";
5121 if (AT->isVolatile())
5122 cerr << " <volatile>";
5123 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005124 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005125}
5126
Chris Lattnerde202b32005-11-09 23:47:37 +00005127static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005128 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5129 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005130 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005131 else
Bill Wendling832171c2006-12-07 20:04:42 +00005132 cerr << "\n" << std::string(indent+2, ' ')
5133 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005134
Chris Lattnerea946cd2005-01-09 20:38:33 +00005135
Bill Wendling832171c2006-12-07 20:04:42 +00005136 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005137 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005138}
5139
Chris Lattnerc3aae252005-01-07 07:46:32 +00005140void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005141 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005142
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005143 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5144 I != E; ++I) {
5145 const SDNode *N = I;
5146 if (!N->hasOneUse() && N != getRoot().Val)
5147 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005148 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005149
Jim Laskey26f7fa72006-10-17 19:33:52 +00005150 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005151
Bill Wendling832171c2006-12-07 20:04:42 +00005152 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005153}
5154
Evan Chengd6594ae2006-09-12 21:00:35 +00005155const Type *ConstantPoolSDNode::getType() const {
5156 if (isMachineConstantPoolEntry())
5157 return Val.MachineCPVal->getType();
5158 return Val.ConstVal->getType();
5159}