blob: b22bf1dc6210ef9326a956ce4fed00ef64d19045 [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,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000326 SDOperandPtr 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
Jim Laskey583bd472006-10-27 23:46:08 +0000333static void AddNodeIDNode(FoldingSetNodeID &ID,
334 unsigned short OpC, SDVTList VTList,
Dan Gohman35b31be2008-04-17 23:02:12 +0000335 SDOperandPtr OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000336 AddNodeIDOpcode(ID, OpC);
337 AddNodeIDValueTypes(ID, VTList);
338 AddNodeIDOperands(ID, OpList, N);
339}
340
Roman Levenstein9cac5252008-04-16 16:15:27 +0000341
Jim Laskeydef69b92006-10-27 23:52:51 +0000342/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
343/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
345 AddNodeIDOpcode(ID, N->getOpcode());
346 // Add the return value info.
347 AddNodeIDValueTypes(ID, N->getVTList());
348 // Add the operand info.
349 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
350
351 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000352 switch (N->getOpcode()) {
353 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000354 case ISD::ARG_FLAGS:
355 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
356 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000357 case ISD::TargetConstant:
358 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000359 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 break;
361 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000362 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000363 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000365 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000367 case ISD::GlobalAddress:
368 case ISD::TargetGlobalTLSAddress:
369 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
371 ID.AddPointer(GA->getGlobal());
372 ID.AddInteger(GA->getOffset());
373 break;
374 }
375 case ISD::BasicBlock:
376 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
377 break;
378 case ISD::Register:
379 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
380 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000381 case ISD::DBG_STOPPOINT: {
382 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
383 ID.AddInteger(DSP->getLine());
384 ID.AddInteger(DSP->getColumn());
385 ID.AddPointer(DSP->getCompileUnit());
386 break;
387 }
Dan Gohman44066042008-07-01 00:05:16 +0000388 case ISD::DBG_LABEL:
389 case ISD::EH_LABEL:
390 ID.AddInteger(cast<LabelSDNode>(N)->getLabelID());
391 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000392 case ISD::SRCVALUE:
393 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
394 break;
395 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000396 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000397 ID.AddPointer(MO.getValue());
398 ID.AddInteger(MO.getFlags());
399 ID.AddInteger(MO.getOffset());
400 ID.AddInteger(MO.getSize());
401 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000402 break;
403 }
404 case ISD::FrameIndex:
405 case ISD::TargetFrameIndex:
406 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
407 break;
408 case ISD::JumpTable:
409 case ISD::TargetJumpTable:
410 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
414 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
418 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
419 else
420 ID.AddPointer(CP->getConstVal());
421 break;
422 }
423 case ISD::LOAD: {
424 LoadSDNode *LD = cast<LoadSDNode>(N);
425 ID.AddInteger(LD->getAddressingMode());
426 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000427 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAlignment());
429 ID.AddInteger(LD->isVolatile());
430 break;
431 }
432 case ISD::STORE: {
433 StoreSDNode *ST = cast<StoreSDNode>(N);
434 ID.AddInteger(ST->getAddressingMode());
435 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000436 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000437 ID.AddInteger(ST->getAlignment());
438 ID.AddInteger(ST->isVolatile());
439 break;
440 }
Mon P Wang28873102008-06-25 08:15:39 +0000441 case ISD::ATOMIC_CMP_SWAP:
442 case ISD::ATOMIC_LOAD_ADD:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_SUB:
445 case ISD::ATOMIC_LOAD_AND:
446 case ISD::ATOMIC_LOAD_OR:
447 case ISD::ATOMIC_LOAD_XOR:
448 case ISD::ATOMIC_LOAD_NAND:
449 case ISD::ATOMIC_LOAD_MIN:
450 case ISD::ATOMIC_LOAD_MAX:
451 case ISD::ATOMIC_LOAD_UMIN:
452 case ISD::ATOMIC_LOAD_UMAX: {
453 AtomicSDNode *AT = cast<AtomicSDNode>(N);
454 ID.AddInteger(AT->getAlignment());
455 ID.AddInteger(AT->isVolatile());
456 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000457 }
Mon P Wang28873102008-06-25 08:15:39 +0000458 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000459}
460
461//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000462// SelectionDAG Class
463//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000464
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000465/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000466/// SelectionDAG.
467void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000468 // Create a dummy node (which is not added to allnodes), that adds a reference
469 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000470 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000471
Chris Lattner190a4182006-08-04 17:45:20 +0000472 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000473
Chris Lattner190a4182006-08-04 17:45:20 +0000474 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000475 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000476 if (I->use_empty())
477 DeadNodes.push_back(I);
478
479 // Process the worklist, deleting the nodes and adding their uses to the
480 // worklist.
481 while (!DeadNodes.empty()) {
482 SDNode *N = DeadNodes.back();
483 DeadNodes.pop_back();
484
485 // Take the node out of the appropriate CSE map.
486 RemoveNodeFromCSEMaps(N);
487
488 // Next, brutally remove the operand list. This is safe to do, as there are
489 // no cycles in the graph.
490 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000491 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000492 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000493
494 // Now that we removed this operand, see if there are no uses of it left.
495 if (Operand->use_empty())
496 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000497 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000498 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000499 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000500 }
Chris Lattner190a4182006-08-04 17:45:20 +0000501 N->OperandList = 0;
502 N->NumOperands = 0;
503
504 // Finally, remove N itself.
505 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000506 }
507
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000509 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000510}
511
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000512void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000513 SmallVector<SDNode*, 16> DeadNodes;
514 DeadNodes.push_back(N);
515
516 // Process the worklist, deleting the nodes and adding their uses to the
517 // worklist.
518 while (!DeadNodes.empty()) {
519 SDNode *N = DeadNodes.back();
520 DeadNodes.pop_back();
521
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000522 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +0000523 UpdateListener->NodeDeleted(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000524
Evan Cheng130a6472006-10-12 20:34:05 +0000525 // Take the node out of the appropriate CSE map.
526 RemoveNodeFromCSEMaps(N);
527
528 // Next, brutally remove the operand list. This is safe to do, as there are
529 // no cycles in the graph.
Owen Anderson4474c792008-07-01 22:34:11 +0000530 unsigned op_num = 0;
Evan Cheng130a6472006-10-12 20:34:05 +0000531 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000532 SDNode *Operand = I->getVal();
Owen Anderson4474c792008-07-01 22:34:11 +0000533 Operand->removeUser(op_num, N);
Evan Cheng130a6472006-10-12 20:34:05 +0000534
535 // Now that we removed this operand, see if there are no uses of it left.
536 if (Operand->use_empty())
537 DeadNodes.push_back(Operand);
Owen Anderson4474c792008-07-01 22:34:11 +0000538
539 op_num++;
Evan Cheng130a6472006-10-12 20:34:05 +0000540 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000541 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000542 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000543 }
Evan Cheng130a6472006-10-12 20:34:05 +0000544 N->OperandList = 0;
545 N->NumOperands = 0;
546
547 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000548 AllNodes.erase(N);
549 }
550}
551
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000552void SelectionDAG::DeleteNode(SDNode *N) {
553 assert(N->use_empty() && "Cannot delete a node that is not dead!");
554
555 // First take this out of the appropriate CSE map.
556 RemoveNodeFromCSEMaps(N);
557
Chris Lattner1e111c72005-09-07 05:37:01 +0000558 // Finally, remove uses due to operands of this node, remove from the
559 // AllNodes list, and delete the node.
560 DeleteNodeNotInCSEMaps(N);
561}
562
563void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
564
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000565 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000566 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000567
568 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000569 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000570 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000571 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000572 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000573 }
Chris Lattner65113b22005-11-08 22:07:03 +0000574 N->OperandList = 0;
575 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000576
577 delete N;
578}
579
Chris Lattner149c58c2005-08-16 18:17:10 +0000580/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
581/// correspond to it. This is useful when we're about to delete or repurpose
582/// the node. We don't want future request for structurally identical nodes
583/// to return N anymore.
584void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000585 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000586 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000587 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000588 case ISD::CONDCODE:
589 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
590 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000591 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000592 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
593 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000594 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000595 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000597 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000598 Erased =
599 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000600 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000601 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000602 MVT VT = cast<VTSDNode>(N)->getVT();
603 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000604 Erased = ExtendedValueTypeNodes.erase(VT);
605 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000606 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
607 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000608 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000609 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000610 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000612 // Remove it from the CSE Map.
613 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000614 break;
615 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000616#ifndef NDEBUG
617 // Verify that the node was actually in one of the CSE maps, unless it has a
618 // flag result (which cannot be CSE'd) or is one of the special cases that are
619 // not subject to CSE.
620 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000621 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000622 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000623 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000624 assert(0 && "Node is not in map!");
625 }
626#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000627}
628
Chris Lattner8b8749f2005-08-17 19:00:20 +0000629/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
630/// has been taken out and modified in some way. If the specified node already
631/// exists in the CSE maps, do not modify the maps, but return the existing node
632/// instead. If it doesn't exist, add it and return null.
633///
634SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
635 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000636 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000637 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000638
Chris Lattner9f8cc692005-12-19 22:21:21 +0000639 // Check that remaining values produced are not flags.
640 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
641 if (N->getValueType(i) == MVT::Flag)
642 return 0; // Never CSE anything that produces a flag.
643
Chris Lattnera5682852006-08-07 23:03:03 +0000644 SDNode *New = CSEMap.GetOrInsertNode(N);
645 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000646 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000647}
648
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
650/// were replaced with those specified. If this node is never memoized,
651/// return null, otherwise return a pointer to the slot it would take. If a
652/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000653SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
654 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000655 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000656 return 0; // Never add these nodes.
657
658 // Check that remaining values produced are not flags.
659 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
660 if (N->getValueType(i) == MVT::Flag)
661 return 0; // Never CSE anything that produces a flag.
662
Chris Lattner63e3f142007-02-04 07:28:00 +0000663 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000664 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000665 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000666 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000667}
668
669/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
670/// were replaced with those specified. If this node is never memoized,
671/// return null, otherwise return a pointer to the slot it would take. If a
672/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000673SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
674 SDOperand Op1, SDOperand Op2,
675 void *&InsertPos) {
676 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
677 return 0; // Never add these nodes.
678
679 // Check that remaining values produced are not flags.
680 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
681 if (N->getValueType(i) == MVT::Flag)
682 return 0; // Never CSE anything that produces a flag.
683
Chris Lattner63e3f142007-02-04 07:28:00 +0000684 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000685 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000686 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000687 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
688}
689
690
691/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
692/// were replaced with those specified. If this node is never memoized,
693/// return null, otherwise return a pointer to the slot it would take. If a
694/// node already exists with these operands, the slot will be non-null.
695SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000696 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000697 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000698 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699 return 0; // Never add these nodes.
700
701 // Check that remaining values produced are not flags.
702 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
703 if (N->getValueType(i) == MVT::Flag)
704 return 0; // Never CSE anything that produces a flag.
705
Jim Laskey583bd472006-10-27 23:46:08 +0000706 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000707 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000708
Evan Cheng9629aba2006-10-11 01:47:58 +0000709 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
710 ID.AddInteger(LD->getAddressingMode());
711 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000712 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000713 ID.AddInteger(LD->getAlignment());
714 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000715 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
716 ID.AddInteger(ST->getAddressingMode());
717 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000718 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000719 ID.AddInteger(ST->getAlignment());
720 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000721 }
Jim Laskey583bd472006-10-27 23:46:08 +0000722
Chris Lattnera5682852006-08-07 23:03:03 +0000723 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000724}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000725
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000726
Chris Lattner78ec3112003-08-11 14:57:33 +0000727SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000728 while (!AllNodes.empty()) {
729 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000730 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000731 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000732 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000733 }
Chris Lattner65113b22005-11-08 22:07:03 +0000734 N->OperandList = 0;
735 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000736 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000737 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000738}
739
Duncan Sands83ec4b62008-06-06 12:08:01 +0000740SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000741 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000742 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000743 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000744 return getNode(ISD::AND, Op.getValueType(), Op,
745 getConstant(Imm, Op.getValueType()));
746}
747
Duncan Sands83ec4b62008-06-06 12:08:01 +0000748SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000749 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000750 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000751}
752
Duncan Sands83ec4b62008-06-06 12:08:01 +0000753SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
754 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000755
Evan Cheng0ff39b32008-06-30 07:31:25 +0000756 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000757 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000758 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000759
760 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000761 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000762 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000763 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000764 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000765 SDNode *N = NULL;
766 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000767 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000768 return SDOperand(N, 0);
769 if (!N) {
770 N = new ConstantSDNode(isT, Val, EltVT);
771 CSEMap.InsertNode(N, IP);
772 AllNodes.push_back(N);
773 }
774
775 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000777 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000779 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
780 }
781 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000782}
783
Chris Lattner0bd48932008-01-17 07:00:52 +0000784SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
785 return getConstant(Val, TLI.getPointerTy(), isTarget);
786}
787
788
Duncan Sands83ec4b62008-06-06 12:08:01 +0000789SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
790 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000791
Duncan Sands83ec4b62008-06-06 12:08:01 +0000792 MVT EltVT =
793 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000794
Chris Lattnerd8658612005-02-17 20:17:32 +0000795 // Do the map lookup using the actual bit pattern for the floating point
796 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
797 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000798 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000799 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000800 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000801 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000802 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000803 SDNode *N = NULL;
804 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000806 return SDOperand(N, 0);
807 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000808 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000809 CSEMap.InsertNode(N, IP);
810 AllNodes.push_back(N);
811 }
812
Dan Gohman7f321562007-06-25 16:23:39 +0000813 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000815 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000817 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
818 }
819 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820}
821
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
823 MVT EltVT =
824 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000825 if (EltVT==MVT::f32)
826 return getConstantFP(APFloat((float)Val), VT, isTarget);
827 else
828 return getConstantFP(APFloat(Val), VT, isTarget);
829}
830
Chris Lattnerc3aae252005-01-07 07:46:32 +0000831SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000832 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000833 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000835
836 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
837 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000838 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000839 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
840 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
841 }
842
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000843 if (GVar && GVar->isThreadLocal())
844 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
845 else
846 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000847
Jim Laskey583bd472006-10-27 23:46:08 +0000848 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000849 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000850 ID.AddPointer(GV);
851 ID.AddInteger(Offset);
852 void *IP = 0;
853 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
854 return SDOperand(E, 0);
855 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
856 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000857 AllNodes.push_back(N);
858 return SDOperand(N, 0);
859}
860
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000862 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000863 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000864 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000865 ID.AddInteger(FI);
866 void *IP = 0;
867 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
868 return SDOperand(E, 0);
869 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
870 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000871 AllNodes.push_back(N);
872 return SDOperand(N, 0);
873}
874
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000876 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000877 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000878 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000879 ID.AddInteger(JTI);
880 void *IP = 0;
881 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
882 return SDOperand(E, 0);
883 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
884 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000885 AllNodes.push_back(N);
886 return SDOperand(N, 0);
887}
888
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000890 unsigned Alignment, int Offset,
891 bool isTarget) {
892 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000893 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000894 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000895 ID.AddInteger(Alignment);
896 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000897 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000898 void *IP = 0;
899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
900 return SDOperand(E, 0);
901 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
902 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000903 AllNodes.push_back(N);
904 return SDOperand(N, 0);
905}
906
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907
Duncan Sands83ec4b62008-06-06 12:08:01 +0000908SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000909 unsigned Alignment, int Offset,
910 bool isTarget) {
911 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000912 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000913 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000914 ID.AddInteger(Alignment);
915 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000916 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
924}
925
926
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000928 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000929 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000930 ID.AddPointer(MBB);
931 void *IP = 0;
932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
933 return SDOperand(E, 0);
934 SDNode *N = new BasicBlockSDNode(MBB);
935 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Duncan Sands276dcbd2008-03-21 09:14:45 +0000940SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
941 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000942 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000943 ID.AddInteger(Flags.getRawBits());
944 void *IP = 0;
945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
946 return SDOperand(E, 0);
947 SDNode *N = new ARG_FLAGSSDNode(Flags);
948 CSEMap.InsertNode(N, IP);
949 AllNodes.push_back(N);
950 return SDOperand(N, 0);
951}
952
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953SDOperand SelectionDAG::getValueType(MVT VT) {
954 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
955 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000956
Duncan Sands83ec4b62008-06-06 12:08:01 +0000957 SDNode *&N = VT.isExtended() ?
958 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000959
960 if (N) return SDOperand(N, 0);
961 N = new VTSDNode(VT);
962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000964}
965
Duncan Sands83ec4b62008-06-06 12:08:01 +0000966SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 SDNode *&N = ExternalSymbols[Sym];
968 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000969 N = new ExternalSymbolSDNode(false, Sym, VT);
970 AllNodes.push_back(N);
971 return SDOperand(N, 0);
972}
973
Duncan Sands83ec4b62008-06-06 12:08:01 +0000974SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000975 SDNode *&N = TargetExternalSymbols[Sym];
976 if (N) return SDOperand(N, 0);
977 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
980}
981
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000982SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
983 if ((unsigned)Cond >= CondCodeNodes.size())
984 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000985
Chris Lattner079a27a2005-08-09 20:40:02 +0000986 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000987 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000988 AllNodes.push_back(CondCodeNodes[Cond]);
989 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000990 return SDOperand(CondCodeNodes[Cond], 0);
991}
992
Duncan Sands83ec4b62008-06-06 12:08:01 +0000993SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000994 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000995 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000996 ID.AddInteger(RegNo);
997 void *IP = 0;
998 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
999 return SDOperand(E, 0);
1000 SDNode *N = new RegisterSDNode(RegNo, VT);
1001 CSEMap.InsertNode(N, IP);
1002 AllNodes.push_back(N);
1003 return SDOperand(N, 0);
1004}
1005
Dan Gohman7f460202008-06-30 20:59:49 +00001006SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1007 unsigned Line, unsigned Col,
1008 const CompileUnitDesc *CU) {
1009 FoldingSetNodeID ID;
1010 SDOperand Ops[] = { Root };
1011 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
1012 ID.AddInteger(Line);
1013 ID.AddInteger(Col);
1014 ID.AddPointer(CU);
1015 void *IP = 0;
1016 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1017 return SDOperand(E, 0);
1018 SDNode *N = new DbgStopPointSDNode(Root, Line, Col, CU);
1019 CSEMap.InsertNode(N, IP);
1020 AllNodes.push_back(N);
1021 return SDOperand(N, 0);
1022}
1023
Dan Gohman44066042008-07-01 00:05:16 +00001024SDOperand SelectionDAG::getLabel(unsigned Opcode,
1025 SDOperand Root,
1026 unsigned LabelID) {
1027 FoldingSetNodeID ID;
1028 SDOperand Ops[] = { Root };
1029 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1030 ID.AddInteger(LabelID);
1031 void *IP = 0;
1032 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1033 return SDOperand(E, 0);
1034 SDNode *N = new LabelSDNode(Opcode, Root, LabelID);
1035 CSEMap.InsertNode(N, IP);
1036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
1038}
1039
Dan Gohman69de1932008-02-06 22:27:42 +00001040SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001041 assert((!V || isa<PointerType>(V->getType())) &&
1042 "SrcValue is not a pointer?");
1043
Jim Laskey583bd472006-10-27 23:46:08 +00001044 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001045 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001046 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001047
Chris Lattner61b09412006-08-11 21:01:22 +00001048 void *IP = 0;
1049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1050 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001051
1052 SDNode *N = new SrcValueSDNode(V);
1053 CSEMap.InsertNode(N, IP);
1054 AllNodes.push_back(N);
1055 return SDOperand(N, 0);
1056}
1057
Dan Gohman36b5c132008-04-07 19:35:22 +00001058SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001059 const Value *v = MO.getValue();
1060 assert((!v || isa<PointerType>(v->getType())) &&
1061 "SrcValue is not a pointer?");
1062
1063 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001064 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001065 ID.AddPointer(v);
1066 ID.AddInteger(MO.getFlags());
1067 ID.AddInteger(MO.getOffset());
1068 ID.AddInteger(MO.getSize());
1069 ID.AddInteger(MO.getAlignment());
1070
1071 void *IP = 0;
1072 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1073 return SDOperand(E, 0);
1074
1075 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001076 CSEMap.InsertNode(N, IP);
1077 AllNodes.push_back(N);
1078 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001079}
1080
Chris Lattner37ce9df2007-10-15 17:47:20 +00001081/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1082/// specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001083SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001084 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 unsigned ByteSize = VT.getSizeInBits()/8;
1086 const Type *Ty = VT.getTypeForMVT();
Chris Lattner37ce9df2007-10-15 17:47:20 +00001087 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1088 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1089 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1090}
1091
1092
Duncan Sands83ec4b62008-06-06 12:08:01 +00001093SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001094 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095 // These setcc operations always fold.
1096 switch (Cond) {
1097 default: break;
1098 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001099 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001101 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001102
1103 case ISD::SETOEQ:
1104 case ISD::SETOGT:
1105 case ISD::SETOGE:
1106 case ISD::SETOLT:
1107 case ISD::SETOLE:
1108 case ISD::SETONE:
1109 case ISD::SETO:
1110 case ISD::SETUO:
1111 case ISD::SETUEQ:
1112 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001113 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001114 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001116
Chris Lattner67255a12005-04-07 18:14:58 +00001117 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001118 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001119 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001120 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001121
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 switch (Cond) {
1123 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001124 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1125 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001126 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1127 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1128 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1129 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1130 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1131 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1132 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1133 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001134 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001135 }
Chris Lattner67255a12005-04-07 18:14:58 +00001136 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001137 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001139 // No compile time operations on this type yet.
1140 if (N1C->getValueType(0) == MVT::ppcf128)
1141 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001142
1143 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001145 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001146 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1147 return getNode(ISD::UNDEF, VT);
1148 // fall through
1149 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1150 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1151 return getNode(ISD::UNDEF, VT);
1152 // fall through
1153 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001154 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001155 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1156 return getNode(ISD::UNDEF, VT);
1157 // fall through
1158 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1159 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1160 return getNode(ISD::UNDEF, VT);
1161 // fall through
1162 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1163 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1164 return getNode(ISD::UNDEF, VT);
1165 // fall through
1166 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001167 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001168 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1169 return getNode(ISD::UNDEF, VT);
1170 // fall through
1171 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001172 R==APFloat::cmpEqual, VT);
1173 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1174 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1175 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1176 R==APFloat::cmpEqual, VT);
1177 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1178 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1179 R==APFloat::cmpLessThan, VT);
1180 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1181 R==APFloat::cmpUnordered, VT);
1182 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1183 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 }
1185 } else {
1186 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001187 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001188 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001189 }
1190
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001191 // Could not fold it.
1192 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193}
1194
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001195/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1196/// use this predicate to simplify operations downstream.
1197bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1198 unsigned BitWidth = Op.getValueSizeInBits();
1199 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1200}
1201
Dan Gohmanea859be2007-06-22 14:59:07 +00001202/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1203/// this predicate to simplify operations downstream. Mask is known to be zero
1204/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001205bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001206 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001207 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001208 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1209 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1210 return (KnownZero & Mask) == Mask;
1211}
1212
1213/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1214/// known to be either zero or one and return them in the KnownZero/KnownOne
1215/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1216/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001217void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001218 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001219 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001220 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001221 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001222 "Mask size mismatches value type size!");
1223
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001224 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001225 if (Depth == 6 || Mask == 0)
1226 return; // Limit search depth.
1227
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001228 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001229
1230 switch (Op.getOpcode()) {
1231 case ISD::Constant:
1232 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001233 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001234 KnownZero = ~KnownOne & Mask;
1235 return;
1236 case ISD::AND:
1237 // If either the LHS or the RHS are Zero, the result is zero.
1238 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001239 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1240 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001241 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1242 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1243
1244 // Output known-1 bits are only known if set in both the LHS & RHS.
1245 KnownOne &= KnownOne2;
1246 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1247 KnownZero |= KnownZero2;
1248 return;
1249 case ISD::OR:
1250 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001251 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1252 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001253 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1254 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1255
1256 // Output known-0 bits are only known if clear in both the LHS & RHS.
1257 KnownZero &= KnownZero2;
1258 // Output known-1 are known to be set if set in either the LHS | RHS.
1259 KnownOne |= KnownOne2;
1260 return;
1261 case ISD::XOR: {
1262 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1263 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1264 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 known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001268 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001269 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1270 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1271 KnownZero = KnownZeroOut;
1272 return;
1273 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001274 case ISD::MUL: {
1275 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1276 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1277 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1278 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1279 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1280
1281 // If low bits are zero in either operand, output low known-0 bits.
1282 // Also compute a conserative estimate for high known-0 bits.
1283 // More trickiness is possible, but this is sufficient for the
1284 // interesting case of alignment computation.
1285 KnownOne.clear();
1286 unsigned TrailZ = KnownZero.countTrailingOnes() +
1287 KnownZero2.countTrailingOnes();
1288 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001289 KnownZero2.countLeadingOnes(),
1290 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001291
1292 TrailZ = std::min(TrailZ, BitWidth);
1293 LeadZ = std::min(LeadZ, BitWidth);
1294 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1295 APInt::getHighBitsSet(BitWidth, LeadZ);
1296 KnownZero &= Mask;
1297 return;
1298 }
1299 case ISD::UDIV: {
1300 // For the purposes of computing leading zeros we can conservatively
1301 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001302 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001303 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1304 ComputeMaskedBits(Op.getOperand(0),
1305 AllOnes, KnownZero2, KnownOne2, Depth+1);
1306 unsigned LeadZ = KnownZero2.countLeadingOnes();
1307
1308 KnownOne2.clear();
1309 KnownZero2.clear();
1310 ComputeMaskedBits(Op.getOperand(1),
1311 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001312 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1313 if (RHSUnknownLeadingOnes != BitWidth)
1314 LeadZ = std::min(BitWidth,
1315 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001316
1317 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1318 return;
1319 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001320 case ISD::SELECT:
1321 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1322 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1323 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1324 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1325
1326 // Only known if known in both the LHS and RHS.
1327 KnownOne &= KnownOne2;
1328 KnownZero &= KnownZero2;
1329 return;
1330 case ISD::SELECT_CC:
1331 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1332 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1333 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1334 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1335
1336 // Only known if known in both the LHS and RHS.
1337 KnownOne &= KnownOne2;
1338 KnownZero &= KnownZero2;
1339 return;
1340 case ISD::SETCC:
1341 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001342 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1343 BitWidth > 1)
1344 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001345 return;
1346 case ISD::SHL:
1347 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1348 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001349 unsigned ShAmt = SA->getValue();
1350
1351 // If the shift count is an invalid immediate, don't do anything.
1352 if (ShAmt >= BitWidth)
1353 return;
1354
1355 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001356 KnownZero, KnownOne, Depth+1);
1357 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001358 KnownZero <<= ShAmt;
1359 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001360 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001361 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001362 }
1363 return;
1364 case ISD::SRL:
1365 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1366 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001367 unsigned ShAmt = SA->getValue();
1368
Dan Gohmand4cf9922008-02-26 18:50:50 +00001369 // If the shift count is an invalid immediate, don't do anything.
1370 if (ShAmt >= BitWidth)
1371 return;
1372
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001373 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001374 KnownZero, KnownOne, Depth+1);
1375 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001376 KnownZero = KnownZero.lshr(ShAmt);
1377 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001378
Dan Gohman72d2fd52008-02-13 22:43:25 +00001379 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001380 KnownZero |= HighBits; // High bits known zero.
1381 }
1382 return;
1383 case ISD::SRA:
1384 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001385 unsigned ShAmt = SA->getValue();
1386
Dan Gohmand4cf9922008-02-26 18:50:50 +00001387 // If the shift count is an invalid immediate, don't do anything.
1388 if (ShAmt >= BitWidth)
1389 return;
1390
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001391 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001392 // If any of the demanded bits are produced by the sign extension, we also
1393 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001394 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1395 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001396 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001397
1398 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1399 Depth+1);
1400 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001401 KnownZero = KnownZero.lshr(ShAmt);
1402 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001403
1404 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001405 APInt SignBit = APInt::getSignBit(BitWidth);
1406 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001407
Dan Gohmanca93a432008-02-20 16:30:17 +00001408 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001409 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001410 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001411 KnownOne |= HighBits; // New bits are known one.
1412 }
1413 }
1414 return;
1415 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001416 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1417 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001418
1419 // Sign extension. Compute the demanded bits in the result that are not
1420 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001421 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001422
Dan Gohman977a76f2008-02-13 22:28:48 +00001423 APInt InSignBit = APInt::getSignBit(EBits);
1424 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001425
1426 // If the sign extended bits are demanded, we know that the sign
1427 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001428 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001429 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 InputDemandedBits |= InSignBit;
1431
1432 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1433 KnownZero, KnownOne, Depth+1);
1434 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1435
1436 // If the sign bit of the input is known set or clear, then we know the
1437 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001438 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 KnownZero |= NewBits;
1440 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001441 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001442 KnownOne |= NewBits;
1443 KnownZero &= ~NewBits;
1444 } else { // Input sign bit unknown
1445 KnownZero &= ~NewBits;
1446 KnownOne &= ~NewBits;
1447 }
1448 return;
1449 }
1450 case ISD::CTTZ:
1451 case ISD::CTLZ:
1452 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001453 unsigned LowBits = Log2_32(BitWidth)+1;
1454 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001455 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001456 return;
1457 }
1458 case ISD::LOAD: {
1459 if (ISD::isZEXTLoad(Op.Val)) {
1460 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001461 MVT VT = LD->getMemoryVT();
1462 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001463 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001464 }
1465 return;
1466 }
1467 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001468 MVT InVT = Op.getOperand(0).getValueType();
1469 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001470 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1471 APInt InMask = Mask;
1472 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001473 KnownZero.trunc(InBits);
1474 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001475 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001476 KnownZero.zext(BitWidth);
1477 KnownOne.zext(BitWidth);
1478 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001479 return;
1480 }
1481 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 MVT InVT = Op.getOperand(0).getValueType();
1483 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001484 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001485 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1486 APInt InMask = Mask;
1487 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001488
1489 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001490 // bit is demanded. Temporarily set this bit in the mask for our callee.
1491 if (NewBits.getBoolValue())
1492 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001493
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001494 KnownZero.trunc(InBits);
1495 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001496 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1497
1498 // Note if the sign bit is known to be zero or one.
1499 bool SignBitKnownZero = KnownZero.isNegative();
1500 bool SignBitKnownOne = KnownOne.isNegative();
1501 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1502 "Sign bit can't be known to be both zero and one!");
1503
1504 // If the sign bit wasn't actually demanded by our caller, we don't
1505 // want it set in the KnownZero and KnownOne result values. Reset the
1506 // mask and reapply it to the result values.
1507 InMask = Mask;
1508 InMask.trunc(InBits);
1509 KnownZero &= InMask;
1510 KnownOne &= InMask;
1511
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001512 KnownZero.zext(BitWidth);
1513 KnownOne.zext(BitWidth);
1514
Dan Gohman977a76f2008-02-13 22:28:48 +00001515 // If the sign bit is known zero or one, the top bits match.
1516 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001517 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001518 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001519 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 return;
1521 }
1522 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001523 MVT InVT = Op.getOperand(0).getValueType();
1524 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001525 APInt InMask = Mask;
1526 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001527 KnownZero.trunc(InBits);
1528 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001529 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001530 KnownZero.zext(BitWidth);
1531 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001532 return;
1533 }
1534 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001535 MVT InVT = Op.getOperand(0).getValueType();
1536 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001537 APInt InMask = Mask;
1538 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001539 KnownZero.zext(InBits);
1540 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001541 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001542 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001543 KnownZero.trunc(BitWidth);
1544 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001545 break;
1546 }
1547 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001548 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1549 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001550 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1551 KnownOne, Depth+1);
1552 KnownZero |= (~InMask) & Mask;
1553 return;
1554 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001555 case ISD::FGETSIGN:
1556 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001557 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001558 return;
1559
Dan Gohman23e8b712008-04-28 17:02:21 +00001560 case ISD::SUB: {
1561 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1562 // We know that the top bits of C-X are clear if X contains less bits
1563 // than C (i.e. no wrap-around can happen). For example, 20-X is
1564 // positive if we can prove that X is >= 0 and < 16.
1565 if (CLHS->getAPIntValue().isNonNegative()) {
1566 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1567 // NLZ can't be BitWidth with no sign bit
1568 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1569 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1570 Depth+1);
1571
1572 // If all of the MaskV bits are known to be zero, then we know the
1573 // output top bits are zero, because we now know that the output is
1574 // from [0-C].
1575 if ((KnownZero2 & MaskV) == MaskV) {
1576 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1577 // Top bits known zero.
1578 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1579 }
1580 }
1581 }
1582 }
1583 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001584 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001585 // Output known-0 bits are known if clear or set in both the low clear bits
1586 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1587 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001588 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1589 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1590 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1591 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1592
1593 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1594 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1595 KnownZeroOut = std::min(KnownZeroOut,
1596 KnownZero2.countTrailingOnes());
1597
1598 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001599 return;
1600 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001601 case ISD::SREM:
1602 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1603 APInt RA = Rem->getAPIntValue();
1604 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001605 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001606 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1607 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001608
Dan Gohman23e8b712008-04-28 17:02:21 +00001609 // The sign of a remainder is equal to the sign of the first
1610 // operand (zero being positive).
1611 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1612 KnownZero2 |= ~LowBits;
1613 else if (KnownOne2[BitWidth-1])
1614 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001615
Dan Gohman23e8b712008-04-28 17:02:21 +00001616 KnownZero |= KnownZero2 & Mask;
1617 KnownOne |= KnownOne2 & Mask;
1618
1619 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001620 }
1621 }
1622 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001623 case ISD::UREM: {
1624 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1625 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001626 if (RA.isPowerOf2()) {
1627 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001628 APInt Mask2 = LowBits & Mask;
1629 KnownZero |= ~LowBits & Mask;
1630 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1631 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1632 break;
1633 }
1634 }
1635
1636 // Since the result is less than or equal to either operand, any leading
1637 // zero bits in either operand must also exist in the result.
1638 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1639 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1640 Depth+1);
1641 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1642 Depth+1);
1643
1644 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1645 KnownZero2.countLeadingOnes());
1646 KnownOne.clear();
1647 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1648 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001649 }
1650 default:
1651 // Allow the target to implement this method for its nodes.
1652 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1653 case ISD::INTRINSIC_WO_CHAIN:
1654 case ISD::INTRINSIC_W_CHAIN:
1655 case ISD::INTRINSIC_VOID:
1656 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1657 }
1658 return;
1659 }
1660}
1661
1662/// ComputeNumSignBits - Return the number of times the sign bit of the
1663/// register is replicated into the other bits. We know that at least 1 bit
1664/// is always equal to the sign bit (itself), but other cases can give us
1665/// information. For example, immediately after an "SRA X, 2", we know that
1666/// the top 3 bits are all equal to each other, so we return 3.
1667unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 MVT VT = Op.getValueType();
1669 assert(VT.isInteger() && "Invalid VT!");
1670 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001671 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001672 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001673
1674 if (Depth == 6)
1675 return 1; // Limit search depth.
1676
1677 switch (Op.getOpcode()) {
1678 default: break;
1679 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001680 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001681 return VTBits-Tmp+1;
1682 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001683 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001684 return VTBits-Tmp;
1685
1686 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001687 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1688 // If negative, return # leading ones.
1689 if (Val.isNegative())
1690 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001691
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001692 // Return # leading zeros.
1693 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001694 }
1695
1696 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001697 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001698 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1699
1700 case ISD::SIGN_EXTEND_INREG:
1701 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001702 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001703 Tmp = VTBits-Tmp+1;
1704
1705 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1706 return std::max(Tmp, Tmp2);
1707
1708 case ISD::SRA:
1709 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1710 // SRA X, C -> adds C sign bits.
1711 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1712 Tmp += C->getValue();
1713 if (Tmp > VTBits) Tmp = VTBits;
1714 }
1715 return Tmp;
1716 case ISD::SHL:
1717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1718 // shl destroys sign bits.
1719 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1720 if (C->getValue() >= VTBits || // Bad shift.
1721 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1722 return Tmp - C->getValue();
1723 }
1724 break;
1725 case ISD::AND:
1726 case ISD::OR:
1727 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001728 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001729 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001730 if (Tmp != 1) {
1731 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1732 FirstAnswer = std::min(Tmp, Tmp2);
1733 // We computed what we know about the sign bits as our first
1734 // answer. Now proceed to the generic code that uses
1735 // ComputeMaskedBits, and pick whichever answer is better.
1736 }
1737 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001738
1739 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001740 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001742 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001743 return std::min(Tmp, Tmp2);
1744
1745 case ISD::SETCC:
1746 // If setcc returns 0/-1, all bits are sign bits.
1747 if (TLI.getSetCCResultContents() ==
1748 TargetLowering::ZeroOrNegativeOneSetCCResult)
1749 return VTBits;
1750 break;
1751 case ISD::ROTL:
1752 case ISD::ROTR:
1753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1754 unsigned RotAmt = C->getValue() & (VTBits-1);
1755
1756 // Handle rotate right by N like a rotate left by 32-N.
1757 if (Op.getOpcode() == ISD::ROTR)
1758 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1759
1760 // If we aren't rotating out all of the known-in sign bits, return the
1761 // number that are left. This handles rotl(sext(x), 1) for example.
1762 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1763 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1764 }
1765 break;
1766 case ISD::ADD:
1767 // Add can have at most one carry bit. Thus we know that the output
1768 // is, at worst, one more bit than the inputs.
1769 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1770 if (Tmp == 1) return 1; // Early out.
1771
1772 // Special case decrementing a value (ADD X, -1):
1773 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1774 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001775 APInt KnownZero, KnownOne;
1776 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1778
1779 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1780 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001781 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001782 return VTBits;
1783
1784 // If we are subtracting one from a positive number, there is no carry
1785 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001786 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001787 return Tmp;
1788 }
1789
1790 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1791 if (Tmp2 == 1) return 1;
1792 return std::min(Tmp, Tmp2)-1;
1793 break;
1794
1795 case ISD::SUB:
1796 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1797 if (Tmp2 == 1) return 1;
1798
1799 // Handle NEG.
1800 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001801 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001802 APInt KnownZero, KnownOne;
1803 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001804 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1805 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1806 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001807 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 return VTBits;
1809
1810 // If the input is known to be positive (the sign bit is known clear),
1811 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001812 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 return Tmp2;
1814
1815 // Otherwise, we treat this like a SUB.
1816 }
1817
1818 // Sub can have at most one carry bit. Thus we know that the output
1819 // is, at worst, one more bit than the inputs.
1820 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1821 if (Tmp == 1) return 1; // Early out.
1822 return std::min(Tmp, Tmp2)-1;
1823 break;
1824 case ISD::TRUNCATE:
1825 // FIXME: it's tricky to do anything useful for this, but it is an important
1826 // case for targets like X86.
1827 break;
1828 }
1829
1830 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1831 if (Op.getOpcode() == ISD::LOAD) {
1832 LoadSDNode *LD = cast<LoadSDNode>(Op);
1833 unsigned ExtType = LD->getExtensionType();
1834 switch (ExtType) {
1835 default: break;
1836 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 return VTBits-Tmp+1;
1839 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001840 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 return VTBits-Tmp;
1842 }
1843 }
1844
1845 // Allow the target to implement this method for its nodes.
1846 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1847 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1848 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1849 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1850 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001851 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 }
1853
1854 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1855 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001856 APInt KnownZero, KnownOne;
1857 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1859
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001860 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001862 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 Mask = KnownOne;
1864 } else {
1865 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001866 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001867 }
1868
1869 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1870 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001871 Mask = ~Mask;
1872 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001873 // Return # leading zeros. We use 'min' here in case Val was zero before
1874 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001875 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001876}
1877
Chris Lattner51dabfb2006-10-14 00:41:01 +00001878
Evan Chenga844bde2008-02-02 04:07:54 +00001879bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1880 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1881 if (!GA) return false;
1882 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1883 if (!GV) return false;
1884 MachineModuleInfo *MMI = getMachineModuleInfo();
1885 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1886}
1887
1888
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001889/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1890/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001891SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001892 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001893 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001894 SDOperand Idx = PermMask.getOperand(i);
1895 if (Idx.getOpcode() == ISD::UNDEF)
1896 return getNode(ISD::UNDEF, VT.getVectorElementType());
1897 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001898 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001899 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1900 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001901
1902 if (V.getOpcode() == ISD::BIT_CONVERT) {
1903 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001904 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001905 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001906 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001907 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001908 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001909 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001910 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001911 return V.getOperand(Index);
1912 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1913 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001914 return SDOperand();
1915}
1916
1917
Chris Lattnerc3aae252005-01-07 07:46:32 +00001918/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001919///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001920SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001921 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001922 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001923 void *IP = 0;
1924 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1925 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001926 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001927 CSEMap.InsertNode(N, IP);
1928
1929 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001930 return SDOperand(N, 0);
1931}
1932
Duncan Sands83ec4b62008-06-06 12:08:01 +00001933SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001934 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001935 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001936 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001937 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001938 switch (Opcode) {
1939 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001940 case ISD::SIGN_EXTEND:
1941 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001942 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001943 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001944 case ISD::TRUNCATE:
1945 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001946 case ISD::UINT_TO_FP:
1947 case ISD::SINT_TO_FP: {
1948 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001949 // No compile time operations on this type.
1950 if (VT==MVT::ppcf128)
1951 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001952 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1953 (void)apf.convertFromAPInt(Val,
1954 Opcode==ISD::SINT_TO_FP,
1955 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001956 return getConstantFP(apf, VT);
1957 }
Chris Lattner94683772005-12-23 05:30:37 +00001958 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001959 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001960 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001961 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001962 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001963 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001964 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001965 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001966 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001967 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001968 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001969 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001970 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001971 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001972 }
1973 }
1974
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001975 // Constant fold unary operations with a floating point constant operand.
1976 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1977 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001978 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001979 switch (Opcode) {
1980 case ISD::FNEG:
1981 V.changeSign();
1982 return getConstantFP(V, VT);
1983 case ISD::FABS:
1984 V.clearSign();
1985 return getConstantFP(V, VT);
1986 case ISD::FP_ROUND:
1987 case ISD::FP_EXTEND:
1988 // This can return overflow, underflow, or inexact; we don't care.
1989 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001990 (void)V.convert(*MVTToAPFloatSemantics(VT),
1991 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001992 return getConstantFP(V, VT);
1993 case ISD::FP_TO_SINT:
1994 case ISD::FP_TO_UINT: {
1995 integerPart x;
1996 assert(integerPartWidth >= 64);
1997 // FIXME need to be more flexible about rounding mode.
1998 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1999 Opcode==ISD::FP_TO_SINT,
2000 APFloat::rmTowardZero);
2001 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2002 break;
2003 return getConstant(x, VT);
2004 }
2005 case ISD::BIT_CONVERT:
2006 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2007 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2008 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2009 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002010 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002011 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002012 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002014
2015 unsigned OpOpcode = Operand.Val->getOpcode();
2016 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002017 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002018 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002019 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002020 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002021 assert(VT.isFloatingPoint() &&
2022 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002023 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002024 if (Operand.getOpcode() == ISD::UNDEF)
2025 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002026 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002027 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002028 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002029 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002030 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002031 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002032 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002033 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2034 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2035 break;
2036 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002037 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002038 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002039 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002040 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002041 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002042 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002043 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002044 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002045 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002046 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002047 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002048 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002049 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002050 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002051 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2052 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2053 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2054 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002055 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002056 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002057 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002058 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002059 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002060 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002061 if (OpOpcode == ISD::TRUNCATE)
2062 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002063 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2064 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002065 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002066 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002067 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002068 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002069 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2070 else
2071 return Operand.Val->getOperand(0);
2072 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002073 break;
Chris Lattner35481892005-12-23 00:16:34 +00002074 case ISD::BIT_CONVERT:
2075 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002076 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002077 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002078 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002079 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2080 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002081 if (OpOpcode == ISD::UNDEF)
2082 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002083 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002084 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2086 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002087 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002088 if (OpOpcode == ISD::UNDEF)
2089 return getNode(ISD::UNDEF, VT);
2090 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2091 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2092 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2093 Operand.getConstantOperandVal(1) == 0 &&
2094 Operand.getOperand(0).getValueType() == VT)
2095 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002096 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002097 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002098 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2099 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002100 Operand.Val->getOperand(0));
2101 if (OpOpcode == ISD::FNEG) // --X -> X
2102 return Operand.Val->getOperand(0);
2103 break;
2104 case ISD::FABS:
2105 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2106 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2107 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002108 }
2109
Chris Lattner43247a12005-08-25 19:12:10 +00002110 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002111 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002112 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002113 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002114 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002115 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002116 void *IP = 0;
2117 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2118 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002119 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002120 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002121 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002122 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002123 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002124 AllNodes.push_back(N);
2125 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002126}
2127
Chris Lattner36019aa2005-04-18 03:48:41 +00002128
2129
Duncan Sands83ec4b62008-06-06 12:08:01 +00002130SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002131 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002132 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2133 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002134 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002135 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002136 case ISD::TokenFactor:
2137 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2138 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002139 // Fold trivial token factors.
2140 if (N1.getOpcode() == ISD::EntryToken) return N2;
2141 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002142 break;
Chris Lattner76365122005-01-16 02:23:22 +00002143 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002144 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002145 N1.getValueType() == VT && "Binary operator types must match!");
2146 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2147 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002148 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002149 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002150 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2151 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002152 break;
Chris Lattner76365122005-01-16 02:23:22 +00002153 case ISD::OR:
2154 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002155 case ISD::ADD:
2156 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002157 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002158 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002159 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2160 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002161 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002162 return N1;
2163 break;
Chris Lattner76365122005-01-16 02:23:22 +00002164 case ISD::UDIV:
2165 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002166 case ISD::MULHU:
2167 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002168 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002169 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002170 case ISD::MUL:
2171 case ISD::SDIV:
2172 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002173 case ISD::FADD:
2174 case ISD::FSUB:
2175 case ISD::FMUL:
2176 case ISD::FDIV:
2177 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002178 assert(N1.getValueType() == N2.getValueType() &&
2179 N1.getValueType() == VT && "Binary operator types must match!");
2180 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002181 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2182 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 N1.getValueType().isFloatingPoint() &&
2184 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002185 "Invalid FCOPYSIGN!");
2186 break;
Chris Lattner76365122005-01-16 02:23:22 +00002187 case ISD::SHL:
2188 case ISD::SRA:
2189 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002190 case ISD::ROTL:
2191 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002192 assert(VT == N1.getValueType() &&
2193 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002195 "Shifts only work on integers");
2196
2197 // Always fold shifts of i1 values so the code generator doesn't need to
2198 // handle them. Since we know the size of the shift has to be less than the
2199 // size of the value, the shift/rotate count is guaranteed to be zero.
2200 if (VT == MVT::i1)
2201 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002202 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002203 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002205 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002207 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002208 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002209 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002210 break;
2211 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002212 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002213 assert(VT.isFloatingPoint() &&
2214 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002215 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002216 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002217 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002218 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002219 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002220 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002222 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002224 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002225 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002226 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002227 break;
2228 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002229 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002230 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002231 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002232 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002233 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002234 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002235 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002236
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002237 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002238 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002239 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002240 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002241 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002242 return getConstant(Val, VT);
2243 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002244 break;
2245 }
2246 case ISD::EXTRACT_VECTOR_ELT:
2247 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2248
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002249 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2250 if (N1.getOpcode() == ISD::UNDEF)
2251 return getNode(ISD::UNDEF, VT);
2252
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002253 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2254 // expanding copies of large vectors from registers.
2255 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2256 N1.getNumOperands() > 0) {
2257 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002258 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002259 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2260 N1.getOperand(N2C->getValue() / Factor),
2261 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2262 }
2263
2264 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2265 // expanding large vector constants.
2266 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2267 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002268
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002269 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2270 // operations are lowered to scalars.
2271 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2272 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2273 if (IEC == N2C)
2274 return N1.getOperand(1);
2275 else
2276 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2277 }
2278 break;
2279 case ISD::EXTRACT_ELEMENT:
2280 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002281 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2282 (N1.getValueType().isInteger() == VT.isInteger()) &&
2283 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002284
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002285 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2286 // 64-bit integers into 32-bit parts. Instead of building the extract of
2287 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2288 if (N1.getOpcode() == ISD::BUILD_PAIR)
2289 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002290
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002291 // EXTRACT_ELEMENT of a constant int is also very common.
2292 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002293 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002294 unsigned Shift = ElementSize * N2C->getValue();
2295 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2296 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002297 }
2298 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002299 case ISD::EXTRACT_SUBVECTOR:
2300 if (N1.getValueType() == VT) // Trivial extraction.
2301 return N1;
2302 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002303 }
2304
2305 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002306 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002307 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002308 switch (Opcode) {
2309 case ISD::ADD: return getConstant(C1 + C2, VT);
2310 case ISD::SUB: return getConstant(C1 - C2, VT);
2311 case ISD::MUL: return getConstant(C1 * C2, VT);
2312 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002313 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002314 break;
2315 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002316 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002317 break;
2318 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002319 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 break;
2321 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002322 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 break;
2324 case ISD::AND : return getConstant(C1 & C2, VT);
2325 case ISD::OR : return getConstant(C1 | C2, VT);
2326 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002327 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002328 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2329 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2330 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2331 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002332 default: break;
2333 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 } else { // Cannonicalize constant to RHS if commutative
2335 if (isCommutativeBinOp(Opcode)) {
2336 std::swap(N1C, N2C);
2337 std::swap(N1, N2);
2338 }
2339 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002340 }
2341
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002342 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002343 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2344 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002345 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002346 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2347 // Cannonicalize constant to RHS if commutative
2348 std::swap(N1CFP, N2CFP);
2349 std::swap(N1, N2);
2350 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002351 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2352 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002353 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002354 case ISD::FADD:
2355 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002356 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002357 return getConstantFP(V1, VT);
2358 break;
2359 case ISD::FSUB:
2360 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2361 if (s!=APFloat::opInvalidOp)
2362 return getConstantFP(V1, VT);
2363 break;
2364 case ISD::FMUL:
2365 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2366 if (s!=APFloat::opInvalidOp)
2367 return getConstantFP(V1, VT);
2368 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002369 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002370 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2371 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2372 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002374 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002375 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2376 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2377 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002378 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002379 case ISD::FCOPYSIGN:
2380 V1.copySign(V2);
2381 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002382 default: break;
2383 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002385 }
Chris Lattner62b57722006-04-20 05:39:12 +00002386
2387 // Canonicalize an UNDEF to the RHS, even over a constant.
2388 if (N1.getOpcode() == ISD::UNDEF) {
2389 if (isCommutativeBinOp(Opcode)) {
2390 std::swap(N1, N2);
2391 } else {
2392 switch (Opcode) {
2393 case ISD::FP_ROUND_INREG:
2394 case ISD::SIGN_EXTEND_INREG:
2395 case ISD::SUB:
2396 case ISD::FSUB:
2397 case ISD::FDIV:
2398 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002399 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002400 return N1; // fold op(undef, arg2) -> undef
2401 case ISD::UDIV:
2402 case ISD::SDIV:
2403 case ISD::UREM:
2404 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002405 case ISD::SRL:
2406 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002407 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002408 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2409 // For vectors, we can't easily build an all zero vector, just return
2410 // the LHS.
2411 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002412 }
2413 }
2414 }
2415
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002416 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002417 if (N2.getOpcode() == ISD::UNDEF) {
2418 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002419 case ISD::XOR:
2420 if (N1.getOpcode() == ISD::UNDEF)
2421 // Handle undef ^ undef -> 0 special case. This is a common
2422 // idiom (misuse).
2423 return getConstant(0, VT);
2424 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002425 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002426 case ISD::ADDC:
2427 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002428 case ISD::SUB:
2429 case ISD::FADD:
2430 case ISD::FSUB:
2431 case ISD::FMUL:
2432 case ISD::FDIV:
2433 case ISD::FREM:
2434 case ISD::UDIV:
2435 case ISD::SDIV:
2436 case ISD::UREM:
2437 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002438 return N2; // fold op(arg1, undef) -> undef
2439 case ISD::MUL:
2440 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002441 case ISD::SRL:
2442 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002443 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002444 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2445 // For vectors, we can't easily build an all zero vector, just return
2446 // the LHS.
2447 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002448 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002449 if (!VT.isVector())
2450 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002451 // For vectors, we can't easily build an all one vector, just return
2452 // the LHS.
2453 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002454 case ISD::SRA:
2455 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002456 }
2457 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002458
Chris Lattner27e9b412005-05-11 18:57:39 +00002459 // Memoize this node if possible.
2460 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002461 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002462 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002463 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002464 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002465 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002466 void *IP = 0;
2467 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2468 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002469 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002470 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002471 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002472 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002473 }
2474
Chris Lattnerc3aae252005-01-07 07:46:32 +00002475 AllNodes.push_back(N);
2476 return SDOperand(N, 0);
2477}
2478
Duncan Sands83ec4b62008-06-06 12:08:01 +00002479SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002480 SDOperand N1, SDOperand N2, SDOperand N3) {
2481 // Perform various simplifications.
2482 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2483 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002485 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002486 // Use FoldSetCC to simplify SETCC's.
2487 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002488 if (Simp.Val) return Simp;
2489 break;
2490 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002491 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002492 if (N1C) {
2493 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002494 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002495 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002496 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002497 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002498
2499 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002501 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002502 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002503 if (N2C->getValue()) // Unconditional branch
2504 return getNode(ISD::BR, MVT::Other, N1, N3);
2505 else
2506 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002507 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002508 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002509 case ISD::VECTOR_SHUFFLE:
2510 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002512 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002513 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002514 "Illegal VECTOR_SHUFFLE node!");
2515 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002516 case ISD::BIT_CONVERT:
2517 // Fold bit_convert nodes from a type to themselves.
2518 if (N1.getValueType() == VT)
2519 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002520 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002521 }
2522
Chris Lattner43247a12005-08-25 19:12:10 +00002523 // Memoize node if it doesn't produce a flag.
2524 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002525 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002526 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002527 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002528 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002529 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002530 void *IP = 0;
2531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2532 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002533 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002534 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002535 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002536 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002537 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002538 AllNodes.push_back(N);
2539 return SDOperand(N, 0);
2540}
2541
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002543 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002544 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002545 SDOperand Ops[] = { N1, N2, N3, N4 };
2546 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002547}
2548
Duncan Sands83ec4b62008-06-06 12:08:01 +00002549SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002550 SDOperand N1, SDOperand N2, SDOperand N3,
2551 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002552 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2553 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002554}
2555
Dan Gohman707e0182008-04-12 04:36:06 +00002556/// getMemsetValue - Vectorized representation of the memset value
2557/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2559 unsigned NumBits = VT.isVector() ?
2560 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002561 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002562 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002563 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002564 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002565 Val = (Val << Shift) | Val;
2566 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002567 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002569 return DAG.getConstant(Val, VT);
2570 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002571 }
Evan Chengf0df0312008-05-15 08:39:06 +00002572
2573 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2574 unsigned Shift = 8;
2575 for (unsigned i = NumBits; i > 8; i >>= 1) {
2576 Value = DAG.getNode(ISD::OR, VT,
2577 DAG.getNode(ISD::SHL, VT, Value,
2578 DAG.getConstant(Shift, MVT::i8)), Value);
2579 Shift <<= 1;
2580 }
2581
2582 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002583}
2584
Dan Gohman707e0182008-04-12 04:36:06 +00002585/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2586/// used when a memcpy is turned into a memset when the source is a constant
2587/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002588static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002589 const TargetLowering &TLI,
2590 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002591 // Handle vector with all elements zero.
2592 if (Str.empty()) {
2593 if (VT.isInteger())
2594 return DAG.getConstant(0, VT);
2595 unsigned NumElts = VT.getVectorNumElements();
2596 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2597 return DAG.getNode(ISD::BIT_CONVERT, VT,
2598 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2599 }
2600
Duncan Sands83ec4b62008-06-06 12:08:01 +00002601 assert(!VT.isVector() && "Can't handle vector type here!");
2602 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002603 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002604 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002605 if (TLI.isLittleEndian())
2606 Offset = Offset + MSB - 1;
2607 for (unsigned i = 0; i != MSB; ++i) {
2608 Val = (Val << 8) | (unsigned char)Str[Offset];
2609 Offset += TLI.isLittleEndian() ? -1 : 1;
2610 }
2611 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002612}
2613
Dan Gohman707e0182008-04-12 04:36:06 +00002614/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002615///
Dan Gohman707e0182008-04-12 04:36:06 +00002616static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2617 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002618 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002619 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2620}
2621
Evan Chengf0df0312008-05-15 08:39:06 +00002622/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2623///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002624static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002625 unsigned SrcDelta = 0;
2626 GlobalAddressSDNode *G = NULL;
2627 if (Src.getOpcode() == ISD::GlobalAddress)
2628 G = cast<GlobalAddressSDNode>(Src);
2629 else if (Src.getOpcode() == ISD::ADD &&
2630 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2631 Src.getOperand(1).getOpcode() == ISD::Constant) {
2632 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2633 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2634 }
2635 if (!G)
2636 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002637
Evan Chengf0df0312008-05-15 08:39:06 +00002638 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002639 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2640 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002641
Evan Chengf0df0312008-05-15 08:39:06 +00002642 return false;
2643}
Dan Gohman707e0182008-04-12 04:36:06 +00002644
Evan Chengf0df0312008-05-15 08:39:06 +00002645/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2646/// to replace the memset / memcpy is below the threshold. It also returns the
2647/// types of the sequence of memory ops to perform memset / memcpy.
2648static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002650 SDOperand Dst, SDOperand Src,
2651 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002652 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002653 SelectionDAG &DAG,
2654 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002655 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002656 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002657 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002658 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002659 if (VT != MVT::iAny) {
2660 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002662 // If source is a string constant, this will require an unaligned load.
2663 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2664 if (Dst.getOpcode() != ISD::FrameIndex) {
2665 // Can't change destination alignment. It requires a unaligned store.
2666 if (AllowUnalign)
2667 VT = MVT::iAny;
2668 } else {
2669 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2670 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2671 if (MFI->isFixedObjectIndex(FI)) {
2672 // Can't change destination alignment. It requires a unaligned store.
2673 if (AllowUnalign)
2674 VT = MVT::iAny;
2675 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002676 // Give the stack frame object a larger alignment if needed.
2677 if (MFI->getObjectAlignment(FI) < NewAlign)
2678 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002679 Align = NewAlign;
2680 }
2681 }
2682 }
2683 }
2684
2685 if (VT == MVT::iAny) {
2686 if (AllowUnalign) {
2687 VT = MVT::i64;
2688 } else {
2689 switch (Align & 7) {
2690 case 0: VT = MVT::i64; break;
2691 case 4: VT = MVT::i32; break;
2692 case 2: VT = MVT::i16; break;
2693 default: VT = MVT::i8; break;
2694 }
2695 }
2696
Duncan Sands83ec4b62008-06-06 12:08:01 +00002697 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002698 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002699 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2700 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002701
Duncan Sands8e4eb092008-06-08 20:54:56 +00002702 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002703 VT = LVT;
2704 }
Dan Gohman707e0182008-04-12 04:36:06 +00002705
2706 unsigned NumMemOps = 0;
2707 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002708 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002709 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002710 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002711 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002712 VT = MVT::i64;
2713 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002714 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2715 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002716 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002717 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002718 VTSize >>= 1;
2719 }
Dan Gohman707e0182008-04-12 04:36:06 +00002720 }
Dan Gohman707e0182008-04-12 04:36:06 +00002721
2722 if (++NumMemOps > Limit)
2723 return false;
2724 MemOps.push_back(VT);
2725 Size -= VTSize;
2726 }
2727
2728 return true;
2729}
2730
2731static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2732 SDOperand Chain, SDOperand Dst,
2733 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002734 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002735 const Value *DstSV, uint64_t DstSVOff,
2736 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002737 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2738
Dan Gohman21323f32008-05-29 19:42:22 +00002739 // Expand memcpy to a series of load and store ops if the size operand falls
2740 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002741 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002742 uint64_t Limit = -1;
2743 if (!AlwaysInline)
2744 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002745 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002746 std::string Str;
2747 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002748 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002749 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002750 return SDOperand();
2751
Dan Gohman707e0182008-04-12 04:36:06 +00002752
Evan Cheng0ff39b32008-06-30 07:31:25 +00002753 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002754 SmallVector<SDOperand, 8> OutChains;
2755 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002756 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002757 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758 MVT VT = MemOps[i];
2759 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002760 SDOperand Value, Store;
2761
Evan Cheng0ff39b32008-06-30 07:31:25 +00002762 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002763 // It's unlikely a store of a vector immediate can be done in a single
2764 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002765 // We also handle store a vector with all zero's.
2766 // FIXME: Handle other cases where store of vector immediate is done in
2767 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002768 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002769 Store = DAG.getStore(Chain, Value,
2770 getMemBasePlusOffset(Dst, DstOff, DAG),
2771 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002772 } else {
2773 Value = DAG.getLoad(VT, Chain,
2774 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002775 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002776 Store = DAG.getStore(Chain, Value,
2777 getMemBasePlusOffset(Dst, DstOff, DAG),
2778 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002779 }
2780 OutChains.push_back(Store);
2781 SrcOff += VTSize;
2782 DstOff += VTSize;
2783 }
2784
2785 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2786 &OutChains[0], OutChains.size());
2787}
2788
Dan Gohman21323f32008-05-29 19:42:22 +00002789static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2790 SDOperand Chain, SDOperand Dst,
2791 SDOperand Src, uint64_t Size,
2792 unsigned Align, bool AlwaysInline,
2793 const Value *DstSV, uint64_t DstSVOff,
2794 const Value *SrcSV, uint64_t SrcSVOff){
2795 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2796
2797 // Expand memmove to a series of load and store ops if the size operand falls
2798 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002799 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002800 uint64_t Limit = -1;
2801 if (!AlwaysInline)
2802 Limit = TLI.getMaxStoresPerMemmove();
2803 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002804 std::string Str;
2805 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002806 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002807 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002808 return SDOperand();
2809
Dan Gohman21323f32008-05-29 19:42:22 +00002810 uint64_t SrcOff = 0, DstOff = 0;
2811
2812 SmallVector<SDOperand, 8> LoadValues;
2813 SmallVector<SDOperand, 8> LoadChains;
2814 SmallVector<SDOperand, 8> OutChains;
2815 unsigned NumMemOps = MemOps.size();
2816 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002817 MVT VT = MemOps[i];
2818 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002819 SDOperand Value, Store;
2820
2821 Value = DAG.getLoad(VT, Chain,
2822 getMemBasePlusOffset(Src, SrcOff, DAG),
2823 SrcSV, SrcSVOff + SrcOff, false, Align);
2824 LoadValues.push_back(Value);
2825 LoadChains.push_back(Value.getValue(1));
2826 SrcOff += VTSize;
2827 }
2828 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2829 &LoadChains[0], LoadChains.size());
2830 OutChains.clear();
2831 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002832 MVT VT = MemOps[i];
2833 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002834 SDOperand Value, Store;
2835
2836 Store = DAG.getStore(Chain, LoadValues[i],
2837 getMemBasePlusOffset(Dst, DstOff, DAG),
2838 DstSV, DstSVOff + DstOff, false, DstAlign);
2839 OutChains.push_back(Store);
2840 DstOff += VTSize;
2841 }
2842
2843 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2844 &OutChains[0], OutChains.size());
2845}
2846
Dan Gohman707e0182008-04-12 04:36:06 +00002847static SDOperand getMemsetStores(SelectionDAG &DAG,
2848 SDOperand Chain, SDOperand Dst,
2849 SDOperand Src, uint64_t Size,
2850 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002851 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002852 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2853
2854 // Expand memset to a series of load/store ops if the size operand
2855 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002856 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002857 std::string Str;
2858 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002859 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002860 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002861 return SDOperand();
2862
2863 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002864 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002865
2866 unsigned NumMemOps = MemOps.size();
2867 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 MVT VT = MemOps[i];
2869 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002870 SDOperand Value = getMemsetValue(Src, VT, DAG);
2871 SDOperand Store = DAG.getStore(Chain, Value,
2872 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002873 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002874 OutChains.push_back(Store);
2875 DstOff += VTSize;
2876 }
2877
2878 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2879 &OutChains[0], OutChains.size());
2880}
2881
2882SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002883 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002884 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002885 const Value *DstSV, uint64_t DstSVOff,
2886 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002887
2888 // Check to see if we should lower the memcpy to loads and stores first.
2889 // For cases within the target-specified limits, this is the best choice.
2890 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2891 if (ConstantSize) {
2892 // Memcpy with size zero? Just return the original chain.
2893 if (ConstantSize->isNullValue())
2894 return Chain;
2895
2896 SDOperand Result =
2897 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002898 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002899 if (Result.Val)
2900 return Result;
2901 }
2902
2903 // Then check to see if we should lower the memcpy with target-specific
2904 // code. If the target chooses to do this, this is the next best.
2905 SDOperand Result =
2906 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2907 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002908 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002909 if (Result.Val)
2910 return Result;
2911
2912 // If we really need inline code and the target declined to provide it,
2913 // use a (potentially long) sequence of loads and stores.
2914 if (AlwaysInline) {
2915 assert(ConstantSize && "AlwaysInline requires a constant size!");
2916 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2917 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002918 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002919 }
2920
2921 // Emit a library call.
2922 TargetLowering::ArgListTy Args;
2923 TargetLowering::ArgListEntry Entry;
2924 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2925 Entry.Node = Dst; Args.push_back(Entry);
2926 Entry.Node = Src; Args.push_back(Entry);
2927 Entry.Node = Size; Args.push_back(Entry);
2928 std::pair<SDOperand,SDOperand> CallResult =
2929 TLI.LowerCallTo(Chain, Type::VoidTy,
2930 false, false, false, CallingConv::C, false,
2931 getExternalSymbol("memcpy", TLI.getPointerTy()),
2932 Args, *this);
2933 return CallResult.second;
2934}
2935
2936SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2937 SDOperand Src, SDOperand Size,
2938 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002939 const Value *DstSV, uint64_t DstSVOff,
2940 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002941
Dan Gohman21323f32008-05-29 19:42:22 +00002942 // Check to see if we should lower the memmove to loads and stores first.
2943 // For cases within the target-specified limits, this is the best choice.
2944 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2945 if (ConstantSize) {
2946 // Memmove with size zero? Just return the original chain.
2947 if (ConstantSize->isNullValue())
2948 return Chain;
2949
2950 SDOperand Result =
2951 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2952 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2953 if (Result.Val)
2954 return Result;
2955 }
Dan Gohman707e0182008-04-12 04:36:06 +00002956
2957 // Then check to see if we should lower the memmove with target-specific
2958 // code. If the target chooses to do this, this is the next best.
2959 SDOperand Result =
2960 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002961 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002962 if (Result.Val)
2963 return Result;
2964
2965 // Emit a library call.
2966 TargetLowering::ArgListTy Args;
2967 TargetLowering::ArgListEntry Entry;
2968 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2969 Entry.Node = Dst; Args.push_back(Entry);
2970 Entry.Node = Src; Args.push_back(Entry);
2971 Entry.Node = Size; Args.push_back(Entry);
2972 std::pair<SDOperand,SDOperand> CallResult =
2973 TLI.LowerCallTo(Chain, Type::VoidTy,
2974 false, false, false, CallingConv::C, false,
2975 getExternalSymbol("memmove", TLI.getPointerTy()),
2976 Args, *this);
2977 return CallResult.second;
2978}
2979
2980SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2981 SDOperand Src, SDOperand Size,
2982 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002983 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002984
2985 // Check to see if we should lower the memset to stores first.
2986 // For cases within the target-specified limits, this is the best choice.
2987 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2988 if (ConstantSize) {
2989 // Memset with size zero? Just return the original chain.
2990 if (ConstantSize->isNullValue())
2991 return Chain;
2992
2993 SDOperand Result =
2994 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002995 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002996 if (Result.Val)
2997 return Result;
2998 }
2999
3000 // Then check to see if we should lower the memset with target-specific
3001 // code. If the target chooses to do this, this is the next best.
3002 SDOperand Result =
3003 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003004 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003005 if (Result.Val)
3006 return Result;
3007
3008 // Emit a library call.
3009 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3010 TargetLowering::ArgListTy Args;
3011 TargetLowering::ArgListEntry Entry;
3012 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3013 Args.push_back(Entry);
3014 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003015 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003016 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3017 else
3018 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3019 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3020 Args.push_back(Entry);
3021 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3022 Args.push_back(Entry);
3023 std::pair<SDOperand,SDOperand> CallResult =
3024 TLI.LowerCallTo(Chain, Type::VoidTy,
3025 false, false, false, CallingConv::C, false,
3026 getExternalSymbol("memset", TLI.getPointerTy()),
3027 Args, *this);
3028 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003029}
3030
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003031SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003032 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003033 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003034 unsigned Alignment) {
3035 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003036 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3037 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003038 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003039 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003040 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003041 void* IP = 0;
3042 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3043 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003044 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003045 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003046 CSEMap.InsertNode(N, IP);
3047 AllNodes.push_back(N);
3048 return SDOperand(N, 0);
3049}
3050
3051SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003052 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003053 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003054 unsigned Alignment) {
3055 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003056 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3057 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003058 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003059 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3060 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003061 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003062 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003063 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003064 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003065 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003066 void* IP = 0;
3067 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3068 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003069 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003070 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003071 CSEMap.InsertNode(N, IP);
3072 AllNodes.push_back(N);
3073 return SDOperand(N, 0);
3074}
3075
Duncan Sands14ea39c2008-03-27 20:23:40 +00003076SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003077SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003078 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003079 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003080 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003081 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003082 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3083 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003084 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003085 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003086 } else if (SV) {
3087 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3088 assert(PT && "Value for load must be a pointer");
3089 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003090 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003091 assert(Ty && "Could not get type information for load");
3092 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3093 }
Evan Cheng466685d2006-10-09 20:57:25 +00003094
Duncan Sands14ea39c2008-03-27 20:23:40 +00003095 if (VT == EVT) {
3096 ExtType = ISD::NON_EXTLOAD;
3097 } else if (ExtType == ISD::NON_EXTLOAD) {
3098 assert(VT == EVT && "Non-extending load from different memory type!");
3099 } else {
3100 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003101 if (VT.isVector())
3102 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003103 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003104 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003106 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003107 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003108 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003109 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003110 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003111
3112 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003113 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003114 "Unindexed load with an offset!");
3115
3116 SDVTList VTs = Indexed ?
3117 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3118 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003119 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003120 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003121 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003122 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003123 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003124 ID.AddInteger(Alignment);
3125 ID.AddInteger(isVolatile);
3126 void *IP = 0;
3127 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3128 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003129 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3130 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003131 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003132 AllNodes.push_back(N);
3133 return SDOperand(N, 0);
3134}
3135
Duncan Sands83ec4b62008-06-06 12:08:01 +00003136SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003137 SDOperand Chain, SDOperand Ptr,
3138 const Value *SV, int SVOffset,
3139 bool isVolatile, unsigned Alignment) {
3140 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003141 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3142 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003143}
3144
Duncan Sands83ec4b62008-06-06 12:08:01 +00003145SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003146 SDOperand Chain, SDOperand Ptr,
3147 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003148 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003149 bool isVolatile, unsigned Alignment) {
3150 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003151 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3152 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003153}
3154
Evan Cheng144d8f02006-11-09 17:55:04 +00003155SDOperand
3156SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3157 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003158 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003159 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3160 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003161 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3162 LD->getChain(), Base, Offset, LD->getSrcValue(),
3163 LD->getSrcValueOffset(), LD->getMemoryVT(),
3164 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003165}
3166
Jeff Cohend41b30d2006-11-05 19:31:28 +00003167SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003168 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003169 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003170 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003171
Dan Gohman575e2f42007-06-04 15:49:41 +00003172 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3173 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003174 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003175 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003176 } else if (SV) {
3177 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3178 assert(PT && "Value for store must be a pointer");
3179 Ty = PT->getElementType();
3180 }
3181 assert(Ty && "Could not get type information for store");
3182 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3183 }
Evan Chengad071e12006-10-05 22:57:11 +00003184 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003185 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003186 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003187 FoldingSetNodeID ID;
3188 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003189 ID.AddInteger(ISD::UNINDEXED);
3190 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003191 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003192 ID.AddInteger(Alignment);
3193 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003194 void *IP = 0;
3195 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3196 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003197 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003198 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003199 CSEMap.InsertNode(N, IP);
3200 AllNodes.push_back(N);
3201 return SDOperand(N, 0);
3202}
3203
Jeff Cohend41b30d2006-11-05 19:31:28 +00003204SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003205 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003207 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003208 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003209
3210 if (VT == SVT)
3211 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003212
Duncan Sands8e4eb092008-06-08 20:54:56 +00003213 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003214 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003215 "Can't do FP-INT conversion!");
3216
Dan Gohman575e2f42007-06-04 15:49:41 +00003217 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3218 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003219 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003220 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003221 } else if (SV) {
3222 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3223 assert(PT && "Value for store must be a pointer");
3224 Ty = PT->getElementType();
3225 }
3226 assert(Ty && "Could not get type information for store");
3227 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3228 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003229 SDVTList VTs = getVTList(MVT::Other);
3230 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003231 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003232 FoldingSetNodeID ID;
3233 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003234 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003235 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003236 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003237 ID.AddInteger(Alignment);
3238 ID.AddInteger(isVolatile);
3239 void *IP = 0;
3240 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3241 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003242 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003243 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003244 CSEMap.InsertNode(N, IP);
3245 AllNodes.push_back(N);
3246 return SDOperand(N, 0);
3247}
3248
Evan Cheng144d8f02006-11-09 17:55:04 +00003249SDOperand
3250SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3251 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003252 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3253 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3254 "Store is already a indexed store!");
3255 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3256 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3257 FoldingSetNodeID ID;
3258 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3259 ID.AddInteger(AM);
3260 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003261 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003262 ID.AddInteger(ST->getAlignment());
3263 ID.AddInteger(ST->isVolatile());
3264 void *IP = 0;
3265 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3266 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003267 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003268 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003269 ST->getSrcValue(), ST->getSrcValueOffset(),
3270 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003271 CSEMap.InsertNode(N, IP);
3272 AllNodes.push_back(N);
3273 return SDOperand(N, 0);
3274}
3275
Duncan Sands83ec4b62008-06-06 12:08:01 +00003276SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003277 SDOperand Chain, SDOperand Ptr,
3278 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003279 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003280 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003281}
3282
Duncan Sands83ec4b62008-06-06 12:08:01 +00003283SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003284 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003285 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003286 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003287 case 1: return getNode(Opcode, VT, Ops[0]);
3288 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3289 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003290 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003291 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003292
Chris Lattneref847df2005-04-09 03:27:28 +00003293 switch (Opcode) {
3294 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003295 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003296 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003297 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3298 "LHS and RHS of condition must have same type!");
3299 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3300 "True and False arms of SelectCC must have same type!");
3301 assert(Ops[2].getValueType() == VT &&
3302 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003303 break;
3304 }
3305 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003306 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003307 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3308 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003309 break;
3310 }
Chris Lattneref847df2005-04-09 03:27:28 +00003311 }
3312
Chris Lattner385328c2005-05-14 07:42:29 +00003313 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003314 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003315 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003316 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003317 FoldingSetNodeID ID;
3318 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003319 void *IP = 0;
3320 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3321 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003322 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003323 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003324 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003325 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003326 }
Chris Lattneref847df2005-04-09 03:27:28 +00003327 AllNodes.push_back(N);
3328 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003329}
3330
Chris Lattner89c34632005-05-14 06:20:26 +00003331SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003332 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003333 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003334 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3335 Ops, NumOps);
3336}
3337
3338SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003339 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003340 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003341 if (NumVTs == 1)
3342 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003343 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3344}
3345
3346SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003347 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003348 if (VTList.NumVTs == 1)
3349 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003350
Chris Lattner5f056bf2005-07-10 01:55:33 +00003351 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003352 // FIXME: figure out how to safely handle things like
3353 // int foo(int x) { return 1 << (x & 255); }
3354 // int bar() { return foo(256); }
3355#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003356 case ISD::SRA_PARTS:
3357 case ISD::SRL_PARTS:
3358 case ISD::SHL_PARTS:
3359 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003360 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003361 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3362 else if (N3.getOpcode() == ISD::AND)
3363 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3364 // If the and is only masking out bits that cannot effect the shift,
3365 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003366 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003367 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3368 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3369 }
3370 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003371#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003372 }
Chris Lattner89c34632005-05-14 06:20:26 +00003373
Chris Lattner43247a12005-08-25 19:12:10 +00003374 // Memoize the node unless it returns a flag.
3375 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003376 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003377 FoldingSetNodeID ID;
3378 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003379 void *IP = 0;
3380 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3381 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003382 if (NumOps == 1)
3383 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3384 else if (NumOps == 2)
3385 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3386 else if (NumOps == 3)
3387 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3388 else
3389 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003390 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003391 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003392 if (NumOps == 1)
3393 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3394 else if (NumOps == 2)
3395 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3396 else if (NumOps == 3)
3397 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3398 else
3399 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003400 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003401 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003402 return SDOperand(N, 0);
3403}
3404
Dan Gohman08ce9762007-10-08 15:49:58 +00003405SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003406 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003407}
3408
3409SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3410 SDOperand N1) {
3411 SDOperand Ops[] = { N1 };
3412 return getNode(Opcode, VTList, Ops, 1);
3413}
3414
3415SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3416 SDOperand N1, SDOperand N2) {
3417 SDOperand Ops[] = { N1, N2 };
3418 return getNode(Opcode, VTList, Ops, 2);
3419}
3420
3421SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3422 SDOperand N1, SDOperand N2, SDOperand N3) {
3423 SDOperand Ops[] = { N1, N2, N3 };
3424 return getNode(Opcode, VTList, Ops, 3);
3425}
3426
3427SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3428 SDOperand N1, SDOperand N2, SDOperand N3,
3429 SDOperand N4) {
3430 SDOperand Ops[] = { N1, N2, N3, N4 };
3431 return getNode(Opcode, VTList, Ops, 4);
3432}
3433
3434SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3435 SDOperand N1, SDOperand N2, SDOperand N3,
3436 SDOperand N4, SDOperand N5) {
3437 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3438 return getNode(Opcode, VTList, Ops, 5);
3439}
3440
Duncan Sands83ec4b62008-06-06 12:08:01 +00003441SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003442 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003443}
3444
Duncan Sands83ec4b62008-06-06 12:08:01 +00003445SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3446 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003447 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003448 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003449 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003450 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003451 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003452 V.push_back(VT1);
3453 V.push_back(VT2);
3454 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003455 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003456}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003457SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3458 MVT VT3) {
3459 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003460 E = VTList.end(); I != E; ++I) {
3461 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3462 (*I)[2] == VT3)
3463 return makeVTList(&(*I)[0], 3);
3464 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003465 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003466 V.push_back(VT1);
3467 V.push_back(VT2);
3468 V.push_back(VT3);
3469 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003470 return makeVTList(&(*VTList.begin())[0], 3);
3471}
3472
Duncan Sands83ec4b62008-06-06 12:08:01 +00003473SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003474 switch (NumVTs) {
3475 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003476 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003477 case 2: return getVTList(VTs[0], VTs[1]);
3478 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3479 default: break;
3480 }
3481
Duncan Sands83ec4b62008-06-06 12:08:01 +00003482 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003483 E = VTList.end(); I != E; ++I) {
3484 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3485
3486 bool NoMatch = false;
3487 for (unsigned i = 2; i != NumVTs; ++i)
3488 if (VTs[i] != (*I)[i]) {
3489 NoMatch = true;
3490 break;
3491 }
3492 if (!NoMatch)
3493 return makeVTList(&*I->begin(), NumVTs);
3494 }
3495
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003497 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003498}
3499
3500
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003501/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3502/// specified operands. If the resultant node already exists in the DAG,
3503/// this does not modify the specified node, instead it returns the node that
3504/// already exists. If the resultant node does not exist in the DAG, the
3505/// input node is returned. As a degenerate case, if you specify the same
3506/// input operands as the node already has, the input node is returned.
3507SDOperand SelectionDAG::
3508UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3509 SDNode *N = InN.Val;
3510 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3511
3512 // Check to see if there is no change.
3513 if (Op == N->getOperand(0)) return InN;
3514
3515 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003516 void *InsertPos = 0;
3517 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3518 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003519
3520 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003521 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003522 RemoveNodeFromCSEMaps(N);
3523
3524 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003525 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003526 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003527 N->OperandList[0].setUser(N);
3528 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003529
3530 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003531 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003532 return InN;
3533}
3534
3535SDOperand SelectionDAG::
3536UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3537 SDNode *N = InN.Val;
3538 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3539
3540 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003541 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3542 return InN; // No operands changed, just return the input node.
3543
3544 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003545 void *InsertPos = 0;
3546 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3547 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003548
3549 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003550 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003551 RemoveNodeFromCSEMaps(N);
3552
3553 // Now we update the operands.
3554 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003555 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003556 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003557 N->OperandList[0].setUser(N);
3558 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003559 }
3560 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003561 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003562 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003563 N->OperandList[1].setUser(N);
3564 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003565 }
3566
3567 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003568 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003569 return InN;
3570}
3571
3572SDOperand SelectionDAG::
3573UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003574 SDOperand Ops[] = { Op1, Op2, Op3 };
3575 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003576}
3577
3578SDOperand SelectionDAG::
3579UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3580 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003581 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3582 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003583}
3584
3585SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003586UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3587 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003588 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3589 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003590}
3591
Chris Lattner809ec112006-01-28 10:09:25 +00003592SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003593UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003594 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003595 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003596 "Update with wrong number of operands");
3597
3598 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003599 bool AnyChange = false;
3600 for (unsigned i = 0; i != NumOps; ++i) {
3601 if (Ops[i] != N->getOperand(i)) {
3602 AnyChange = true;
3603 break;
3604 }
3605 }
3606
3607 // No operands changed, just return the input node.
3608 if (!AnyChange) return InN;
3609
3610 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003611 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003612 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003613 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003614
Dan Gohman7ceda162008-05-02 00:05:03 +00003615 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003616 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003617 RemoveNodeFromCSEMaps(N);
3618
3619 // Now we update the operands.
3620 for (unsigned i = 0; i != NumOps; ++i) {
3621 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003622 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003623 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003624 N->OperandList[i].setUser(N);
3625 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003626 }
3627 }
3628
3629 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003630 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003631 return InN;
3632}
3633
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003634/// MorphNodeTo - This frees the operands of the current node, resets the
3635/// opcode, types, and operands to the specified value. This should only be
3636/// used by the SelectionDAG class.
3637void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003638 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003639 NodeType = Opc;
3640 ValueList = L.VTs;
3641 NumValues = L.NumVTs;
3642
3643 // Clear the operands list, updating used nodes to remove this from their
3644 // use list.
3645 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003646 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003647
3648 // If NumOps is larger than the # of operands we currently have, reallocate
3649 // the operand list.
3650 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003651 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003652 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003653 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003654 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003655 OperandsNeedDelete = true;
3656 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003657
3658 // Assign the new operands.
3659 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003660
3661 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3662 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003663 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003664 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003665 N->addUser(i, this);
3666 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003667 }
3668}
Chris Lattner149c58c2005-08-16 18:17:10 +00003669
3670/// SelectNodeTo - These are used for target selectors to *mutate* the
3671/// specified node to have the specified return type, Target opcode, and
3672/// operands. Note that target opcodes are stored as
3673/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003674///
3675/// Note that SelectNodeTo returns the resultant node. If there is already a
3676/// node of the specified opcode and operands, it returns that node instead of
3677/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003678SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003679 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003680 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003681 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003682 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003683 void *IP = 0;
3684 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003685 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003686
Chris Lattner7651fa42005-08-24 23:00:29 +00003687 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003688
Dan Gohman35b31be2008-04-17 23:02:12 +00003689 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003690
Chris Lattner4a283e92006-08-11 18:38:11 +00003691 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003692 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003693}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003694
Evan Cheng95514ba2006-08-26 08:00:10 +00003695SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003696 MVT VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003697 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003698 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003699 SDOperand Ops[] = { Op1 };
3700
Jim Laskey583bd472006-10-27 23:46:08 +00003701 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003702 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003703 void *IP = 0;
3704 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003705 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003706
Chris Lattner149c58c2005-08-16 18:17:10 +00003707 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003708 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003709 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003710 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003711}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003712
Evan Cheng95514ba2006-08-26 08:00:10 +00003713SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003715 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003716 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003717 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003718 SDOperand Ops[] = { Op1, Op2 };
3719
Jim Laskey583bd472006-10-27 23:46:08 +00003720 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003721 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003722 void *IP = 0;
3723 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003724 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003725
Chris Lattner149c58c2005-08-16 18:17:10 +00003726 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003727
Chris Lattner63e3f142007-02-04 07:28:00 +00003728 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003729
Chris Lattnera5682852006-08-07 23:03:03 +00003730 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003731 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003732}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003733
Evan Cheng95514ba2006-08-26 08:00:10 +00003734SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003735 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003736 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003737 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003738 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003739 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003740 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003741 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003742 void *IP = 0;
3743 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003744 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003745
Chris Lattner149c58c2005-08-16 18:17:10 +00003746 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003747
Chris Lattner63e3f142007-02-04 07:28:00 +00003748 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003749
Chris Lattnera5682852006-08-07 23:03:03 +00003750 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003751 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003752}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003753
Evan Cheng95514ba2006-08-26 08:00:10 +00003754SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003756 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003757 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003758 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003759 FoldingSetNodeID ID;
3760 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003761 void *IP = 0;
3762 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003763 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003764
Chris Lattner6b09a292005-08-21 18:49:33 +00003765 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003766 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003767
Chris Lattnera5682852006-08-07 23:03:03 +00003768 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003769 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003770}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003771
Evan Cheng95514ba2006-08-26 08:00:10 +00003772SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003773 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003774 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003775 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003776 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003777 SDOperand Ops[] = { Op1, Op2 };
3778 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003779 void *IP = 0;
3780 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003781 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003782
Chris Lattner0fb094f2005-11-19 01:44:53 +00003783 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003784 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003785 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003786 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003787}
3788
Evan Cheng95514ba2006-08-26 08:00:10 +00003789SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003791 SDOperand Op1, SDOperand Op2,
3792 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003793 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003794 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003795 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003796 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003797 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003798 void *IP = 0;
3799 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003800 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003801
Chris Lattner0fb094f2005-11-19 01:44:53 +00003802 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003803
Chris Lattner63e3f142007-02-04 07:28:00 +00003804 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003805 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003806 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003807}
3808
Chris Lattner0fb094f2005-11-19 01:44:53 +00003809
Evan Cheng6ae46c42006-02-09 07:15:23 +00003810/// getTargetNode - These are used for target selectors to create a new node
3811/// with specified return type(s), target opcode, and operands.
3812///
3813/// Note that getTargetNode returns the resultant node. If there is already a
3814/// node of the specified opcode and operands, it returns that node instead of
3815/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003816SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003817 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3818}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003819SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003820 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3821}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003823 SDOperand Op1, SDOperand Op2) {
3824 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3825}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003826SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003827 SDOperand Op1, SDOperand Op2,
3828 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003829 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3830}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003831SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003832 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003833 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003834}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3836 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003837 SDOperand Op;
3838 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3839}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003840SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3841 MVT VT2, SDOperand Op1) {
3842 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003843 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003844}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003845SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3846 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003847 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003848 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003849 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003850 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003851}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3853 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003854 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003856 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003857 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003858}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003859SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003860 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003862 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003863}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003864SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003865 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003866 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003867 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003868 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003869}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003870SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003871 SDOperand Op1, SDOperand Op2,
3872 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003874 SDOperand Ops[] = { Op1, Op2, Op3 };
3875 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3876}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003878 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003879 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003880 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003881}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3883 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003884 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003885 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003886 VTList.push_back(VT1);
3887 VTList.push_back(VT2);
3888 VTList.push_back(VT3);
3889 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003890 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003891 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3892}
Evan Cheng39305cf2007-10-05 01:10:49 +00003893SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003895 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003896 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003897 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3898 Ops, NumOps).Val;
3899}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003900
Evan Cheng08b11732008-03-22 01:55:50 +00003901/// getNodeIfExists - Get the specified node if it's already available, or
3902/// else return NULL.
3903SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003904 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003905 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3906 FoldingSetNodeID ID;
3907 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3908 void *IP = 0;
3909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3910 return E;
3911 }
3912 return NULL;
3913}
3914
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003915
Evan Cheng99157a02006-08-07 22:13:29 +00003916/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003917/// This can cause recursive merging of nodes in the DAG.
3918///
Chris Lattner11d049c2008-02-03 03:35:22 +00003919/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003920///
Chris Lattner11d049c2008-02-03 03:35:22 +00003921void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003922 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003923 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003924 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003925 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003926 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003927
Chris Lattner8b8749f2005-08-17 19:00:20 +00003928 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003929 SDNode::use_iterator UI = From->use_begin();
3930 SDNode *U = UI->getUser();
3931
Chris Lattner8b8749f2005-08-17 19:00:20 +00003932 // This node is about to morph, remove its old self from the CSE maps.
3933 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003934 int operandNum = 0;
3935 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3936 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003937 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003938 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003939 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003940 I->setUser(U);
3941 To.Val->addUser(operandNum, U);
3942 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003943
3944 // Now that we have modified U, add it back to the CSE maps. If it already
3945 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003946 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003947 ReplaceAllUsesWith(U, Existing, UpdateListener);
3948 // U is now dead. Inform the listener if it exists and delete it.
3949 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003950 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003951 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003952 } else {
3953 // If the node doesn't already exist, we updated it. Inform a listener if
3954 // it exists.
3955 if (UpdateListener)
3956 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003957 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003958 }
3959}
3960
3961/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3962/// This can cause recursive merging of nodes in the DAG.
3963///
3964/// This version assumes From/To have matching types and numbers of result
3965/// values.
3966///
Chris Lattner1e111c72005-09-07 05:37:01 +00003967void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003968 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003969 assert(From != To && "Cannot replace uses of with self");
3970 assert(From->getNumValues() == To->getNumValues() &&
3971 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003972 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003973 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3974 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003975
3976 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003977 SDNode::use_iterator UI = From->use_begin();
3978 SDNode *U = UI->getUser();
3979
Chris Lattner8b52f212005-08-26 18:36:28 +00003980 // This node is about to morph, remove its old self from the CSE maps.
3981 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003982 int operandNum = 0;
3983 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3984 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003985 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003986 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003987 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003988 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003989 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003990
Chris Lattner8b8749f2005-08-17 19:00:20 +00003991 // Now that we have modified U, add it back to the CSE maps. If it already
3992 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003993 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003994 ReplaceAllUsesWith(U, Existing, UpdateListener);
3995 // U is now dead. Inform the listener if it exists and delete it.
3996 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003997 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003998 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003999 } else {
4000 // If the node doesn't already exist, we updated it. Inform a listener if
4001 // it exists.
4002 if (UpdateListener)
4003 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004004 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004005 }
4006}
4007
Chris Lattner8b52f212005-08-26 18:36:28 +00004008/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4009/// This can cause recursive merging of nodes in the DAG.
4010///
4011/// This version can replace From with any result values. To must match the
4012/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004013void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00004014 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004015 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004016 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004017 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004018
4019 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004020 SDNode::use_iterator UI = From->use_begin();
4021 SDNode *U = UI->getUser();
4022
Chris Lattner7b2880c2005-08-24 22:44:39 +00004023 // This node is about to morph, remove its old self from the CSE maps.
4024 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004025 int operandNum = 0;
4026 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4027 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004028 if (I->getVal() == From) {
4029 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004030 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004031 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004032 I->setUser(U);
4033 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004034 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004035
Chris Lattner7b2880c2005-08-24 22:44:39 +00004036 // Now that we have modified U, add it back to the CSE maps. If it already
4037 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004038 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004039 ReplaceAllUsesWith(U, Existing, UpdateListener);
4040 // U is now dead. Inform the listener if it exists and delete it.
4041 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004042 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004043 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004044 } else {
4045 // If the node doesn't already exist, we updated it. Inform a listener if
4046 // it exists.
4047 if (UpdateListener)
4048 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004049 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004050 }
4051}
4052
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004053namespace {
4054 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4055 /// any deleted nodes from the set passed into its constructor and recursively
4056 /// notifies another update listener if specified.
4057 class ChainedSetUpdaterListener :
4058 public SelectionDAG::DAGUpdateListener {
4059 SmallSetVector<SDNode*, 16> &Set;
4060 SelectionDAG::DAGUpdateListener *Chain;
4061 public:
4062 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4063 SelectionDAG::DAGUpdateListener *chain)
4064 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004065
Duncan Sandsedfcf592008-06-11 11:42:12 +00004066 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004067 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004068 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004069 }
4070 virtual void NodeUpdated(SDNode *N) {
4071 if (Chain) Chain->NodeUpdated(N);
4072 }
4073 };
4074}
4075
Chris Lattner012f2412006-02-17 21:58:01 +00004076/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4077/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004078/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004079void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004080 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004081 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082
Chris Lattner012f2412006-02-17 21:58:01 +00004083 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004084 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004085 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004086 return;
4087 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004088
4089 if (From.use_empty()) return;
4090
Chris Lattnercf5640b2007-02-04 00:14:31 +00004091 // Get all of the users of From.Val. We want these in a nice,
4092 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004093 SmallSetVector<SDNode*, 16> Users;
4094 for (SDNode::use_iterator UI = From.Val->use_begin(),
4095 E = From.Val->use_end(); UI != E; ++UI) {
4096 SDNode *User = UI->getUser();
4097 if (!Users.count(User))
4098 Users.insert(User);
4099 }
Chris Lattner012f2412006-02-17 21:58:01 +00004100
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004101 // When one of the recursive merges deletes nodes from the graph, we need to
4102 // make sure that UpdateListener is notified *and* that the node is removed
4103 // from Users if present. CSUL does this.
4104 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004105
Chris Lattner012f2412006-02-17 21:58:01 +00004106 while (!Users.empty()) {
4107 // We know that this user uses some value of From. If it is the right
4108 // value, update it.
4109 SDNode *User = Users.back();
4110 Users.pop_back();
4111
Chris Lattner01d029b2007-10-15 06:10:22 +00004112 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004113 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004114 for (; Op != E; ++Op)
4115 if (*Op == From) break;
4116
4117 // If there are no matches, the user must use some other result of From.
4118 if (Op == E) continue;
4119
4120 // Okay, we know this user needs to be updated. Remove its old self
4121 // from the CSE maps.
4122 RemoveNodeFromCSEMaps(User);
4123
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004124 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004125 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004126 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004127 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004128 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004129 Op->setUser(User);
4130 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004131 }
4132 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004133
4134 // Now that we have modified User, add it back to the CSE maps. If it
4135 // already exists there, recursively merge the results together.
4136 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004137 if (!Existing) {
4138 if (UpdateListener) UpdateListener->NodeUpdated(User);
4139 continue; // Continue on to next user.
4140 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004141
4142 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004143 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004144 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004145 // can cause deletion of nodes that used the old value. To handle this, we
4146 // use CSUL to remove them from the Users set.
4147 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004148
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004149 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004150 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004151 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004152 }
4153}
4154
Evan Chenge6f35d82006-08-01 08:20:41 +00004155/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4156/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004157unsigned SelectionDAG::AssignNodeIds() {
4158 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004159 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4160 SDNode *N = I;
4161 N->setNodeId(Id++);
4162 }
4163 return Id;
4164}
4165
Evan Chenge6f35d82006-08-01 08:20:41 +00004166/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004167/// based on their topological order. It returns the maximum id and a vector
4168/// of the SDNodes* in assigned order by reference.
4169unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004170 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004171 std::vector<unsigned> InDegree(DAGSize);
4172 std::vector<SDNode*> Sources;
4173
4174 // Use a two pass approach to avoid using a std::map which is slow.
4175 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004176 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4177 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004178 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004179 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004180 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004181 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004182 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004183 }
4184
Evan Cheng99157a02006-08-07 22:13:29 +00004185 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004186 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004187 SDNode *N = Sources.back();
4188 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004189 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004190 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004191 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004192 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004193 if (Degree == 0)
4194 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004195 }
4196 }
4197
Evan Chengc384d6c2006-08-02 22:00:34 +00004198 // Second pass, assign the actual topological order as node ids.
4199 Id = 0;
4200 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4201 TI != TE; ++TI)
4202 (*TI)->setNodeId(Id++);
4203
4204 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004205}
4206
4207
Evan Cheng091cba12006-07-27 06:39:06 +00004208
Jim Laskey58b968b2005-08-17 20:08:02 +00004209//===----------------------------------------------------------------------===//
4210// SDNode Class
4211//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004212
Chris Lattner917d2c92006-07-19 00:00:37 +00004213// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004214void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004215void UnarySDNode::ANCHOR() {}
4216void BinarySDNode::ANCHOR() {}
4217void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004218void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004219void ConstantSDNode::ANCHOR() {}
4220void ConstantFPSDNode::ANCHOR() {}
4221void GlobalAddressSDNode::ANCHOR() {}
4222void FrameIndexSDNode::ANCHOR() {}
4223void JumpTableSDNode::ANCHOR() {}
4224void ConstantPoolSDNode::ANCHOR() {}
4225void BasicBlockSDNode::ANCHOR() {}
4226void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004227void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004228void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004229void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004230void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004231void ExternalSymbolSDNode::ANCHOR() {}
4232void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004233void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004234void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004235void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004236void LoadSDNode::ANCHOR() {}
4237void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004238void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004239
Chris Lattner48b85922007-02-04 02:41:42 +00004240HandleSDNode::~HandleSDNode() {
4241 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004242 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004243}
4244
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004245GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004246 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004247 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004248 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004249 // Thread Local
4250 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4251 // Non Thread Local
4252 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4253 getSDVTList(VT)), Offset(o) {
4254 TheGlobal = const_cast<GlobalValue*>(GA);
4255}
Chris Lattner48b85922007-02-04 02:41:42 +00004256
Dan Gohman36b5c132008-04-07 19:35:22 +00004257/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004258/// reference performed by this atomic.
4259MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004260 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004261 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4262 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4263
4264 // Check if the atomic references a frame index
4265 const FrameIndexSDNode *FI =
4266 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4267 if (!getSrcValue() && FI)
4268 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4269 FI->getIndex(), Size, getAlignment());
4270 else
4271 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4272 Size, getAlignment());
4273}
4274
4275/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004276/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004277MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004278 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004279 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004280 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4281 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004282 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004283
4284 // Check if the load references a frame index, and does not have
4285 // an SV attached.
4286 const FrameIndexSDNode *FI =
4287 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4288 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004289 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004290 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004291 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004292 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004293 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004294}
4295
Jim Laskey583bd472006-10-27 23:46:08 +00004296/// Profile - Gather unique data for the node.
4297///
4298void SDNode::Profile(FoldingSetNodeID &ID) {
4299 AddNodeIDNode(ID, this);
4300}
4301
Chris Lattnera3255112005-11-08 23:30:28 +00004302/// getValueTypeList - Return a pointer to the specified value type.
4303///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004304const MVT *SDNode::getValueTypeList(MVT VT) {
4305 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004306 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004307 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004308 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 static MVT VTs[MVT::LAST_VALUETYPE];
4310 VTs[VT.getSimpleVT()] = VT;
4311 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004312 }
Chris Lattnera3255112005-11-08 23:30:28 +00004313}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004314
Chris Lattner5c884562005-01-12 18:37:47 +00004315/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4316/// indicated value. This method ignores uses of other values defined by this
4317/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004318bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004319 assert(Value < getNumValues() && "Bad value!");
4320
4321 // If there is only one value, this is easy.
4322 if (getNumValues() == 1)
4323 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004324 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004325
Evan Cheng4ee62112006-02-05 06:29:23 +00004326 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004327
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004328 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004329
Roman Levensteindc1adac2008-04-07 10:06:32 +00004330 // TODO: Only iterate over uses of a given value of the node
4331 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4332 if (*UI == TheValue) {
4333 if (NUses == 0)
4334 return false;
4335 --NUses;
4336 }
Chris Lattner5c884562005-01-12 18:37:47 +00004337 }
4338
4339 // Found exactly the right number of uses?
4340 return NUses == 0;
4341}
4342
4343
Evan Cheng33d55952007-08-02 05:29:38 +00004344/// hasAnyUseOfValue - Return true if there are any use of the indicated
4345/// value. This method ignores uses of other values defined by this operation.
4346bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4347 assert(Value < getNumValues() && "Bad value!");
4348
Dan Gohman30359592008-01-29 13:02:09 +00004349 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004350
4351 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4352
4353 SmallPtrSet<SDNode*, 32> UsersHandled;
4354
Roman Levensteindc1adac2008-04-07 10:06:32 +00004355 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4356 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004357 if (User->getNumOperands() == 1 ||
4358 UsersHandled.insert(User)) // First time we've seen this?
4359 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4360 if (User->getOperand(i) == TheValue) {
4361 return true;
4362 }
4363 }
4364
4365 return false;
4366}
4367
4368
Evan Cheng917be682008-03-04 00:41:45 +00004369/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004370///
Evan Cheng917be682008-03-04 00:41:45 +00004371bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004372 bool Seen = false;
4373 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004374 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004375 if (User == this)
4376 Seen = true;
4377 else
4378 return false;
4379 }
4380
4381 return Seen;
4382}
4383
Evan Chenge6e97e62006-11-03 07:31:32 +00004384/// isOperand - Return true if this node is an operand of N.
4385///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004386bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004387 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4388 if (*this == N->getOperand(i))
4389 return true;
4390 return false;
4391}
4392
Evan Cheng917be682008-03-04 00:41:45 +00004393bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004394 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004395 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004396 return true;
4397 return false;
4398}
Evan Cheng4ee62112006-02-05 06:29:23 +00004399
Chris Lattner572dee72008-01-16 05:49:24 +00004400/// reachesChainWithoutSideEffects - Return true if this operand (which must
4401/// be a chain) reaches the specified operand without crossing any
4402/// side-effecting instructions. In practice, this looks through token
4403/// factors and non-volatile loads. In order to remain efficient, this only
4404/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004405bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004406 unsigned Depth) const {
4407 if (*this == Dest) return true;
4408
4409 // Don't search too deeply, we just want to be able to see through
4410 // TokenFactor's etc.
4411 if (Depth == 0) return false;
4412
4413 // If this is a token factor, all inputs to the TF happen in parallel. If any
4414 // of the operands of the TF reach dest, then we can do the xform.
4415 if (getOpcode() == ISD::TokenFactor) {
4416 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4417 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4418 return true;
4419 return false;
4420 }
4421
4422 // Loads don't have side effects, look through them.
4423 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4424 if (!Ld->isVolatile())
4425 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4426 }
4427 return false;
4428}
4429
4430
Evan Chengc5fc57d2006-11-03 03:05:24 +00004431static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004432 SmallPtrSet<SDNode *, 32> &Visited) {
4433 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004434 return;
4435
4436 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4437 SDNode *Op = N->getOperand(i).Val;
4438 if (Op == P) {
4439 found = true;
4440 return;
4441 }
4442 findPredecessor(Op, P, found, Visited);
4443 }
4444}
4445
Evan Cheng917be682008-03-04 00:41:45 +00004446/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004447/// is either an operand of N or it can be reached by recursively traversing
4448/// up the operands.
4449/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004450bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004451 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004452 bool found = false;
4453 findPredecessor(N, this, found, Visited);
4454 return found;
4455}
4456
Evan Chengc5484282006-10-04 00:56:09 +00004457uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4458 assert(Num < NumOperands && "Invalid child # of SDNode!");
4459 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4460}
4461
Reid Spencer577cc322007-04-01 07:32:19 +00004462std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004463 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004464 default:
4465 if (getOpcode() < ISD::BUILTIN_OP_END)
4466 return "<<Unknown DAG Node>>";
4467 else {
Evan Cheng72261582005-12-20 06:22:03 +00004468 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004469 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004470 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004471 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004472
Evan Cheng72261582005-12-20 06:22:03 +00004473 TargetLowering &TLI = G->getTargetLoweringInfo();
4474 const char *Name =
4475 TLI.getTargetNodeName(getOpcode());
4476 if (Name) return Name;
4477 }
4478
4479 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004480 }
4481
Evan Cheng27b7db52008-03-08 00:58:38 +00004482 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004483 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004484 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4485 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4486 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004487 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4488 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4489 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004490 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004491 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4492 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4493 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4494 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4495 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004496 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004497 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004498 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004499 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004500 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004501 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004502 case ISD::AssertSext: return "AssertSext";
4503 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004504
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004505 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004506 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004507 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004508 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004509
4510 case ISD::Constant: return "Constant";
4511 case ISD::ConstantFP: return "ConstantFP";
4512 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004513 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004514 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004515 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004516 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004517 case ISD::RETURNADDR: return "RETURNADDR";
4518 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004519 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004520 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4521 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004522 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004523 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004524 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004525 case ISD::INTRINSIC_WO_CHAIN: {
4526 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4527 return Intrinsic::getName((Intrinsic::ID)IID);
4528 }
4529 case ISD::INTRINSIC_VOID:
4530 case ISD::INTRINSIC_W_CHAIN: {
4531 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004532 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004533 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004534
Chris Lattnerb2827b02006-03-19 00:52:58 +00004535 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004536 case ISD::TargetConstant: return "TargetConstant";
4537 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004538 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004539 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004540 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004541 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004542 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004543 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004544
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004545 case ISD::CopyToReg: return "CopyToReg";
4546 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004547 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004548 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004549 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004550 case ISD::DBG_LABEL: return "dbg_label";
4551 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004552 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004553 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004554 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004555 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004556
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004557 // Unary operators
4558 case ISD::FABS: return "fabs";
4559 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004560 case ISD::FSQRT: return "fsqrt";
4561 case ISD::FSIN: return "fsin";
4562 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004563 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004564 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004565
4566 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004567 case ISD::ADD: return "add";
4568 case ISD::SUB: return "sub";
4569 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004570 case ISD::MULHU: return "mulhu";
4571 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004572 case ISD::SDIV: return "sdiv";
4573 case ISD::UDIV: return "udiv";
4574 case ISD::SREM: return "srem";
4575 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004576 case ISD::SMUL_LOHI: return "smul_lohi";
4577 case ISD::UMUL_LOHI: return "umul_lohi";
4578 case ISD::SDIVREM: return "sdivrem";
4579 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004580 case ISD::AND: return "and";
4581 case ISD::OR: return "or";
4582 case ISD::XOR: return "xor";
4583 case ISD::SHL: return "shl";
4584 case ISD::SRA: return "sra";
4585 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004586 case ISD::ROTL: return "rotl";
4587 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004588 case ISD::FADD: return "fadd";
4589 case ISD::FSUB: return "fsub";
4590 case ISD::FMUL: return "fmul";
4591 case ISD::FDIV: return "fdiv";
4592 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004593 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004594 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004595
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004596 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004597 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004598 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004599 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004600 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004601 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004602 case ISD::CONCAT_VECTORS: return "concat_vectors";
4603 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004604 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004605 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004606 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004607 case ISD::ADDC: return "addc";
4608 case ISD::ADDE: return "adde";
4609 case ISD::SUBC: return "subc";
4610 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004611 case ISD::SHL_PARTS: return "shl_parts";
4612 case ISD::SRA_PARTS: return "sra_parts";
4613 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004614
4615 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4616 case ISD::INSERT_SUBREG: return "insert_subreg";
4617
Chris Lattner7f644642005-04-28 21:44:03 +00004618 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004619 case ISD::SIGN_EXTEND: return "sign_extend";
4620 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004621 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004622 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004623 case ISD::TRUNCATE: return "truncate";
4624 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004625 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004626 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004627 case ISD::FP_EXTEND: return "fp_extend";
4628
4629 case ISD::SINT_TO_FP: return "sint_to_fp";
4630 case ISD::UINT_TO_FP: return "uint_to_fp";
4631 case ISD::FP_TO_SINT: return "fp_to_sint";
4632 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004633 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004634
4635 // Control flow instructions
4636 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004637 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004638 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004639 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004640 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004641 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004642 case ISD::CALLSEQ_START: return "callseq_start";
4643 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004644
4645 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004646 case ISD::LOAD: return "load";
4647 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004648 case ISD::VAARG: return "vaarg";
4649 case ISD::VACOPY: return "vacopy";
4650 case ISD::VAEND: return "vaend";
4651 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004652 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004653 case ISD::EXTRACT_ELEMENT: return "extract_element";
4654 case ISD::BUILD_PAIR: return "build_pair";
4655 case ISD::STACKSAVE: return "stacksave";
4656 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004657 case ISD::TRAP: return "trap";
4658
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004659 // Bit manipulation
4660 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004661 case ISD::CTPOP: return "ctpop";
4662 case ISD::CTTZ: return "cttz";
4663 case ISD::CTLZ: return "ctlz";
4664
Chris Lattner36ce6912005-11-29 06:21:05 +00004665 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004666 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004667 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004668
Duncan Sands36397f52007-07-27 12:58:54 +00004669 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004670 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004671
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004672 case ISD::CONDCODE:
4673 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004674 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004675 case ISD::SETOEQ: return "setoeq";
4676 case ISD::SETOGT: return "setogt";
4677 case ISD::SETOGE: return "setoge";
4678 case ISD::SETOLT: return "setolt";
4679 case ISD::SETOLE: return "setole";
4680 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004681
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004682 case ISD::SETO: return "seto";
4683 case ISD::SETUO: return "setuo";
4684 case ISD::SETUEQ: return "setue";
4685 case ISD::SETUGT: return "setugt";
4686 case ISD::SETUGE: return "setuge";
4687 case ISD::SETULT: return "setult";
4688 case ISD::SETULE: return "setule";
4689 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004690
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004691 case ISD::SETEQ: return "seteq";
4692 case ISD::SETGT: return "setgt";
4693 case ISD::SETGE: return "setge";
4694 case ISD::SETLT: return "setlt";
4695 case ISD::SETLE: return "setle";
4696 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004697 }
4698 }
4699}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004700
Evan Cheng144d8f02006-11-09 17:55:04 +00004701const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004702 switch (AM) {
4703 default:
4704 return "";
4705 case ISD::PRE_INC:
4706 return "<pre-inc>";
4707 case ISD::PRE_DEC:
4708 return "<pre-dec>";
4709 case ISD::POST_INC:
4710 return "<post-inc>";
4711 case ISD::POST_DEC:
4712 return "<post-dec>";
4713 }
4714}
4715
Duncan Sands276dcbd2008-03-21 09:14:45 +00004716std::string ISD::ArgFlagsTy::getArgFlagsString() {
4717 std::string S = "< ";
4718
4719 if (isZExt())
4720 S += "zext ";
4721 if (isSExt())
4722 S += "sext ";
4723 if (isInReg())
4724 S += "inreg ";
4725 if (isSRet())
4726 S += "sret ";
4727 if (isByVal())
4728 S += "byval ";
4729 if (isNest())
4730 S += "nest ";
4731 if (getByValAlign())
4732 S += "byval-align:" + utostr(getByValAlign()) + " ";
4733 if (getOrigAlign())
4734 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4735 if (getByValSize())
4736 S += "byval-size:" + utostr(getByValSize()) + " ";
4737 return S + ">";
4738}
4739
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004740void SDNode::dump() const { dump(0); }
4741void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004742 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004743
4744 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004745 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004746 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004747 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004748 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004749 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004750 }
Bill Wendling832171c2006-12-07 20:04:42 +00004751 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004752
Bill Wendling832171c2006-12-07 20:04:42 +00004753 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004754 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004755 if (i) cerr << ", ";
4756 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004757 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004758 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004759 }
4760
Evan Chengce254432007-12-11 02:08:35 +00004761 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4762 SDNode *Mask = getOperand(2).Val;
4763 cerr << "<";
4764 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4765 if (i) cerr << ",";
4766 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4767 cerr << "u";
4768 else
4769 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4770 }
4771 cerr << ">";
4772 }
4773
Chris Lattnerc3aae252005-01-07 07:46:32 +00004774 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004775 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004776 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004777 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4778 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4779 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4780 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4781 else {
4782 cerr << "<APFloat(";
4783 CSDN->getValueAPF().convertToAPInt().dump();
4784 cerr << ")>";
4785 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004786 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004787 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004788 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004789 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004790 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004791 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004792 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004793 else
Bill Wendling832171c2006-12-07 20:04:42 +00004794 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004795 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004796 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004797 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004798 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004799 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004800 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004801 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004802 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004803 else
Bill Wendling832171c2006-12-07 20:04:42 +00004804 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004805 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004806 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004807 else
Bill Wendling832171c2006-12-07 20:04:42 +00004808 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004809 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004810 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004811 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4812 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004813 cerr << LBB->getName() << " ";
4814 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004815 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004816 if (G && R->getReg() &&
4817 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004818 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004819 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004820 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004821 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004822 } else if (const ExternalSymbolSDNode *ES =
4823 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004824 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004825 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4826 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004827 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004828 else
Dan Gohman69de1932008-02-06 22:27:42 +00004829 cerr << "<null>";
4830 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4831 if (M->MO.getValue())
4832 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4833 else
4834 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004835 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4836 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004837 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004838 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004839 }
4840 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004841 const Value *SrcValue = LD->getSrcValue();
4842 int SrcOffset = LD->getSrcValueOffset();
4843 cerr << " <";
4844 if (SrcValue)
4845 cerr << SrcValue;
4846 else
4847 cerr << "null";
4848 cerr << ":" << SrcOffset << ">";
4849
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004850 bool doExt = true;
4851 switch (LD->getExtensionType()) {
4852 default: doExt = false; break;
4853 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004854 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004855 break;
4856 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004857 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004858 break;
4859 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004860 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004861 break;
4862 }
4863 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004864 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004865
Evan Cheng144d8f02006-11-09 17:55:04 +00004866 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004867 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004868 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004869 if (LD->isVolatile())
4870 cerr << " <volatile>";
4871 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004872 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004873 const Value *SrcValue = ST->getSrcValue();
4874 int SrcOffset = ST->getSrcValueOffset();
4875 cerr << " <";
4876 if (SrcValue)
4877 cerr << SrcValue;
4878 else
4879 cerr << "null";
4880 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004881
4882 if (ST->isTruncatingStore())
4883 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004884 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004885
4886 const char *AM = getIndexedModeName(ST->getAddressingMode());
4887 if (*AM)
4888 cerr << " " << AM;
4889 if (ST->isVolatile())
4890 cerr << " <volatile>";
4891 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004892 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4893 const Value *SrcValue = AT->getSrcValue();
4894 int SrcOffset = AT->getSrcValueOffset();
4895 cerr << " <";
4896 if (SrcValue)
4897 cerr << SrcValue;
4898 else
4899 cerr << "null";
4900 cerr << ":" << SrcOffset << ">";
4901 if (AT->isVolatile())
4902 cerr << " <volatile>";
4903 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004905}
4906
Chris Lattnerde202b32005-11-09 23:47:37 +00004907static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004908 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4909 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004910 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004911 else
Bill Wendling832171c2006-12-07 20:04:42 +00004912 cerr << "\n" << std::string(indent+2, ' ')
4913 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004914
Chris Lattnerea946cd2005-01-09 20:38:33 +00004915
Bill Wendling832171c2006-12-07 20:04:42 +00004916 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004917 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004918}
4919
Chris Lattnerc3aae252005-01-07 07:46:32 +00004920void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004921 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004922 std::vector<const SDNode*> Nodes;
4923 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4924 I != E; ++I)
4925 Nodes.push_back(I);
4926
Chris Lattner49d24712005-01-09 20:26:36 +00004927 std::sort(Nodes.begin(), Nodes.end());
4928
4929 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004930 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004931 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004932 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004933
Jim Laskey26f7fa72006-10-17 19:33:52 +00004934 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004935
Bill Wendling832171c2006-12-07 20:04:42 +00004936 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004937}
4938
Evan Chengd6594ae2006-09-12 21:00:35 +00004939const Type *ConstantPoolSDNode::getType() const {
4940 if (isMachineConstantPoolEntry())
4941 return Val.MachineCPVal->getType();
4942 return Val.ConstVal->getType();
4943}