blob: 3e3890142f566513b41730592958c3a535d383ff [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.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000355 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: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000380 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000381 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 Gohmanb8d2f552008-08-20 15:58:01 +0000403 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000404 break;
405 }
406 case ISD::FrameIndex:
407 case ISD::TargetFrameIndex:
408 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
409 break;
410 case ISD::JumpTable:
411 case ISD::TargetJumpTable:
412 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
413 break;
414 case ISD::ConstantPool:
415 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000416 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000417 ID.AddInteger(CP->getAlignment());
418 ID.AddInteger(CP->getOffset());
419 if (CP->isMachineConstantPoolEntry())
420 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
421 else
422 ID.AddPointer(CP->getConstVal());
423 break;
424 }
425 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000426 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000427 ID.AddInteger(LD->getAddressingMode());
428 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000429 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000430 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 break;
432 }
433 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000434 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000435 ID.AddInteger(ST->getAddressingMode());
436 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000437 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000438 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000439 break;
440 }
Mon P Wang28873102008-06-25 08:15:39 +0000441 case ISD::ATOMIC_CMP_SWAP:
442 case ISD::ATOMIC_LOAD_ADD:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_SUB:
445 case ISD::ATOMIC_LOAD_AND:
446 case ISD::ATOMIC_LOAD_OR:
447 case ISD::ATOMIC_LOAD_XOR:
448 case ISD::ATOMIC_LOAD_NAND:
449 case ISD::ATOMIC_LOAD_MIN:
450 case ISD::ATOMIC_LOAD_MAX:
451 case ISD::ATOMIC_LOAD_UMIN:
452 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000453 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
454 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000455 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000456 }
Mon P Wang28873102008-06-25 08:15:39 +0000457 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000458}
459
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000460/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
461/// the CSE map that carries both alignment and volatility information.
462///
463static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
464 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
465}
466
Jim Laskey583bd472006-10-27 23:46:08 +0000467//===----------------------------------------------------------------------===//
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());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000725 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000726 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
727 ID.AddInteger(ST->getAddressingMode());
728 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000729 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000730 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000731 }
Jim Laskey583bd472006-10-27 23:46:08 +0000732
Chris Lattnera5682852006-08-07 23:03:03 +0000733 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000734}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000735
Duncan Sandsd038e042008-07-21 10:20:31 +0000736/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
737void SelectionDAG::VerifyNode(SDNode *N) {
738 switch (N->getOpcode()) {
739 default:
740 break;
741 case ISD::BUILD_VECTOR: {
742 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
743 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
744 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
745 "Wrong number of BUILD_VECTOR operands!");
746 MVT EltVT = N->getValueType(0).getVectorElementType();
747 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000748 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000749 "Wrong BUILD_VECTOR operand type!");
750 break;
751 }
752 }
753}
754
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000755/// getMVTAlignment - Compute the default alignment value for the
756/// given type.
757///
758unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
759 const Type *Ty = VT == MVT::iPTR ?
760 PointerType::get(Type::Int8Ty, 0) :
761 VT.getTypeForMVT();
762
763 return TLI.getTargetData()->getABITypeAlignment(Ty);
764}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000765
Dan Gohman6f179662008-08-23 00:50:30 +0000766SelectionDAG::SelectionDAG(TargetLowering &tli, MachineFunction &mf,
767 FunctionLoweringInfo &fli, MachineModuleInfo *mmi,
768 NodeAllocatorType &nodeallocator)
769 : TLI(tli), MF(mf), FLI(fli), MMI(mmi), NodeAllocator(nodeallocator) {
770 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
771}
772
Chris Lattner78ec3112003-08-11 14:57:33 +0000773SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000774 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000775 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000776 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000777 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000778 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000779 }
Chris Lattner65113b22005-11-08 22:07:03 +0000780 N->OperandList = 0;
781 N->NumOperands = 0;
Chris Lattner65113b22005-11-08 22:07:03 +0000782 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000783}
784
Dan Gohman475871a2008-07-27 21:46:04 +0000785SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000786 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000787 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000788 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000789 return getNode(ISD::AND, Op.getValueType(), Op,
790 getConstant(Imm, Op.getValueType()));
791}
792
Dan Gohman475871a2008-07-27 21:46:04 +0000793SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000794 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000795 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000796}
797
Dan Gohman475871a2008-07-27 21:46:04 +0000798SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000800
Evan Cheng0ff39b32008-06-30 07:31:25 +0000801 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000802 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000803 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000804
805 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000806 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000807 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000808 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000809 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000810 SDNode *N = NULL;
811 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000812 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000813 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000814 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000815 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000816 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000817 CSEMap.InsertNode(N, IP);
818 AllNodes.push_back(N);
819 }
820
Dan Gohman475871a2008-07-27 21:46:04 +0000821 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000823 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000825 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
826 }
827 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000828}
829
Dan Gohman475871a2008-07-27 21:46:04 +0000830SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000831 return getConstant(Val, TLI.getPointerTy(), isTarget);
832}
833
834
Dan Gohman475871a2008-07-27 21:46:04 +0000835SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000836 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000837
Duncan Sands83ec4b62008-06-06 12:08:01 +0000838 MVT EltVT =
839 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000840
Chris Lattnerd8658612005-02-17 20:17:32 +0000841 // Do the map lookup using the actual bit pattern for the floating point
842 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
843 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000844 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000845 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000846 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000847 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000848 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000849 SDNode *N = NULL;
850 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000851 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000852 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000853 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000854 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000855 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000856 CSEMap.InsertNode(N, IP);
857 AllNodes.push_back(N);
858 }
859
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000864 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
865 }
866 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000867}
868
Dan Gohman475871a2008-07-27 21:46:04 +0000869SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870 MVT EltVT =
871 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000872 if (EltVT==MVT::f32)
873 return getConstantFP(APFloat((float)Val), VT, isTarget);
874 else
875 return getConstantFP(APFloat(Val), VT, isTarget);
876}
877
Dan Gohman475871a2008-07-27 21:46:04 +0000878SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
879 MVT VT, int Offset,
880 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000881 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000882
883 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
884 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000885 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000886 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
887 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
888 }
889
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000890 if (GVar && GVar->isThreadLocal())
891 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
892 else
893 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000894
Jim Laskey583bd472006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000896 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000897 ID.AddPointer(GV);
898 ID.AddInteger(Offset);
899 void *IP = 0;
900 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000901 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000902 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000903 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000904 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000906 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907}
908
Dan Gohman475871a2008-07-27 21:46:04 +0000909SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000910 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000911 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000912 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000913 ID.AddInteger(FI);
914 void *IP = 0;
915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000916 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000917 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000918 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000919 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000920 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000921 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000922}
923
Dan Gohman475871a2008-07-27 21:46:04 +0000924SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000925 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000926 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000927 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000928 ID.AddInteger(JTI);
929 void *IP = 0;
930 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000931 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000932 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000933 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000934 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000935 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000936 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000937}
938
Dan Gohman475871a2008-07-27 21:46:04 +0000939SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
940 unsigned Alignment, int Offset,
941 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000942 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000943 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000944 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000945 ID.AddInteger(Alignment);
946 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000947 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000948 void *IP = 0;
949 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000950 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000951 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000952 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000953 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000954 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000955 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000956}
957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958
Dan Gohman475871a2008-07-27 21:46:04 +0000959SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
960 unsigned Alignment, int Offset,
961 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000962 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000963 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000964 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000965 ID.AddInteger(Alignment);
966 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000967 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000968 void *IP = 0;
969 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000970 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000971 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000972 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000973 CSEMap.InsertNode(N, IP);
974 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000975 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000976}
977
978
Dan Gohman475871a2008-07-27 21:46:04 +0000979SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000980 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000981 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000982 ID.AddPointer(MBB);
983 void *IP = 0;
984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000985 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000986 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000987 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +0000988 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000990 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991}
992
Dan Gohman475871a2008-07-27 21:46:04 +0000993SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +0000994 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000995 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000996 ID.AddInteger(Flags.getRawBits());
997 void *IP = 0;
998 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000999 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001000 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001001 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001002 CSEMap.InsertNode(N, IP);
1003 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001004 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001005}
1006
Dan Gohman475871a2008-07-27 21:46:04 +00001007SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1009 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001010
Duncan Sands83ec4b62008-06-06 12:08:01 +00001011 SDNode *&N = VT.isExtended() ?
1012 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001013
Dan Gohman475871a2008-07-27 21:46:04 +00001014 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001015 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001016 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001017 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001018 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001019}
1020
Dan Gohman475871a2008-07-27 21:46:04 +00001021SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001023 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001024 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001025 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001026 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001027 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001028}
1029
Dan Gohman475871a2008-07-27 21:46:04 +00001030SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001031 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001032 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001033 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001034 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001036 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037}
1038
Dan Gohman475871a2008-07-27 21:46:04 +00001039SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001040 if ((unsigned)Cond >= CondCodeNodes.size())
1041 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001042
Chris Lattner079a27a2005-08-09 20:40:02 +00001043 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001044 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001045 new (N) CondCodeSDNode(Cond);
1046 CondCodeNodes[Cond] = N;
1047 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001048 }
Dan Gohman475871a2008-07-27 21:46:04 +00001049 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001050}
1051
Dan Gohman475871a2008-07-27 21:46:04 +00001052SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001053 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001054 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001055 ID.AddInteger(RegNo);
1056 void *IP = 0;
1057 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001059 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001060 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001061 CSEMap.InsertNode(N, IP);
1062 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001063 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001064}
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001067 unsigned Line, unsigned Col,
1068 const CompileUnitDesc *CU) {
1069 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001070 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001071 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001072 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001073}
1074
Dan Gohman475871a2008-07-27 21:46:04 +00001075SDValue SelectionDAG::getLabel(unsigned Opcode,
1076 SDValue Root,
1077 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001078 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001079 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001080 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1081 ID.AddInteger(LabelID);
1082 void *IP = 0;
1083 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001084 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001085 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001086 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001087 CSEMap.InsertNode(N, IP);
1088 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001089 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001090}
1091
Dan Gohman475871a2008-07-27 21:46:04 +00001092SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001093 assert((!V || isa<PointerType>(V->getType())) &&
1094 "SrcValue is not a pointer?");
1095
Jim Laskey583bd472006-10-27 23:46:08 +00001096 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001097 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001098 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001099
Chris Lattner61b09412006-08-11 21:01:22 +00001100 void *IP = 0;
1101 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001102 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001103
Dan Gohmanfed90b62008-07-28 21:51:04 +00001104 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001105 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001106 CSEMap.InsertNode(N, IP);
1107 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001108 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001109}
1110
Dan Gohman475871a2008-07-27 21:46:04 +00001111SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001112 const Value *v = MO.getValue();
1113 assert((!v || isa<PointerType>(v->getType())) &&
1114 "SrcValue is not a pointer?");
1115
1116 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001117 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001118 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001119
1120 void *IP = 0;
1121 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001122 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001123
Dan Gohmanfed90b62008-07-28 21:51:04 +00001124 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001125 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001126 CSEMap.InsertNode(N, IP);
1127 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001128 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001129}
1130
Chris Lattner37ce9df2007-10-15 17:47:20 +00001131/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1132/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001133SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001134 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001135 unsigned ByteSize = VT.getSizeInBits()/8;
1136 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001137 unsigned StackAlign =
1138 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1139
Chris Lattner37ce9df2007-10-15 17:47:20 +00001140 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1141 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1142}
1143
Dan Gohman475871a2008-07-27 21:46:04 +00001144SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1145 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 // These setcc operations always fold.
1147 switch (Cond) {
1148 default: break;
1149 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001150 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001151 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001152 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001153
1154 case ISD::SETOEQ:
1155 case ISD::SETOGT:
1156 case ISD::SETOGE:
1157 case ISD::SETOLT:
1158 case ISD::SETOLE:
1159 case ISD::SETONE:
1160 case ISD::SETO:
1161 case ISD::SETUO:
1162 case ISD::SETUEQ:
1163 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001165 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001166 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001167
Chris Lattner67255a12005-04-07 18:14:58 +00001168 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001169 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001170 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001171 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001172
Chris Lattnerc3aae252005-01-07 07:46:32 +00001173 switch (Cond) {
1174 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001175 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1176 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001177 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1178 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1179 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1180 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1181 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1182 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1183 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1184 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001186 }
Chris Lattner67255a12005-04-07 18:14:58 +00001187 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001188 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001189 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001190 // No compile time operations on this type yet.
1191 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001192 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001193
1194 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001196 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001197 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1198 return getNode(ISD::UNDEF, VT);
1199 // fall through
1200 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1201 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1202 return getNode(ISD::UNDEF, VT);
1203 // fall through
1204 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001205 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001206 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1207 return getNode(ISD::UNDEF, VT);
1208 // fall through
1209 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1210 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1211 return getNode(ISD::UNDEF, VT);
1212 // fall through
1213 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1214 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1215 return getNode(ISD::UNDEF, VT);
1216 // fall through
1217 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001218 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001219 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1220 return getNode(ISD::UNDEF, VT);
1221 // fall through
1222 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001223 R==APFloat::cmpEqual, VT);
1224 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1225 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1226 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1227 R==APFloat::cmpEqual, VT);
1228 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1229 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1230 R==APFloat::cmpLessThan, VT);
1231 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1232 R==APFloat::cmpUnordered, VT);
1233 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1234 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 }
1236 } else {
1237 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001238 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001240 }
1241
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001242 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001243 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244}
1245
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001246/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1247/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001248bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001249 unsigned BitWidth = Op.getValueSizeInBits();
1250 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1251}
1252
Dan Gohmanea859be2007-06-22 14:59:07 +00001253/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1254/// this predicate to simplify operations downstream. Mask is known to be zero
1255/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001256bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001257 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001258 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001259 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1260 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1261 return (KnownZero & Mask) == Mask;
1262}
1263
1264/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1265/// known to be either zero or one and return them in the KnownZero/KnownOne
1266/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1267/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001268void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001269 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001270 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001271 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001272 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001273 "Mask size mismatches value type size!");
1274
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001275 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001276 if (Depth == 6 || Mask == 0)
1277 return; // Limit search depth.
1278
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001279 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001280
1281 switch (Op.getOpcode()) {
1282 case ISD::Constant:
1283 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001284 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001285 KnownZero = ~KnownOne & Mask;
1286 return;
1287 case ISD::AND:
1288 // If either the LHS or the RHS are Zero, the result is zero.
1289 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001290 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1291 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1294
1295 // Output known-1 bits are only known if set in both the LHS & RHS.
1296 KnownOne &= KnownOne2;
1297 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1298 KnownZero |= KnownZero2;
1299 return;
1300 case ISD::OR:
1301 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001302 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1303 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001304 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1305 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1306
1307 // Output known-0 bits are only known if clear in both the LHS & RHS.
1308 KnownZero &= KnownZero2;
1309 // Output known-1 are known to be set if set in either the LHS | RHS.
1310 KnownOne |= KnownOne2;
1311 return;
1312 case ISD::XOR: {
1313 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1314 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1315 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1316 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1317
1318 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001319 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001320 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1321 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1322 KnownZero = KnownZeroOut;
1323 return;
1324 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001325 case ISD::MUL: {
1326 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1327 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1328 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1329 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1330 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1331
1332 // If low bits are zero in either operand, output low known-0 bits.
1333 // Also compute a conserative estimate for high known-0 bits.
1334 // More trickiness is possible, but this is sufficient for the
1335 // interesting case of alignment computation.
1336 KnownOne.clear();
1337 unsigned TrailZ = KnownZero.countTrailingOnes() +
1338 KnownZero2.countTrailingOnes();
1339 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001340 KnownZero2.countLeadingOnes(),
1341 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001342
1343 TrailZ = std::min(TrailZ, BitWidth);
1344 LeadZ = std::min(LeadZ, BitWidth);
1345 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1346 APInt::getHighBitsSet(BitWidth, LeadZ);
1347 KnownZero &= Mask;
1348 return;
1349 }
1350 case ISD::UDIV: {
1351 // For the purposes of computing leading zeros we can conservatively
1352 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001353 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001354 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1355 ComputeMaskedBits(Op.getOperand(0),
1356 AllOnes, KnownZero2, KnownOne2, Depth+1);
1357 unsigned LeadZ = KnownZero2.countLeadingOnes();
1358
1359 KnownOne2.clear();
1360 KnownZero2.clear();
1361 ComputeMaskedBits(Op.getOperand(1),
1362 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001363 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1364 if (RHSUnknownLeadingOnes != BitWidth)
1365 LeadZ = std::min(BitWidth,
1366 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001367
1368 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1369 return;
1370 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001371 case ISD::SELECT:
1372 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1373 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1374 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1375 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1376
1377 // Only known if known in both the LHS and RHS.
1378 KnownOne &= KnownOne2;
1379 KnownZero &= KnownZero2;
1380 return;
1381 case ISD::SELECT_CC:
1382 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1383 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1384 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1385 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1386
1387 // Only known if known in both the LHS and RHS.
1388 KnownOne &= KnownOne2;
1389 KnownZero &= KnownZero2;
1390 return;
1391 case ISD::SETCC:
1392 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001393 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1394 BitWidth > 1)
1395 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001396 return;
1397 case ISD::SHL:
1398 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1399 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001400 unsigned ShAmt = SA->getValue();
1401
1402 // If the shift count is an invalid immediate, don't do anything.
1403 if (ShAmt >= BitWidth)
1404 return;
1405
1406 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001407 KnownZero, KnownOne, Depth+1);
1408 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001409 KnownZero <<= ShAmt;
1410 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001411 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001412 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001413 }
1414 return;
1415 case ISD::SRL:
1416 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1417 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 unsigned ShAmt = SA->getValue();
1419
Dan Gohmand4cf9922008-02-26 18:50:50 +00001420 // If the shift count is an invalid immediate, don't do anything.
1421 if (ShAmt >= BitWidth)
1422 return;
1423
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001424 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001425 KnownZero, KnownOne, Depth+1);
1426 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001427 KnownZero = KnownZero.lshr(ShAmt);
1428 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001429
Dan Gohman72d2fd52008-02-13 22:43:25 +00001430 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001431 KnownZero |= HighBits; // High bits known zero.
1432 }
1433 return;
1434 case ISD::SRA:
1435 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 unsigned ShAmt = SA->getValue();
1437
Dan Gohmand4cf9922008-02-26 18:50:50 +00001438 // If the shift count is an invalid immediate, don't do anything.
1439 if (ShAmt >= BitWidth)
1440 return;
1441
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001442 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001443 // If any of the demanded bits are produced by the sign extension, we also
1444 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001445 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1446 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001447 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001448
1449 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1450 Depth+1);
1451 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 KnownZero = KnownZero.lshr(ShAmt);
1453 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001454
1455 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001456 APInt SignBit = APInt::getSignBit(BitWidth);
1457 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001458
Dan Gohmanca93a432008-02-20 16:30:17 +00001459 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001461 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001462 KnownOne |= HighBits; // New bits are known one.
1463 }
1464 }
1465 return;
1466 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001467 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1468 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001469
1470 // Sign extension. Compute the demanded bits in the result that are not
1471 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001472 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001473
Dan Gohman977a76f2008-02-13 22:28:48 +00001474 APInt InSignBit = APInt::getSignBit(EBits);
1475 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001476
1477 // If the sign extended bits are demanded, we know that the sign
1478 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001479 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001480 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001481 InputDemandedBits |= InSignBit;
1482
1483 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1484 KnownZero, KnownOne, Depth+1);
1485 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1486
1487 // If the sign bit of the input is known set or clear, then we know the
1488 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001489 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001490 KnownZero |= NewBits;
1491 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001492 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001493 KnownOne |= NewBits;
1494 KnownZero &= ~NewBits;
1495 } else { // Input sign bit unknown
1496 KnownZero &= ~NewBits;
1497 KnownOne &= ~NewBits;
1498 }
1499 return;
1500 }
1501 case ISD::CTTZ:
1502 case ISD::CTLZ:
1503 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504 unsigned LowBits = Log2_32(BitWidth)+1;
1505 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001506 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001507 return;
1508 }
1509 case ISD::LOAD: {
1510 if (ISD::isZEXTLoad(Op.Val)) {
1511 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001512 MVT VT = LD->getMemoryVT();
1513 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001514 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001515 }
1516 return;
1517 }
1518 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001519 MVT InVT = Op.getOperand(0).getValueType();
1520 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001521 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1522 APInt InMask = Mask;
1523 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001524 KnownZero.trunc(InBits);
1525 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001526 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001527 KnownZero.zext(BitWidth);
1528 KnownOne.zext(BitWidth);
1529 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001530 return;
1531 }
1532 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001533 MVT InVT = Op.getOperand(0).getValueType();
1534 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001535 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001536 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1537 APInt InMask = Mask;
1538 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001539
1540 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001541 // bit is demanded. Temporarily set this bit in the mask for our callee.
1542 if (NewBits.getBoolValue())
1543 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001544
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001545 KnownZero.trunc(InBits);
1546 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001547 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1548
1549 // Note if the sign bit is known to be zero or one.
1550 bool SignBitKnownZero = KnownZero.isNegative();
1551 bool SignBitKnownOne = KnownOne.isNegative();
1552 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1553 "Sign bit can't be known to be both zero and one!");
1554
1555 // If the sign bit wasn't actually demanded by our caller, we don't
1556 // want it set in the KnownZero and KnownOne result values. Reset the
1557 // mask and reapply it to the result values.
1558 InMask = Mask;
1559 InMask.trunc(InBits);
1560 KnownZero &= InMask;
1561 KnownOne &= InMask;
1562
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001563 KnownZero.zext(BitWidth);
1564 KnownOne.zext(BitWidth);
1565
Dan Gohman977a76f2008-02-13 22:28:48 +00001566 // If the sign bit is known zero or one, the top bits match.
1567 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001568 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001569 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001571 return;
1572 }
1573 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 MVT InVT = Op.getOperand(0).getValueType();
1575 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001576 APInt InMask = Mask;
1577 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001578 KnownZero.trunc(InBits);
1579 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001580 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001581 KnownZero.zext(BitWidth);
1582 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001583 return;
1584 }
1585 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586 MVT InVT = Op.getOperand(0).getValueType();
1587 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001588 APInt InMask = Mask;
1589 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001590 KnownZero.zext(InBits);
1591 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001592 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001593 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001594 KnownZero.trunc(BitWidth);
1595 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001596 break;
1597 }
1598 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001599 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1600 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001601 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1602 KnownOne, Depth+1);
1603 KnownZero |= (~InMask) & Mask;
1604 return;
1605 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001606 case ISD::FGETSIGN:
1607 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001608 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001609 return;
1610
Dan Gohman23e8b712008-04-28 17:02:21 +00001611 case ISD::SUB: {
1612 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1613 // We know that the top bits of C-X are clear if X contains less bits
1614 // than C (i.e. no wrap-around can happen). For example, 20-X is
1615 // positive if we can prove that X is >= 0 and < 16.
1616 if (CLHS->getAPIntValue().isNonNegative()) {
1617 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1618 // NLZ can't be BitWidth with no sign bit
1619 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1620 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1621 Depth+1);
1622
1623 // If all of the MaskV bits are known to be zero, then we know the
1624 // output top bits are zero, because we now know that the output is
1625 // from [0-C].
1626 if ((KnownZero2 & MaskV) == MaskV) {
1627 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1628 // Top bits known zero.
1629 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1630 }
1631 }
1632 }
1633 }
1634 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001636 // Output known-0 bits are known if clear or set in both the low clear bits
1637 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1638 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001639 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1640 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1641 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1642 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1643
1644 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1645 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1646 KnownZeroOut = std::min(KnownZeroOut,
1647 KnownZero2.countTrailingOnes());
1648
1649 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001650 return;
1651 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001652 case ISD::SREM:
1653 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001654 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001655 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001656 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001657 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1658 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001659
Dan Gohmana60832b2008-08-13 23:12:35 +00001660 // If the sign bit of the first operand is zero, the sign bit of
1661 // the result is zero. If the first operand has no one bits below
1662 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001663 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1664 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001665
Dan Gohman23e8b712008-04-28 17:02:21 +00001666 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001667
1668 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 }
1670 }
1671 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001672 case ISD::UREM: {
1673 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001674 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001675 if (RA.isPowerOf2()) {
1676 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001677 APInt Mask2 = LowBits & Mask;
1678 KnownZero |= ~LowBits & Mask;
1679 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1680 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1681 break;
1682 }
1683 }
1684
1685 // Since the result is less than or equal to either operand, any leading
1686 // zero bits in either operand must also exist in the result.
1687 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1688 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1689 Depth+1);
1690 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1691 Depth+1);
1692
1693 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1694 KnownZero2.countLeadingOnes());
1695 KnownOne.clear();
1696 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1697 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001698 }
1699 default:
1700 // Allow the target to implement this method for its nodes.
1701 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1702 case ISD::INTRINSIC_WO_CHAIN:
1703 case ISD::INTRINSIC_W_CHAIN:
1704 case ISD::INTRINSIC_VOID:
1705 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1706 }
1707 return;
1708 }
1709}
1710
1711/// ComputeNumSignBits - Return the number of times the sign bit of the
1712/// register is replicated into the other bits. We know that at least 1 bit
1713/// is always equal to the sign bit (itself), but other cases can give us
1714/// information. For example, immediately after an "SRA X, 2", we know that
1715/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001716unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 MVT VT = Op.getValueType();
1718 assert(VT.isInteger() && "Invalid VT!");
1719 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001720 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001721 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001722
1723 if (Depth == 6)
1724 return 1; // Limit search depth.
1725
1726 switch (Op.getOpcode()) {
1727 default: break;
1728 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001730 return VTBits-Tmp+1;
1731 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001732 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001733 return VTBits-Tmp;
1734
1735 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001736 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1737 // If negative, return # leading ones.
1738 if (Val.isNegative())
1739 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001740
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001741 // Return # leading zeros.
1742 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001743 }
1744
1745 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001746 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001747 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1748
1749 case ISD::SIGN_EXTEND_INREG:
1750 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001751 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 Tmp = VTBits-Tmp+1;
1753
1754 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1755 return std::max(Tmp, Tmp2);
1756
1757 case ISD::SRA:
1758 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1759 // SRA X, C -> adds C sign bits.
1760 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1761 Tmp += C->getValue();
1762 if (Tmp > VTBits) Tmp = VTBits;
1763 }
1764 return Tmp;
1765 case ISD::SHL:
1766 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1767 // shl destroys sign bits.
1768 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1769 if (C->getValue() >= VTBits || // Bad shift.
1770 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1771 return Tmp - C->getValue();
1772 }
1773 break;
1774 case ISD::AND:
1775 case ISD::OR:
1776 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001777 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001779 if (Tmp != 1) {
1780 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1781 FirstAnswer = std::min(Tmp, Tmp2);
1782 // We computed what we know about the sign bits as our first
1783 // answer. Now proceed to the generic code that uses
1784 // ComputeMaskedBits, and pick whichever answer is better.
1785 }
1786 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001787
1788 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001789 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001790 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001791 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 return std::min(Tmp, Tmp2);
1793
1794 case ISD::SETCC:
1795 // If setcc returns 0/-1, all bits are sign bits.
1796 if (TLI.getSetCCResultContents() ==
1797 TargetLowering::ZeroOrNegativeOneSetCCResult)
1798 return VTBits;
1799 break;
1800 case ISD::ROTL:
1801 case ISD::ROTR:
1802 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1803 unsigned RotAmt = C->getValue() & (VTBits-1);
1804
1805 // Handle rotate right by N like a rotate left by 32-N.
1806 if (Op.getOpcode() == ISD::ROTR)
1807 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1808
1809 // If we aren't rotating out all of the known-in sign bits, return the
1810 // number that are left. This handles rotl(sext(x), 1) for example.
1811 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1812 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1813 }
1814 break;
1815 case ISD::ADD:
1816 // Add can have at most one carry bit. Thus we know that the output
1817 // is, at worst, one more bit than the inputs.
1818 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1819 if (Tmp == 1) return 1; // Early out.
1820
1821 // Special case decrementing a value (ADD X, -1):
1822 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1823 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001824 APInt KnownZero, KnownOne;
1825 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1827
1828 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1829 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001830 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001831 return VTBits;
1832
1833 // If we are subtracting one from a positive number, there is no carry
1834 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001835 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001836 return Tmp;
1837 }
1838
1839 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1840 if (Tmp2 == 1) return 1;
1841 return std::min(Tmp, Tmp2)-1;
1842 break;
1843
1844 case ISD::SUB:
1845 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1846 if (Tmp2 == 1) return 1;
1847
1848 // Handle NEG.
1849 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001850 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001851 APInt KnownZero, KnownOne;
1852 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001853 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1854 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1855 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001856 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 return VTBits;
1858
1859 // If the input is known to be positive (the sign bit is known clear),
1860 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001861 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001862 return Tmp2;
1863
1864 // Otherwise, we treat this like a SUB.
1865 }
1866
1867 // Sub can have at most one carry bit. Thus we know that the output
1868 // is, at worst, one more bit than the inputs.
1869 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1870 if (Tmp == 1) return 1; // Early out.
1871 return std::min(Tmp, Tmp2)-1;
1872 break;
1873 case ISD::TRUNCATE:
1874 // FIXME: it's tricky to do anything useful for this, but it is an important
1875 // case for targets like X86.
1876 break;
1877 }
1878
1879 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1880 if (Op.getOpcode() == ISD::LOAD) {
1881 LoadSDNode *LD = cast<LoadSDNode>(Op);
1882 unsigned ExtType = LD->getExtensionType();
1883 switch (ExtType) {
1884 default: break;
1885 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001886 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001887 return VTBits-Tmp+1;
1888 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001889 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001890 return VTBits-Tmp;
1891 }
1892 }
1893
1894 // Allow the target to implement this method for its nodes.
1895 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1896 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1897 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1898 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1899 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001900 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 }
1902
1903 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1904 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001905 APInt KnownZero, KnownOne;
1906 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001907 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1908
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001909 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001911 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001912 Mask = KnownOne;
1913 } else {
1914 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001915 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001916 }
1917
1918 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1919 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001920 Mask = ~Mask;
1921 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001922 // Return # leading zeros. We use 'min' here in case Val was zero before
1923 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001924 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001925}
1926
Chris Lattner51dabfb2006-10-14 00:41:01 +00001927
Dan Gohman475871a2008-07-27 21:46:04 +00001928bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001929 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1930 if (!GA) return false;
1931 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1932 if (!GV) return false;
1933 MachineModuleInfo *MMI = getMachineModuleInfo();
1934 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1935}
1936
1937
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001938/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1939/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00001940SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001941 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001942 SDValue PermMask = N->getOperand(2);
1943 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00001944 if (Idx.getOpcode() == ISD::UNDEF)
1945 return getNode(ISD::UNDEF, VT.getVectorElementType());
1946 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001947 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00001949 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001950
1951 if (V.getOpcode() == ISD::BIT_CONVERT) {
1952 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001953 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00001954 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001955 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001956 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001957 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001958 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001959 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001960 return V.getOperand(Index);
1961 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1962 return getShuffleScalarElt(V.Val, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00001963 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001964}
1965
1966
Chris Lattnerc3aae252005-01-07 07:46:32 +00001967/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001968///
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001970 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001971 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001972 void *IP = 0;
1973 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001974 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001975 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001976 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001977 CSEMap.InsertNode(N, IP);
1978
1979 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00001980#ifndef NDEBUG
1981 VerifyNode(N);
1982#endif
Dan Gohman475871a2008-07-27 21:46:04 +00001983 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001984}
1985
Dan Gohman475871a2008-07-27 21:46:04 +00001986SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001987 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001989 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001990 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001991 switch (Opcode) {
1992 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001993 case ISD::SIGN_EXTEND:
1994 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001995 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001996 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001997 case ISD::TRUNCATE:
1998 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001999 case ISD::UINT_TO_FP:
2000 case ISD::SINT_TO_FP: {
2001 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002002 // No compile time operations on this type.
2003 if (VT==MVT::ppcf128)
2004 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002005 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2006 (void)apf.convertFromAPInt(Val,
2007 Opcode==ISD::SINT_TO_FP,
2008 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002009 return getConstantFP(apf, VT);
2010 }
Chris Lattner94683772005-12-23 05:30:37 +00002011 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002012 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002013 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002014 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002015 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002016 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002017 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002018 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002019 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002020 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002021 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002022 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002023 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002024 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002025 }
2026 }
2027
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002028 // Constant fold unary operations with a floating point constant operand.
2029 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2030 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002031 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002032 switch (Opcode) {
2033 case ISD::FNEG:
2034 V.changeSign();
2035 return getConstantFP(V, VT);
2036 case ISD::FABS:
2037 V.clearSign();
2038 return getConstantFP(V, VT);
2039 case ISD::FP_ROUND:
2040 case ISD::FP_EXTEND:
2041 // This can return overflow, underflow, or inexact; we don't care.
2042 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002043 (void)V.convert(*MVTToAPFloatSemantics(VT),
2044 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002045 return getConstantFP(V, VT);
2046 case ISD::FP_TO_SINT:
2047 case ISD::FP_TO_UINT: {
2048 integerPart x;
2049 assert(integerPartWidth >= 64);
2050 // FIXME need to be more flexible about rounding mode.
2051 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2052 Opcode==ISD::FP_TO_SINT,
2053 APFloat::rmTowardZero);
2054 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2055 break;
2056 return getConstant(x, VT);
2057 }
2058 case ISD::BIT_CONVERT:
2059 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2060 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2061 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2062 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002063 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002064 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002065 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002066 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002067
2068 unsigned OpOpcode = Operand.Val->getOpcode();
2069 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002070 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002071 case ISD::CONCAT_VECTORS:
2072 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002073 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002074 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 assert(VT.isFloatingPoint() &&
2076 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002077 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002078 if (Operand.getOpcode() == ISD::UNDEF)
2079 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002080 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002081 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002083 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002084 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002085 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002086 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002087 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2088 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2089 break;
2090 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002091 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002092 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002094 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002095 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002096 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002097 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002098 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002099 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002100 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002101 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002102 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002103 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002104 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002105 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2106 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2107 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2108 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002110 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002111 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002113 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002114 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002115 if (OpOpcode == ISD::TRUNCATE)
2116 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002117 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2118 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002119 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002120 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002121 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002122 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002123 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2124 else
2125 return Operand.Val->getOperand(0);
2126 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002127 break;
Chris Lattner35481892005-12-23 00:16:34 +00002128 case ISD::BIT_CONVERT:
2129 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002130 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002131 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002132 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002133 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2134 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002135 if (OpOpcode == ISD::UNDEF)
2136 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002137 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002138 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002139 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2140 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002141 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002142 if (OpOpcode == ISD::UNDEF)
2143 return getNode(ISD::UNDEF, VT);
2144 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2145 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2146 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2147 Operand.getConstantOperandVal(1) == 0 &&
2148 Operand.getOperand(0).getValueType() == VT)
2149 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002150 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002151 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002152 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2153 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002154 Operand.Val->getOperand(0));
2155 if (OpOpcode == ISD::FNEG) // --X -> X
2156 return Operand.Val->getOperand(0);
2157 break;
2158 case ISD::FABS:
2159 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2160 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2161 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162 }
2163
Chris Lattner43247a12005-08-25 19:12:10 +00002164 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002165 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002166 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002167 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002169 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002170 void *IP = 0;
2171 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002172 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002173 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002174 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002175 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002176 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002177 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002178 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002179 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002180
Chris Lattnerc3aae252005-01-07 07:46:32 +00002181 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002182#ifndef NDEBUG
2183 VerifyNode(N);
2184#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002185 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002186}
2187
Dan Gohman475871a2008-07-27 21:46:04 +00002188SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2189 SDValue N1, SDValue N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002190 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2191 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002192 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002193 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002194 case ISD::TokenFactor:
2195 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2196 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002197 // Fold trivial token factors.
2198 if (N1.getOpcode() == ISD::EntryToken) return N2;
2199 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002200 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002201 case ISD::CONCAT_VECTORS:
2202 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2203 // one big BUILD_VECTOR.
2204 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2205 N2.getOpcode() == ISD::BUILD_VECTOR) {
2206 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2207 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2208 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2209 }
2210 break;
Chris Lattner76365122005-01-16 02:23:22 +00002211 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002213 N1.getValueType() == VT && "Binary operator types must match!");
2214 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2215 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002216 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002217 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002218 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2219 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002220 break;
Chris Lattner76365122005-01-16 02:23:22 +00002221 case ISD::OR:
2222 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002223 case ISD::ADD:
2224 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002225 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002226 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002227 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2228 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002229 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002230 return N1;
2231 break;
Chris Lattner76365122005-01-16 02:23:22 +00002232 case ISD::UDIV:
2233 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002234 case ISD::MULHU:
2235 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002236 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002237 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002238 case ISD::MUL:
2239 case ISD::SDIV:
2240 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002241 case ISD::FADD:
2242 case ISD::FSUB:
2243 case ISD::FMUL:
2244 case ISD::FDIV:
2245 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002246 assert(N1.getValueType() == N2.getValueType() &&
2247 N1.getValueType() == VT && "Binary operator types must match!");
2248 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002249 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2250 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002251 N1.getValueType().isFloatingPoint() &&
2252 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002253 "Invalid FCOPYSIGN!");
2254 break;
Chris Lattner76365122005-01-16 02:23:22 +00002255 case ISD::SHL:
2256 case ISD::SRA:
2257 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002258 case ISD::ROTL:
2259 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002260 assert(VT == N1.getValueType() &&
2261 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002262 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002263 "Shifts only work on integers");
2264
2265 // Always fold shifts of i1 values so the code generator doesn't need to
2266 // handle them. Since we know the size of the shift has to be less than the
2267 // size of the value, the shift/rotate count is guaranteed to be zero.
2268 if (VT == MVT::i1)
2269 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002270 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002271 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002272 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002273 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002274 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002275 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002276 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002277 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002278 break;
2279 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002280 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002281 assert(VT.isFloatingPoint() &&
2282 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002283 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002284 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002285 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002286 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002287 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002288 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002289 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002290 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002291 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002292 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002293 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002294 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002295 break;
2296 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002297 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002298 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002299 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002300 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002301 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002302 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002303 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002304
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002305 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002306 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002307 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002308 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002309 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002310 return getConstant(Val, VT);
2311 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002312 break;
2313 }
2314 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002315 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2316 if (N1.getOpcode() == ISD::UNDEF)
2317 return getNode(ISD::UNDEF, VT);
2318
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002319 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2320 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002321 if (N2C &&
2322 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002323 N1.getNumOperands() > 0) {
2324 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002326 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2327 N1.getOperand(N2C->getValue() / Factor),
2328 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2329 }
2330
2331 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2332 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002333 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002334 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002335
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002336 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2337 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002338 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2339 if (N1.getOperand(2) == N2)
2340 return N1.getOperand(1);
2341 else
2342 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2343 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002344 break;
2345 case ISD::EXTRACT_ELEMENT:
2346 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002347 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2348 (N1.getValueType().isInteger() == VT.isInteger()) &&
2349 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002350
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002351 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2352 // 64-bit integers into 32-bit parts. Instead of building the extract of
2353 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2354 if (N1.getOpcode() == ISD::BUILD_PAIR)
2355 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002356
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 // EXTRACT_ELEMENT of a constant int is also very common.
2358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002359 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002360 unsigned Shift = ElementSize * N2C->getValue();
2361 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2362 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002363 }
2364 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002365 case ISD::EXTRACT_SUBVECTOR:
2366 if (N1.getValueType() == VT) // Trivial extraction.
2367 return N1;
2368 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002369 }
2370
2371 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002373 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 switch (Opcode) {
2375 case ISD::ADD: return getConstant(C1 + C2, VT);
2376 case ISD::SUB: return getConstant(C1 - C2, VT);
2377 case ISD::MUL: return getConstant(C1 * C2, VT);
2378 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002379 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002380 break;
2381 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002382 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002383 break;
2384 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002385 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002386 break;
2387 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002388 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002389 break;
2390 case ISD::AND : return getConstant(C1 & C2, VT);
2391 case ISD::OR : return getConstant(C1 | C2, VT);
2392 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002393 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002394 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2395 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2396 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2397 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002398 default: break;
2399 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002400 } else { // Cannonicalize constant to RHS if commutative
2401 if (isCommutativeBinOp(Opcode)) {
2402 std::swap(N1C, N2C);
2403 std::swap(N1, N2);
2404 }
2405 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002406 }
2407
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002408 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002409 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2410 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002411 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002412 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2413 // Cannonicalize constant to RHS if commutative
2414 std::swap(N1CFP, N2CFP);
2415 std::swap(N1, N2);
2416 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002417 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2418 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002419 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002420 case ISD::FADD:
2421 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002422 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002423 return getConstantFP(V1, VT);
2424 break;
2425 case ISD::FSUB:
2426 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2427 if (s!=APFloat::opInvalidOp)
2428 return getConstantFP(V1, VT);
2429 break;
2430 case ISD::FMUL:
2431 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2432 if (s!=APFloat::opInvalidOp)
2433 return getConstantFP(V1, VT);
2434 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002435 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002436 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2437 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2438 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002439 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002440 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002441 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2442 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2443 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002444 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002445 case ISD::FCOPYSIGN:
2446 V1.copySign(V2);
2447 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 default: break;
2449 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002450 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002451 }
Chris Lattner62b57722006-04-20 05:39:12 +00002452
2453 // Canonicalize an UNDEF to the RHS, even over a constant.
2454 if (N1.getOpcode() == ISD::UNDEF) {
2455 if (isCommutativeBinOp(Opcode)) {
2456 std::swap(N1, N2);
2457 } else {
2458 switch (Opcode) {
2459 case ISD::FP_ROUND_INREG:
2460 case ISD::SIGN_EXTEND_INREG:
2461 case ISD::SUB:
2462 case ISD::FSUB:
2463 case ISD::FDIV:
2464 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002465 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002466 return N1; // fold op(undef, arg2) -> undef
2467 case ISD::UDIV:
2468 case ISD::SDIV:
2469 case ISD::UREM:
2470 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002471 case ISD::SRL:
2472 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002473 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002474 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2475 // For vectors, we can't easily build an all zero vector, just return
2476 // the LHS.
2477 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002478 }
2479 }
2480 }
2481
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002482 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002483 if (N2.getOpcode() == ISD::UNDEF) {
2484 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002485 case ISD::XOR:
2486 if (N1.getOpcode() == ISD::UNDEF)
2487 // Handle undef ^ undef -> 0 special case. This is a common
2488 // idiom (misuse).
2489 return getConstant(0, VT);
2490 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002491 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002492 case ISD::ADDC:
2493 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002494 case ISD::SUB:
2495 case ISD::FADD:
2496 case ISD::FSUB:
2497 case ISD::FMUL:
2498 case ISD::FDIV:
2499 case ISD::FREM:
2500 case ISD::UDIV:
2501 case ISD::SDIV:
2502 case ISD::UREM:
2503 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002504 return N2; // fold op(arg1, undef) -> undef
2505 case ISD::MUL:
2506 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002507 case ISD::SRL:
2508 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002509 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002510 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2511 // For vectors, we can't easily build an all zero vector, just return
2512 // the LHS.
2513 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002514 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002515 if (!VT.isVector())
2516 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002517 // For vectors, we can't easily build an all one vector, just return
2518 // the LHS.
2519 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002520 case ISD::SRA:
2521 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002522 }
2523 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002524
Chris Lattner27e9b412005-05-11 18:57:39 +00002525 // Memoize this node if possible.
2526 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002527 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002528 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002530 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002531 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002532 void *IP = 0;
2533 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002534 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002535 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002536 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002537 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002538 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002539 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002540 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002541 }
2542
Chris Lattnerc3aae252005-01-07 07:46:32 +00002543 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002544#ifndef NDEBUG
2545 VerifyNode(N);
2546#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002547 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002548}
2549
Dan Gohman475871a2008-07-27 21:46:04 +00002550SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2551 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002552 // Perform various simplifications.
2553 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2554 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002555 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002556 case ISD::CONCAT_VECTORS:
2557 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2558 // one big BUILD_VECTOR.
2559 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2560 N2.getOpcode() == ISD::BUILD_VECTOR &&
2561 N3.getOpcode() == ISD::BUILD_VECTOR) {
2562 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2563 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2564 Elts.insert(Elts.end(), N3.Val->op_begin(), N3.Val->op_end());
2565 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2566 }
2567 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002568 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002569 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002570 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002571 if (Simp.Val) return Simp;
2572 break;
2573 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002574 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002575 if (N1C) {
2576 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002577 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002578 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002579 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002580 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002581
2582 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002583 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002584 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002585 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002586 if (N2C->getValue()) // Unconditional branch
2587 return getNode(ISD::BR, MVT::Other, N1, N3);
2588 else
2589 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002590 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002591 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002592 case ISD::VECTOR_SHUFFLE:
2593 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002594 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002595 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002597 "Illegal VECTOR_SHUFFLE node!");
2598 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002599 case ISD::BIT_CONVERT:
2600 // Fold bit_convert nodes from a type to themselves.
2601 if (N1.getValueType() == VT)
2602 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002603 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002604 }
2605
Chris Lattner43247a12005-08-25 19:12:10 +00002606 // Memoize node if it doesn't produce a flag.
2607 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002608 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002609 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002610 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002611 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002612 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002613 void *IP = 0;
2614 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002615 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002616 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002617 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002618 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002619 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002620 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002621 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002622 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002623 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002624#ifndef NDEBUG
2625 VerifyNode(N);
2626#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002627 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002628}
2629
Dan Gohman475871a2008-07-27 21:46:04 +00002630SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2631 SDValue N1, SDValue N2, SDValue N3,
2632 SDValue N4) {
2633 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002634 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002635}
2636
Dan Gohman475871a2008-07-27 21:46:04 +00002637SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2638 SDValue N1, SDValue N2, SDValue N3,
2639 SDValue N4, SDValue N5) {
2640 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002641 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002642}
2643
Dan Gohman707e0182008-04-12 04:36:06 +00002644/// getMemsetValue - Vectorized representation of the memset value
2645/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002646static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002647 unsigned NumBits = VT.isVector() ?
2648 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002649 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002650 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002651 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002652 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002653 Val = (Val << Shift) | Val;
2654 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002655 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002656 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002657 return DAG.getConstant(Val, VT);
2658 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002659 }
Evan Chengf0df0312008-05-15 08:39:06 +00002660
2661 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2662 unsigned Shift = 8;
2663 for (unsigned i = NumBits; i > 8; i >>= 1) {
2664 Value = DAG.getNode(ISD::OR, VT,
2665 DAG.getNode(ISD::SHL, VT, Value,
2666 DAG.getConstant(Shift, MVT::i8)), Value);
2667 Shift <<= 1;
2668 }
2669
2670 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002671}
2672
Dan Gohman707e0182008-04-12 04:36:06 +00002673/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2674/// used when a memcpy is turned into a memset when the source is a constant
2675/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002676static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002677 const TargetLowering &TLI,
2678 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002679 // Handle vector with all elements zero.
2680 if (Str.empty()) {
2681 if (VT.isInteger())
2682 return DAG.getConstant(0, VT);
2683 unsigned NumElts = VT.getVectorNumElements();
2684 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2685 return DAG.getNode(ISD::BIT_CONVERT, VT,
2686 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2687 }
2688
Duncan Sands83ec4b62008-06-06 12:08:01 +00002689 assert(!VT.isVector() && "Can't handle vector type here!");
2690 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002691 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002692 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002693 if (TLI.isLittleEndian())
2694 Offset = Offset + MSB - 1;
2695 for (unsigned i = 0; i != MSB; ++i) {
2696 Val = (Val << 8) | (unsigned char)Str[Offset];
2697 Offset += TLI.isLittleEndian() ? -1 : 1;
2698 }
2699 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002700}
2701
Dan Gohman707e0182008-04-12 04:36:06 +00002702/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002703///
Dan Gohman475871a2008-07-27 21:46:04 +00002704static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002705 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002707 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2708}
2709
Evan Chengf0df0312008-05-15 08:39:06 +00002710/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2711///
Dan Gohman475871a2008-07-27 21:46:04 +00002712static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002713 unsigned SrcDelta = 0;
2714 GlobalAddressSDNode *G = NULL;
2715 if (Src.getOpcode() == ISD::GlobalAddress)
2716 G = cast<GlobalAddressSDNode>(Src);
2717 else if (Src.getOpcode() == ISD::ADD &&
2718 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2719 Src.getOperand(1).getOpcode() == ISD::Constant) {
2720 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2721 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2722 }
2723 if (!G)
2724 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002725
Evan Chengf0df0312008-05-15 08:39:06 +00002726 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002727 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2728 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002729
Evan Chengf0df0312008-05-15 08:39:06 +00002730 return false;
2731}
Dan Gohman707e0182008-04-12 04:36:06 +00002732
Evan Chengf0df0312008-05-15 08:39:06 +00002733/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2734/// to replace the memset / memcpy is below the threshold. It also returns the
2735/// types of the sequence of memory ops to perform memset / memcpy.
2736static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002737bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002738 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002739 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002740 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002741 SelectionDAG &DAG,
2742 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002743 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002744 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002745 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002747 if (VT != MVT::iAny) {
2748 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002750 // If source is a string constant, this will require an unaligned load.
2751 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2752 if (Dst.getOpcode() != ISD::FrameIndex) {
2753 // Can't change destination alignment. It requires a unaligned store.
2754 if (AllowUnalign)
2755 VT = MVT::iAny;
2756 } else {
2757 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2758 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2759 if (MFI->isFixedObjectIndex(FI)) {
2760 // Can't change destination alignment. It requires a unaligned store.
2761 if (AllowUnalign)
2762 VT = MVT::iAny;
2763 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002764 // Give the stack frame object a larger alignment if needed.
2765 if (MFI->getObjectAlignment(FI) < NewAlign)
2766 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002767 Align = NewAlign;
2768 }
2769 }
2770 }
2771 }
2772
2773 if (VT == MVT::iAny) {
2774 if (AllowUnalign) {
2775 VT = MVT::i64;
2776 } else {
2777 switch (Align & 7) {
2778 case 0: VT = MVT::i64; break;
2779 case 4: VT = MVT::i32; break;
2780 case 2: VT = MVT::i16; break;
2781 default: VT = MVT::i8; break;
2782 }
2783 }
2784
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002786 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2788 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002789
Duncan Sands8e4eb092008-06-08 20:54:56 +00002790 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002791 VT = LVT;
2792 }
Dan Gohman707e0182008-04-12 04:36:06 +00002793
2794 unsigned NumMemOps = 0;
2795 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002796 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002797 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002798 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002799 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002800 VT = MVT::i64;
2801 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002802 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2803 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002804 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002805 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002806 VTSize >>= 1;
2807 }
Dan Gohman707e0182008-04-12 04:36:06 +00002808 }
Dan Gohman707e0182008-04-12 04:36:06 +00002809
2810 if (++NumMemOps > Limit)
2811 return false;
2812 MemOps.push_back(VT);
2813 Size -= VTSize;
2814 }
2815
2816 return true;
2817}
2818
Dan Gohman475871a2008-07-27 21:46:04 +00002819static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2820 SDValue Chain, SDValue Dst,
2821 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002822 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002823 const Value *DstSV, uint64_t DstSVOff,
2824 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002825 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2826
Dan Gohman21323f32008-05-29 19:42:22 +00002827 // Expand memcpy to a series of load and store ops if the size operand falls
2828 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002829 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002830 uint64_t Limit = -1;
2831 if (!AlwaysInline)
2832 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002833 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002834 std::string Str;
2835 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002836 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002837 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002838 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002839
Dan Gohman707e0182008-04-12 04:36:06 +00002840
Evan Cheng0ff39b32008-06-30 07:31:25 +00002841 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002842 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002843 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002844 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002845 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002846 MVT VT = MemOps[i];
2847 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002848 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002849
Evan Cheng0ff39b32008-06-30 07:31:25 +00002850 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002851 // It's unlikely a store of a vector immediate can be done in a single
2852 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002853 // We also handle store a vector with all zero's.
2854 // FIXME: Handle other cases where store of vector immediate is done in
2855 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002856 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002857 Store = DAG.getStore(Chain, Value,
2858 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002859 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002860 } else {
2861 Value = DAG.getLoad(VT, Chain,
2862 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002863 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002864 Store = DAG.getStore(Chain, Value,
2865 getMemBasePlusOffset(Dst, DstOff, DAG),
2866 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002867 }
2868 OutChains.push_back(Store);
2869 SrcOff += VTSize;
2870 DstOff += VTSize;
2871 }
2872
2873 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2874 &OutChains[0], OutChains.size());
2875}
2876
Dan Gohman475871a2008-07-27 21:46:04 +00002877static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2878 SDValue Chain, SDValue Dst,
2879 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002880 unsigned Align, bool AlwaysInline,
2881 const Value *DstSV, uint64_t DstSVOff,
2882 const Value *SrcSV, uint64_t SrcSVOff){
2883 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2884
2885 // Expand memmove to a series of load and store ops if the size operand falls
2886 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002887 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002888 uint64_t Limit = -1;
2889 if (!AlwaysInline)
2890 Limit = TLI.getMaxStoresPerMemmove();
2891 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002892 std::string Str;
2893 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002894 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002895 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002896 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002897
Dan Gohman21323f32008-05-29 19:42:22 +00002898 uint64_t SrcOff = 0, DstOff = 0;
2899
Dan Gohman475871a2008-07-27 21:46:04 +00002900 SmallVector<SDValue, 8> LoadValues;
2901 SmallVector<SDValue, 8> LoadChains;
2902 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002903 unsigned NumMemOps = MemOps.size();
2904 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002905 MVT VT = MemOps[i];
2906 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002907 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002908
2909 Value = DAG.getLoad(VT, Chain,
2910 getMemBasePlusOffset(Src, SrcOff, DAG),
2911 SrcSV, SrcSVOff + SrcOff, false, Align);
2912 LoadValues.push_back(Value);
2913 LoadChains.push_back(Value.getValue(1));
2914 SrcOff += VTSize;
2915 }
2916 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2917 &LoadChains[0], LoadChains.size());
2918 OutChains.clear();
2919 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002920 MVT VT = MemOps[i];
2921 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002923
2924 Store = DAG.getStore(Chain, LoadValues[i],
2925 getMemBasePlusOffset(Dst, DstOff, DAG),
2926 DstSV, DstSVOff + DstOff, false, DstAlign);
2927 OutChains.push_back(Store);
2928 DstOff += VTSize;
2929 }
2930
2931 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2932 &OutChains[0], OutChains.size());
2933}
2934
Dan Gohman475871a2008-07-27 21:46:04 +00002935static SDValue getMemsetStores(SelectionDAG &DAG,
2936 SDValue Chain, SDValue Dst,
2937 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002938 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002939 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002940 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2941
2942 // Expand memset to a series of load/store ops if the size operand
2943 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002944 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002945 std::string Str;
2946 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002947 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002948 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002949 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002950
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002952 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002953
2954 unsigned NumMemOps = MemOps.size();
2955 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002956 MVT VT = MemOps[i];
2957 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002958 SDValue Value = getMemsetValue(Src, VT, DAG);
2959 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00002960 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002961 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002962 OutChains.push_back(Store);
2963 DstOff += VTSize;
2964 }
2965
2966 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2967 &OutChains[0], OutChains.size());
2968}
2969
Dan Gohman475871a2008-07-27 21:46:04 +00002970SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
2971 SDValue Src, SDValue Size,
2972 unsigned Align, bool AlwaysInline,
2973 const Value *DstSV, uint64_t DstSVOff,
2974 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002975
2976 // Check to see if we should lower the memcpy to loads and stores first.
2977 // For cases within the target-specified limits, this is the best choice.
2978 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2979 if (ConstantSize) {
2980 // Memcpy with size zero? Just return the original chain.
2981 if (ConstantSize->isNullValue())
2982 return Chain;
2983
Dan Gohman475871a2008-07-27 21:46:04 +00002984 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00002985 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002986 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002987 if (Result.Val)
2988 return Result;
2989 }
2990
2991 // Then check to see if we should lower the memcpy with target-specific
2992 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00002993 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00002994 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2995 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002996 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002997 if (Result.Val)
2998 return Result;
2999
3000 // If we really need inline code and the target declined to provide it,
3001 // use a (potentially long) sequence of loads and stores.
3002 if (AlwaysInline) {
3003 assert(ConstantSize && "AlwaysInline requires a constant size!");
3004 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3005 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003006 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003007 }
3008
3009 // Emit a library call.
3010 TargetLowering::ArgListTy Args;
3011 TargetLowering::ArgListEntry Entry;
3012 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3013 Entry.Node = Dst; Args.push_back(Entry);
3014 Entry.Node = Src; Args.push_back(Entry);
3015 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003016 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003017 TLI.LowerCallTo(Chain, Type::VoidTy,
3018 false, false, false, CallingConv::C, false,
3019 getExternalSymbol("memcpy", TLI.getPointerTy()),
3020 Args, *this);
3021 return CallResult.second;
3022}
3023
Dan Gohman475871a2008-07-27 21:46:04 +00003024SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3025 SDValue Src, SDValue Size,
3026 unsigned Align,
3027 const Value *DstSV, uint64_t DstSVOff,
3028 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003029
Dan Gohman21323f32008-05-29 19:42:22 +00003030 // Check to see if we should lower the memmove to loads and stores first.
3031 // For cases within the target-specified limits, this is the best choice.
3032 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3033 if (ConstantSize) {
3034 // Memmove with size zero? Just return the original chain.
3035 if (ConstantSize->isNullValue())
3036 return Chain;
3037
Dan Gohman475871a2008-07-27 21:46:04 +00003038 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003039 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3040 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3041 if (Result.Val)
3042 return Result;
3043 }
Dan Gohman707e0182008-04-12 04:36:06 +00003044
3045 // Then check to see if we should lower the memmove with target-specific
3046 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003047 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003048 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003049 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003050 if (Result.Val)
3051 return Result;
3052
3053 // Emit a library call.
3054 TargetLowering::ArgListTy Args;
3055 TargetLowering::ArgListEntry Entry;
3056 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3057 Entry.Node = Dst; Args.push_back(Entry);
3058 Entry.Node = Src; Args.push_back(Entry);
3059 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003060 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003061 TLI.LowerCallTo(Chain, Type::VoidTy,
3062 false, false, false, CallingConv::C, false,
3063 getExternalSymbol("memmove", TLI.getPointerTy()),
3064 Args, *this);
3065 return CallResult.second;
3066}
3067
Dan Gohman475871a2008-07-27 21:46:04 +00003068SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3069 SDValue Src, SDValue Size,
3070 unsigned Align,
3071 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003072
3073 // Check to see if we should lower the memset to stores first.
3074 // For cases within the target-specified limits, this is the best choice.
3075 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3076 if (ConstantSize) {
3077 // Memset with size zero? Just return the original chain.
3078 if (ConstantSize->isNullValue())
3079 return Chain;
3080
Dan Gohman475871a2008-07-27 21:46:04 +00003081 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003082 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003083 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003084 if (Result.Val)
3085 return Result;
3086 }
3087
3088 // Then check to see if we should lower the memset with target-specific
3089 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003090 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003091 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003092 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003093 if (Result.Val)
3094 return Result;
3095
3096 // Emit a library call.
3097 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3098 TargetLowering::ArgListTy Args;
3099 TargetLowering::ArgListEntry Entry;
3100 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3101 Args.push_back(Entry);
3102 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003103 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003104 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3105 else
3106 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3107 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3108 Args.push_back(Entry);
3109 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3110 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003111 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003112 TLI.LowerCallTo(Chain, Type::VoidTy,
3113 false, false, false, CallingConv::C, false,
3114 getExternalSymbol("memset", TLI.getPointerTy()),
3115 Args, *this);
3116 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003117}
3118
Dan Gohman475871a2008-07-27 21:46:04 +00003119SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3120 SDValue Ptr, SDValue Cmp,
3121 SDValue Swp, const Value* PtrVal,
3122 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003123 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003124 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003125
3126 MVT VT = Cmp.getValueType();
3127
3128 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3129 Alignment = getMVTAlignment(VT);
3130
3131 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003132 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003134 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003135 void* IP = 0;
3136 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003137 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003138 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003139 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003140 CSEMap.InsertNode(N, IP);
3141 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003142 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003143}
3144
Dan Gohman475871a2008-07-27 21:46:04 +00003145SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3146 SDValue Ptr, SDValue Val,
3147 const Value* PtrVal,
3148 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003149 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003150 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3151 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003152 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003153 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3154 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003155 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003156
3157 MVT VT = Val.getValueType();
3158
3159 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3160 Alignment = getMVTAlignment(VT);
3161
3162 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003163 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003164 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003165 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003166 void* IP = 0;
3167 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003168 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003169 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003170 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003171 CSEMap.InsertNode(N, IP);
3172 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003173 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003174}
3175
Duncan Sands4bdcb612008-07-02 17:40:58 +00003176/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3177/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003178SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3179 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003180 if (Simplify && NumOps == 1)
3181 return Ops[0];
3182
3183 SmallVector<MVT, 4> VTs;
3184 VTs.reserve(NumOps);
3185 for (unsigned i = 0; i < NumOps; ++i)
3186 VTs.push_back(Ops[i].getValueType());
3187 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3188}
3189
Dan Gohman475871a2008-07-27 21:46:04 +00003190SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003191SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003192 MVT VT, SDValue Chain,
3193 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003194 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003195 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003196 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3197 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003198
Duncan Sands14ea39c2008-03-27 20:23:40 +00003199 if (VT == EVT) {
3200 ExtType = ISD::NON_EXTLOAD;
3201 } else if (ExtType == ISD::NON_EXTLOAD) {
3202 assert(VT == EVT && "Non-extending load from different memory type!");
3203 } else {
3204 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003205 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003206 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3207 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003208 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003209 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003210 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003211 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003212 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003213 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003214 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003215 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003216
3217 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003218 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003219 "Unindexed load with an offset!");
3220
3221 SDVTList VTs = Indexed ?
3222 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003223 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003224 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003225 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003226 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003227 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003228 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003229 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003230 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());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003288 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003289 void *IP = 0;
3290 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003291 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003292 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003293 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3294 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003295 CSEMap.InsertNode(N, IP);
3296 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003297 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003298}
3299
Dan Gohman475871a2008-07-27 21:46:04 +00003300SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3301 SDValue Ptr, const Value *SV,
3302 int SVOffset, MVT SVT,
3303 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003304 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003305
3306 if (VT == SVT)
3307 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003308
Duncan Sands8e4eb092008-06-08 20:54:56 +00003309 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003310 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003311 "Can't do FP-INT conversion!");
3312
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003313 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3314 Alignment = getMVTAlignment(VT);
3315
Evan Cheng8b2794a2006-10-13 21:14:26 +00003316 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003317 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3318 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003319 FoldingSetNodeID ID;
3320 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003321 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003322 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003323 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003324 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003325 void *IP = 0;
3326 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003327 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003328 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003329 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3330 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003331 CSEMap.InsertNode(N, IP);
3332 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003333 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003334}
3335
Dan Gohman475871a2008-07-27 21:46:04 +00003336SDValue
3337SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3338 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003339 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3340 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3341 "Store is already a indexed store!");
3342 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003343 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003344 FoldingSetNodeID ID;
3345 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3346 ID.AddInteger(AM);
3347 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003348 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003349 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003350 void *IP = 0;
3351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003352 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003353 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003354 new (N) StoreSDNode(Ops, VTs, AM,
3355 ST->isTruncatingStore(), ST->getMemoryVT(),
3356 ST->getSrcValue(), ST->getSrcValueOffset(),
3357 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003358 CSEMap.InsertNode(N, IP);
3359 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003360 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003361}
3362
Dan Gohman475871a2008-07-27 21:46:04 +00003363SDValue SelectionDAG::getVAArg(MVT VT,
3364 SDValue Chain, SDValue Ptr,
3365 SDValue SV) {
3366 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003367 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003368}
3369
Dan Gohman475871a2008-07-27 21:46:04 +00003370SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3371 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003372 switch (NumOps) {
3373 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003374 case 1: return getNode(Opcode, VT, Ops[0]);
3375 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3376 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003377 default: break;
3378 }
3379
Dan Gohman475871a2008-07-27 21:46:04 +00003380 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003381 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3383 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003384}
3385
Dan Gohman475871a2008-07-27 21:46:04 +00003386SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3387 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003388 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003389 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003390 case 1: return getNode(Opcode, VT, Ops[0]);
3391 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3392 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003393 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003394 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003395
Chris Lattneref847df2005-04-09 03:27:28 +00003396 switch (Opcode) {
3397 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003398 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003399 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003400 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3401 "LHS and RHS of condition must have same type!");
3402 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3403 "True and False arms of SelectCC must have same type!");
3404 assert(Ops[2].getValueType() == VT &&
3405 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003406 break;
3407 }
3408 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003409 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003410 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3411 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003412 break;
3413 }
Chris Lattneref847df2005-04-09 03:27:28 +00003414 }
3415
Chris Lattner385328c2005-05-14 07:42:29 +00003416 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003417 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003418 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003419 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003420 FoldingSetNodeID ID;
3421 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003422 void *IP = 0;
3423 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003424 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003425 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003426 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003427 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003428 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003429 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003430 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003431 }
Chris Lattneref847df2005-04-09 03:27:28 +00003432 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003433#ifndef NDEBUG
3434 VerifyNode(N);
3435#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003436 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003437}
3438
Dan Gohman475871a2008-07-27 21:46:04 +00003439SDValue SelectionDAG::getNode(unsigned Opcode,
3440 const std::vector<MVT> &ResultTys,
3441 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003442 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3443 Ops, NumOps);
3444}
3445
Dan Gohman475871a2008-07-27 21:46:04 +00003446SDValue SelectionDAG::getNode(unsigned Opcode,
3447 const MVT *VTs, unsigned NumVTs,
3448 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003449 if (NumVTs == 1)
3450 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003451 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3452}
3453
Dan Gohman475871a2008-07-27 21:46:04 +00003454SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3455 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003456 if (VTList.NumVTs == 1)
3457 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003458
Chris Lattner5f056bf2005-07-10 01:55:33 +00003459 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003460 // FIXME: figure out how to safely handle things like
3461 // int foo(int x) { return 1 << (x & 255); }
3462 // int bar() { return foo(256); }
3463#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003464 case ISD::SRA_PARTS:
3465 case ISD::SRL_PARTS:
3466 case ISD::SHL_PARTS:
3467 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003468 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003469 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3470 else if (N3.getOpcode() == ISD::AND)
3471 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3472 // If the and is only masking out bits that cannot effect the shift,
3473 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003474 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003475 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3476 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3477 }
3478 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003479#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003480 }
Chris Lattner89c34632005-05-14 06:20:26 +00003481
Chris Lattner43247a12005-08-25 19:12:10 +00003482 // Memoize the node unless it returns a flag.
3483 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003484 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003485 FoldingSetNodeID ID;
3486 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003487 void *IP = 0;
3488 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003489 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003490 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003491 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003492 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3493 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003494 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003495 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3496 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003497 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003498 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3499 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003500 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003501 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3502 }
Chris Lattnera5682852006-08-07 23:03:03 +00003503 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003504 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003505 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003506 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003507 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3508 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003509 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003510 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3511 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003512 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003513 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3514 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003515 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003516 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3517 }
Chris Lattner43247a12005-08-25 19:12:10 +00003518 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003519 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003520#ifndef NDEBUG
3521 VerifyNode(N);
3522#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003523 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003524}
3525
Dan Gohman475871a2008-07-27 21:46:04 +00003526SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003527 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003528}
3529
Dan Gohman475871a2008-07-27 21:46:04 +00003530SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3531 SDValue N1) {
3532 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003533 return getNode(Opcode, VTList, Ops, 1);
3534}
3535
Dan Gohman475871a2008-07-27 21:46:04 +00003536SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3537 SDValue N1, SDValue N2) {
3538 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003539 return getNode(Opcode, VTList, Ops, 2);
3540}
3541
Dan Gohman475871a2008-07-27 21:46:04 +00003542SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3543 SDValue N1, SDValue N2, SDValue N3) {
3544 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003545 return getNode(Opcode, VTList, Ops, 3);
3546}
3547
Dan Gohman475871a2008-07-27 21:46:04 +00003548SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3549 SDValue N1, SDValue N2, SDValue N3,
3550 SDValue N4) {
3551 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003552 return getNode(Opcode, VTList, Ops, 4);
3553}
3554
Dan Gohman475871a2008-07-27 21:46:04 +00003555SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3556 SDValue N1, SDValue N2, SDValue N3,
3557 SDValue N4, SDValue N5) {
3558 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003559 return getNode(Opcode, VTList, Ops, 5);
3560}
3561
Duncan Sands83ec4b62008-06-06 12:08:01 +00003562SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003563 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003564}
3565
Duncan Sands83ec4b62008-06-06 12:08:01 +00003566SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003567 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3568 E = VTList.rend(); I != E; ++I)
3569 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3570 return *I;
3571
3572 MVT *Array = Allocator.Allocate<MVT>(2);
3573 Array[0] = VT1;
3574 Array[1] = VT2;
3575 SDVTList Result = makeVTList(Array, 2);
3576 VTList.push_back(Result);
3577 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003578}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003579
3580SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3581 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3582 E = VTList.rend(); I != E; ++I)
3583 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3584 I->VTs[2] == VT3)
3585 return *I;
3586
3587 MVT *Array = Allocator.Allocate<MVT>(3);
3588 Array[0] = VT1;
3589 Array[1] = VT2;
3590 Array[2] = VT3;
3591 SDVTList Result = makeVTList(Array, 3);
3592 VTList.push_back(Result);
3593 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003594}
3595
Duncan Sands83ec4b62008-06-06 12:08:01 +00003596SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003597 switch (NumVTs) {
3598 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003599 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003600 case 2: return getVTList(VTs[0], VTs[1]);
3601 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3602 default: break;
3603 }
3604
Dan Gohmane8be6c62008-07-17 19:10:17 +00003605 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3606 E = VTList.rend(); I != E; ++I) {
3607 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3608 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003609
3610 bool NoMatch = false;
3611 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003612 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003613 NoMatch = true;
3614 break;
3615 }
3616 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003617 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003618 }
3619
Dan Gohmane8be6c62008-07-17 19:10:17 +00003620 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3621 std::copy(VTs, VTs+NumVTs, Array);
3622 SDVTList Result = makeVTList(Array, NumVTs);
3623 VTList.push_back(Result);
3624 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003625}
3626
3627
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003628/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3629/// specified operands. If the resultant node already exists in the DAG,
3630/// this does not modify the specified node, instead it returns the node that
3631/// already exists. If the resultant node does not exist in the DAG, the
3632/// input node is returned. As a degenerate case, if you specify the same
3633/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003634SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003635 SDNode *N = InN.Val;
3636 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3637
3638 // Check to see if there is no change.
3639 if (Op == N->getOperand(0)) return InN;
3640
3641 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003642 void *InsertPos = 0;
3643 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003644 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003645
Dan Gohman79acd2b2008-07-21 22:38:59 +00003646 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003647 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003648 RemoveNodeFromCSEMaps(N);
3649
3650 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003651 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003652 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003653 N->OperandList[0].setUser(N);
3654 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003655
3656 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003657 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003658 return InN;
3659}
3660
Dan Gohman475871a2008-07-27 21:46:04 +00003661SDValue SelectionDAG::
3662UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003663 SDNode *N = InN.Val;
3664 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3665
3666 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003667 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3668 return InN; // No operands changed, just return the input node.
3669
3670 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003671 void *InsertPos = 0;
3672 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003673 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003674
Dan Gohman79acd2b2008-07-21 22:38:59 +00003675 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003676 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677 RemoveNodeFromCSEMaps(N);
3678
3679 // Now we update the operands.
3680 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003681 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003682 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003683 N->OperandList[0].setUser(N);
3684 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003685 }
3686 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003687 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003688 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003689 N->OperandList[1].setUser(N);
3690 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003691 }
3692
3693 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003694 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003695 return InN;
3696}
3697
Dan Gohman475871a2008-07-27 21:46:04 +00003698SDValue SelectionDAG::
3699UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3700 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003701 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003702}
3703
Dan Gohman475871a2008-07-27 21:46:04 +00003704SDValue SelectionDAG::
3705UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3706 SDValue Op3, SDValue Op4) {
3707 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003708 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003709}
3710
Dan Gohman475871a2008-07-27 21:46:04 +00003711SDValue SelectionDAG::
3712UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3713 SDValue Op3, SDValue Op4, SDValue Op5) {
3714 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003715 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003716}
3717
Dan Gohman475871a2008-07-27 21:46:04 +00003718SDValue SelectionDAG::
3719UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003720 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003721 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003722 "Update with wrong number of operands");
3723
3724 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003725 bool AnyChange = false;
3726 for (unsigned i = 0; i != NumOps; ++i) {
3727 if (Ops[i] != N->getOperand(i)) {
3728 AnyChange = true;
3729 break;
3730 }
3731 }
3732
3733 // No operands changed, just return the input node.
3734 if (!AnyChange) return InN;
3735
3736 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003737 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003738 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003739 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003740
Dan Gohman7ceda162008-05-02 00:05:03 +00003741 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003742 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003743 RemoveNodeFromCSEMaps(N);
3744
3745 // Now we update the operands.
3746 for (unsigned i = 0; i != NumOps; ++i) {
3747 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003748 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003749 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003750 N->OperandList[i].setUser(N);
3751 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003752 }
3753 }
3754
3755 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003756 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003757 return InN;
3758}
3759
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003760/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003761/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003762void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003763 // Unlike the code in MorphNodeTo that does this, we don't need to
3764 // watch for dead nodes here.
3765 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3766 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3767
3768 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003769}
Chris Lattner149c58c2005-08-16 18:17:10 +00003770
Dan Gohmane8be6c62008-07-17 19:10:17 +00003771/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3772/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003773///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003774SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003775 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003776 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003777 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003778}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003779
Dan Gohmane8be6c62008-07-17 19:10:17 +00003780SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003781 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003782 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003784 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003785}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003786
Dan Gohmane8be6c62008-07-17 19:10:17 +00003787SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003788 MVT VT, SDValue Op1,
3789 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003790 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003791 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003792 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003793}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003794
Dan Gohmane8be6c62008-07-17 19:10:17 +00003795SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003796 MVT VT, SDValue Op1,
3797 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003798 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003799 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003800 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003801}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003802
Dan Gohmane8be6c62008-07-17 19:10:17 +00003803SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003804 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003805 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003806 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003807 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003808}
3809
Dan Gohmane8be6c62008-07-17 19:10:17 +00003810SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003811 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003812 unsigned NumOps) {
3813 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003814 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003815}
3816
Dan Gohmane8be6c62008-07-17 19:10:17 +00003817SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003818 MVT VT1, MVT VT2) {
3819 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003820 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003821}
3822
Dan Gohmane8be6c62008-07-17 19:10:17 +00003823SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003824 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003825 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003826 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003827 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003828}
3829
Dan Gohmane8be6c62008-07-17 19:10:17 +00003830SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003831 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003832 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003833 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003834 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003835 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003836}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003837
Dan Gohmane8be6c62008-07-17 19:10:17 +00003838SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003839 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003840 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003841 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003842 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003843 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003844}
3845
Dan Gohmane8be6c62008-07-17 19:10:17 +00003846SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003848 SDValue Op1, SDValue Op2,
3849 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003850 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003851 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003852 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003853}
3854
Dan Gohmane8be6c62008-07-17 19:10:17 +00003855SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003856 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003857 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003858 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3859}
3860
3861SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3862 MVT VT) {
3863 SDVTList VTs = getVTList(VT);
3864 return MorphNodeTo(N, Opc, VTs, 0, 0);
3865}
3866
3867SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003868 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003869 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003870 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003871 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3872}
3873
3874SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003875 MVT VT, SDValue Op1,
3876 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003877 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003878 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003879 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3880}
3881
3882SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003883 MVT VT, SDValue Op1,
3884 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003885 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003886 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003887 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3888}
3889
3890SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003891 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003892 unsigned NumOps) {
3893 SDVTList VTs = getVTList(VT);
3894 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3895}
3896
3897SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003898 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003899 unsigned NumOps) {
3900 SDVTList VTs = getVTList(VT1, VT2);
3901 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3902}
3903
3904SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3905 MVT VT1, MVT VT2) {
3906 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908}
3909
3910SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3911 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003912 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003913 SDVTList VTs = getVTList(VT1, VT2, VT3);
3914 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3915}
3916
3917SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3918 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003919 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003920 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003921 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003922 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3923}
3924
3925SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3926 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003927 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003928 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003929 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003930 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3931}
3932
3933SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3934 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003935 SDValue Op1, SDValue Op2,
3936 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003937 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003939 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3940}
3941
3942/// MorphNodeTo - These *mutate* the specified node to have the specified
3943/// return type, opcode, and operands.
3944///
3945/// Note that MorphNodeTo returns the resultant node. If there is already a
3946/// node of the specified opcode and operands, it returns that node instead of
3947/// the current one.
3948///
3949/// Using MorphNodeTo is faster than creating a new node and swapping it in
3950/// with ReplaceAllUsesWith both because it often avoids allocating a new
3951/// node, and because it doesn't require CSE recalulation for any of
3952/// the node's users.
3953///
3954SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003955 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003956 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003957 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003958 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003959 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3960 FoldingSetNodeID ID;
3961 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3962 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3963 return ON;
3964 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003965
Chris Lattner0fb094f2005-11-19 01:44:53 +00003966 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003967
Dan Gohmane8be6c62008-07-17 19:10:17 +00003968 // Start the morphing.
3969 N->NodeType = Opc;
3970 N->ValueList = VTs.VTs;
3971 N->NumValues = VTs.NumVTs;
3972
3973 // Clear the operands list, updating used nodes to remove this from their
3974 // use list. Keep track of any operands that become dead as a result.
3975 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3976 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
3977 I != E; ++I) {
3978 SDNode *Used = I->getVal();
3979 Used->removeUser(std::distance(B, I), N);
3980 if (Used->use_empty())
3981 DeadNodeSet.insert(Used);
3982 }
3983
3984 // If NumOps is larger than the # of operands we currently have, reallocate
3985 // the operand list.
3986 if (NumOps > N->NumOperands) {
3987 if (N->OperandsNeedDelete)
3988 delete[] N->OperandList;
3989 if (N->isMachineOpcode()) {
3990 // We're creating a final node that will live unmorphed for the
3991 // remainder of this SelectionDAG's duration, so we can allocate the
3992 // operands directly out of the pool with no recycling metadata.
3993 N->OperandList = Allocator.Allocate<SDUse>(NumOps);
3994 N->OperandsNeedDelete = false;
3995 } else {
3996 N->OperandList = new SDUse[NumOps];
3997 N->OperandsNeedDelete = true;
3998 }
3999 }
4000
4001 // Assign the new operands.
4002 N->NumOperands = NumOps;
4003 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4004 N->OperandList[i] = Ops[i];
4005 N->OperandList[i].setUser(N);
4006 SDNode *ToUse = N->OperandList[i].getVal();
4007 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008 }
4009
4010 // Delete any nodes that are still dead after adding the uses for the
4011 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004012 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004013 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4014 E = DeadNodeSet.end(); I != E; ++I)
4015 if ((*I)->use_empty())
4016 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004017 RemoveDeadNodes(DeadNodes);
4018
Dan Gohmane8be6c62008-07-17 19:10:17 +00004019 if (IP)
4020 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004021 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004022}
4023
Chris Lattner0fb094f2005-11-19 01:44:53 +00004024
Evan Cheng6ae46c42006-02-09 07:15:23 +00004025/// getTargetNode - These are used for target selectors to create a new node
4026/// with specified return type(s), target opcode, and operands.
4027///
4028/// Note that getTargetNode returns the resultant node. If there is already a
4029/// node of the specified opcode and operands, it returns that node instead of
4030/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004031SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004032 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004033}
Dan Gohman475871a2008-07-27 21:46:04 +00004034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004035 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004036}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004037SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004038 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004039 return getNode(~Opcode, VT, Op1, Op2).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,
4043 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004045}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004047 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004048 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004049}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4051 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004052 SDValue Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004053 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004054}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004055SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004056 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004057 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004058 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004059}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004060SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004061 MVT VT2, SDValue Op1,
4062 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004063 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004064 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004065 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004066}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004068 MVT VT2, SDValue Op1,
4069 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004070 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004071 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004072 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004073}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004074SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004075 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004076 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004077 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004078}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004080 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004082 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004083 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004084}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004086 SDValue Op1, SDValue Op2,
4087 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004089 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004090 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004091}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004092SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004093 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004094 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004095 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004096}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004097SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4098 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004099 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004100 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004101 VTList.push_back(VT1);
4102 VTList.push_back(VT2);
4103 VTList.push_back(VT3);
4104 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004105 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004106 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004107}
Evan Cheng39305cf2007-10-05 01:10:49 +00004108SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004109 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004110 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004111 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004112 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004113 Ops, NumOps).Val;
4114}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004115
Evan Cheng08b11732008-03-22 01:55:50 +00004116/// getNodeIfExists - Get the specified node if it's already available, or
4117/// else return NULL.
4118SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004119 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004120 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4121 FoldingSetNodeID ID;
4122 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4123 void *IP = 0;
4124 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4125 return E;
4126 }
4127 return NULL;
4128}
4129
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004130
Evan Cheng99157a02006-08-07 22:13:29 +00004131/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004132/// This can cause recursive merging of nodes in the DAG.
4133///
Chris Lattner11d049c2008-02-03 03:35:22 +00004134/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004135///
Dan Gohman475871a2008-07-27 21:46:04 +00004136void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004137 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004138 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004139 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004140 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004141 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004142
Chris Lattner8b8749f2005-08-17 19:00:20 +00004143 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004144 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004145 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004146
Chris Lattner8b8749f2005-08-17 19:00:20 +00004147 // This node is about to morph, remove its old self from the CSE maps.
4148 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004149 int operandNum = 0;
4150 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4151 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004152 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004153 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004154 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004155 I->setUser(U);
4156 To.Val->addUser(operandNum, U);
4157 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004158
4159 // Now that we have modified U, add it back to the CSE maps. If it already
4160 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004161 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004162 ReplaceAllUsesWith(U, Existing, UpdateListener);
4163 // U is now dead. Inform the listener if it exists and delete it.
4164 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004165 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004166 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004167 } else {
4168 // If the node doesn't already exist, we updated it. Inform a listener if
4169 // it exists.
4170 if (UpdateListener)
4171 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004172 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004173 }
4174}
4175
4176/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4177/// This can cause recursive merging of nodes in the DAG.
4178///
4179/// This version assumes From/To have matching types and numbers of result
4180/// values.
4181///
Chris Lattner1e111c72005-09-07 05:37:01 +00004182void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004183 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004184 assert(From->getVTList().VTs == To->getVTList().VTs &&
4185 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004186 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004187
4188 // Handle the trivial case.
4189 if (From == To)
4190 return;
4191
Chris Lattner8b52f212005-08-26 18:36:28 +00004192 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004193 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004194 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004195
Chris Lattner8b52f212005-08-26 18:36:28 +00004196 // This node is about to morph, remove its old self from the CSE maps.
4197 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004198 int operandNum = 0;
4199 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4200 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004201 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004202 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004203 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004204 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004205 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004206
Chris Lattner8b8749f2005-08-17 19:00:20 +00004207 // Now that we have modified U, add it back to the CSE maps. If it already
4208 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004209 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004210 ReplaceAllUsesWith(U, Existing, UpdateListener);
4211 // U is now dead. Inform the listener if it exists and delete it.
4212 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004213 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004214 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004215 } else {
4216 // If the node doesn't already exist, we updated it. Inform a listener if
4217 // it exists.
4218 if (UpdateListener)
4219 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004220 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004221 }
4222}
4223
Chris Lattner8b52f212005-08-26 18:36:28 +00004224/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4225/// This can cause recursive merging of nodes in the DAG.
4226///
4227/// This version can replace From with any result values. To must match the
4228/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004229void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004230 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004231 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004232 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004233 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004234
4235 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004236 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004237 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004238
Chris Lattner7b2880c2005-08-24 22:44:39 +00004239 // This node is about to morph, remove its old self from the CSE maps.
4240 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004241 int operandNum = 0;
4242 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4243 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004244 if (I->getVal() == From) {
Dan Gohman475871a2008-07-27 21:46:04 +00004245 const SDValue &ToOp = To[I->getSDValue().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004246 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004247 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004248 I->setUser(U);
4249 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004250 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004251
Chris Lattner7b2880c2005-08-24 22:44:39 +00004252 // Now that we have modified U, add it back to the CSE maps. If it already
4253 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004254 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004255 ReplaceAllUsesWith(U, Existing, UpdateListener);
4256 // U is now dead. Inform the listener if it exists and delete it.
4257 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004258 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004259 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004260 } else {
4261 // If the node doesn't already exist, we updated it. Inform a listener if
4262 // it exists.
4263 if (UpdateListener)
4264 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004265 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004266 }
4267}
4268
Chris Lattner012f2412006-02-17 21:58:01 +00004269/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4270/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004271/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004272void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004273 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004274 // Handle the really simple, really trivial case efficiently.
4275 if (From == To) return;
4276
Chris Lattner012f2412006-02-17 21:58:01 +00004277 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004278 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004279 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004280 return;
4281 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004282
Chris Lattnercf5640b2007-02-04 00:14:31 +00004283 // Get all of the users of From.Val. We want these in a nice,
4284 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004285 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004286
4287 while (!Users.empty()) {
4288 // We know that this user uses some value of From. If it is the right
4289 // value, update it.
4290 SDNode *User = Users.back();
4291 Users.pop_back();
4292
Chris Lattner01d029b2007-10-15 06:10:22 +00004293 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004294 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004295 for (; Op != E; ++Op)
4296 if (*Op == From) break;
4297
4298 // If there are no matches, the user must use some other result of From.
4299 if (Op == E) continue;
4300
4301 // Okay, we know this user needs to be updated. Remove its old self
4302 // from the CSE maps.
4303 RemoveNodeFromCSEMaps(User);
4304
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004305 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004306 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004307 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004308 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004309 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004310 Op->setUser(User);
4311 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004312 }
4313 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004314
4315 // Now that we have modified User, add it back to the CSE maps. If it
4316 // already exists there, recursively merge the results together.
4317 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004318 if (!Existing) {
4319 if (UpdateListener) UpdateListener->NodeUpdated(User);
4320 continue; // Continue on to next user.
4321 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004322
4323 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004324 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004325 // recursive merging of other unrelated nodes down the line.
4326 ReplaceAllUsesWith(User, Existing, UpdateListener);
4327
4328 // User is now dead. Notify a listener if present.
4329 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4330 DeleteNodeNotInCSEMaps(User);
4331 }
4332}
4333
4334/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4335/// uses of other values produced by From.Val alone. The same value may
4336/// appear in both the From and To list. The Deleted vector is
4337/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004338void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4339 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004340 unsigned Num,
4341 DAGUpdateListener *UpdateListener){
4342 // Handle the simple, trivial case efficiently.
4343 if (Num == 1)
4344 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4345
4346 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4347 for (unsigned i = 0; i != Num; ++i)
4348 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4349 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004350 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004351
4352 while (!Users.empty()) {
4353 // We know that this user uses some value of From. If it is the right
4354 // value, update it.
4355 SDNode *User = Users.back().first;
4356 unsigned i = Users.back().second;
4357 Users.pop_back();
4358
4359 // Scan for an operand that matches From.
4360 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4361 for (; Op != E; ++Op)
4362 if (*Op == From[i]) break;
4363
4364 // If there are no matches, the user must use some other result of From.
4365 if (Op == E) continue;
4366
4367 // Okay, we know this user needs to be updated. Remove its old self
4368 // from the CSE maps.
4369 RemoveNodeFromCSEMaps(User);
4370
4371 // Update all operands that match "From" in case there are multiple uses.
4372 for (; Op != E; ++Op) {
4373 if (*Op == From[i]) {
4374 From[i].Val->removeUser(Op-User->op_begin(), User);
4375 *Op = To[i];
4376 Op->setUser(User);
4377 To[i].Val->addUser(Op-User->op_begin(), User);
4378 }
4379 }
4380
4381 // Now that we have modified User, add it back to the CSE maps. If it
4382 // already exists there, recursively merge the results together.
4383 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4384 if (!Existing) {
4385 if (UpdateListener) UpdateListener->NodeUpdated(User);
4386 continue; // Continue on to next user.
4387 }
4388
4389 // If there was already an existing matching node, use ReplaceAllUsesWith
4390 // to replace the dead one with the existing one. This can cause
4391 // recursive merging of other unrelated nodes down the line.
4392 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004393
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004394 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004395 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004396 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004397 }
4398}
4399
Evan Chenge6f35d82006-08-01 08:20:41 +00004400/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004401/// based on their topological order. It returns the maximum id and a vector
4402/// of the SDNodes* in assigned order by reference.
4403unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004404 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004405 std::vector<unsigned> InDegree(DAGSize);
4406 std::vector<SDNode*> Sources;
4407
4408 // Use a two pass approach to avoid using a std::map which is slow.
4409 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004410 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4411 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004412 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004413 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004414 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004415 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004416 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004417 }
4418
Evan Cheng99157a02006-08-07 22:13:29 +00004419 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004420 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004421 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004422 SDNode *N = Sources.back();
4423 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004424 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004425 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004426 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004427 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004428 if (Degree == 0)
4429 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004430 }
4431 }
4432
Evan Chengc384d6c2006-08-02 22:00:34 +00004433 // Second pass, assign the actual topological order as node ids.
4434 Id = 0;
4435 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4436 TI != TE; ++TI)
4437 (*TI)->setNodeId(Id++);
4438
4439 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004440}
4441
4442
Evan Cheng091cba12006-07-27 06:39:06 +00004443
Jim Laskey58b968b2005-08-17 20:08:02 +00004444//===----------------------------------------------------------------------===//
4445// SDNode Class
4446//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004447
Chris Lattner917d2c92006-07-19 00:00:37 +00004448// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004449void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004450void UnarySDNode::ANCHOR() {}
4451void BinarySDNode::ANCHOR() {}
4452void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004453void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004454void ConstantSDNode::ANCHOR() {}
4455void ConstantFPSDNode::ANCHOR() {}
4456void GlobalAddressSDNode::ANCHOR() {}
4457void FrameIndexSDNode::ANCHOR() {}
4458void JumpTableSDNode::ANCHOR() {}
4459void ConstantPoolSDNode::ANCHOR() {}
4460void BasicBlockSDNode::ANCHOR() {}
4461void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004462void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004463void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004464void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004465void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004466void ExternalSymbolSDNode::ANCHOR() {}
4467void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004468void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004469void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004470void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004471void LoadSDNode::ANCHOR() {}
4472void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004473void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004474
Chris Lattner48b85922007-02-04 02:41:42 +00004475HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004476 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004477}
4478
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004479GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004480 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004481 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004482 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004483 // Thread Local
4484 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4485 // Non Thread Local
4486 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4487 getSDVTList(VT)), Offset(o) {
4488 TheGlobal = const_cast<GlobalValue*>(GA);
4489}
Chris Lattner48b85922007-02-04 02:41:42 +00004490
Dan Gohman1ea58a52008-07-09 22:08:04 +00004491MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004492 const Value *srcValue, int SVO,
4493 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004494 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004495 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004496
4497 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4498 assert(getAlignment() == alignment && "Alignment representation error!");
4499 assert(isVolatile() == vol && "Volatile representation error!");
4500}
4501
Dan Gohman36b5c132008-04-07 19:35:22 +00004502/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004503/// reference performed by this memory reference.
4504MachineMemOperand MemSDNode::getMemOperand() const {
4505 int Flags;
4506 if (isa<LoadSDNode>(this))
4507 Flags = MachineMemOperand::MOLoad;
4508 else if (isa<StoreSDNode>(this))
4509 Flags = MachineMemOperand::MOStore;
4510 else {
4511 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4512 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4513 }
4514
4515 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004516 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4517
Dan Gohman1ea58a52008-07-09 22:08:04 +00004518 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004519 const FrameIndexSDNode *FI =
4520 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4521 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004522 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4523 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004524 else
4525 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4526 Size, getAlignment());
4527}
4528
Jim Laskey583bd472006-10-27 23:46:08 +00004529/// Profile - Gather unique data for the node.
4530///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004531void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004532 AddNodeIDNode(ID, this);
4533}
4534
Chris Lattnera3255112005-11-08 23:30:28 +00004535/// getValueTypeList - Return a pointer to the specified value type.
4536///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004537const MVT *SDNode::getValueTypeList(MVT VT) {
4538 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004539 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004540 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004541 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004542 static MVT VTs[MVT::LAST_VALUETYPE];
4543 VTs[VT.getSimpleVT()] = VT;
4544 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004545 }
Chris Lattnera3255112005-11-08 23:30:28 +00004546}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004547
Chris Lattner5c884562005-01-12 18:37:47 +00004548/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4549/// indicated value. This method ignores uses of other values defined by this
4550/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004551bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004552 assert(Value < getNumValues() && "Bad value!");
4553
Roman Levensteindc1adac2008-04-07 10:06:32 +00004554 // TODO: Only iterate over uses of a given value of the node
4555 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman475871a2008-07-27 21:46:04 +00004556 if (UI.getUse().getSDValue().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004557 if (NUses == 0)
4558 return false;
4559 --NUses;
4560 }
Chris Lattner5c884562005-01-12 18:37:47 +00004561 }
4562
4563 // Found exactly the right number of uses?
4564 return NUses == 0;
4565}
4566
4567
Evan Cheng33d55952007-08-02 05:29:38 +00004568/// hasAnyUseOfValue - Return true if there are any use of the indicated
4569/// value. This method ignores uses of other values defined by this operation.
4570bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4571 assert(Value < getNumValues() && "Bad value!");
4572
Dan Gohman1373c1c2008-07-09 22:39:01 +00004573 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman475871a2008-07-27 21:46:04 +00004574 if (UI.getUse().getSDValue().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004575 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004576
4577 return false;
4578}
4579
4580
Dan Gohman2a629952008-07-27 18:06:42 +00004581/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004582///
Dan Gohman2a629952008-07-27 18:06:42 +00004583bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004584 bool Seen = false;
4585 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004586 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004587 if (User == this)
4588 Seen = true;
4589 else
4590 return false;
4591 }
4592
4593 return Seen;
4594}
4595
Evan Chenge6e97e62006-11-03 07:31:32 +00004596/// isOperand - Return true if this node is an operand of N.
4597///
Dan Gohman475871a2008-07-27 21:46:04 +00004598bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004599 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4600 if (*this == N->getOperand(i))
4601 return true;
4602 return false;
4603}
4604
Evan Cheng917be682008-03-04 00:41:45 +00004605bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004606 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004607 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004608 return true;
4609 return false;
4610}
Evan Cheng4ee62112006-02-05 06:29:23 +00004611
Chris Lattner572dee72008-01-16 05:49:24 +00004612/// reachesChainWithoutSideEffects - Return true if this operand (which must
4613/// be a chain) reaches the specified operand without crossing any
4614/// side-effecting instructions. In practice, this looks through token
4615/// factors and non-volatile loads. In order to remain efficient, this only
4616/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004617bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004618 unsigned Depth) const {
4619 if (*this == Dest) return true;
4620
4621 // Don't search too deeply, we just want to be able to see through
4622 // TokenFactor's etc.
4623 if (Depth == 0) return false;
4624
4625 // If this is a token factor, all inputs to the TF happen in parallel. If any
4626 // of the operands of the TF reach dest, then we can do the xform.
4627 if (getOpcode() == ISD::TokenFactor) {
4628 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4629 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4630 return true;
4631 return false;
4632 }
4633
4634 // Loads don't have side effects, look through them.
4635 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4636 if (!Ld->isVolatile())
4637 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4638 }
4639 return false;
4640}
4641
4642
Evan Chengc5fc57d2006-11-03 03:05:24 +00004643static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004644 SmallPtrSet<SDNode *, 32> &Visited) {
4645 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004646 return;
4647
4648 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4649 SDNode *Op = N->getOperand(i).Val;
4650 if (Op == P) {
4651 found = true;
4652 return;
4653 }
4654 findPredecessor(Op, P, found, Visited);
4655 }
4656}
4657
Evan Cheng917be682008-03-04 00:41:45 +00004658/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004659/// is either an operand of N or it can be reached by recursively traversing
4660/// up the operands.
4661/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004662bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004663 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004664 bool found = false;
4665 findPredecessor(N, this, found, Visited);
4666 return found;
4667}
4668
Evan Chengc5484282006-10-04 00:56:09 +00004669uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4670 assert(Num < NumOperands && "Invalid child # of SDNode!");
4671 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4672}
4673
Reid Spencer577cc322007-04-01 07:32:19 +00004674std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004675 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004676 default:
4677 if (getOpcode() < ISD::BUILTIN_OP_END)
4678 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004679 if (isMachineOpcode()) {
4680 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004681 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004682 if (getMachineOpcode() < TII->getNumOpcodes())
4683 return TII->get(getMachineOpcode()).getName();
4684 return "<<Unknown Machine Node>>";
4685 }
4686 if (G) {
4687 TargetLowering &TLI = G->getTargetLoweringInfo();
4688 const char *Name = TLI.getTargetNodeName(getOpcode());
4689 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004690 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004691 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004692 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004693
Dan Gohmane8be6c62008-07-17 19:10:17 +00004694#ifndef NDEBUG
4695 case ISD::DELETED_NODE:
4696 return "<<Deleted Node!>>";
4697#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004698 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004699 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004700 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4701 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4702 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004703 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4704 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4705 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004706 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004707 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4708 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4709 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4710 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4711 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004712 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004713 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004714 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004715 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004716 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004717 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004718 case ISD::AssertSext: return "AssertSext";
4719 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004720
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004721 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004722 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004723 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004724 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004725
4726 case ISD::Constant: return "Constant";
4727 case ISD::ConstantFP: return "ConstantFP";
4728 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004729 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004730 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004731 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004732 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004733 case ISD::RETURNADDR: return "RETURNADDR";
4734 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004735 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004736 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4737 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004738 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004739 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004740 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004741 case ISD::INTRINSIC_WO_CHAIN: {
4742 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4743 return Intrinsic::getName((Intrinsic::ID)IID);
4744 }
4745 case ISD::INTRINSIC_VOID:
4746 case ISD::INTRINSIC_W_CHAIN: {
4747 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004748 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004749 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004750
Chris Lattnerb2827b02006-03-19 00:52:58 +00004751 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004752 case ISD::TargetConstant: return "TargetConstant";
4753 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004754 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004755 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004756 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004757 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004758 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004759 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004760
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004761 case ISD::CopyToReg: return "CopyToReg";
4762 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004763 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004764 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004765 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004766 case ISD::DBG_LABEL: return "dbg_label";
4767 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004768 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004769 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004770 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004771 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004772
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004773 // Unary operators
4774 case ISD::FABS: return "fabs";
4775 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004776 case ISD::FSQRT: return "fsqrt";
4777 case ISD::FSIN: return "fsin";
4778 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004779 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004780 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004781 case ISD::FTRUNC: return "ftrunc";
4782 case ISD::FFLOOR: return "ffloor";
4783 case ISD::FCEIL: return "fceil";
4784 case ISD::FRINT: return "frint";
4785 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004786
4787 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004788 case ISD::ADD: return "add";
4789 case ISD::SUB: return "sub";
4790 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004791 case ISD::MULHU: return "mulhu";
4792 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004793 case ISD::SDIV: return "sdiv";
4794 case ISD::UDIV: return "udiv";
4795 case ISD::SREM: return "srem";
4796 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004797 case ISD::SMUL_LOHI: return "smul_lohi";
4798 case ISD::UMUL_LOHI: return "umul_lohi";
4799 case ISD::SDIVREM: return "sdivrem";
4800 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004801 case ISD::AND: return "and";
4802 case ISD::OR: return "or";
4803 case ISD::XOR: return "xor";
4804 case ISD::SHL: return "shl";
4805 case ISD::SRA: return "sra";
4806 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004807 case ISD::ROTL: return "rotl";
4808 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004809 case ISD::FADD: return "fadd";
4810 case ISD::FSUB: return "fsub";
4811 case ISD::FMUL: return "fmul";
4812 case ISD::FDIV: return "fdiv";
4813 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004814 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004815 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004816
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004817 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004818 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004819 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004820 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004821 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004822 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004823 case ISD::CONCAT_VECTORS: return "concat_vectors";
4824 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004825 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004826 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004827 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004828 case ISD::ADDC: return "addc";
4829 case ISD::ADDE: return "adde";
4830 case ISD::SUBC: return "subc";
4831 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004832 case ISD::SHL_PARTS: return "shl_parts";
4833 case ISD::SRA_PARTS: return "sra_parts";
4834 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004835
4836 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4837 case ISD::INSERT_SUBREG: return "insert_subreg";
4838
Chris Lattner7f644642005-04-28 21:44:03 +00004839 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004840 case ISD::SIGN_EXTEND: return "sign_extend";
4841 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004842 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004843 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004844 case ISD::TRUNCATE: return "truncate";
4845 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004846 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004847 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004848 case ISD::FP_EXTEND: return "fp_extend";
4849
4850 case ISD::SINT_TO_FP: return "sint_to_fp";
4851 case ISD::UINT_TO_FP: return "uint_to_fp";
4852 case ISD::FP_TO_SINT: return "fp_to_sint";
4853 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004854 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004855
4856 // Control flow instructions
4857 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004858 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004859 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004860 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004861 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004862 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004863 case ISD::CALLSEQ_START: return "callseq_start";
4864 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004865
4866 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004867 case ISD::LOAD: return "load";
4868 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004869 case ISD::VAARG: return "vaarg";
4870 case ISD::VACOPY: return "vacopy";
4871 case ISD::VAEND: return "vaend";
4872 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004873 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004874 case ISD::EXTRACT_ELEMENT: return "extract_element";
4875 case ISD::BUILD_PAIR: return "build_pair";
4876 case ISD::STACKSAVE: return "stacksave";
4877 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004878 case ISD::TRAP: return "trap";
4879
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004880 // Bit manipulation
4881 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004882 case ISD::CTPOP: return "ctpop";
4883 case ISD::CTTZ: return "cttz";
4884 case ISD::CTLZ: return "ctlz";
4885
Chris Lattner36ce6912005-11-29 06:21:05 +00004886 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004887 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004888 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004889
Duncan Sands36397f52007-07-27 12:58:54 +00004890 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004891 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004892
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004893 case ISD::CONDCODE:
4894 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004895 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004896 case ISD::SETOEQ: return "setoeq";
4897 case ISD::SETOGT: return "setogt";
4898 case ISD::SETOGE: return "setoge";
4899 case ISD::SETOLT: return "setolt";
4900 case ISD::SETOLE: return "setole";
4901 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004902
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004903 case ISD::SETO: return "seto";
4904 case ISD::SETUO: return "setuo";
4905 case ISD::SETUEQ: return "setue";
4906 case ISD::SETUGT: return "setugt";
4907 case ISD::SETUGE: return "setuge";
4908 case ISD::SETULT: return "setult";
4909 case ISD::SETULE: return "setule";
4910 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004911
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004912 case ISD::SETEQ: return "seteq";
4913 case ISD::SETGT: return "setgt";
4914 case ISD::SETGE: return "setge";
4915 case ISD::SETLT: return "setlt";
4916 case ISD::SETLE: return "setle";
4917 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004918 }
4919 }
4920}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004921
Evan Cheng144d8f02006-11-09 17:55:04 +00004922const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004923 switch (AM) {
4924 default:
4925 return "";
4926 case ISD::PRE_INC:
4927 return "<pre-inc>";
4928 case ISD::PRE_DEC:
4929 return "<pre-dec>";
4930 case ISD::POST_INC:
4931 return "<post-inc>";
4932 case ISD::POST_DEC:
4933 return "<post-dec>";
4934 }
4935}
4936
Duncan Sands276dcbd2008-03-21 09:14:45 +00004937std::string ISD::ArgFlagsTy::getArgFlagsString() {
4938 std::string S = "< ";
4939
4940 if (isZExt())
4941 S += "zext ";
4942 if (isSExt())
4943 S += "sext ";
4944 if (isInReg())
4945 S += "inreg ";
4946 if (isSRet())
4947 S += "sret ";
4948 if (isByVal())
4949 S += "byval ";
4950 if (isNest())
4951 S += "nest ";
4952 if (getByValAlign())
4953 S += "byval-align:" + utostr(getByValAlign()) + " ";
4954 if (getOrigAlign())
4955 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4956 if (getByValSize())
4957 S += "byval-size:" + utostr(getByValSize()) + " ";
4958 return S + ">";
4959}
4960
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004961void SDNode::dump() const { dump(0); }
4962void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004963 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004964
4965 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004966 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004967 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004968 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004969 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004970 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004971 }
Bill Wendling832171c2006-12-07 20:04:42 +00004972 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004973
Bill Wendling832171c2006-12-07 20:04:42 +00004974 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004975 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004976 if (i) cerr << ", ";
4977 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004978 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004979 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004980 }
4981
Evan Chengce254432007-12-11 02:08:35 +00004982 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4983 SDNode *Mask = getOperand(2).Val;
4984 cerr << "<";
4985 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4986 if (i) cerr << ",";
4987 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4988 cerr << "u";
4989 else
4990 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4991 }
4992 cerr << ">";
4993 }
4994
Chris Lattnerc3aae252005-01-07 07:46:32 +00004995 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner65a7bd82008-08-19 04:44:30 +00004996 cerr << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00004997 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004998 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner65a7bd82008-08-19 04:44:30 +00004999 cerr << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005000 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner65a7bd82008-08-19 04:44:30 +00005001 cerr << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005002 else {
5003 cerr << "<APFloat(";
5004 CSDN->getValueAPF().convertToAPInt().dump();
5005 cerr << ")>";
5006 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005007 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005008 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005009 int offset = GADN->getOffset();
Chris Lattner65a7bd82008-08-19 04:44:30 +00005010 cerr << '<';
5011 WriteAsOperand(*cerr.stream(), GADN->getGlobal());
5012 cerr << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005013 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005014 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005015 else
Bill Wendling832171c2006-12-07 20:04:42 +00005016 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005017 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005018 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005019 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005020 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005021 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005022 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005023 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00005024 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005025 else
Bill Wendling832171c2006-12-07 20:04:42 +00005026 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005027 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00005028 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005029 else
Bill Wendling832171c2006-12-07 20:04:42 +00005030 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005031 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005032 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005033 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5034 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00005035 cerr << LBB->getName() << " ";
5036 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005037 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005038 if (G && R->getReg() &&
5039 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00005040 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005041 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00005042 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005043 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005044 } else if (const ExternalSymbolSDNode *ES =
5045 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00005046 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005047 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5048 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00005049 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005050 else
Dan Gohman69de1932008-02-06 22:27:42 +00005051 cerr << "<null>";
5052 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5053 if (M->MO.getValue())
5054 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
5055 else
5056 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005057 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
5058 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005059 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005060 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005061 }
5062 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005063 const Value *SrcValue = LD->getSrcValue();
5064 int SrcOffset = LD->getSrcValueOffset();
5065 cerr << " <";
5066 if (SrcValue)
5067 cerr << SrcValue;
5068 else
5069 cerr << "null";
5070 cerr << ":" << SrcOffset << ">";
5071
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005072 bool doExt = true;
5073 switch (LD->getExtensionType()) {
5074 default: doExt = false; break;
5075 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005076 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005077 break;
5078 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005079 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005080 break;
5081 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00005082 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005083 break;
5084 }
5085 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00005086 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005087
Evan Cheng144d8f02006-11-09 17:55:04 +00005088 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005089 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00005090 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005091 if (LD->isVolatile())
5092 cerr << " <volatile>";
5093 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005094 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005095 const Value *SrcValue = ST->getSrcValue();
5096 int SrcOffset = ST->getSrcValueOffset();
5097 cerr << " <";
5098 if (SrcValue)
5099 cerr << SrcValue;
5100 else
5101 cerr << "null";
5102 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005103
5104 if (ST->isTruncatingStore())
5105 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00005106 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005107
5108 const char *AM = getIndexedModeName(ST->getAddressingMode());
5109 if (*AM)
5110 cerr << " " << AM;
5111 if (ST->isVolatile())
5112 cerr << " <volatile>";
5113 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005114 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5115 const Value *SrcValue = AT->getSrcValue();
5116 int SrcOffset = AT->getSrcValueOffset();
5117 cerr << " <";
5118 if (SrcValue)
5119 cerr << SrcValue;
5120 else
5121 cerr << "null";
5122 cerr << ":" << SrcOffset << ">";
5123 if (AT->isVolatile())
5124 cerr << " <volatile>";
5125 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005126 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005127}
5128
Chris Lattnerde202b32005-11-09 23:47:37 +00005129static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005130 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5131 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005132 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005133 else
Bill Wendling832171c2006-12-07 20:04:42 +00005134 cerr << "\n" << std::string(indent+2, ' ')
5135 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005136
Chris Lattnerea946cd2005-01-09 20:38:33 +00005137
Bill Wendling832171c2006-12-07 20:04:42 +00005138 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005139 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005140}
5141
Chris Lattnerc3aae252005-01-07 07:46:32 +00005142void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005143 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005144
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005145 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5146 I != E; ++I) {
5147 const SDNode *N = I;
5148 if (!N->hasOneUse() && N != getRoot().Val)
5149 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005150 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005151
Jim Laskey26f7fa72006-10-17 19:33:52 +00005152 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005153
Bill Wendling832171c2006-12-07 20:04:42 +00005154 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005155}
5156
Evan Chengd6594ae2006-09-12 21:00:35 +00005157const Type *ConstantPoolSDNode::getType() const {
5158 if (isMachineConstantPoolEntry())
5159 return Val.MachineCPVal->getType();
5160 return Val.ConstVal->getType();
5161}