blob: b9f84080ba89c65f806a04e6bb67402e9de920c4 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000033#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000034#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000036#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000037#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000038#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000039#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner0b3e5252006-08-15 19:11:05 +000042/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands83ec4b62008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattnerf8dc0612008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Jim Laskey58b968b2005-08-17 20:08:02 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000072}
73
Duncan Sands83ec4b62008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000077
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000113 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Evan Chengefec7512008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Chengbb81d972008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000195/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
200 if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::DBG_LABEL)
202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000326 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
338 ID.AddPointer(Ops->getSDOperand().Val);
339 ID.AddInteger(Ops->getSDOperand().ResNo);
340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345 const SDOperand *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Roman Levenstein9cac5252008-04-16 16:15:27 +0000351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
353/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
355 AddNodeIDOpcode(ID, N->getOpcode());
356 // Add the return value info.
357 AddNodeIDValueTypes(ID, N->getVTList());
358 // Add the operand info.
359 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
360
361 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 switch (N->getOpcode()) {
363 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000364 case ISD::ARG_FLAGS:
365 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
366 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetConstant:
368 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000369 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 break;
371 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000372 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000373 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000377 case ISD::GlobalAddress:
378 case ISD::TargetGlobalTLSAddress:
379 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
381 ID.AddPointer(GA->getGlobal());
382 ID.AddInteger(GA->getOffset());
383 break;
384 }
385 case ISD::BasicBlock:
386 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
387 break;
388 case ISD::Register:
389 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
390 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000391 case ISD::DBG_STOPPOINT: {
392 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
393 ID.AddInteger(DSP->getLine());
394 ID.AddInteger(DSP->getColumn());
395 ID.AddPointer(DSP->getCompileUnit());
396 break;
397 }
Dan Gohman44066042008-07-01 00:05:16 +0000398 case ISD::DBG_LABEL:
399 case ISD::EH_LABEL:
400 ID.AddInteger(cast<LabelSDNode>(N)->getLabelID());
401 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000402 case ISD::SRCVALUE:
403 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
404 break;
405 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000406 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000407 ID.AddPointer(MO.getValue());
408 ID.AddInteger(MO.getFlags());
409 ID.AddInteger(MO.getOffset());
410 ID.AddInteger(MO.getSize());
411 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000412 break;
413 }
414 case ISD::FrameIndex:
415 case ISD::TargetFrameIndex:
416 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
417 break;
418 case ISD::JumpTable:
419 case ISD::TargetJumpTable:
420 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
421 break;
422 case ISD::ConstantPool:
423 case ISD::TargetConstantPool: {
424 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
425 ID.AddInteger(CP->getAlignment());
426 ID.AddInteger(CP->getOffset());
427 if (CP->isMachineConstantPoolEntry())
428 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
429 else
430 ID.AddPointer(CP->getConstVal());
431 break;
432 }
433 case ISD::LOAD: {
434 LoadSDNode *LD = cast<LoadSDNode>(N);
435 ID.AddInteger(LD->getAddressingMode());
436 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000437 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000438 ID.AddInteger(LD->getAlignment());
439 ID.AddInteger(LD->isVolatile());
440 break;
441 }
442 case ISD::STORE: {
443 StoreSDNode *ST = cast<StoreSDNode>(N);
444 ID.AddInteger(ST->getAddressingMode());
445 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000446 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000447 ID.AddInteger(ST->getAlignment());
448 ID.AddInteger(ST->isVolatile());
449 break;
450 }
Mon P Wang28873102008-06-25 08:15:39 +0000451 case ISD::ATOMIC_CMP_SWAP:
452 case ISD::ATOMIC_LOAD_ADD:
453 case ISD::ATOMIC_SWAP:
454 case ISD::ATOMIC_LOAD_SUB:
455 case ISD::ATOMIC_LOAD_AND:
456 case ISD::ATOMIC_LOAD_OR:
457 case ISD::ATOMIC_LOAD_XOR:
458 case ISD::ATOMIC_LOAD_NAND:
459 case ISD::ATOMIC_LOAD_MIN:
460 case ISD::ATOMIC_LOAD_MAX:
461 case ISD::ATOMIC_LOAD_UMIN:
462 case ISD::ATOMIC_LOAD_UMAX: {
463 AtomicSDNode *AT = cast<AtomicSDNode>(N);
464 ID.AddInteger(AT->getAlignment());
465 ID.AddInteger(AT->isVolatile());
466 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000467 }
Mon P Wang28873102008-06-25 08:15:39 +0000468 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000469}
470
471//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000472// SelectionDAG Class
473//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000474
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000475/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000476/// SelectionDAG.
477void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000478 // Create a dummy node (which is not added to allnodes), that adds a reference
479 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000480 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000481
Chris Lattner190a4182006-08-04 17:45:20 +0000482 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000483
Chris Lattner190a4182006-08-04 17:45:20 +0000484 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000485 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000486 if (I->use_empty())
487 DeadNodes.push_back(I);
488
489 // Process the worklist, deleting the nodes and adding their uses to the
490 // worklist.
491 while (!DeadNodes.empty()) {
492 SDNode *N = DeadNodes.back();
493 DeadNodes.pop_back();
494
495 // Take the node out of the appropriate CSE map.
496 RemoveNodeFromCSEMaps(N);
497
498 // Next, brutally remove the operand list. This is safe to do, as there are
499 // no cycles in the graph.
500 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000501 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000502 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000503
504 // Now that we removed this operand, see if there are no uses of it left.
505 if (Operand->use_empty())
506 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000507 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000508 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000509 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000510 }
Chris Lattner190a4182006-08-04 17:45:20 +0000511 N->OperandList = 0;
512 N->NumOperands = 0;
513
514 // Finally, remove N itself.
515 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000516 }
517
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000518 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000519 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000520}
521
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000522void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000523 SmallVector<SDNode*, 16> DeadNodes;
524 DeadNodes.push_back(N);
525
526 // Process the worklist, deleting the nodes and adding their uses to the
527 // worklist.
528 while (!DeadNodes.empty()) {
529 SDNode *N = DeadNodes.back();
530 DeadNodes.pop_back();
531
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +0000533 UpdateListener->NodeDeleted(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534
Evan Cheng130a6472006-10-12 20:34:05 +0000535 // Take the node out of the appropriate CSE map.
536 RemoveNodeFromCSEMaps(N);
537
538 // Next, brutally remove the operand list. This is safe to do, as there are
539 // no cycles in the graph.
Owen Anderson4474c792008-07-01 22:34:11 +0000540 unsigned op_num = 0;
Evan Cheng130a6472006-10-12 20:34:05 +0000541 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000542 SDNode *Operand = I->getVal();
Owen Anderson4474c792008-07-01 22:34:11 +0000543 Operand->removeUser(op_num, N);
Evan Cheng130a6472006-10-12 20:34:05 +0000544
545 // Now that we removed this operand, see if there are no uses of it left.
546 if (Operand->use_empty())
547 DeadNodes.push_back(Operand);
Owen Anderson4474c792008-07-01 22:34:11 +0000548
549 op_num++;
Evan Cheng130a6472006-10-12 20:34:05 +0000550 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000551 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000552 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000553 }
Evan Cheng130a6472006-10-12 20:34:05 +0000554 N->OperandList = 0;
555 N->NumOperands = 0;
556
557 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000558 AllNodes.erase(N);
559 }
560}
561
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000562void SelectionDAG::DeleteNode(SDNode *N) {
563 assert(N->use_empty() && "Cannot delete a node that is not dead!");
564
565 // First take this out of the appropriate CSE map.
566 RemoveNodeFromCSEMaps(N);
567
Chris Lattner1e111c72005-09-07 05:37:01 +0000568 // Finally, remove uses due to operands of this node, remove from the
569 // AllNodes list, and delete the node.
570 DeleteNodeNotInCSEMaps(N);
571}
572
573void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
574
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000575 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000576 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000577
578 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000579 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000580 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000581 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000582 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000583 }
Chris Lattner65113b22005-11-08 22:07:03 +0000584 N->OperandList = 0;
585 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000586
587 delete N;
588}
589
Chris Lattner149c58c2005-08-16 18:17:10 +0000590/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
591/// correspond to it. This is useful when we're about to delete or repurpose
592/// the node. We don't want future request for structurally identical nodes
593/// to return N anymore.
594void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000595 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000597 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000598 case ISD::CONDCODE:
599 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
600 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000601 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000602 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
603 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000604 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000605 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000606 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000607 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000608 Erased =
609 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000610 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000611 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000612 MVT VT = cast<VTSDNode>(N)->getVT();
613 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000614 Erased = ExtendedValueTypeNodes.erase(VT);
615 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000616 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
617 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000618 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000619 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000620 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000621 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000622 // Remove it from the CSE Map.
623 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000624 break;
625 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000626#ifndef NDEBUG
627 // Verify that the node was actually in one of the CSE maps, unless it has a
628 // flag result (which cannot be CSE'd) or is one of the special cases that are
629 // not subject to CSE.
630 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000631 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000632 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000633 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000634 assert(0 && "Node is not in map!");
635 }
636#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000637}
638
Chris Lattner8b8749f2005-08-17 19:00:20 +0000639/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
640/// has been taken out and modified in some way. If the specified node already
641/// exists in the CSE maps, do not modify the maps, but return the existing node
642/// instead. If it doesn't exist, add it and return null.
643///
644SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
645 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000646 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000647 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000648
Chris Lattner9f8cc692005-12-19 22:21:21 +0000649 // Check that remaining values produced are not flags.
650 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
651 if (N->getValueType(i) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
653
Chris Lattnera5682852006-08-07 23:03:03 +0000654 SDNode *New = CSEMap.GetOrInsertNode(N);
655 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000656 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000657}
658
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000659/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
660/// were replaced with those specified. If this node is never memoized,
661/// return null, otherwise return a pointer to the slot it would take. If a
662/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000663SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
664 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000665 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000666 return 0; // Never add these nodes.
667
668 // Check that remaining values produced are not flags.
669 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
670 if (N->getValueType(i) == MVT::Flag)
671 return 0; // Never CSE anything that produces a flag.
672
Chris Lattner63e3f142007-02-04 07:28:00 +0000673 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000674 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000675 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000676 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000677}
678
679/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
680/// were replaced with those specified. If this node is never memoized,
681/// return null, otherwise return a pointer to the slot it would take. If a
682/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000683SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
684 SDOperand Op1, SDOperand Op2,
685 void *&InsertPos) {
686 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
687 return 0; // Never add these nodes.
688
689 // Check that remaining values produced are not flags.
690 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
691 if (N->getValueType(i) == MVT::Flag)
692 return 0; // Never CSE anything that produces a flag.
693
Chris Lattner63e3f142007-02-04 07:28:00 +0000694 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000695 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000696 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000697 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
698}
699
700
701/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
702/// were replaced with those specified. If this node is never memoized,
703/// return null, otherwise return a pointer to the slot it would take. If a
704/// node already exists with these operands, the slot will be non-null.
705SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000706 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000707 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000708 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000709 return 0; // Never add these nodes.
710
711 // Check that remaining values produced are not flags.
712 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
713 if (N->getValueType(i) == MVT::Flag)
714 return 0; // Never CSE anything that produces a flag.
715
Jim Laskey583bd472006-10-27 23:46:08 +0000716 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000717 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000718
Evan Cheng9629aba2006-10-11 01:47:58 +0000719 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
720 ID.AddInteger(LD->getAddressingMode());
721 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000722 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000723 ID.AddInteger(LD->getAlignment());
724 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000725 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
726 ID.AddInteger(ST->getAddressingMode());
727 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000728 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000729 ID.AddInteger(ST->getAlignment());
730 ID.AddInteger(ST->isVolatile());
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
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000736
Chris Lattner78ec3112003-08-11 14:57:33 +0000737SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000738 while (!AllNodes.empty()) {
739 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000740 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000741 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000742 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000743 }
Chris Lattner65113b22005-11-08 22:07:03 +0000744 N->OperandList = 0;
745 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000746 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000747 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000748}
749
Duncan Sands83ec4b62008-06-06 12:08:01 +0000750SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000751 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000752 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000753 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000754 return getNode(ISD::AND, Op.getValueType(), Op,
755 getConstant(Imm, Op.getValueType()));
756}
757
Duncan Sands83ec4b62008-06-06 12:08:01 +0000758SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000759 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000760 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000761}
762
Duncan Sands83ec4b62008-06-06 12:08:01 +0000763SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
764 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000765
Evan Cheng0ff39b32008-06-30 07:31:25 +0000766 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000767 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000768 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000769
770 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000771 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000772 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000773 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000774 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000775 SDNode *N = NULL;
776 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000777 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000778 return SDOperand(N, 0);
779 if (!N) {
780 N = new ConstantSDNode(isT, Val, EltVT);
781 CSEMap.InsertNode(N, IP);
782 AllNodes.push_back(N);
783 }
784
785 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000786 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000787 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000788 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000789 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
790 }
791 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000792}
793
Chris Lattner0bd48932008-01-17 07:00:52 +0000794SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
795 return getConstant(Val, TLI.getPointerTy(), isTarget);
796}
797
798
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
800 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000801
Duncan Sands83ec4b62008-06-06 12:08:01 +0000802 MVT EltVT =
803 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000804
Chris Lattnerd8658612005-02-17 20:17:32 +0000805 // Do the map lookup using the actual bit pattern for the floating point
806 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
807 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000808 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000809 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000810 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000811 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000812 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000813 SDNode *N = NULL;
814 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000815 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000816 return SDOperand(N, 0);
817 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000818 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000819 CSEMap.InsertNode(N, IP);
820 AllNodes.push_back(N);
821 }
822
Dan Gohman7f321562007-06-25 16:23:39 +0000823 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000825 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000826 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000827 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
828 }
829 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000830}
831
Duncan Sands83ec4b62008-06-06 12:08:01 +0000832SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
833 MVT EltVT =
834 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000835 if (EltVT==MVT::f32)
836 return getConstantFP(APFloat((float)Val), VT, isTarget);
837 else
838 return getConstantFP(APFloat(Val), VT, isTarget);
839}
840
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000843 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000844 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000845
846 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
847 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000848 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000849 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
850 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
851 }
852
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000853 if (GVar && GVar->isThreadLocal())
854 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
855 else
856 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000857
Jim Laskey583bd472006-10-27 23:46:08 +0000858 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000859 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000860 ID.AddPointer(GV);
861 ID.AddInteger(Offset);
862 void *IP = 0;
863 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
864 return SDOperand(E, 0);
865 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
866 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000867 AllNodes.push_back(N);
868 return SDOperand(N, 0);
869}
870
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000872 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000873 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000874 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000875 ID.AddInteger(FI);
876 void *IP = 0;
877 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
878 return SDOperand(E, 0);
879 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
880 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000881 AllNodes.push_back(N);
882 return SDOperand(N, 0);
883}
884
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000887 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000888 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000889 ID.AddInteger(JTI);
890 void *IP = 0;
891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
892 return SDOperand(E, 0);
893 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
894 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
897}
898
Duncan Sands83ec4b62008-06-06 12:08:01 +0000899SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000900 unsigned Alignment, int Offset,
901 bool isTarget) {
902 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000903 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000904 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000905 ID.AddInteger(Alignment);
906 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000907 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000908 void *IP = 0;
909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
910 return SDOperand(E, 0);
911 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
912 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000913 AllNodes.push_back(N);
914 return SDOperand(N, 0);
915}
916
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917
Duncan Sands83ec4b62008-06-06 12:08:01 +0000918SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000919 unsigned Alignment, int Offset,
920 bool isTarget) {
921 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000922 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000923 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000924 ID.AddInteger(Alignment);
925 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000926 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000927 void *IP = 0;
928 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
929 return SDOperand(E, 0);
930 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
931 CSEMap.InsertNode(N, IP);
932 AllNodes.push_back(N);
933 return SDOperand(N, 0);
934}
935
936
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000938 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000939 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000940 ID.AddPointer(MBB);
941 void *IP = 0;
942 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
943 return SDOperand(E, 0);
944 SDNode *N = new BasicBlockSDNode(MBB);
945 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000946 AllNodes.push_back(N);
947 return SDOperand(N, 0);
948}
949
Duncan Sands276dcbd2008-03-21 09:14:45 +0000950SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
951 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000952 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000953 ID.AddInteger(Flags.getRawBits());
954 void *IP = 0;
955 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
956 return SDOperand(E, 0);
957 SDNode *N = new ARG_FLAGSSDNode(Flags);
958 CSEMap.InsertNode(N, IP);
959 AllNodes.push_back(N);
960 return SDOperand(N, 0);
961}
962
Duncan Sands83ec4b62008-06-06 12:08:01 +0000963SDOperand SelectionDAG::getValueType(MVT VT) {
964 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
965 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000966
Duncan Sands83ec4b62008-06-06 12:08:01 +0000967 SDNode *&N = VT.isExtended() ?
968 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000969
970 if (N) return SDOperand(N, 0);
971 N = new VTSDNode(VT);
972 AllNodes.push_back(N);
973 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000974}
975
Duncan Sands83ec4b62008-06-06 12:08:01 +0000976SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 SDNode *&N = ExternalSymbols[Sym];
978 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000979 N = new ExternalSymbolSDNode(false, Sym, VT);
980 AllNodes.push_back(N);
981 return SDOperand(N, 0);
982}
983
Duncan Sands83ec4b62008-06-06 12:08:01 +0000984SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000985 SDNode *&N = TargetExternalSymbols[Sym];
986 if (N) return SDOperand(N, 0);
987 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 AllNodes.push_back(N);
989 return SDOperand(N, 0);
990}
991
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000992SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
993 if ((unsigned)Cond >= CondCodeNodes.size())
994 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000995
Chris Lattner079a27a2005-08-09 20:40:02 +0000996 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000997 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000998 AllNodes.push_back(CondCodeNodes[Cond]);
999 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001000 return SDOperand(CondCodeNodes[Cond], 0);
1001}
1002
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001004 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001005 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001006 ID.AddInteger(RegNo);
1007 void *IP = 0;
1008 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1009 return SDOperand(E, 0);
1010 SDNode *N = new RegisterSDNode(RegNo, VT);
1011 CSEMap.InsertNode(N, IP);
1012 AllNodes.push_back(N);
1013 return SDOperand(N, 0);
1014}
1015
Dan Gohman7f460202008-06-30 20:59:49 +00001016SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1017 unsigned Line, unsigned Col,
1018 const CompileUnitDesc *CU) {
1019 FoldingSetNodeID ID;
1020 SDOperand Ops[] = { Root };
1021 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
1022 ID.AddInteger(Line);
1023 ID.AddInteger(Col);
1024 ID.AddPointer(CU);
1025 void *IP = 0;
1026 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1027 return SDOperand(E, 0);
1028 SDNode *N = new DbgStopPointSDNode(Root, Line, Col, CU);
1029 CSEMap.InsertNode(N, IP);
1030 AllNodes.push_back(N);
1031 return SDOperand(N, 0);
1032}
1033
Dan Gohman44066042008-07-01 00:05:16 +00001034SDOperand SelectionDAG::getLabel(unsigned Opcode,
1035 SDOperand Root,
1036 unsigned LabelID) {
1037 FoldingSetNodeID ID;
1038 SDOperand Ops[] = { Root };
1039 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1040 ID.AddInteger(LabelID);
1041 void *IP = 0;
1042 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1043 return SDOperand(E, 0);
1044 SDNode *N = new LabelSDNode(Opcode, Root, LabelID);
1045 CSEMap.InsertNode(N, IP);
1046 AllNodes.push_back(N);
1047 return SDOperand(N, 0);
1048}
1049
Dan Gohman69de1932008-02-06 22:27:42 +00001050SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001051 assert((!V || isa<PointerType>(V->getType())) &&
1052 "SrcValue is not a pointer?");
1053
Jim Laskey583bd472006-10-27 23:46:08 +00001054 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001055 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001056 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001057
Chris Lattner61b09412006-08-11 21:01:22 +00001058 void *IP = 0;
1059 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1060 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001061
1062 SDNode *N = new SrcValueSDNode(V);
1063 CSEMap.InsertNode(N, IP);
1064 AllNodes.push_back(N);
1065 return SDOperand(N, 0);
1066}
1067
Dan Gohman36b5c132008-04-07 19:35:22 +00001068SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001069 const Value *v = MO.getValue();
1070 assert((!v || isa<PointerType>(v->getType())) &&
1071 "SrcValue is not a pointer?");
1072
1073 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001074 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001075 ID.AddPointer(v);
1076 ID.AddInteger(MO.getFlags());
1077 ID.AddInteger(MO.getOffset());
1078 ID.AddInteger(MO.getSize());
1079 ID.AddInteger(MO.getAlignment());
1080
1081 void *IP = 0;
1082 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1083 return SDOperand(E, 0);
1084
1085 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001086 CSEMap.InsertNode(N, IP);
1087 AllNodes.push_back(N);
1088 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001089}
1090
Chris Lattner37ce9df2007-10-15 17:47:20 +00001091/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1092/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001093SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001094 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001095 unsigned ByteSize = VT.getSizeInBits()/8;
1096 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001097 unsigned StackAlign =
1098 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1099
Chris Lattner37ce9df2007-10-15 17:47:20 +00001100 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1101 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1102}
1103
Duncan Sands83ec4b62008-06-06 12:08:01 +00001104SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001105 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 // These setcc operations always fold.
1107 switch (Cond) {
1108 default: break;
1109 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001110 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001112 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001113
1114 case ISD::SETOEQ:
1115 case ISD::SETOGT:
1116 case ISD::SETOGE:
1117 case ISD::SETOLT:
1118 case ISD::SETOLE:
1119 case ISD::SETONE:
1120 case ISD::SETO:
1121 case ISD::SETUO:
1122 case ISD::SETUEQ:
1123 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001124 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001125 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001127
Chris Lattner67255a12005-04-07 18:14:58 +00001128 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001129 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001130 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001131 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001132
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 switch (Cond) {
1134 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001135 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1136 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001137 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1138 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1139 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1140 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1141 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1142 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1143 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1144 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 }
Chris Lattner67255a12005-04-07 18:14:58 +00001147 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001148 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001150 // No compile time operations on this type yet.
1151 if (N1C->getValueType(0) == MVT::ppcf128)
1152 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001153
1154 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001156 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001157 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1158 return getNode(ISD::UNDEF, VT);
1159 // fall through
1160 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1161 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1162 return getNode(ISD::UNDEF, VT);
1163 // fall through
1164 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001165 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001166 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1167 return getNode(ISD::UNDEF, VT);
1168 // fall through
1169 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1170 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1171 return getNode(ISD::UNDEF, VT);
1172 // fall through
1173 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1174 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1175 return getNode(ISD::UNDEF, VT);
1176 // fall through
1177 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001178 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001179 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1180 return getNode(ISD::UNDEF, VT);
1181 // fall through
1182 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001183 R==APFloat::cmpEqual, VT);
1184 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1185 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1186 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1187 R==APFloat::cmpEqual, VT);
1188 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1189 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1190 R==APFloat::cmpLessThan, VT);
1191 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1192 R==APFloat::cmpUnordered, VT);
1193 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1194 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 }
1196 } else {
1197 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001198 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001199 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001200 }
1201
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001202 // Could not fold it.
1203 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001204}
1205
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001206/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1207/// use this predicate to simplify operations downstream.
1208bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1209 unsigned BitWidth = Op.getValueSizeInBits();
1210 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1211}
1212
Dan Gohmanea859be2007-06-22 14:59:07 +00001213/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1214/// this predicate to simplify operations downstream. Mask is known to be zero
1215/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001216bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001217 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001218 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001219 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1221 return (KnownZero & Mask) == Mask;
1222}
1223
1224/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1225/// known to be either zero or one and return them in the KnownZero/KnownOne
1226/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1227/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001228void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001229 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001230 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001231 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001232 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001233 "Mask size mismatches value type size!");
1234
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001235 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001236 if (Depth == 6 || Mask == 0)
1237 return; // Limit search depth.
1238
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001239 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001240
1241 switch (Op.getOpcode()) {
1242 case ISD::Constant:
1243 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001244 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001245 KnownZero = ~KnownOne & Mask;
1246 return;
1247 case ISD::AND:
1248 // If either the LHS or the RHS are Zero, the result is zero.
1249 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001250 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1251 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001252 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1253 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1254
1255 // Output known-1 bits are only known if set in both the LHS & RHS.
1256 KnownOne &= KnownOne2;
1257 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1258 KnownZero |= KnownZero2;
1259 return;
1260 case ISD::OR:
1261 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001262 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1263 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001264 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1265 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1266
1267 // Output known-0 bits are only known if clear in both the LHS & RHS.
1268 KnownZero &= KnownZero2;
1269 // Output known-1 are known to be set if set in either the LHS | RHS.
1270 KnownOne |= KnownOne2;
1271 return;
1272 case ISD::XOR: {
1273 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1274 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1275 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1276 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1277
1278 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001279 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001280 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1281 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1282 KnownZero = KnownZeroOut;
1283 return;
1284 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001285 case ISD::MUL: {
1286 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1287 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1288 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1289 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1290 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1291
1292 // If low bits are zero in either operand, output low known-0 bits.
1293 // Also compute a conserative estimate for high known-0 bits.
1294 // More trickiness is possible, but this is sufficient for the
1295 // interesting case of alignment computation.
1296 KnownOne.clear();
1297 unsigned TrailZ = KnownZero.countTrailingOnes() +
1298 KnownZero2.countTrailingOnes();
1299 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001300 KnownZero2.countLeadingOnes(),
1301 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001302
1303 TrailZ = std::min(TrailZ, BitWidth);
1304 LeadZ = std::min(LeadZ, BitWidth);
1305 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1306 APInt::getHighBitsSet(BitWidth, LeadZ);
1307 KnownZero &= Mask;
1308 return;
1309 }
1310 case ISD::UDIV: {
1311 // For the purposes of computing leading zeros we can conservatively
1312 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001313 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001314 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1315 ComputeMaskedBits(Op.getOperand(0),
1316 AllOnes, KnownZero2, KnownOne2, Depth+1);
1317 unsigned LeadZ = KnownZero2.countLeadingOnes();
1318
1319 KnownOne2.clear();
1320 KnownZero2.clear();
1321 ComputeMaskedBits(Op.getOperand(1),
1322 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001323 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1324 if (RHSUnknownLeadingOnes != BitWidth)
1325 LeadZ = std::min(BitWidth,
1326 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001327
1328 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1329 return;
1330 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001331 case ISD::SELECT:
1332 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1333 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1335 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1336
1337 // Only known if known in both the LHS and RHS.
1338 KnownOne &= KnownOne2;
1339 KnownZero &= KnownZero2;
1340 return;
1341 case ISD::SELECT_CC:
1342 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1343 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1344 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1345 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1346
1347 // Only known if known in both the LHS and RHS.
1348 KnownOne &= KnownOne2;
1349 KnownZero &= KnownZero2;
1350 return;
1351 case ISD::SETCC:
1352 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001353 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1354 BitWidth > 1)
1355 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001356 return;
1357 case ISD::SHL:
1358 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1359 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001360 unsigned ShAmt = SA->getValue();
1361
1362 // If the shift count is an invalid immediate, don't do anything.
1363 if (ShAmt >= BitWidth)
1364 return;
1365
1366 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001367 KnownZero, KnownOne, Depth+1);
1368 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001369 KnownZero <<= ShAmt;
1370 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001371 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001372 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001373 }
1374 return;
1375 case ISD::SRL:
1376 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1377 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001378 unsigned ShAmt = SA->getValue();
1379
Dan Gohmand4cf9922008-02-26 18:50:50 +00001380 // If the shift count is an invalid immediate, don't do anything.
1381 if (ShAmt >= BitWidth)
1382 return;
1383
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001384 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001385 KnownZero, KnownOne, Depth+1);
1386 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001387 KnownZero = KnownZero.lshr(ShAmt);
1388 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001389
Dan Gohman72d2fd52008-02-13 22:43:25 +00001390 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001391 KnownZero |= HighBits; // High bits known zero.
1392 }
1393 return;
1394 case ISD::SRA:
1395 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001396 unsigned ShAmt = SA->getValue();
1397
Dan Gohmand4cf9922008-02-26 18:50:50 +00001398 // If the shift count is an invalid immediate, don't do anything.
1399 if (ShAmt >= BitWidth)
1400 return;
1401
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001402 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001403 // If any of the demanded bits are produced by the sign extension, we also
1404 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001405 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1406 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001407 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001408
1409 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1410 Depth+1);
1411 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001412 KnownZero = KnownZero.lshr(ShAmt);
1413 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001414
1415 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001416 APInt SignBit = APInt::getSignBit(BitWidth);
1417 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001418
Dan Gohmanca93a432008-02-20 16:30:17 +00001419 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001420 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001421 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001422 KnownOne |= HighBits; // New bits are known one.
1423 }
1424 }
1425 return;
1426 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1428 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001429
1430 // Sign extension. Compute the demanded bits in the result that are not
1431 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001432 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001433
Dan Gohman977a76f2008-02-13 22:28:48 +00001434 APInt InSignBit = APInt::getSignBit(EBits);
1435 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001436
1437 // If the sign extended bits are demanded, we know that the sign
1438 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001439 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001440 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001441 InputDemandedBits |= InSignBit;
1442
1443 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1444 KnownZero, KnownOne, Depth+1);
1445 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1446
1447 // If the sign bit of the input is known set or clear, then we know the
1448 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001449 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001450 KnownZero |= NewBits;
1451 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001452 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001453 KnownOne |= NewBits;
1454 KnownZero &= ~NewBits;
1455 } else { // Input sign bit unknown
1456 KnownZero &= ~NewBits;
1457 KnownOne &= ~NewBits;
1458 }
1459 return;
1460 }
1461 case ISD::CTTZ:
1462 case ISD::CTLZ:
1463 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001464 unsigned LowBits = Log2_32(BitWidth)+1;
1465 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001466 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 return;
1468 }
1469 case ISD::LOAD: {
1470 if (ISD::isZEXTLoad(Op.Val)) {
1471 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001472 MVT VT = LD->getMemoryVT();
1473 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001474 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001475 }
1476 return;
1477 }
1478 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001479 MVT InVT = Op.getOperand(0).getValueType();
1480 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001481 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1482 APInt InMask = Mask;
1483 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001484 KnownZero.trunc(InBits);
1485 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001486 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001487 KnownZero.zext(BitWidth);
1488 KnownOne.zext(BitWidth);
1489 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001490 return;
1491 }
1492 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001493 MVT InVT = Op.getOperand(0).getValueType();
1494 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001495 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001496 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1497 APInt InMask = Mask;
1498 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001499
1500 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001501 // bit is demanded. Temporarily set this bit in the mask for our callee.
1502 if (NewBits.getBoolValue())
1503 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001505 KnownZero.trunc(InBits);
1506 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001507 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1508
1509 // Note if the sign bit is known to be zero or one.
1510 bool SignBitKnownZero = KnownZero.isNegative();
1511 bool SignBitKnownOne = KnownOne.isNegative();
1512 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1513 "Sign bit can't be known to be both zero and one!");
1514
1515 // If the sign bit wasn't actually demanded by our caller, we don't
1516 // want it set in the KnownZero and KnownOne result values. Reset the
1517 // mask and reapply it to the result values.
1518 InMask = Mask;
1519 InMask.trunc(InBits);
1520 KnownZero &= InMask;
1521 KnownOne &= InMask;
1522
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001523 KnownZero.zext(BitWidth);
1524 KnownOne.zext(BitWidth);
1525
Dan Gohman977a76f2008-02-13 22:28:48 +00001526 // If the sign bit is known zero or one, the top bits match.
1527 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001528 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001529 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001530 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001531 return;
1532 }
1533 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001534 MVT InVT = Op.getOperand(0).getValueType();
1535 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001536 APInt InMask = Mask;
1537 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001538 KnownZero.trunc(InBits);
1539 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001540 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001541 KnownZero.zext(BitWidth);
1542 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001543 return;
1544 }
1545 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001546 MVT InVT = Op.getOperand(0).getValueType();
1547 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001548 APInt InMask = Mask;
1549 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001550 KnownZero.zext(InBits);
1551 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001552 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001553 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001554 KnownZero.trunc(BitWidth);
1555 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001556 break;
1557 }
1558 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001559 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1560 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001561 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1562 KnownOne, Depth+1);
1563 KnownZero |= (~InMask) & Mask;
1564 return;
1565 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001566 case ISD::FGETSIGN:
1567 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001568 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001569 return;
1570
Dan Gohman23e8b712008-04-28 17:02:21 +00001571 case ISD::SUB: {
1572 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1573 // We know that the top bits of C-X are clear if X contains less bits
1574 // than C (i.e. no wrap-around can happen). For example, 20-X is
1575 // positive if we can prove that X is >= 0 and < 16.
1576 if (CLHS->getAPIntValue().isNonNegative()) {
1577 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1578 // NLZ can't be BitWidth with no sign bit
1579 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1580 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1581 Depth+1);
1582
1583 // If all of the MaskV bits are known to be zero, then we know the
1584 // output top bits are zero, because we now know that the output is
1585 // from [0-C].
1586 if ((KnownZero2 & MaskV) == MaskV) {
1587 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1588 // Top bits known zero.
1589 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1590 }
1591 }
1592 }
1593 }
1594 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001596 // Output known-0 bits are known if clear or set in both the low clear bits
1597 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1598 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001599 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1600 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1601 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1602 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1603
1604 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1605 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1606 KnownZeroOut = std::min(KnownZeroOut,
1607 KnownZero2.countTrailingOnes());
1608
1609 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001610 return;
1611 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001612 case ISD::SREM:
1613 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001614 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001615 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001616 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001617 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1618 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001619
Dan Gohman23e8b712008-04-28 17:02:21 +00001620 // The sign of a remainder is equal to the sign of the first
1621 // operand (zero being positive).
1622 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1623 KnownZero2 |= ~LowBits;
1624 else if (KnownOne2[BitWidth-1])
1625 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001626
Dan Gohman23e8b712008-04-28 17:02:21 +00001627 KnownZero |= KnownZero2 & Mask;
1628 KnownOne |= KnownOne2 & Mask;
1629
1630 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001631 }
1632 }
1633 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001634 case ISD::UREM: {
1635 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001636 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001637 if (RA.isPowerOf2()) {
1638 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001639 APInt Mask2 = LowBits & Mask;
1640 KnownZero |= ~LowBits & Mask;
1641 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1642 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1643 break;
1644 }
1645 }
1646
1647 // Since the result is less than or equal to either operand, any leading
1648 // zero bits in either operand must also exist in the result.
1649 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1650 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1651 Depth+1);
1652 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1653 Depth+1);
1654
1655 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1656 KnownZero2.countLeadingOnes());
1657 KnownOne.clear();
1658 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1659 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001660 }
1661 default:
1662 // Allow the target to implement this method for its nodes.
1663 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1664 case ISD::INTRINSIC_WO_CHAIN:
1665 case ISD::INTRINSIC_W_CHAIN:
1666 case ISD::INTRINSIC_VOID:
1667 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1668 }
1669 return;
1670 }
1671}
1672
1673/// ComputeNumSignBits - Return the number of times the sign bit of the
1674/// register is replicated into the other bits. We know that at least 1 bit
1675/// is always equal to the sign bit (itself), but other cases can give us
1676/// information. For example, immediately after an "SRA X, 2", we know that
1677/// the top 3 bits are all equal to each other, so we return 3.
1678unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001679 MVT VT = Op.getValueType();
1680 assert(VT.isInteger() && "Invalid VT!");
1681 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001683 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001684
1685 if (Depth == 6)
1686 return 1; // Limit search depth.
1687
1688 switch (Op.getOpcode()) {
1689 default: break;
1690 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001691 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001692 return VTBits-Tmp+1;
1693 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001694 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 return VTBits-Tmp;
1696
1697 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001698 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1699 // If negative, return # leading ones.
1700 if (Val.isNegative())
1701 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001702
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001703 // Return # leading zeros.
1704 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001705 }
1706
1707 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001708 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001709 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1710
1711 case ISD::SIGN_EXTEND_INREG:
1712 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 Tmp = VTBits-Tmp+1;
1715
1716 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1717 return std::max(Tmp, Tmp2);
1718
1719 case ISD::SRA:
1720 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1721 // SRA X, C -> adds C sign bits.
1722 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1723 Tmp += C->getValue();
1724 if (Tmp > VTBits) Tmp = VTBits;
1725 }
1726 return Tmp;
1727 case ISD::SHL:
1728 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1729 // shl destroys sign bits.
1730 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1731 if (C->getValue() >= VTBits || // Bad shift.
1732 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1733 return Tmp - C->getValue();
1734 }
1735 break;
1736 case ISD::AND:
1737 case ISD::OR:
1738 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001739 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001740 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001741 if (Tmp != 1) {
1742 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1743 FirstAnswer = std::min(Tmp, Tmp2);
1744 // We computed what we know about the sign bits as our first
1745 // answer. Now proceed to the generic code that uses
1746 // ComputeMaskedBits, and pick whichever answer is better.
1747 }
1748 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001749
1750 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001751 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001753 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001754 return std::min(Tmp, Tmp2);
1755
1756 case ISD::SETCC:
1757 // If setcc returns 0/-1, all bits are sign bits.
1758 if (TLI.getSetCCResultContents() ==
1759 TargetLowering::ZeroOrNegativeOneSetCCResult)
1760 return VTBits;
1761 break;
1762 case ISD::ROTL:
1763 case ISD::ROTR:
1764 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1765 unsigned RotAmt = C->getValue() & (VTBits-1);
1766
1767 // Handle rotate right by N like a rotate left by 32-N.
1768 if (Op.getOpcode() == ISD::ROTR)
1769 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1770
1771 // If we aren't rotating out all of the known-in sign bits, return the
1772 // number that are left. This handles rotl(sext(x), 1) for example.
1773 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1774 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1775 }
1776 break;
1777 case ISD::ADD:
1778 // Add can have at most one carry bit. Thus we know that the output
1779 // is, at worst, one more bit than the inputs.
1780 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1781 if (Tmp == 1) return 1; // Early out.
1782
1783 // Special case decrementing a value (ADD X, -1):
1784 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1785 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001786 APInt KnownZero, KnownOne;
1787 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001788 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1789
1790 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1791 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001792 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001793 return VTBits;
1794
1795 // If we are subtracting one from a positive number, there is no carry
1796 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001797 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001798 return Tmp;
1799 }
1800
1801 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1802 if (Tmp2 == 1) return 1;
1803 return std::min(Tmp, Tmp2)-1;
1804 break;
1805
1806 case ISD::SUB:
1807 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1808 if (Tmp2 == 1) return 1;
1809
1810 // Handle NEG.
1811 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001812 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001813 APInt KnownZero, KnownOne;
1814 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1816 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1817 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001818 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 return VTBits;
1820
1821 // If the input is known to be positive (the sign bit is known clear),
1822 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001823 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001824 return Tmp2;
1825
1826 // Otherwise, we treat this like a SUB.
1827 }
1828
1829 // Sub can have at most one carry bit. Thus we know that the output
1830 // is, at worst, one more bit than the inputs.
1831 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1832 if (Tmp == 1) return 1; // Early out.
1833 return std::min(Tmp, Tmp2)-1;
1834 break;
1835 case ISD::TRUNCATE:
1836 // FIXME: it's tricky to do anything useful for this, but it is an important
1837 // case for targets like X86.
1838 break;
1839 }
1840
1841 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1842 if (Op.getOpcode() == ISD::LOAD) {
1843 LoadSDNode *LD = cast<LoadSDNode>(Op);
1844 unsigned ExtType = LD->getExtensionType();
1845 switch (ExtType) {
1846 default: break;
1847 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001848 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001849 return VTBits-Tmp+1;
1850 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001851 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 return VTBits-Tmp;
1853 }
1854 }
1855
1856 // Allow the target to implement this method for its nodes.
1857 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1858 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1859 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1860 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1861 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001862 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 }
1864
1865 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1866 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001867 APInt KnownZero, KnownOne;
1868 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1870
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001871 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001872 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001873 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001874 Mask = KnownOne;
1875 } else {
1876 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001877 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001878 }
1879
1880 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1881 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001882 Mask = ~Mask;
1883 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001884 // Return # leading zeros. We use 'min' here in case Val was zero before
1885 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001886 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001887}
1888
Chris Lattner51dabfb2006-10-14 00:41:01 +00001889
Evan Chenga844bde2008-02-02 04:07:54 +00001890bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1891 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1892 if (!GA) return false;
1893 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1894 if (!GV) return false;
1895 MachineModuleInfo *MMI = getMachineModuleInfo();
1896 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1897}
1898
1899
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001900/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1901/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001902SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001903 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001904 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001905 SDOperand Idx = PermMask.getOperand(i);
1906 if (Idx.getOpcode() == ISD::UNDEF)
1907 return getNode(ISD::UNDEF, VT.getVectorElementType());
1908 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001909 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001910 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1911 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001912
1913 if (V.getOpcode() == ISD::BIT_CONVERT) {
1914 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001915 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001916 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001917 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001918 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001919 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001920 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001921 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001922 return V.getOperand(Index);
1923 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1924 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001925 return SDOperand();
1926}
1927
1928
Chris Lattnerc3aae252005-01-07 07:46:32 +00001929/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001930///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001931SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001932 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001933 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001934 void *IP = 0;
1935 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1936 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001937 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001938 CSEMap.InsertNode(N, IP);
1939
1940 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001941 return SDOperand(N, 0);
1942}
1943
Duncan Sands83ec4b62008-06-06 12:08:01 +00001944SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001945 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001946 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001947 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001949 switch (Opcode) {
1950 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001951 case ISD::SIGN_EXTEND:
1952 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001953 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001954 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001955 case ISD::TRUNCATE:
1956 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001957 case ISD::UINT_TO_FP:
1958 case ISD::SINT_TO_FP: {
1959 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001960 // No compile time operations on this type.
1961 if (VT==MVT::ppcf128)
1962 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001963 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1964 (void)apf.convertFromAPInt(Val,
1965 Opcode==ISD::SINT_TO_FP,
1966 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001967 return getConstantFP(apf, VT);
1968 }
Chris Lattner94683772005-12-23 05:30:37 +00001969 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001970 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001971 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001972 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001973 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001974 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001975 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001976 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001977 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001978 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001979 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001980 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001981 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001982 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001983 }
1984 }
1985
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001986 // Constant fold unary operations with a floating point constant operand.
1987 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1988 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001989 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001990 switch (Opcode) {
1991 case ISD::FNEG:
1992 V.changeSign();
1993 return getConstantFP(V, VT);
1994 case ISD::FABS:
1995 V.clearSign();
1996 return getConstantFP(V, VT);
1997 case ISD::FP_ROUND:
1998 case ISD::FP_EXTEND:
1999 // This can return overflow, underflow, or inexact; we don't care.
2000 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002001 (void)V.convert(*MVTToAPFloatSemantics(VT),
2002 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002003 return getConstantFP(V, VT);
2004 case ISD::FP_TO_SINT:
2005 case ISD::FP_TO_UINT: {
2006 integerPart x;
2007 assert(integerPartWidth >= 64);
2008 // FIXME need to be more flexible about rounding mode.
2009 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2010 Opcode==ISD::FP_TO_SINT,
2011 APFloat::rmTowardZero);
2012 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2013 break;
2014 return getConstant(x, VT);
2015 }
2016 case ISD::BIT_CONVERT:
2017 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2018 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2019 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2020 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002021 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002022 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002023 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002024 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002025
2026 unsigned OpOpcode = Operand.Val->getOpcode();
2027 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002028 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002029 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002030 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002031 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002032 assert(VT.isFloatingPoint() &&
2033 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002034 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002035 if (Operand.getOpcode() == ISD::UNDEF)
2036 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002037 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002038 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002039 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002040 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002041 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002042 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002043 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002044 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2045 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2046 break;
2047 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002048 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002049 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002050 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002051 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002052 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002053 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002054 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002055 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002056 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002058 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002059 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002060 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002061 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002062 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2063 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2064 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2065 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002066 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002067 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002068 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002069 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002070 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002071 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002072 if (OpOpcode == ISD::TRUNCATE)
2073 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002074 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2075 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002076 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002077 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002078 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002079 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002080 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2081 else
2082 return Operand.Val->getOperand(0);
2083 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002084 break;
Chris Lattner35481892005-12-23 00:16:34 +00002085 case ISD::BIT_CONVERT:
2086 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002087 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002088 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002089 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002090 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2091 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002092 if (OpOpcode == ISD::UNDEF)
2093 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002094 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002095 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2097 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002098 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002099 if (OpOpcode == ISD::UNDEF)
2100 return getNode(ISD::UNDEF, VT);
2101 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2102 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2103 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2104 Operand.getConstantOperandVal(1) == 0 &&
2105 Operand.getOperand(0).getValueType() == VT)
2106 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002107 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002108 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002109 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2110 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002111 Operand.Val->getOperand(0));
2112 if (OpOpcode == ISD::FNEG) // --X -> X
2113 return Operand.Val->getOperand(0);
2114 break;
2115 case ISD::FABS:
2116 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2117 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2118 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 }
2120
Chris Lattner43247a12005-08-25 19:12:10 +00002121 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002122 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002123 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002124 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002125 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002126 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002127 void *IP = 0;
2128 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2129 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002130 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002131 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002132 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002133 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002134 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002135 AllNodes.push_back(N);
2136 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002137}
2138
Chris Lattner36019aa2005-04-18 03:48:41 +00002139
2140
Duncan Sands83ec4b62008-06-06 12:08:01 +00002141SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002142 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002143 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2144 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002145 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002146 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002147 case ISD::TokenFactor:
2148 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2149 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002150 // Fold trivial token factors.
2151 if (N1.getOpcode() == ISD::EntryToken) return N2;
2152 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002153 break;
Chris Lattner76365122005-01-16 02:23:22 +00002154 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002156 N1.getValueType() == VT && "Binary operator types must match!");
2157 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2158 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002159 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002160 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002161 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2162 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002163 break;
Chris Lattner76365122005-01-16 02:23:22 +00002164 case ISD::OR:
2165 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002166 case ISD::ADD:
2167 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002168 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002169 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002170 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2171 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002172 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002173 return N1;
2174 break;
Chris Lattner76365122005-01-16 02:23:22 +00002175 case ISD::UDIV:
2176 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002177 case ISD::MULHU:
2178 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002179 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002180 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002181 case ISD::MUL:
2182 case ISD::SDIV:
2183 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002184 case ISD::FADD:
2185 case ISD::FSUB:
2186 case ISD::FMUL:
2187 case ISD::FDIV:
2188 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002189 assert(N1.getValueType() == N2.getValueType() &&
2190 N1.getValueType() == VT && "Binary operator types must match!");
2191 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002192 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2193 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 N1.getValueType().isFloatingPoint() &&
2195 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002196 "Invalid FCOPYSIGN!");
2197 break;
Chris Lattner76365122005-01-16 02:23:22 +00002198 case ISD::SHL:
2199 case ISD::SRA:
2200 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002201 case ISD::ROTL:
2202 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002203 assert(VT == N1.getValueType() &&
2204 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002206 "Shifts only work on integers");
2207
2208 // Always fold shifts of i1 values so the code generator doesn't need to
2209 // handle them. Since we know the size of the shift has to be less than the
2210 // size of the value, the shift/rotate count is guaranteed to be zero.
2211 if (VT == MVT::i1)
2212 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002213 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002214 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002215 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002216 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002217 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002218 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002219 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002220 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002221 break;
2222 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002223 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 assert(VT.isFloatingPoint() &&
2225 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002226 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002227 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002228 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002229 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002230 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002231 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002232 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002233 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002235 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002236 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002237 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002238 break;
2239 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002240 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002241 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002242 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002243 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002244 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002245 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002246 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002247
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002248 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002249 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002250 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002251 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002252 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002253 return getConstant(Val, VT);
2254 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002255 break;
2256 }
2257 case ISD::EXTRACT_VECTOR_ELT:
2258 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2259
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002260 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2261 if (N1.getOpcode() == ISD::UNDEF)
2262 return getNode(ISD::UNDEF, VT);
2263
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002264 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2265 // expanding copies of large vectors from registers.
2266 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2267 N1.getNumOperands() > 0) {
2268 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002269 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002270 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2271 N1.getOperand(N2C->getValue() / Factor),
2272 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2273 }
2274
2275 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2276 // expanding large vector constants.
2277 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2278 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002279
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002280 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2281 // operations are lowered to scalars.
2282 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2283 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2284 if (IEC == N2C)
2285 return N1.getOperand(1);
2286 else
2287 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2288 }
2289 break;
2290 case ISD::EXTRACT_ELEMENT:
2291 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002292 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2293 (N1.getValueType().isInteger() == VT.isInteger()) &&
2294 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002295
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002296 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2297 // 64-bit integers into 32-bit parts. Instead of building the extract of
2298 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2299 if (N1.getOpcode() == ISD::BUILD_PAIR)
2300 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002301
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002302 // EXTRACT_ELEMENT of a constant int is also very common.
2303 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002304 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002305 unsigned Shift = ElementSize * N2C->getValue();
2306 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2307 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002308 }
2309 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002310 case ISD::EXTRACT_SUBVECTOR:
2311 if (N1.getValueType() == VT) // Trivial extraction.
2312 return N1;
2313 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002314 }
2315
2316 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002317 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002318 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002319 switch (Opcode) {
2320 case ISD::ADD: return getConstant(C1 + C2, VT);
2321 case ISD::SUB: return getConstant(C1 - C2, VT);
2322 case ISD::MUL: return getConstant(C1 * C2, VT);
2323 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002324 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002325 break;
2326 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002327 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328 break;
2329 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002330 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331 break;
2332 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002333 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 break;
2335 case ISD::AND : return getConstant(C1 & C2, VT);
2336 case ISD::OR : return getConstant(C1 | C2, VT);
2337 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002338 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002339 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2340 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2341 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2342 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002343 default: break;
2344 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002345 } else { // Cannonicalize constant to RHS if commutative
2346 if (isCommutativeBinOp(Opcode)) {
2347 std::swap(N1C, N2C);
2348 std::swap(N1, N2);
2349 }
2350 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002351 }
2352
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002353 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2355 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002356 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2358 // Cannonicalize constant to RHS if commutative
2359 std::swap(N1CFP, N2CFP);
2360 std::swap(N1, N2);
2361 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002362 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2363 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002365 case ISD::FADD:
2366 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002367 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002368 return getConstantFP(V1, VT);
2369 break;
2370 case ISD::FSUB:
2371 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2372 if (s!=APFloat::opInvalidOp)
2373 return getConstantFP(V1, VT);
2374 break;
2375 case ISD::FMUL:
2376 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2377 if (s!=APFloat::opInvalidOp)
2378 return getConstantFP(V1, VT);
2379 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002380 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002381 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2382 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2383 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002385 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002386 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2387 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2388 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002389 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002390 case ISD::FCOPYSIGN:
2391 V1.copySign(V2);
2392 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002393 default: break;
2394 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002395 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002396 }
Chris Lattner62b57722006-04-20 05:39:12 +00002397
2398 // Canonicalize an UNDEF to the RHS, even over a constant.
2399 if (N1.getOpcode() == ISD::UNDEF) {
2400 if (isCommutativeBinOp(Opcode)) {
2401 std::swap(N1, N2);
2402 } else {
2403 switch (Opcode) {
2404 case ISD::FP_ROUND_INREG:
2405 case ISD::SIGN_EXTEND_INREG:
2406 case ISD::SUB:
2407 case ISD::FSUB:
2408 case ISD::FDIV:
2409 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002410 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002411 return N1; // fold op(undef, arg2) -> undef
2412 case ISD::UDIV:
2413 case ISD::SDIV:
2414 case ISD::UREM:
2415 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002416 case ISD::SRL:
2417 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002418 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002419 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2420 // For vectors, we can't easily build an all zero vector, just return
2421 // the LHS.
2422 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002423 }
2424 }
2425 }
2426
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002427 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002428 if (N2.getOpcode() == ISD::UNDEF) {
2429 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002430 case ISD::XOR:
2431 if (N1.getOpcode() == ISD::UNDEF)
2432 // Handle undef ^ undef -> 0 special case. This is a common
2433 // idiom (misuse).
2434 return getConstant(0, VT);
2435 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002436 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002437 case ISD::ADDC:
2438 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002439 case ISD::SUB:
2440 case ISD::FADD:
2441 case ISD::FSUB:
2442 case ISD::FMUL:
2443 case ISD::FDIV:
2444 case ISD::FREM:
2445 case ISD::UDIV:
2446 case ISD::SDIV:
2447 case ISD::UREM:
2448 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002449 return N2; // fold op(arg1, undef) -> undef
2450 case ISD::MUL:
2451 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002452 case ISD::SRL:
2453 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002454 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002455 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2456 // For vectors, we can't easily build an all zero vector, just return
2457 // the LHS.
2458 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002459 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002460 if (!VT.isVector())
2461 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002462 // For vectors, we can't easily build an all one vector, just return
2463 // the LHS.
2464 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002465 case ISD::SRA:
2466 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002467 }
2468 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002469
Chris Lattner27e9b412005-05-11 18:57:39 +00002470 // Memoize this node if possible.
2471 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002472 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002473 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002474 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002475 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002476 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002477 void *IP = 0;
2478 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2479 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002480 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002481 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002482 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002483 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002484 }
2485
Chris Lattnerc3aae252005-01-07 07:46:32 +00002486 AllNodes.push_back(N);
2487 return SDOperand(N, 0);
2488}
2489
Duncan Sands83ec4b62008-06-06 12:08:01 +00002490SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002491 SDOperand N1, SDOperand N2, SDOperand N3) {
2492 // Perform various simplifications.
2493 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2494 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002495 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002496 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002497 // Use FoldSetCC to simplify SETCC's.
2498 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002499 if (Simp.Val) return Simp;
2500 break;
2501 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002502 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002503 if (N1C) {
2504 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002505 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002506 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002507 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002508 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002509
2510 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002511 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002512 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002513 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002514 if (N2C->getValue()) // Unconditional branch
2515 return getNode(ISD::BR, MVT::Other, N1, N3);
2516 else
2517 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002518 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002519 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002520 case ISD::VECTOR_SHUFFLE:
2521 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002522 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002523 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002524 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002525 "Illegal VECTOR_SHUFFLE node!");
2526 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002527 case ISD::BIT_CONVERT:
2528 // Fold bit_convert nodes from a type to themselves.
2529 if (N1.getValueType() == VT)
2530 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002531 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002532 }
2533
Chris Lattner43247a12005-08-25 19:12:10 +00002534 // Memoize node if it doesn't produce a flag.
2535 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002536 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002537 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002538 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002539 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002540 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002541 void *IP = 0;
2542 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2543 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002544 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002545 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002546 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002547 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002548 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002549 AllNodes.push_back(N);
2550 return SDOperand(N, 0);
2551}
2552
Duncan Sands83ec4b62008-06-06 12:08:01 +00002553SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002554 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002555 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002556 SDOperand Ops[] = { N1, N2, N3, N4 };
2557 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002558}
2559
Duncan Sands83ec4b62008-06-06 12:08:01 +00002560SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002561 SDOperand N1, SDOperand N2, SDOperand N3,
2562 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002563 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2564 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002565}
2566
Dan Gohman707e0182008-04-12 04:36:06 +00002567/// getMemsetValue - Vectorized representation of the memset value
2568/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2570 unsigned NumBits = VT.isVector() ?
2571 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002572 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002573 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002574 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002575 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002576 Val = (Val << Shift) | Val;
2577 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002578 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002579 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002580 return DAG.getConstant(Val, VT);
2581 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002582 }
Evan Chengf0df0312008-05-15 08:39:06 +00002583
2584 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2585 unsigned Shift = 8;
2586 for (unsigned i = NumBits; i > 8; i >>= 1) {
2587 Value = DAG.getNode(ISD::OR, VT,
2588 DAG.getNode(ISD::SHL, VT, Value,
2589 DAG.getConstant(Shift, MVT::i8)), Value);
2590 Shift <<= 1;
2591 }
2592
2593 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002594}
2595
Dan Gohman707e0182008-04-12 04:36:06 +00002596/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2597/// used when a memcpy is turned into a memset when the source is a constant
2598/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002599static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002600 const TargetLowering &TLI,
2601 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002602 // Handle vector with all elements zero.
2603 if (Str.empty()) {
2604 if (VT.isInteger())
2605 return DAG.getConstant(0, VT);
2606 unsigned NumElts = VT.getVectorNumElements();
2607 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2608 return DAG.getNode(ISD::BIT_CONVERT, VT,
2609 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2610 }
2611
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 assert(!VT.isVector() && "Can't handle vector type here!");
2613 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002614 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002615 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002616 if (TLI.isLittleEndian())
2617 Offset = Offset + MSB - 1;
2618 for (unsigned i = 0; i != MSB; ++i) {
2619 Val = (Val << 8) | (unsigned char)Str[Offset];
2620 Offset += TLI.isLittleEndian() ? -1 : 1;
2621 }
2622 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002623}
2624
Dan Gohman707e0182008-04-12 04:36:06 +00002625/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002626///
Dan Gohman707e0182008-04-12 04:36:06 +00002627static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2628 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002630 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2631}
2632
Evan Chengf0df0312008-05-15 08:39:06 +00002633/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2634///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002635static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002636 unsigned SrcDelta = 0;
2637 GlobalAddressSDNode *G = NULL;
2638 if (Src.getOpcode() == ISD::GlobalAddress)
2639 G = cast<GlobalAddressSDNode>(Src);
2640 else if (Src.getOpcode() == ISD::ADD &&
2641 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2642 Src.getOperand(1).getOpcode() == ISD::Constant) {
2643 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2644 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2645 }
2646 if (!G)
2647 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002648
Evan Chengf0df0312008-05-15 08:39:06 +00002649 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002650 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2651 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002652
Evan Chengf0df0312008-05-15 08:39:06 +00002653 return false;
2654}
Dan Gohman707e0182008-04-12 04:36:06 +00002655
Evan Chengf0df0312008-05-15 08:39:06 +00002656/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2657/// to replace the memset / memcpy is below the threshold. It also returns the
2658/// types of the sequence of memory ops to perform memset / memcpy.
2659static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002660bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002661 SDOperand Dst, SDOperand Src,
2662 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002663 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002664 SelectionDAG &DAG,
2665 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002666 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002667 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002668 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002669 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002670 if (VT != MVT::iAny) {
2671 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002672 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002673 // If source is a string constant, this will require an unaligned load.
2674 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2675 if (Dst.getOpcode() != ISD::FrameIndex) {
2676 // Can't change destination alignment. It requires a unaligned store.
2677 if (AllowUnalign)
2678 VT = MVT::iAny;
2679 } else {
2680 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2681 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2682 if (MFI->isFixedObjectIndex(FI)) {
2683 // Can't change destination alignment. It requires a unaligned store.
2684 if (AllowUnalign)
2685 VT = MVT::iAny;
2686 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002687 // Give the stack frame object a larger alignment if needed.
2688 if (MFI->getObjectAlignment(FI) < NewAlign)
2689 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002690 Align = NewAlign;
2691 }
2692 }
2693 }
2694 }
2695
2696 if (VT == MVT::iAny) {
2697 if (AllowUnalign) {
2698 VT = MVT::i64;
2699 } else {
2700 switch (Align & 7) {
2701 case 0: VT = MVT::i64; break;
2702 case 4: VT = MVT::i32; break;
2703 case 2: VT = MVT::i16; break;
2704 default: VT = MVT::i8; break;
2705 }
2706 }
2707
Duncan Sands83ec4b62008-06-06 12:08:01 +00002708 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002709 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002710 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2711 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002712
Duncan Sands8e4eb092008-06-08 20:54:56 +00002713 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002714 VT = LVT;
2715 }
Dan Gohman707e0182008-04-12 04:36:06 +00002716
2717 unsigned NumMemOps = 0;
2718 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002719 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002720 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002721 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002722 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002723 VT = MVT::i64;
2724 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002725 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2726 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002727 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002728 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002729 VTSize >>= 1;
2730 }
Dan Gohman707e0182008-04-12 04:36:06 +00002731 }
Dan Gohman707e0182008-04-12 04:36:06 +00002732
2733 if (++NumMemOps > Limit)
2734 return false;
2735 MemOps.push_back(VT);
2736 Size -= VTSize;
2737 }
2738
2739 return true;
2740}
2741
2742static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2743 SDOperand Chain, SDOperand Dst,
2744 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002745 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002746 const Value *DstSV, uint64_t DstSVOff,
2747 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002748 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2749
Dan Gohman21323f32008-05-29 19:42:22 +00002750 // Expand memcpy to a series of load and store ops if the size operand falls
2751 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002753 uint64_t Limit = -1;
2754 if (!AlwaysInline)
2755 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002756 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002757 std::string Str;
2758 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002759 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002760 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002761 return SDOperand();
2762
Dan Gohman707e0182008-04-12 04:36:06 +00002763
Evan Cheng0ff39b32008-06-30 07:31:25 +00002764 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002765 SmallVector<SDOperand, 8> OutChains;
2766 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002767 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002768 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002769 MVT VT = MemOps[i];
2770 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002771 SDOperand Value, Store;
2772
Evan Cheng0ff39b32008-06-30 07:31:25 +00002773 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002774 // It's unlikely a store of a vector immediate can be done in a single
2775 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002776 // We also handle store a vector with all zero's.
2777 // FIXME: Handle other cases where store of vector immediate is done in
2778 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002779 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002780 Store = DAG.getStore(Chain, Value,
2781 getMemBasePlusOffset(Dst, DstOff, DAG),
2782 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002783 } else {
2784 Value = DAG.getLoad(VT, Chain,
2785 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002786 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002787 Store = DAG.getStore(Chain, Value,
2788 getMemBasePlusOffset(Dst, DstOff, DAG),
2789 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002790 }
2791 OutChains.push_back(Store);
2792 SrcOff += VTSize;
2793 DstOff += VTSize;
2794 }
2795
2796 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2797 &OutChains[0], OutChains.size());
2798}
2799
Dan Gohman21323f32008-05-29 19:42:22 +00002800static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2801 SDOperand Chain, SDOperand Dst,
2802 SDOperand Src, uint64_t Size,
2803 unsigned Align, bool AlwaysInline,
2804 const Value *DstSV, uint64_t DstSVOff,
2805 const Value *SrcSV, uint64_t SrcSVOff){
2806 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2807
2808 // Expand memmove to a series of load and store ops if the size operand falls
2809 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002810 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002811 uint64_t Limit = -1;
2812 if (!AlwaysInline)
2813 Limit = TLI.getMaxStoresPerMemmove();
2814 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002815 std::string Str;
2816 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002817 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002818 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002819 return SDOperand();
2820
Dan Gohman21323f32008-05-29 19:42:22 +00002821 uint64_t SrcOff = 0, DstOff = 0;
2822
2823 SmallVector<SDOperand, 8> LoadValues;
2824 SmallVector<SDOperand, 8> LoadChains;
2825 SmallVector<SDOperand, 8> OutChains;
2826 unsigned NumMemOps = MemOps.size();
2827 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 MVT VT = MemOps[i];
2829 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002830 SDOperand Value, Store;
2831
2832 Value = DAG.getLoad(VT, Chain,
2833 getMemBasePlusOffset(Src, SrcOff, DAG),
2834 SrcSV, SrcSVOff + SrcOff, false, Align);
2835 LoadValues.push_back(Value);
2836 LoadChains.push_back(Value.getValue(1));
2837 SrcOff += VTSize;
2838 }
2839 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2840 &LoadChains[0], LoadChains.size());
2841 OutChains.clear();
2842 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 MVT VT = MemOps[i];
2844 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002845 SDOperand Value, Store;
2846
2847 Store = DAG.getStore(Chain, LoadValues[i],
2848 getMemBasePlusOffset(Dst, DstOff, DAG),
2849 DstSV, DstSVOff + DstOff, false, DstAlign);
2850 OutChains.push_back(Store);
2851 DstOff += VTSize;
2852 }
2853
2854 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2855 &OutChains[0], OutChains.size());
2856}
2857
Dan Gohman707e0182008-04-12 04:36:06 +00002858static SDOperand getMemsetStores(SelectionDAG &DAG,
2859 SDOperand Chain, SDOperand Dst,
2860 SDOperand Src, uint64_t Size,
2861 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002862 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002863 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2864
2865 // Expand memset to a series of load/store ops if the size operand
2866 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002868 std::string Str;
2869 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002870 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002871 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002872 return SDOperand();
2873
2874 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002875 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002876
2877 unsigned NumMemOps = MemOps.size();
2878 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002879 MVT VT = MemOps[i];
2880 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002881 SDOperand Value = getMemsetValue(Src, VT, DAG);
2882 SDOperand Store = DAG.getStore(Chain, Value,
2883 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002884 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002885 OutChains.push_back(Store);
2886 DstOff += VTSize;
2887 }
2888
2889 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2890 &OutChains[0], OutChains.size());
2891}
2892
2893SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002894 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002895 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002896 const Value *DstSV, uint64_t DstSVOff,
2897 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002898
2899 // Check to see if we should lower the memcpy to loads and stores first.
2900 // For cases within the target-specified limits, this is the best choice.
2901 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2902 if (ConstantSize) {
2903 // Memcpy with size zero? Just return the original chain.
2904 if (ConstantSize->isNullValue())
2905 return Chain;
2906
2907 SDOperand Result =
2908 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002909 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002910 if (Result.Val)
2911 return Result;
2912 }
2913
2914 // Then check to see if we should lower the memcpy with target-specific
2915 // code. If the target chooses to do this, this is the next best.
2916 SDOperand Result =
2917 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2918 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002919 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002920 if (Result.Val)
2921 return Result;
2922
2923 // If we really need inline code and the target declined to provide it,
2924 // use a (potentially long) sequence of loads and stores.
2925 if (AlwaysInline) {
2926 assert(ConstantSize && "AlwaysInline requires a constant size!");
2927 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2928 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002929 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002930 }
2931
2932 // Emit a library call.
2933 TargetLowering::ArgListTy Args;
2934 TargetLowering::ArgListEntry Entry;
2935 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2936 Entry.Node = Dst; Args.push_back(Entry);
2937 Entry.Node = Src; Args.push_back(Entry);
2938 Entry.Node = Size; Args.push_back(Entry);
2939 std::pair<SDOperand,SDOperand> CallResult =
2940 TLI.LowerCallTo(Chain, Type::VoidTy,
2941 false, false, false, CallingConv::C, false,
2942 getExternalSymbol("memcpy", TLI.getPointerTy()),
2943 Args, *this);
2944 return CallResult.second;
2945}
2946
2947SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2948 SDOperand Src, SDOperand Size,
2949 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002950 const Value *DstSV, uint64_t DstSVOff,
2951 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002952
Dan Gohman21323f32008-05-29 19:42:22 +00002953 // Check to see if we should lower the memmove to loads and stores first.
2954 // For cases within the target-specified limits, this is the best choice.
2955 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2956 if (ConstantSize) {
2957 // Memmove with size zero? Just return the original chain.
2958 if (ConstantSize->isNullValue())
2959 return Chain;
2960
2961 SDOperand Result =
2962 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2963 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2964 if (Result.Val)
2965 return Result;
2966 }
Dan Gohman707e0182008-04-12 04:36:06 +00002967
2968 // Then check to see if we should lower the memmove with target-specific
2969 // code. If the target chooses to do this, this is the next best.
2970 SDOperand Result =
2971 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002972 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002973 if (Result.Val)
2974 return Result;
2975
2976 // Emit a library call.
2977 TargetLowering::ArgListTy Args;
2978 TargetLowering::ArgListEntry Entry;
2979 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2980 Entry.Node = Dst; Args.push_back(Entry);
2981 Entry.Node = Src; Args.push_back(Entry);
2982 Entry.Node = Size; Args.push_back(Entry);
2983 std::pair<SDOperand,SDOperand> CallResult =
2984 TLI.LowerCallTo(Chain, Type::VoidTy,
2985 false, false, false, CallingConv::C, false,
2986 getExternalSymbol("memmove", TLI.getPointerTy()),
2987 Args, *this);
2988 return CallResult.second;
2989}
2990
2991SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2992 SDOperand Src, SDOperand Size,
2993 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002994 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002995
2996 // Check to see if we should lower the memset to stores first.
2997 // For cases within the target-specified limits, this is the best choice.
2998 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2999 if (ConstantSize) {
3000 // Memset with size zero? Just return the original chain.
3001 if (ConstantSize->isNullValue())
3002 return Chain;
3003
3004 SDOperand Result =
3005 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003006 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003007 if (Result.Val)
3008 return Result;
3009 }
3010
3011 // Then check to see if we should lower the memset with target-specific
3012 // code. If the target chooses to do this, this is the next best.
3013 SDOperand Result =
3014 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003015 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003016 if (Result.Val)
3017 return Result;
3018
3019 // Emit a library call.
3020 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3021 TargetLowering::ArgListTy Args;
3022 TargetLowering::ArgListEntry Entry;
3023 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3024 Args.push_back(Entry);
3025 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003026 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003027 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3028 else
3029 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3030 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3031 Args.push_back(Entry);
3032 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3033 Args.push_back(Entry);
3034 std::pair<SDOperand,SDOperand> CallResult =
3035 TLI.LowerCallTo(Chain, Type::VoidTy,
3036 false, false, false, CallingConv::C, false,
3037 getExternalSymbol("memset", TLI.getPointerTy()),
3038 Args, *this);
3039 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003040}
3041
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003042SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003043 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003044 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003045 unsigned Alignment) {
3046 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003047 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3048 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003049 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003050 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003051 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003052 void* IP = 0;
3053 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3054 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003055 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003056 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003057 CSEMap.InsertNode(N, IP);
3058 AllNodes.push_back(N);
3059 return SDOperand(N, 0);
3060}
3061
3062SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003063 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003064 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003065 unsigned Alignment) {
3066 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003067 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3068 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003069 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003070 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3071 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003072 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003073 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003074 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003075 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003076 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003077 void* IP = 0;
3078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3079 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003080 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003081 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003082 CSEMap.InsertNode(N, IP);
3083 AllNodes.push_back(N);
3084 return SDOperand(N, 0);
3085}
3086
Duncan Sands4bdcb612008-07-02 17:40:58 +00003087/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3088/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003089SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003090 bool Simplify) {
3091 if (Simplify && NumOps == 1)
3092 return Ops[0];
3093
3094 SmallVector<MVT, 4> VTs;
3095 VTs.reserve(NumOps);
3096 for (unsigned i = 0; i < NumOps; ++i)
3097 VTs.push_back(Ops[i].getValueType());
3098 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3099}
3100
Duncan Sands14ea39c2008-03-27 20:23:40 +00003101SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003102SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003104 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003105 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003106 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003107 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3108 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003109 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003111 } else if (SV) {
3112 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3113 assert(PT && "Value for load must be a pointer");
3114 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003115 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003116 assert(Ty && "Could not get type information for load");
3117 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3118 }
Evan Cheng466685d2006-10-09 20:57:25 +00003119
Duncan Sands14ea39c2008-03-27 20:23:40 +00003120 if (VT == EVT) {
3121 ExtType = ISD::NON_EXTLOAD;
3122 } else if (ExtType == ISD::NON_EXTLOAD) {
3123 assert(VT == EVT && "Non-extending load from different memory type!");
3124 } else {
3125 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003126 if (VT.isVector())
3127 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003128 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003129 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003130 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003131 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003132 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003133 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003134 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003135 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003136
3137 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003138 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003139 "Unindexed load with an offset!");
3140
3141 SDVTList VTs = Indexed ?
3142 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3143 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003144 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003145 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003146 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003147 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003148 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003149 ID.AddInteger(Alignment);
3150 ID.AddInteger(isVolatile);
3151 void *IP = 0;
3152 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3153 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003154 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3155 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003156 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003157 AllNodes.push_back(N);
3158 return SDOperand(N, 0);
3159}
3160
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003162 SDOperand Chain, SDOperand Ptr,
3163 const Value *SV, int SVOffset,
3164 bool isVolatile, unsigned Alignment) {
3165 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003166 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3167 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003168}
3169
Duncan Sands83ec4b62008-06-06 12:08:01 +00003170SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003171 SDOperand Chain, SDOperand Ptr,
3172 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003173 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003174 bool isVolatile, unsigned Alignment) {
3175 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003176 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3177 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003178}
3179
Evan Cheng144d8f02006-11-09 17:55:04 +00003180SDOperand
3181SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3182 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003183 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003184 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3185 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003186 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3187 LD->getChain(), Base, Offset, LD->getSrcValue(),
3188 LD->getSrcValueOffset(), LD->getMemoryVT(),
3189 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003190}
3191
Jeff Cohend41b30d2006-11-05 19:31:28 +00003192SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003193 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003194 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003195 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003196
Dan Gohman575e2f42007-06-04 15:49:41 +00003197 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3198 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003199 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003200 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003201 } else if (SV) {
3202 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3203 assert(PT && "Value for store must be a pointer");
3204 Ty = PT->getElementType();
3205 }
3206 assert(Ty && "Could not get type information for store");
3207 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3208 }
Evan Chengad071e12006-10-05 22:57:11 +00003209 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003210 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003211 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003212 FoldingSetNodeID ID;
3213 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003214 ID.AddInteger(ISD::UNINDEXED);
3215 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003216 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003217 ID.AddInteger(Alignment);
3218 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003219 void *IP = 0;
3220 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3221 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003222 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003223 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003224 CSEMap.InsertNode(N, IP);
3225 AllNodes.push_back(N);
3226 return SDOperand(N, 0);
3227}
3228
Jeff Cohend41b30d2006-11-05 19:31:28 +00003229SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003230 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003232 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003233 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003234
3235 if (VT == SVT)
3236 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003237
Duncan Sands8e4eb092008-06-08 20:54:56 +00003238 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003239 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003240 "Can't do FP-INT conversion!");
3241
Dan Gohman575e2f42007-06-04 15:49:41 +00003242 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3243 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003244 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003245 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003246 } else if (SV) {
3247 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3248 assert(PT && "Value for store must be a pointer");
3249 Ty = PT->getElementType();
3250 }
3251 assert(Ty && "Could not get type information for store");
3252 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3253 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003254 SDVTList VTs = getVTList(MVT::Other);
3255 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003256 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003257 FoldingSetNodeID ID;
3258 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003259 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003260 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003261 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003262 ID.AddInteger(Alignment);
3263 ID.AddInteger(isVolatile);
3264 void *IP = 0;
3265 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3266 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003267 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003268 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003269 CSEMap.InsertNode(N, IP);
3270 AllNodes.push_back(N);
3271 return SDOperand(N, 0);
3272}
3273
Evan Cheng144d8f02006-11-09 17:55:04 +00003274SDOperand
3275SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3276 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003277 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3278 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3279 "Store is already a indexed store!");
3280 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3281 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3282 FoldingSetNodeID ID;
3283 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3284 ID.AddInteger(AM);
3285 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003286 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003287 ID.AddInteger(ST->getAlignment());
3288 ID.AddInteger(ST->isVolatile());
3289 void *IP = 0;
3290 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3291 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003292 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003293 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003294 ST->getSrcValue(), ST->getSrcValueOffset(),
3295 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003296 CSEMap.InsertNode(N, IP);
3297 AllNodes.push_back(N);
3298 return SDOperand(N, 0);
3299}
3300
Duncan Sands83ec4b62008-06-06 12:08:01 +00003301SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003302 SDOperand Chain, SDOperand Ptr,
3303 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003304 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003305 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003306}
3307
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003309 const SDUse *Ops, unsigned NumOps) {
3310 switch (NumOps) {
3311 case 0: return getNode(Opcode, VT);
3312 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3313 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3314 Ops[1].getSDOperand());
3315 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3316 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3317 default: break;
3318 }
3319
3320 // Copy from an SDUse array into an SDOperand array for use with
3321 // the regular getNode logic.
3322 SmallVector<SDOperand, 8> NewOps;
3323 NewOps.reserve(NumOps);
3324 for (unsigned i = 0; i != NumOps; ++i)
3325 NewOps.push_back(Ops[i].getSDOperand());
3326 return getNode(Opcode, VT, Ops, NumOps);
3327}
3328
3329SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3330 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003331 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003332 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003333 case 1: return getNode(Opcode, VT, Ops[0]);
3334 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3335 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003336 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003337 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003338
Chris Lattneref847df2005-04-09 03:27:28 +00003339 switch (Opcode) {
3340 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003341 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003342 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003343 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3344 "LHS and RHS of condition must have same type!");
3345 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3346 "True and False arms of SelectCC must have same type!");
3347 assert(Ops[2].getValueType() == VT &&
3348 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003349 break;
3350 }
3351 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003352 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003353 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3354 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003355 break;
3356 }
Chris Lattneref847df2005-04-09 03:27:28 +00003357 }
3358
Chris Lattner385328c2005-05-14 07:42:29 +00003359 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003360 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003361 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003362 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003363 FoldingSetNodeID ID;
3364 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003365 void *IP = 0;
3366 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3367 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003368 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003369 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003370 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003371 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003372 }
Chris Lattneref847df2005-04-09 03:27:28 +00003373 AllNodes.push_back(N);
3374 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003375}
3376
Chris Lattner89c34632005-05-14 06:20:26 +00003377SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003378 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003379 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003380 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3381 Ops, NumOps);
3382}
3383
3384SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003385 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003386 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003387 if (NumVTs == 1)
3388 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003389 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3390}
3391
3392SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003393 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003394 if (VTList.NumVTs == 1)
3395 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003396
Chris Lattner5f056bf2005-07-10 01:55:33 +00003397 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003398 // FIXME: figure out how to safely handle things like
3399 // int foo(int x) { return 1 << (x & 255); }
3400 // int bar() { return foo(256); }
3401#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003402 case ISD::SRA_PARTS:
3403 case ISD::SRL_PARTS:
3404 case ISD::SHL_PARTS:
3405 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003406 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003407 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3408 else if (N3.getOpcode() == ISD::AND)
3409 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3410 // If the and is only masking out bits that cannot effect the shift,
3411 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003413 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3414 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3415 }
3416 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003417#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003418 }
Chris Lattner89c34632005-05-14 06:20:26 +00003419
Chris Lattner43247a12005-08-25 19:12:10 +00003420 // Memoize the node unless it returns a flag.
3421 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003422 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003423 FoldingSetNodeID ID;
3424 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003425 void *IP = 0;
3426 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3427 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003428 if (NumOps == 1)
3429 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3430 else if (NumOps == 2)
3431 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3432 else if (NumOps == 3)
3433 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3434 else
3435 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003436 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003437 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003438 if (NumOps == 1)
3439 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3440 else if (NumOps == 2)
3441 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3442 else if (NumOps == 3)
3443 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3444 else
3445 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003446 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003447 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003448 return SDOperand(N, 0);
3449}
3450
Dan Gohman08ce9762007-10-08 15:49:58 +00003451SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003452 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003453}
3454
3455SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3456 SDOperand N1) {
3457 SDOperand Ops[] = { N1 };
3458 return getNode(Opcode, VTList, Ops, 1);
3459}
3460
3461SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3462 SDOperand N1, SDOperand N2) {
3463 SDOperand Ops[] = { N1, N2 };
3464 return getNode(Opcode, VTList, Ops, 2);
3465}
3466
3467SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3468 SDOperand N1, SDOperand N2, SDOperand N3) {
3469 SDOperand Ops[] = { N1, N2, N3 };
3470 return getNode(Opcode, VTList, Ops, 3);
3471}
3472
3473SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3474 SDOperand N1, SDOperand N2, SDOperand N3,
3475 SDOperand N4) {
3476 SDOperand Ops[] = { N1, N2, N3, N4 };
3477 return getNode(Opcode, VTList, Ops, 4);
3478}
3479
3480SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3481 SDOperand N1, SDOperand N2, SDOperand N3,
3482 SDOperand N4, SDOperand N5) {
3483 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3484 return getNode(Opcode, VTList, Ops, 5);
3485}
3486
Duncan Sands83ec4b62008-06-06 12:08:01 +00003487SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003488 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003489}
3490
Duncan Sands83ec4b62008-06-06 12:08:01 +00003491SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3492 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003493 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003494 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003495 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003496 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003498 V.push_back(VT1);
3499 V.push_back(VT2);
3500 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003501 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003502}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003503SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3504 MVT VT3) {
3505 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003506 E = VTList.end(); I != E; ++I) {
3507 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3508 (*I)[2] == VT3)
3509 return makeVTList(&(*I)[0], 3);
3510 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003511 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003512 V.push_back(VT1);
3513 V.push_back(VT2);
3514 V.push_back(VT3);
3515 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003516 return makeVTList(&(*VTList.begin())[0], 3);
3517}
3518
Duncan Sands83ec4b62008-06-06 12:08:01 +00003519SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003520 switch (NumVTs) {
3521 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003522 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003523 case 2: return getVTList(VTs[0], VTs[1]);
3524 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3525 default: break;
3526 }
3527
Duncan Sands83ec4b62008-06-06 12:08:01 +00003528 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003529 E = VTList.end(); I != E; ++I) {
3530 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3531
3532 bool NoMatch = false;
3533 for (unsigned i = 2; i != NumVTs; ++i)
3534 if (VTs[i] != (*I)[i]) {
3535 NoMatch = true;
3536 break;
3537 }
3538 if (!NoMatch)
3539 return makeVTList(&*I->begin(), NumVTs);
3540 }
3541
Duncan Sands83ec4b62008-06-06 12:08:01 +00003542 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003543 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003544}
3545
3546
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003547/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3548/// specified operands. If the resultant node already exists in the DAG,
3549/// this does not modify the specified node, instead it returns the node that
3550/// already exists. If the resultant node does not exist in the DAG, the
3551/// input node is returned. As a degenerate case, if you specify the same
3552/// input operands as the node already has, the input node is returned.
3553SDOperand SelectionDAG::
3554UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3555 SDNode *N = InN.Val;
3556 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3557
3558 // Check to see if there is no change.
3559 if (Op == N->getOperand(0)) return InN;
3560
3561 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003562 void *InsertPos = 0;
3563 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3564 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003565
3566 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003567 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003568 RemoveNodeFromCSEMaps(N);
3569
3570 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003571 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003572 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003573 N->OperandList[0].setUser(N);
3574 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003575
3576 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003577 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003578 return InN;
3579}
3580
3581SDOperand SelectionDAG::
3582UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3583 SDNode *N = InN.Val;
3584 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3585
3586 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003587 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3588 return InN; // No operands changed, just return the input node.
3589
3590 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003591 void *InsertPos = 0;
3592 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3593 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003594
3595 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003596 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003597 RemoveNodeFromCSEMaps(N);
3598
3599 // Now we update the operands.
3600 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003601 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003602 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003603 N->OperandList[0].setUser(N);
3604 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003605 }
3606 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003607 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003608 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003609 N->OperandList[1].setUser(N);
3610 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003611 }
3612
3613 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003614 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003615 return InN;
3616}
3617
3618SDOperand SelectionDAG::
3619UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003620 SDOperand Ops[] = { Op1, Op2, Op3 };
3621 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003622}
3623
3624SDOperand SelectionDAG::
3625UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3626 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003627 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3628 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003629}
3630
3631SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003632UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3633 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003634 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3635 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003636}
3637
Chris Lattner809ec112006-01-28 10:09:25 +00003638SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003639UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003640 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003641 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003642 "Update with wrong number of operands");
3643
3644 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003645 bool AnyChange = false;
3646 for (unsigned i = 0; i != NumOps; ++i) {
3647 if (Ops[i] != N->getOperand(i)) {
3648 AnyChange = true;
3649 break;
3650 }
3651 }
3652
3653 // No operands changed, just return the input node.
3654 if (!AnyChange) return InN;
3655
3656 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003657 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003658 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003659 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003660
Dan Gohman7ceda162008-05-02 00:05:03 +00003661 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003662 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003663 RemoveNodeFromCSEMaps(N);
3664
3665 // Now we update the operands.
3666 for (unsigned i = 0; i != NumOps; ++i) {
3667 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003668 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003669 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003670 N->OperandList[i].setUser(N);
3671 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003672 }
3673 }
3674
3675 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003676 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677 return InN;
3678}
3679
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003680/// MorphNodeTo - This frees the operands of the current node, resets the
3681/// opcode, types, and operands to the specified value. This should only be
3682/// used by the SelectionDAG class.
3683void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003684 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003685 NodeType = Opc;
3686 ValueList = L.VTs;
3687 NumValues = L.NumVTs;
3688
3689 // Clear the operands list, updating used nodes to remove this from their
3690 // use list.
3691 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003692 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003693
3694 // If NumOps is larger than the # of operands we currently have, reallocate
3695 // the operand list.
3696 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003697 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003698 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003699 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003700 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003701 OperandsNeedDelete = true;
3702 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003703
3704 // Assign the new operands.
3705 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003706
3707 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3708 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003709 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003710 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003711 N->addUser(i, this);
3712 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003713 }
3714}
Chris Lattner149c58c2005-08-16 18:17:10 +00003715
3716/// SelectNodeTo - These are used for target selectors to *mutate* the
3717/// specified node to have the specified return type, Target opcode, and
3718/// operands. Note that target opcodes are stored as
3719/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003720///
3721/// Note that SelectNodeTo returns the resultant node. If there is already a
3722/// node of the specified opcode and operands, it returns that node instead of
3723/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003724SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003725 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003726 SDVTList VTs = getVTList(VT);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003727 return SelectNodeTo(N, TargetOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003728}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003729
Evan Cheng95514ba2006-08-26 08:00:10 +00003730SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003731 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003732 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003733 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003734 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003735}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003736
Evan Cheng95514ba2006-08-26 08:00:10 +00003737SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003738 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003739 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003740 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003741 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003742 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003743}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003744
Evan Cheng95514ba2006-08-26 08:00:10 +00003745SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003746 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003747 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003748 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003749 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003750 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003751}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003752
Evan Cheng95514ba2006-08-26 08:00:10 +00003753SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003754 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003755 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003756 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003757 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3758}
3759
3760SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003761 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003762 unsigned NumOps) {
3763 SDVTList VTs = getVTList(VT1, VT2);
3764 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3765}
3766
3767SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3768 MVT VT1, MVT VT2) {
3769 SDVTList VTs = getVTList(VT1, VT2);
3770 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3771}
3772
3773SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003774 MVT VT1, MVT VT2, MVT VT3,
3775 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003776 SDVTList VTs = getVTList(VT1, VT2, VT3);
3777 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3778}
3779
3780SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3781 MVT VT1, MVT VT2,
3782 SDOperand Op1) {
3783 SDVTList VTs = getVTList(VT1, VT2);
3784 SDOperand Ops[] = { Op1 };
3785 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003786}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003787
Evan Cheng95514ba2006-08-26 08:00:10 +00003788SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003790 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003791 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003792 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003793 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003794}
3795
Evan Cheng95514ba2006-08-26 08:00:10 +00003796SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003797 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003798 SDOperand Op1, SDOperand Op2,
3799 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003800 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003801 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003802 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3803}
3804
3805SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003806 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003807 unsigned NumOps) {
3808 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003809 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003810 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003811 void *IP = 0;
3812 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003813 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003814
Chris Lattner0fb094f2005-11-19 01:44:53 +00003815 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003816
Dan Gohmancd920d92008-07-02 23:23:19 +00003817 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003818 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003819 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003820}
3821
Chris Lattner0fb094f2005-11-19 01:44:53 +00003822
Evan Cheng6ae46c42006-02-09 07:15:23 +00003823/// getTargetNode - These are used for target selectors to create a new node
3824/// with specified return type(s), target opcode, and operands.
3825///
3826/// Note that getTargetNode returns the resultant node. If there is already a
3827/// node of the specified opcode and operands, it returns that node instead of
3828/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003830 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3831}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003833 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3834}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003836 SDOperand Op1, SDOperand Op2) {
3837 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3838}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003839SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003840 SDOperand Op1, SDOperand Op2,
3841 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003842 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3843}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003844SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003845 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003846 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003847}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003848SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3849 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003850 SDOperand Op;
3851 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3852}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003853SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3854 MVT VT2, SDOperand Op1) {
3855 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003856 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003857}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003858SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3859 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003860 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003862 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003863 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003864}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3866 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003867 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003868 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003869 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003870 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003871}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003873 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003874 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003875 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003876}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003878 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003879 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003880 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003881 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003882}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003883SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003884 SDOperand Op1, SDOperand Op2,
3885 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003886 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003887 SDOperand Ops[] = { Op1, Op2, Op3 };
3888 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3889}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003890SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003891 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003892 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003893 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003894}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003895SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3896 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003897 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003899 VTList.push_back(VT1);
3900 VTList.push_back(VT2);
3901 VTList.push_back(VT3);
3902 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003904 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3905}
Evan Cheng39305cf2007-10-05 01:10:49 +00003906SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003907 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003908 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003910 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3911 Ops, NumOps).Val;
3912}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003913
Evan Cheng08b11732008-03-22 01:55:50 +00003914/// getNodeIfExists - Get the specified node if it's already available, or
3915/// else return NULL.
3916SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003917 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003918 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3919 FoldingSetNodeID ID;
3920 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3921 void *IP = 0;
3922 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3923 return E;
3924 }
3925 return NULL;
3926}
3927
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003928
Evan Cheng99157a02006-08-07 22:13:29 +00003929/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003930/// This can cause recursive merging of nodes in the DAG.
3931///
Chris Lattner11d049c2008-02-03 03:35:22 +00003932/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003933///
Chris Lattner11d049c2008-02-03 03:35:22 +00003934void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003935 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003936 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003937 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003938 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003939 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003940
Chris Lattner8b8749f2005-08-17 19:00:20 +00003941 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003942 SDNode::use_iterator UI = From->use_begin();
3943 SDNode *U = UI->getUser();
3944
Chris Lattner8b8749f2005-08-17 19:00:20 +00003945 // This node is about to morph, remove its old self from the CSE maps.
3946 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003947 int operandNum = 0;
3948 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3949 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003950 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003951 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003952 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003953 I->setUser(U);
3954 To.Val->addUser(operandNum, U);
3955 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003956
3957 // Now that we have modified U, add it back to the CSE maps. If it already
3958 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003959 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003960 ReplaceAllUsesWith(U, Existing, UpdateListener);
3961 // U is now dead. Inform the listener if it exists and delete it.
3962 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003963 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003964 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003965 } else {
3966 // If the node doesn't already exist, we updated it. Inform a listener if
3967 // it exists.
3968 if (UpdateListener)
3969 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003970 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003971 }
3972}
3973
3974/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3975/// This can cause recursive merging of nodes in the DAG.
3976///
3977/// This version assumes From/To have matching types and numbers of result
3978/// values.
3979///
Chris Lattner1e111c72005-09-07 05:37:01 +00003980void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003981 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003982 assert(From != To && "Cannot replace uses of with self");
3983 assert(From->getNumValues() == To->getNumValues() &&
3984 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003985 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003986 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3987 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003988
3989 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003990 SDNode::use_iterator UI = From->use_begin();
3991 SDNode *U = UI->getUser();
3992
Chris Lattner8b52f212005-08-26 18:36:28 +00003993 // This node is about to morph, remove its old self from the CSE maps.
3994 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003995 int operandNum = 0;
3996 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3997 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003998 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003999 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004000 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004001 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004002 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004003
Chris Lattner8b8749f2005-08-17 19:00:20 +00004004 // Now that we have modified U, add it back to the CSE maps. If it already
4005 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004006 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004007 ReplaceAllUsesWith(U, Existing, UpdateListener);
4008 // U is now dead. Inform the listener if it exists and delete it.
4009 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004010 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004011 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004012 } else {
4013 // If the node doesn't already exist, we updated it. Inform a listener if
4014 // it exists.
4015 if (UpdateListener)
4016 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004017 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004018 }
4019}
4020
Chris Lattner8b52f212005-08-26 18:36:28 +00004021/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4022/// This can cause recursive merging of nodes in the DAG.
4023///
4024/// This version can replace From with any result values. To must match the
4025/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004026void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004027 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004028 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004029 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004030 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004031
4032 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004033 SDNode::use_iterator UI = From->use_begin();
4034 SDNode *U = UI->getUser();
4035
Chris Lattner7b2880c2005-08-24 22:44:39 +00004036 // This node is about to morph, remove its old self from the CSE maps.
4037 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004038 int operandNum = 0;
4039 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4040 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004041 if (I->getVal() == From) {
4042 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004043 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004044 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004045 I->setUser(U);
4046 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004047 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004048
Chris Lattner7b2880c2005-08-24 22:44:39 +00004049 // Now that we have modified U, add it back to the CSE maps. If it already
4050 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004051 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004052 ReplaceAllUsesWith(U, Existing, UpdateListener);
4053 // U is now dead. Inform the listener if it exists and delete it.
4054 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004055 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004056 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004057 } else {
4058 // If the node doesn't already exist, we updated it. Inform a listener if
4059 // it exists.
4060 if (UpdateListener)
4061 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004062 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004063 }
4064}
4065
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004066namespace {
4067 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4068 /// any deleted nodes from the set passed into its constructor and recursively
4069 /// notifies another update listener if specified.
4070 class ChainedSetUpdaterListener :
4071 public SelectionDAG::DAGUpdateListener {
4072 SmallSetVector<SDNode*, 16> &Set;
4073 SelectionDAG::DAGUpdateListener *Chain;
4074 public:
4075 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4076 SelectionDAG::DAGUpdateListener *chain)
4077 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004078
Duncan Sandsedfcf592008-06-11 11:42:12 +00004079 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004080 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004081 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082 }
4083 virtual void NodeUpdated(SDNode *N) {
4084 if (Chain) Chain->NodeUpdated(N);
4085 }
4086 };
4087}
4088
Chris Lattner012f2412006-02-17 21:58:01 +00004089/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4090/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004091/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004092void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004093 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004094 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004095
Chris Lattner012f2412006-02-17 21:58:01 +00004096 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004097 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004098 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004099 return;
4100 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004101
4102 if (From.use_empty()) return;
4103
Chris Lattnercf5640b2007-02-04 00:14:31 +00004104 // Get all of the users of From.Val. We want these in a nice,
4105 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004106 SmallSetVector<SDNode*, 16> Users;
4107 for (SDNode::use_iterator UI = From.Val->use_begin(),
4108 E = From.Val->use_end(); UI != E; ++UI) {
4109 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004110 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004111 }
Chris Lattner012f2412006-02-17 21:58:01 +00004112
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004113 // When one of the recursive merges deletes nodes from the graph, we need to
4114 // make sure that UpdateListener is notified *and* that the node is removed
4115 // from Users if present. CSUL does this.
4116 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004117
Chris Lattner012f2412006-02-17 21:58:01 +00004118 while (!Users.empty()) {
4119 // We know that this user uses some value of From. If it is the right
4120 // value, update it.
4121 SDNode *User = Users.back();
4122 Users.pop_back();
4123
Chris Lattner01d029b2007-10-15 06:10:22 +00004124 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004125 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004126 for (; Op != E; ++Op)
4127 if (*Op == From) break;
4128
4129 // If there are no matches, the user must use some other result of From.
4130 if (Op == E) continue;
4131
4132 // Okay, we know this user needs to be updated. Remove its old self
4133 // from the CSE maps.
4134 RemoveNodeFromCSEMaps(User);
4135
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004136 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004137 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004138 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004139 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004140 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004141 Op->setUser(User);
4142 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004143 }
4144 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004145
4146 // Now that we have modified User, add it back to the CSE maps. If it
4147 // already exists there, recursively merge the results together.
4148 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004149 if (!Existing) {
4150 if (UpdateListener) UpdateListener->NodeUpdated(User);
4151 continue; // Continue on to next user.
4152 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004153
4154 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004155 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004156 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004157 // can cause deletion of nodes that used the old value. To handle this, we
4158 // use CSUL to remove them from the Users set.
4159 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004160
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004161 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004162 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004163 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004164 }
4165}
4166
Evan Chenge6f35d82006-08-01 08:20:41 +00004167/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4168/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004169unsigned SelectionDAG::AssignNodeIds() {
4170 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004171 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4172 SDNode *N = I;
4173 N->setNodeId(Id++);
4174 }
4175 return Id;
4176}
4177
Evan Chenge6f35d82006-08-01 08:20:41 +00004178/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004179/// based on their topological order. It returns the maximum id and a vector
4180/// of the SDNodes* in assigned order by reference.
4181unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004182 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004183 std::vector<unsigned> InDegree(DAGSize);
4184 std::vector<SDNode*> Sources;
4185
4186 // Use a two pass approach to avoid using a std::map which is slow.
4187 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004188 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4189 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004190 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004191 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004192 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004193 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004194 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004195 }
4196
Evan Cheng99157a02006-08-07 22:13:29 +00004197 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004198 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004199 SDNode *N = Sources.back();
4200 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004201 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004202 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004203 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004204 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004205 if (Degree == 0)
4206 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004207 }
4208 }
4209
Evan Chengc384d6c2006-08-02 22:00:34 +00004210 // Second pass, assign the actual topological order as node ids.
4211 Id = 0;
4212 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4213 TI != TE; ++TI)
4214 (*TI)->setNodeId(Id++);
4215
4216 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004217}
4218
4219
Evan Cheng091cba12006-07-27 06:39:06 +00004220
Jim Laskey58b968b2005-08-17 20:08:02 +00004221//===----------------------------------------------------------------------===//
4222// SDNode Class
4223//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004224
Chris Lattner917d2c92006-07-19 00:00:37 +00004225// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004226void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004227void UnarySDNode::ANCHOR() {}
4228void BinarySDNode::ANCHOR() {}
4229void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004230void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004231void ConstantSDNode::ANCHOR() {}
4232void ConstantFPSDNode::ANCHOR() {}
4233void GlobalAddressSDNode::ANCHOR() {}
4234void FrameIndexSDNode::ANCHOR() {}
4235void JumpTableSDNode::ANCHOR() {}
4236void ConstantPoolSDNode::ANCHOR() {}
4237void BasicBlockSDNode::ANCHOR() {}
4238void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004239void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004240void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004241void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004242void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004243void ExternalSymbolSDNode::ANCHOR() {}
4244void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004245void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004246void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004247void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004248void LoadSDNode::ANCHOR() {}
4249void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004250void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004251
Chris Lattner48b85922007-02-04 02:41:42 +00004252HandleSDNode::~HandleSDNode() {
4253 SDVTList VTs = { 0, 0 };
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004254 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004255}
4256
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004257GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004258 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004259 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004260 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004261 // Thread Local
4262 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4263 // Non Thread Local
4264 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4265 getSDVTList(VT)), Offset(o) {
4266 TheGlobal = const_cast<GlobalValue*>(GA);
4267}
Chris Lattner48b85922007-02-04 02:41:42 +00004268
Dan Gohman36b5c132008-04-07 19:35:22 +00004269/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004270/// reference performed by this atomic.
4271MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004272 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004273 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4274 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4275
4276 // Check if the atomic references a frame index
4277 const FrameIndexSDNode *FI =
4278 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4279 if (!getSrcValue() && FI)
4280 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4281 FI->getIndex(), Size, getAlignment());
4282 else
4283 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4284 Size, getAlignment());
4285}
4286
4287/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004288/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004289MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004290 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004291 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004292 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4293 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004294 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004295
4296 // Check if the load references a frame index, and does not have
4297 // an SV attached.
4298 const FrameIndexSDNode *FI =
4299 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4300 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004301 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004302 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004303 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004304 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004305 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004306}
4307
Jim Laskey583bd472006-10-27 23:46:08 +00004308/// Profile - Gather unique data for the node.
4309///
4310void SDNode::Profile(FoldingSetNodeID &ID) {
4311 AddNodeIDNode(ID, this);
4312}
4313
Chris Lattnera3255112005-11-08 23:30:28 +00004314/// getValueTypeList - Return a pointer to the specified value type.
4315///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004316const MVT *SDNode::getValueTypeList(MVT VT) {
4317 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004318 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004319 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004320 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004321 static MVT VTs[MVT::LAST_VALUETYPE];
4322 VTs[VT.getSimpleVT()] = VT;
4323 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004324 }
Chris Lattnera3255112005-11-08 23:30:28 +00004325}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004326
Chris Lattner5c884562005-01-12 18:37:47 +00004327/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4328/// indicated value. This method ignores uses of other values defined by this
4329/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004330bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004331 assert(Value < getNumValues() && "Bad value!");
4332
4333 // If there is only one value, this is easy.
4334 if (getNumValues() == 1)
4335 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004336 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004337
Evan Cheng4ee62112006-02-05 06:29:23 +00004338 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004339
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004340 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004341
Roman Levensteindc1adac2008-04-07 10:06:32 +00004342 // TODO: Only iterate over uses of a given value of the node
4343 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4344 if (*UI == TheValue) {
4345 if (NUses == 0)
4346 return false;
4347 --NUses;
4348 }
Chris Lattner5c884562005-01-12 18:37:47 +00004349 }
4350
4351 // Found exactly the right number of uses?
4352 return NUses == 0;
4353}
4354
4355
Evan Cheng33d55952007-08-02 05:29:38 +00004356/// hasAnyUseOfValue - Return true if there are any use of the indicated
4357/// value. This method ignores uses of other values defined by this operation.
4358bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4359 assert(Value < getNumValues() && "Bad value!");
4360
Dan Gohman30359592008-01-29 13:02:09 +00004361 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004362
4363 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4364
4365 SmallPtrSet<SDNode*, 32> UsersHandled;
4366
Roman Levensteindc1adac2008-04-07 10:06:32 +00004367 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4368 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004369 if (User->getNumOperands() == 1 ||
4370 UsersHandled.insert(User)) // First time we've seen this?
4371 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4372 if (User->getOperand(i) == TheValue) {
4373 return true;
4374 }
4375 }
4376
4377 return false;
4378}
4379
4380
Evan Cheng917be682008-03-04 00:41:45 +00004381/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004382///
Evan Cheng917be682008-03-04 00:41:45 +00004383bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004384 bool Seen = false;
4385 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004386 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004387 if (User == this)
4388 Seen = true;
4389 else
4390 return false;
4391 }
4392
4393 return Seen;
4394}
4395
Evan Chenge6e97e62006-11-03 07:31:32 +00004396/// isOperand - Return true if this node is an operand of N.
4397///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004398bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004399 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4400 if (*this == N->getOperand(i))
4401 return true;
4402 return false;
4403}
4404
Evan Cheng917be682008-03-04 00:41:45 +00004405bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004406 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004407 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004408 return true;
4409 return false;
4410}
Evan Cheng4ee62112006-02-05 06:29:23 +00004411
Chris Lattner572dee72008-01-16 05:49:24 +00004412/// reachesChainWithoutSideEffects - Return true if this operand (which must
4413/// be a chain) reaches the specified operand without crossing any
4414/// side-effecting instructions. In practice, this looks through token
4415/// factors and non-volatile loads. In order to remain efficient, this only
4416/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004417bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004418 unsigned Depth) const {
4419 if (*this == Dest) return true;
4420
4421 // Don't search too deeply, we just want to be able to see through
4422 // TokenFactor's etc.
4423 if (Depth == 0) return false;
4424
4425 // If this is a token factor, all inputs to the TF happen in parallel. If any
4426 // of the operands of the TF reach dest, then we can do the xform.
4427 if (getOpcode() == ISD::TokenFactor) {
4428 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4429 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4430 return true;
4431 return false;
4432 }
4433
4434 // Loads don't have side effects, look through them.
4435 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4436 if (!Ld->isVolatile())
4437 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4438 }
4439 return false;
4440}
4441
4442
Evan Chengc5fc57d2006-11-03 03:05:24 +00004443static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004444 SmallPtrSet<SDNode *, 32> &Visited) {
4445 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004446 return;
4447
4448 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4449 SDNode *Op = N->getOperand(i).Val;
4450 if (Op == P) {
4451 found = true;
4452 return;
4453 }
4454 findPredecessor(Op, P, found, Visited);
4455 }
4456}
4457
Evan Cheng917be682008-03-04 00:41:45 +00004458/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004459/// is either an operand of N or it can be reached by recursively traversing
4460/// up the operands.
4461/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004462bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004463 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004464 bool found = false;
4465 findPredecessor(N, this, found, Visited);
4466 return found;
4467}
4468
Evan Chengc5484282006-10-04 00:56:09 +00004469uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4470 assert(Num < NumOperands && "Invalid child # of SDNode!");
4471 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4472}
4473
Reid Spencer577cc322007-04-01 07:32:19 +00004474std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004475 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004476 default:
4477 if (getOpcode() < ISD::BUILTIN_OP_END)
4478 return "<<Unknown DAG Node>>";
4479 else {
Evan Cheng72261582005-12-20 06:22:03 +00004480 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004481 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004482 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004483 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004484
Evan Cheng72261582005-12-20 06:22:03 +00004485 TargetLowering &TLI = G->getTargetLoweringInfo();
4486 const char *Name =
4487 TLI.getTargetNodeName(getOpcode());
4488 if (Name) return Name;
4489 }
4490
4491 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004492 }
4493
Evan Cheng27b7db52008-03-08 00:58:38 +00004494 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004495 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004496 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4497 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4498 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004499 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4500 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4501 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004502 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004503 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4504 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4505 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4506 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4507 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004508 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004509 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004510 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004511 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004512 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004513 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004514 case ISD::AssertSext: return "AssertSext";
4515 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004516
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004517 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004518 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004519 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004520 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004521
4522 case ISD::Constant: return "Constant";
4523 case ISD::ConstantFP: return "ConstantFP";
4524 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004525 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004526 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004527 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004528 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004529 case ISD::RETURNADDR: return "RETURNADDR";
4530 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004531 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004532 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4533 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004534 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004535 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004536 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004537 case ISD::INTRINSIC_WO_CHAIN: {
4538 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4539 return Intrinsic::getName((Intrinsic::ID)IID);
4540 }
4541 case ISD::INTRINSIC_VOID:
4542 case ISD::INTRINSIC_W_CHAIN: {
4543 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004544 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004545 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004546
Chris Lattnerb2827b02006-03-19 00:52:58 +00004547 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004548 case ISD::TargetConstant: return "TargetConstant";
4549 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004550 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004551 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004552 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004553 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004554 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004555 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004556
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004557 case ISD::CopyToReg: return "CopyToReg";
4558 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004559 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004560 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004561 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004562 case ISD::DBG_LABEL: return "dbg_label";
4563 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004564 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004565 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004566 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004567 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004568
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004569 // Unary operators
4570 case ISD::FABS: return "fabs";
4571 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004572 case ISD::FSQRT: return "fsqrt";
4573 case ISD::FSIN: return "fsin";
4574 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004575 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004576 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004577
4578 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004579 case ISD::ADD: return "add";
4580 case ISD::SUB: return "sub";
4581 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004582 case ISD::MULHU: return "mulhu";
4583 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004584 case ISD::SDIV: return "sdiv";
4585 case ISD::UDIV: return "udiv";
4586 case ISD::SREM: return "srem";
4587 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004588 case ISD::SMUL_LOHI: return "smul_lohi";
4589 case ISD::UMUL_LOHI: return "umul_lohi";
4590 case ISD::SDIVREM: return "sdivrem";
4591 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004592 case ISD::AND: return "and";
4593 case ISD::OR: return "or";
4594 case ISD::XOR: return "xor";
4595 case ISD::SHL: return "shl";
4596 case ISD::SRA: return "sra";
4597 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004598 case ISD::ROTL: return "rotl";
4599 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004600 case ISD::FADD: return "fadd";
4601 case ISD::FSUB: return "fsub";
4602 case ISD::FMUL: return "fmul";
4603 case ISD::FDIV: return "fdiv";
4604 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004605 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004606 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004607
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004608 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004609 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004610 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004611 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004612 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004613 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004614 case ISD::CONCAT_VECTORS: return "concat_vectors";
4615 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004616 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004617 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004618 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004619 case ISD::ADDC: return "addc";
4620 case ISD::ADDE: return "adde";
4621 case ISD::SUBC: return "subc";
4622 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004623 case ISD::SHL_PARTS: return "shl_parts";
4624 case ISD::SRA_PARTS: return "sra_parts";
4625 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004626
4627 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4628 case ISD::INSERT_SUBREG: return "insert_subreg";
4629
Chris Lattner7f644642005-04-28 21:44:03 +00004630 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004631 case ISD::SIGN_EXTEND: return "sign_extend";
4632 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004633 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004634 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004635 case ISD::TRUNCATE: return "truncate";
4636 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004637 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004638 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004639 case ISD::FP_EXTEND: return "fp_extend";
4640
4641 case ISD::SINT_TO_FP: return "sint_to_fp";
4642 case ISD::UINT_TO_FP: return "uint_to_fp";
4643 case ISD::FP_TO_SINT: return "fp_to_sint";
4644 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004645 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004646
4647 // Control flow instructions
4648 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004649 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004650 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004651 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004652 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004653 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004654 case ISD::CALLSEQ_START: return "callseq_start";
4655 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004656
4657 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004658 case ISD::LOAD: return "load";
4659 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004660 case ISD::VAARG: return "vaarg";
4661 case ISD::VACOPY: return "vacopy";
4662 case ISD::VAEND: return "vaend";
4663 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004664 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004665 case ISD::EXTRACT_ELEMENT: return "extract_element";
4666 case ISD::BUILD_PAIR: return "build_pair";
4667 case ISD::STACKSAVE: return "stacksave";
4668 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004669 case ISD::TRAP: return "trap";
4670
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004671 // Bit manipulation
4672 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004673 case ISD::CTPOP: return "ctpop";
4674 case ISD::CTTZ: return "cttz";
4675 case ISD::CTLZ: return "ctlz";
4676
Chris Lattner36ce6912005-11-29 06:21:05 +00004677 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004678 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004679 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004680
Duncan Sands36397f52007-07-27 12:58:54 +00004681 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004682 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004683
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004684 case ISD::CONDCODE:
4685 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004686 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004687 case ISD::SETOEQ: return "setoeq";
4688 case ISD::SETOGT: return "setogt";
4689 case ISD::SETOGE: return "setoge";
4690 case ISD::SETOLT: return "setolt";
4691 case ISD::SETOLE: return "setole";
4692 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004693
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004694 case ISD::SETO: return "seto";
4695 case ISD::SETUO: return "setuo";
4696 case ISD::SETUEQ: return "setue";
4697 case ISD::SETUGT: return "setugt";
4698 case ISD::SETUGE: return "setuge";
4699 case ISD::SETULT: return "setult";
4700 case ISD::SETULE: return "setule";
4701 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004702
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004703 case ISD::SETEQ: return "seteq";
4704 case ISD::SETGT: return "setgt";
4705 case ISD::SETGE: return "setge";
4706 case ISD::SETLT: return "setlt";
4707 case ISD::SETLE: return "setle";
4708 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004709 }
4710 }
4711}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004712
Evan Cheng144d8f02006-11-09 17:55:04 +00004713const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004714 switch (AM) {
4715 default:
4716 return "";
4717 case ISD::PRE_INC:
4718 return "<pre-inc>";
4719 case ISD::PRE_DEC:
4720 return "<pre-dec>";
4721 case ISD::POST_INC:
4722 return "<post-inc>";
4723 case ISD::POST_DEC:
4724 return "<post-dec>";
4725 }
4726}
4727
Duncan Sands276dcbd2008-03-21 09:14:45 +00004728std::string ISD::ArgFlagsTy::getArgFlagsString() {
4729 std::string S = "< ";
4730
4731 if (isZExt())
4732 S += "zext ";
4733 if (isSExt())
4734 S += "sext ";
4735 if (isInReg())
4736 S += "inreg ";
4737 if (isSRet())
4738 S += "sret ";
4739 if (isByVal())
4740 S += "byval ";
4741 if (isNest())
4742 S += "nest ";
4743 if (getByValAlign())
4744 S += "byval-align:" + utostr(getByValAlign()) + " ";
4745 if (getOrigAlign())
4746 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4747 if (getByValSize())
4748 S += "byval-size:" + utostr(getByValSize()) + " ";
4749 return S + ">";
4750}
4751
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004752void SDNode::dump() const { dump(0); }
4753void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004754 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004755
4756 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004757 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004758 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004759 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004760 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004761 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004762 }
Bill Wendling832171c2006-12-07 20:04:42 +00004763 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004764
Bill Wendling832171c2006-12-07 20:04:42 +00004765 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004766 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004767 if (i) cerr << ", ";
4768 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004769 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004770 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004771 }
4772
Evan Chengce254432007-12-11 02:08:35 +00004773 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4774 SDNode *Mask = getOperand(2).Val;
4775 cerr << "<";
4776 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4777 if (i) cerr << ",";
4778 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4779 cerr << "u";
4780 else
4781 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4782 }
4783 cerr << ">";
4784 }
4785
Chris Lattnerc3aae252005-01-07 07:46:32 +00004786 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004787 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004788 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004789 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4790 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4791 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4792 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4793 else {
4794 cerr << "<APFloat(";
4795 CSDN->getValueAPF().convertToAPInt().dump();
4796 cerr << ")>";
4797 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004798 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004799 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004800 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004801 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004802 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004803 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004804 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004805 else
Bill Wendling832171c2006-12-07 20:04:42 +00004806 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004807 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004808 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004809 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004810 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004811 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004812 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004813 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004814 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004815 else
Bill Wendling832171c2006-12-07 20:04:42 +00004816 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004817 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004818 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004819 else
Bill Wendling832171c2006-12-07 20:04:42 +00004820 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004821 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004822 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004823 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4824 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004825 cerr << LBB->getName() << " ";
4826 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004827 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004828 if (G && R->getReg() &&
4829 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004830 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004831 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004832 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004833 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004834 } else if (const ExternalSymbolSDNode *ES =
4835 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004836 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004837 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4838 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004839 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004840 else
Dan Gohman69de1932008-02-06 22:27:42 +00004841 cerr << "<null>";
4842 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4843 if (M->MO.getValue())
4844 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4845 else
4846 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004847 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4848 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004849 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004850 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004851 }
4852 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004853 const Value *SrcValue = LD->getSrcValue();
4854 int SrcOffset = LD->getSrcValueOffset();
4855 cerr << " <";
4856 if (SrcValue)
4857 cerr << SrcValue;
4858 else
4859 cerr << "null";
4860 cerr << ":" << SrcOffset << ">";
4861
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004862 bool doExt = true;
4863 switch (LD->getExtensionType()) {
4864 default: doExt = false; break;
4865 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004866 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004867 break;
4868 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004869 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004870 break;
4871 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004872 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004873 break;
4874 }
4875 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004876 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004877
Evan Cheng144d8f02006-11-09 17:55:04 +00004878 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004879 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004880 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004881 if (LD->isVolatile())
4882 cerr << " <volatile>";
4883 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004884 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004885 const Value *SrcValue = ST->getSrcValue();
4886 int SrcOffset = ST->getSrcValueOffset();
4887 cerr << " <";
4888 if (SrcValue)
4889 cerr << SrcValue;
4890 else
4891 cerr << "null";
4892 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004893
4894 if (ST->isTruncatingStore())
4895 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004897
4898 const char *AM = getIndexedModeName(ST->getAddressingMode());
4899 if (*AM)
4900 cerr << " " << AM;
4901 if (ST->isVolatile())
4902 cerr << " <volatile>";
4903 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004904 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4905 const Value *SrcValue = AT->getSrcValue();
4906 int SrcOffset = AT->getSrcValueOffset();
4907 cerr << " <";
4908 if (SrcValue)
4909 cerr << SrcValue;
4910 else
4911 cerr << "null";
4912 cerr << ":" << SrcOffset << ">";
4913 if (AT->isVolatile())
4914 cerr << " <volatile>";
4915 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004916 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004917}
4918
Chris Lattnerde202b32005-11-09 23:47:37 +00004919static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004920 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4921 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004922 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004923 else
Bill Wendling832171c2006-12-07 20:04:42 +00004924 cerr << "\n" << std::string(indent+2, ' ')
4925 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004926
Chris Lattnerea946cd2005-01-09 20:38:33 +00004927
Bill Wendling832171c2006-12-07 20:04:42 +00004928 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004929 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004930}
4931
Chris Lattnerc3aae252005-01-07 07:46:32 +00004932void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004933 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004934 std::vector<const SDNode*> Nodes;
4935 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4936 I != E; ++I)
4937 Nodes.push_back(I);
4938
Chris Lattner49d24712005-01-09 20:26:36 +00004939 std::sort(Nodes.begin(), Nodes.end());
4940
4941 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004942 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004943 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004944 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004945
Jim Laskey26f7fa72006-10-17 19:33:52 +00004946 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004947
Bill Wendling832171c2006-12-07 20:04:42 +00004948 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004949}
4950
Evan Chengd6594ae2006-09-12 21:00:35 +00004951const Type *ConstantPoolSDNode::getType() const {
4952 if (isMachineConstantPoolEntry())
4953 return Val.MachineCPVal->getType();
4954 return Val.ConstVal->getType();
4955}