blob: 3f262df713fada0eeb35520f61709852dd2382d1 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000033#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000034#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000036#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000037#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000038#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000039#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner0b3e5252006-08-15 19:11:05 +000042/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands83ec4b62008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattnerf8dc0612008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Jim Laskey58b968b2005-08-17 20:08:02 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000072}
73
Duncan Sands83ec4b62008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000077
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000113 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Evan Chengefec7512008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Chengbb81d972008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000195/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
200 if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::DBG_LABEL)
202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000326 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
338 ID.AddPointer(Ops->getSDOperand().Val);
339 ID.AddInteger(Ops->getSDOperand().ResNo);
340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
344 unsigned short OpC, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345 const SDOperand *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Roman Levenstein9cac5252008-04-16 16:15:27 +0000351
Jim Laskeydef69b92006-10-27 23:52:51 +0000352/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
353/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
355 AddNodeIDOpcode(ID, N->getOpcode());
356 // Add the return value info.
357 AddNodeIDValueTypes(ID, N->getVTList());
358 // Add the operand info.
359 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
360
361 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 switch (N->getOpcode()) {
363 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000364 case ISD::ARG_FLAGS:
365 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
366 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 case ISD::TargetConstant:
368 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000369 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 break;
371 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000372 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000373 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000377 case ISD::GlobalAddress:
378 case ISD::TargetGlobalTLSAddress:
379 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
381 ID.AddPointer(GA->getGlobal());
382 ID.AddInteger(GA->getOffset());
383 break;
384 }
385 case ISD::BasicBlock:
386 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
387 break;
388 case ISD::Register:
389 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
390 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000391 case ISD::DBG_STOPPOINT: {
392 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
393 ID.AddInteger(DSP->getLine());
394 ID.AddInteger(DSP->getColumn());
395 ID.AddPointer(DSP->getCompileUnit());
396 break;
397 }
Dan Gohman44066042008-07-01 00:05:16 +0000398 case ISD::DBG_LABEL:
399 case ISD::EH_LABEL:
400 ID.AddInteger(cast<LabelSDNode>(N)->getLabelID());
401 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000402 case ISD::SRCVALUE:
403 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
404 break;
405 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000406 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000407 ID.AddPointer(MO.getValue());
408 ID.AddInteger(MO.getFlags());
409 ID.AddInteger(MO.getOffset());
410 ID.AddInteger(MO.getSize());
411 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000412 break;
413 }
414 case ISD::FrameIndex:
415 case ISD::TargetFrameIndex:
416 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
417 break;
418 case ISD::JumpTable:
419 case ISD::TargetJumpTable:
420 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
421 break;
422 case ISD::ConstantPool:
423 case ISD::TargetConstantPool: {
424 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
425 ID.AddInteger(CP->getAlignment());
426 ID.AddInteger(CP->getOffset());
427 if (CP->isMachineConstantPoolEntry())
428 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
429 else
430 ID.AddPointer(CP->getConstVal());
431 break;
432 }
433 case ISD::LOAD: {
434 LoadSDNode *LD = cast<LoadSDNode>(N);
435 ID.AddInteger(LD->getAddressingMode());
436 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000437 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000438 ID.AddInteger(LD->getAlignment());
439 ID.AddInteger(LD->isVolatile());
440 break;
441 }
442 case ISD::STORE: {
443 StoreSDNode *ST = cast<StoreSDNode>(N);
444 ID.AddInteger(ST->getAddressingMode());
445 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000446 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000447 ID.AddInteger(ST->getAlignment());
448 ID.AddInteger(ST->isVolatile());
449 break;
450 }
Mon P Wang28873102008-06-25 08:15:39 +0000451 case ISD::ATOMIC_CMP_SWAP:
452 case ISD::ATOMIC_LOAD_ADD:
453 case ISD::ATOMIC_SWAP:
454 case ISD::ATOMIC_LOAD_SUB:
455 case ISD::ATOMIC_LOAD_AND:
456 case ISD::ATOMIC_LOAD_OR:
457 case ISD::ATOMIC_LOAD_XOR:
458 case ISD::ATOMIC_LOAD_NAND:
459 case ISD::ATOMIC_LOAD_MIN:
460 case ISD::ATOMIC_LOAD_MAX:
461 case ISD::ATOMIC_LOAD_UMIN:
462 case ISD::ATOMIC_LOAD_UMAX: {
463 AtomicSDNode *AT = cast<AtomicSDNode>(N);
464 ID.AddInteger(AT->getAlignment());
465 ID.AddInteger(AT->isVolatile());
466 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000467 }
Mon P Wang28873102008-06-25 08:15:39 +0000468 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000469}
470
471//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000472// SelectionDAG Class
473//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000474
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000475/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000476/// SelectionDAG.
477void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000478 // Create a dummy node (which is not added to allnodes), that adds a reference
479 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000480 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000481
Chris Lattner190a4182006-08-04 17:45:20 +0000482 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000483
Chris Lattner190a4182006-08-04 17:45:20 +0000484 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000485 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000486 if (I->use_empty())
487 DeadNodes.push_back(I);
488
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000489 RemoveDeadNodes(DeadNodes);
490
491 // If the root changed (e.g. it was a dead load, update the root).
492 setRoot(Dummy.getValue());
493}
494
495/// RemoveDeadNodes - This method deletes the unreachable nodes in the
496/// given list, and any nodes that become unreachable as a result.
497void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
498 DAGUpdateListener *UpdateListener) {
499
Chris Lattner190a4182006-08-04 17:45:20 +0000500 // Process the worklist, deleting the nodes and adding their uses to the
501 // worklist.
502 while (!DeadNodes.empty()) {
503 SDNode *N = DeadNodes.back();
504 DeadNodes.pop_back();
505
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000506 if (UpdateListener)
507 UpdateListener->NodeDeleted(N, 0);
508
Chris Lattner190a4182006-08-04 17:45:20 +0000509 // Take the node out of the appropriate CSE map.
510 RemoveNodeFromCSEMaps(N);
511
512 // Next, brutally remove the operand list. This is safe to do, as there are
513 // no cycles in the graph.
514 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000515 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000516 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000517
518 // Now that we removed this operand, see if there are no uses of it left.
519 if (Operand->use_empty())
520 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000521 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000522 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000523 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000524 }
Chris Lattner190a4182006-08-04 17:45:20 +0000525 N->OperandList = 0;
526 N->NumOperands = 0;
527
528 // Finally, remove N itself.
529 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000530 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000531}
532
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000533void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000534 SmallVector<SDNode*, 16> DeadNodes;
535 DeadNodes.push_back(N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000536 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000537}
538
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000539void SelectionDAG::DeleteNode(SDNode *N) {
540 assert(N->use_empty() && "Cannot delete a node that is not dead!");
541
542 // First take this out of the appropriate CSE map.
543 RemoveNodeFromCSEMaps(N);
544
Chris Lattner1e111c72005-09-07 05:37:01 +0000545 // Finally, remove uses due to operands of this node, remove from the
546 // AllNodes list, and delete the node.
547 DeleteNodeNotInCSEMaps(N);
548}
549
550void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
551
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000552 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000553 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000554
555 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000556 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000557 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000558 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000559 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000560 }
Chris Lattner65113b22005-11-08 22:07:03 +0000561 N->OperandList = 0;
562 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000563
564 delete N;
565}
566
Chris Lattner149c58c2005-08-16 18:17:10 +0000567/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
568/// correspond to it. This is useful when we're about to delete or repurpose
569/// the node. We don't want future request for structurally identical nodes
570/// to return N anymore.
571void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000573 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000574 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000575 case ISD::CONDCODE:
576 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
577 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000578 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000579 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
580 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000582 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000583 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000584 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000585 Erased =
586 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000587 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000588 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000589 MVT VT = cast<VTSDNode>(N)->getVT();
590 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000591 Erased = ExtendedValueTypeNodes.erase(VT);
592 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000593 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
594 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000595 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000596 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000597 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000598 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000599 // Remove it from the CSE Map.
600 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000601 break;
602 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000603#ifndef NDEBUG
604 // Verify that the node was actually in one of the CSE maps, unless it has a
605 // flag result (which cannot be CSE'd) or is one of the special cases that are
606 // not subject to CSE.
607 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000608 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000609 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000610 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000611 assert(0 && "Node is not in map!");
612 }
613#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000614}
615
Chris Lattner8b8749f2005-08-17 19:00:20 +0000616/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
617/// has been taken out and modified in some way. If the specified node already
618/// exists in the CSE maps, do not modify the maps, but return the existing node
619/// instead. If it doesn't exist, add it and return null.
620///
621SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
622 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000623 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000624 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000625
Chris Lattner9f8cc692005-12-19 22:21:21 +0000626 // Check that remaining values produced are not flags.
627 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
628 if (N->getValueType(i) == MVT::Flag)
629 return 0; // Never CSE anything that produces a flag.
630
Chris Lattnera5682852006-08-07 23:03:03 +0000631 SDNode *New = CSEMap.GetOrInsertNode(N);
632 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000633 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000634}
635
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000636/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
637/// were replaced with those specified. If this node is never memoized,
638/// return null, otherwise return a pointer to the slot it would take. If a
639/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000640SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
641 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000642 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000643 return 0; // Never add these nodes.
644
645 // Check that remaining values produced are not flags.
646 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
647 if (N->getValueType(i) == MVT::Flag)
648 return 0; // Never CSE anything that produces a flag.
649
Chris Lattner63e3f142007-02-04 07:28:00 +0000650 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000651 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000652 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000653 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000654}
655
656/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
657/// were replaced with those specified. If this node is never memoized,
658/// return null, otherwise return a pointer to the slot it would take. If a
659/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000660SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
661 SDOperand Op1, SDOperand Op2,
662 void *&InsertPos) {
663 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
664 return 0; // Never add these nodes.
665
666 // Check that remaining values produced are not flags.
667 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
668 if (N->getValueType(i) == MVT::Flag)
669 return 0; // Never CSE anything that produces a flag.
670
Chris Lattner63e3f142007-02-04 07:28:00 +0000671 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000672 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000673 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000674 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
675}
676
677
678/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
679/// were replaced with those specified. If this node is never memoized,
680/// return null, otherwise return a pointer to the slot it would take. If a
681/// node already exists with these operands, the slot will be non-null.
682SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000683 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000684 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000685 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000686 return 0; // Never add these nodes.
687
688 // Check that remaining values produced are not flags.
689 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
690 if (N->getValueType(i) == MVT::Flag)
691 return 0; // Never CSE anything that produces a flag.
692
Jim Laskey583bd472006-10-27 23:46:08 +0000693 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000694 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000695
Evan Cheng9629aba2006-10-11 01:47:58 +0000696 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
697 ID.AddInteger(LD->getAddressingMode());
698 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000699 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000700 ID.AddInteger(LD->getAlignment());
701 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000702 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
703 ID.AddInteger(ST->getAddressingMode());
704 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000705 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000706 ID.AddInteger(ST->getAlignment());
707 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000708 }
Jim Laskey583bd472006-10-27 23:46:08 +0000709
Chris Lattnera5682852006-08-07 23:03:03 +0000710 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000711}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000712
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000713
Chris Lattner78ec3112003-08-11 14:57:33 +0000714SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000715 while (!AllNodes.empty()) {
716 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000717 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000718 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000719 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000720 }
Chris Lattner65113b22005-11-08 22:07:03 +0000721 N->OperandList = 0;
722 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000723 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000724 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000725}
726
Duncan Sands83ec4b62008-06-06 12:08:01 +0000727SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000728 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000729 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000730 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000731 return getNode(ISD::AND, Op.getValueType(), Op,
732 getConstant(Imm, Op.getValueType()));
733}
734
Duncan Sands83ec4b62008-06-06 12:08:01 +0000735SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000736 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000737 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000738}
739
Duncan Sands83ec4b62008-06-06 12:08:01 +0000740SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
741 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000742
Evan Cheng0ff39b32008-06-30 07:31:25 +0000743 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000744 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000745 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000746
747 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000748 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000749 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000750 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000751 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000752 SDNode *N = NULL;
753 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000754 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000755 return SDOperand(N, 0);
756 if (!N) {
757 N = new ConstantSDNode(isT, Val, EltVT);
758 CSEMap.InsertNode(N, IP);
759 AllNodes.push_back(N);
760 }
761
762 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000763 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000764 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000765 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000766 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
767 }
768 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000769}
770
Chris Lattner0bd48932008-01-17 07:00:52 +0000771SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
772 return getConstant(Val, TLI.getPointerTy(), isTarget);
773}
774
775
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
777 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000778
Duncan Sands83ec4b62008-06-06 12:08:01 +0000779 MVT EltVT =
780 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000781
Chris Lattnerd8658612005-02-17 20:17:32 +0000782 // Do the map lookup using the actual bit pattern for the floating point
783 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
784 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000785 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000786 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000787 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000788 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000789 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000790 SDNode *N = NULL;
791 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000792 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000793 return SDOperand(N, 0);
794 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000795 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000796 CSEMap.InsertNode(N, IP);
797 AllNodes.push_back(N);
798 }
799
Dan Gohman7f321562007-06-25 16:23:39 +0000800 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000801 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000802 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000803 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000804 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
805 }
806 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000807}
808
Duncan Sands83ec4b62008-06-06 12:08:01 +0000809SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
810 MVT EltVT =
811 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000812 if (EltVT==MVT::f32)
813 return getConstantFP(APFloat((float)Val), VT, isTarget);
814 else
815 return getConstantFP(APFloat(Val), VT, isTarget);
816}
817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000819 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000820 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000821 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000822
823 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
824 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000825 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000826 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
827 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
828 }
829
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000830 if (GVar && GVar->isThreadLocal())
831 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
832 else
833 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000834
Jim Laskey583bd472006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000836 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000837 ID.AddPointer(GV);
838 ID.AddInteger(Offset);
839 void *IP = 0;
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
843 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
846}
847
Duncan Sands83ec4b62008-06-06 12:08:01 +0000848SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000849 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000850 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000851 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000852 ID.AddInteger(FI);
853 void *IP = 0;
854 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
855 return SDOperand(E, 0);
856 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
857 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000858 AllNodes.push_back(N);
859 return SDOperand(N, 0);
860}
861
Duncan Sands83ec4b62008-06-06 12:08:01 +0000862SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000863 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000864 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000865 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000866 ID.AddInteger(JTI);
867 void *IP = 0;
868 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
869 return SDOperand(E, 0);
870 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
871 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
874}
875
Duncan Sands83ec4b62008-06-06 12:08:01 +0000876SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000877 unsigned Alignment, int Offset,
878 bool isTarget) {
879 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000880 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000881 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000882 ID.AddInteger(Alignment);
883 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000884 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000885 void *IP = 0;
886 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
887 return SDOperand(E, 0);
888 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
889 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
892}
893
Chris Lattnerc3aae252005-01-07 07:46:32 +0000894
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000896 unsigned Alignment, int Offset,
897 bool isTarget) {
898 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000899 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000900 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000901 ID.AddInteger(Alignment);
902 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000903 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000904 void *IP = 0;
905 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
906 return SDOperand(E, 0);
907 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
908 CSEMap.InsertNode(N, IP);
909 AllNodes.push_back(N);
910 return SDOperand(N, 0);
911}
912
913
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000915 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000916 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000917 ID.AddPointer(MBB);
918 void *IP = 0;
919 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
920 return SDOperand(E, 0);
921 SDNode *N = new BasicBlockSDNode(MBB);
922 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 AllNodes.push_back(N);
924 return SDOperand(N, 0);
925}
926
Duncan Sands276dcbd2008-03-21 09:14:45 +0000927SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
928 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000929 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000930 ID.AddInteger(Flags.getRawBits());
931 void *IP = 0;
932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
933 return SDOperand(E, 0);
934 SDNode *N = new ARG_FLAGSSDNode(Flags);
935 CSEMap.InsertNode(N, IP);
936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Duncan Sands83ec4b62008-06-06 12:08:01 +0000940SDOperand SelectionDAG::getValueType(MVT VT) {
941 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
942 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000943
Duncan Sands83ec4b62008-06-06 12:08:01 +0000944 SDNode *&N = VT.isExtended() ?
945 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000946
947 if (N) return SDOperand(N, 0);
948 N = new VTSDNode(VT);
949 AllNodes.push_back(N);
950 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000951}
952
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 SDNode *&N = ExternalSymbols[Sym];
955 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000956 N = new ExternalSymbolSDNode(false, Sym, VT);
957 AllNodes.push_back(N);
958 return SDOperand(N, 0);
959}
960
Duncan Sands83ec4b62008-06-06 12:08:01 +0000961SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000962 SDNode *&N = TargetExternalSymbols[Sym];
963 if (N) return SDOperand(N, 0);
964 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965 AllNodes.push_back(N);
966 return SDOperand(N, 0);
967}
968
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000969SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
970 if ((unsigned)Cond >= CondCodeNodes.size())
971 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000972
Chris Lattner079a27a2005-08-09 20:40:02 +0000973 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000974 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000975 AllNodes.push_back(CondCodeNodes[Cond]);
976 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000977 return SDOperand(CondCodeNodes[Cond], 0);
978}
979
Duncan Sands83ec4b62008-06-06 12:08:01 +0000980SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000981 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000982 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000983 ID.AddInteger(RegNo);
984 void *IP = 0;
985 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
986 return SDOperand(E, 0);
987 SDNode *N = new RegisterSDNode(RegNo, VT);
988 CSEMap.InsertNode(N, IP);
989 AllNodes.push_back(N);
990 return SDOperand(N, 0);
991}
992
Dan Gohman7f460202008-06-30 20:59:49 +0000993SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
994 unsigned Line, unsigned Col,
995 const CompileUnitDesc *CU) {
996 FoldingSetNodeID ID;
997 SDOperand Ops[] = { Root };
998 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
999 ID.AddInteger(Line);
1000 ID.AddInteger(Col);
1001 ID.AddPointer(CU);
1002 void *IP = 0;
1003 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1004 return SDOperand(E, 0);
1005 SDNode *N = new DbgStopPointSDNode(Root, Line, Col, CU);
1006 CSEMap.InsertNode(N, IP);
1007 AllNodes.push_back(N);
1008 return SDOperand(N, 0);
1009}
1010
Dan Gohman44066042008-07-01 00:05:16 +00001011SDOperand SelectionDAG::getLabel(unsigned Opcode,
1012 SDOperand Root,
1013 unsigned LabelID) {
1014 FoldingSetNodeID ID;
1015 SDOperand Ops[] = { Root };
1016 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1017 ID.AddInteger(LabelID);
1018 void *IP = 0;
1019 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1020 return SDOperand(E, 0);
1021 SDNode *N = new LabelSDNode(Opcode, Root, LabelID);
1022 CSEMap.InsertNode(N, IP);
1023 AllNodes.push_back(N);
1024 return SDOperand(N, 0);
1025}
1026
Dan Gohman69de1932008-02-06 22:27:42 +00001027SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001028 assert((!V || isa<PointerType>(V->getType())) &&
1029 "SrcValue is not a pointer?");
1030
Jim Laskey583bd472006-10-27 23:46:08 +00001031 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001032 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001033 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001034
Chris Lattner61b09412006-08-11 21:01:22 +00001035 void *IP = 0;
1036 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1037 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001038
1039 SDNode *N = new SrcValueSDNode(V);
1040 CSEMap.InsertNode(N, IP);
1041 AllNodes.push_back(N);
1042 return SDOperand(N, 0);
1043}
1044
Dan Gohman36b5c132008-04-07 19:35:22 +00001045SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001046 const Value *v = MO.getValue();
1047 assert((!v || isa<PointerType>(v->getType())) &&
1048 "SrcValue is not a pointer?");
1049
1050 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001051 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001052 ID.AddPointer(v);
1053 ID.AddInteger(MO.getFlags());
1054 ID.AddInteger(MO.getOffset());
1055 ID.AddInteger(MO.getSize());
1056 ID.AddInteger(MO.getAlignment());
1057
1058 void *IP = 0;
1059 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1060 return SDOperand(E, 0);
1061
1062 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001063 CSEMap.InsertNode(N, IP);
1064 AllNodes.push_back(N);
1065 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001066}
1067
Chris Lattner37ce9df2007-10-15 17:47:20 +00001068/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1069/// specified value type.
Mon P Wang364d73d2008-07-05 20:40:31 +00001070SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001071 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001072 unsigned ByteSize = VT.getSizeInBits()/8;
1073 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001074 unsigned StackAlign =
1075 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1076
Chris Lattner37ce9df2007-10-15 17:47:20 +00001077 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1078 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1079}
1080
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001082 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 // These setcc operations always fold.
1084 switch (Cond) {
1085 default: break;
1086 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001087 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001089 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001090
1091 case ISD::SETOEQ:
1092 case ISD::SETOGT:
1093 case ISD::SETOGE:
1094 case ISD::SETOLT:
1095 case ISD::SETOLE:
1096 case ISD::SETONE:
1097 case ISD::SETO:
1098 case ISD::SETUO:
1099 case ISD::SETUEQ:
1100 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001101 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001102 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001104
Chris Lattner67255a12005-04-07 18:14:58 +00001105 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001106 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001107 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001108 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001109
Chris Lattnerc3aae252005-01-07 07:46:32 +00001110 switch (Cond) {
1111 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001112 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1113 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001114 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1115 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1116 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1117 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1118 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1119 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1120 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1121 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123 }
Chris Lattner67255a12005-04-07 18:14:58 +00001124 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001125 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001127 // No compile time operations on this type yet.
1128 if (N1C->getValueType(0) == MVT::ppcf128)
1129 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001130
1131 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001133 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001134 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1135 return getNode(ISD::UNDEF, VT);
1136 // fall through
1137 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1138 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1139 return getNode(ISD::UNDEF, VT);
1140 // fall through
1141 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001142 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001143 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1144 return getNode(ISD::UNDEF, VT);
1145 // fall through
1146 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1147 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1148 return getNode(ISD::UNDEF, VT);
1149 // fall through
1150 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1151 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1152 return getNode(ISD::UNDEF, VT);
1153 // fall through
1154 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001155 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001156 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1157 return getNode(ISD::UNDEF, VT);
1158 // fall through
1159 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001160 R==APFloat::cmpEqual, VT);
1161 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1162 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1163 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1164 R==APFloat::cmpEqual, VT);
1165 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1166 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1167 R==APFloat::cmpLessThan, VT);
1168 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1169 R==APFloat::cmpUnordered, VT);
1170 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1171 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
1173 } else {
1174 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001175 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001177 }
1178
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001179 // Could not fold it.
1180 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001181}
1182
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001183/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1184/// use this predicate to simplify operations downstream.
1185bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1186 unsigned BitWidth = Op.getValueSizeInBits();
1187 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1188}
1189
Dan Gohmanea859be2007-06-22 14:59:07 +00001190/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1191/// this predicate to simplify operations downstream. Mask is known to be zero
1192/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001193bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001194 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001195 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001196 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1197 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1198 return (KnownZero & Mask) == Mask;
1199}
1200
1201/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1202/// known to be either zero or one and return them in the KnownZero/KnownOne
1203/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1204/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001205void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001206 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001207 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001208 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001209 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001210 "Mask size mismatches value type size!");
1211
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001212 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001213 if (Depth == 6 || Mask == 0)
1214 return; // Limit search depth.
1215
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001216 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001217
1218 switch (Op.getOpcode()) {
1219 case ISD::Constant:
1220 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001221 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001222 KnownZero = ~KnownOne & Mask;
1223 return;
1224 case ISD::AND:
1225 // If either the LHS or the RHS are Zero, the result is zero.
1226 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001227 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1228 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001229 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1230 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1231
1232 // Output known-1 bits are only known if set in both the LHS & RHS.
1233 KnownOne &= KnownOne2;
1234 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1235 KnownZero |= KnownZero2;
1236 return;
1237 case ISD::OR:
1238 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001239 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
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-0 bits are only known if clear in both the LHS & RHS.
1245 KnownZero &= KnownZero2;
1246 // Output known-1 are known to be set if set in either the LHS | RHS.
1247 KnownOne |= KnownOne2;
1248 return;
1249 case ISD::XOR: {
1250 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1251 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1252 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1253 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1254
1255 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001256 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001257 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1258 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1259 KnownZero = KnownZeroOut;
1260 return;
1261 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001262 case ISD::MUL: {
1263 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1264 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1265 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1266 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1267 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1268
1269 // If low bits are zero in either operand, output low known-0 bits.
1270 // Also compute a conserative estimate for high known-0 bits.
1271 // More trickiness is possible, but this is sufficient for the
1272 // interesting case of alignment computation.
1273 KnownOne.clear();
1274 unsigned TrailZ = KnownZero.countTrailingOnes() +
1275 KnownZero2.countTrailingOnes();
1276 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001277 KnownZero2.countLeadingOnes(),
1278 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001279
1280 TrailZ = std::min(TrailZ, BitWidth);
1281 LeadZ = std::min(LeadZ, BitWidth);
1282 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1283 APInt::getHighBitsSet(BitWidth, LeadZ);
1284 KnownZero &= Mask;
1285 return;
1286 }
1287 case ISD::UDIV: {
1288 // For the purposes of computing leading zeros we can conservatively
1289 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001290 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001291 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1292 ComputeMaskedBits(Op.getOperand(0),
1293 AllOnes, KnownZero2, KnownOne2, Depth+1);
1294 unsigned LeadZ = KnownZero2.countLeadingOnes();
1295
1296 KnownOne2.clear();
1297 KnownZero2.clear();
1298 ComputeMaskedBits(Op.getOperand(1),
1299 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001300 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1301 if (RHSUnknownLeadingOnes != BitWidth)
1302 LeadZ = std::min(BitWidth,
1303 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001304
1305 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1306 return;
1307 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001308 case ISD::SELECT:
1309 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1310 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1311 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1312 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1313
1314 // Only known if known in both the LHS and RHS.
1315 KnownOne &= KnownOne2;
1316 KnownZero &= KnownZero2;
1317 return;
1318 case ISD::SELECT_CC:
1319 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1320 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1321 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1322 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1323
1324 // Only known if known in both the LHS and RHS.
1325 KnownOne &= KnownOne2;
1326 KnownZero &= KnownZero2;
1327 return;
1328 case ISD::SETCC:
1329 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001330 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1331 BitWidth > 1)
1332 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 return;
1334 case ISD::SHL:
1335 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1336 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001337 unsigned ShAmt = SA->getValue();
1338
1339 // If the shift count is an invalid immediate, don't do anything.
1340 if (ShAmt >= BitWidth)
1341 return;
1342
1343 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001344 KnownZero, KnownOne, Depth+1);
1345 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001346 KnownZero <<= ShAmt;
1347 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001348 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001349 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001350 }
1351 return;
1352 case ISD::SRL:
1353 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1354 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001355 unsigned ShAmt = SA->getValue();
1356
Dan Gohmand4cf9922008-02-26 18:50:50 +00001357 // If the shift count is an invalid immediate, don't do anything.
1358 if (ShAmt >= BitWidth)
1359 return;
1360
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001361 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001362 KnownZero, KnownOne, Depth+1);
1363 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001364 KnownZero = KnownZero.lshr(ShAmt);
1365 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001366
Dan Gohman72d2fd52008-02-13 22:43:25 +00001367 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001368 KnownZero |= HighBits; // High bits known zero.
1369 }
1370 return;
1371 case ISD::SRA:
1372 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001373 unsigned ShAmt = SA->getValue();
1374
Dan Gohmand4cf9922008-02-26 18:50:50 +00001375 // If the shift count is an invalid immediate, don't do anything.
1376 if (ShAmt >= BitWidth)
1377 return;
1378
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001379 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001380 // If any of the demanded bits are produced by the sign extension, we also
1381 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001382 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1383 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001384 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001385
1386 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1387 Depth+1);
1388 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001389 KnownZero = KnownZero.lshr(ShAmt);
1390 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001391
1392 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001393 APInt SignBit = APInt::getSignBit(BitWidth);
1394 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001395
Dan Gohmanca93a432008-02-20 16:30:17 +00001396 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001397 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001398 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001399 KnownOne |= HighBits; // New bits are known one.
1400 }
1401 }
1402 return;
1403 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001404 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1405 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001406
1407 // Sign extension. Compute the demanded bits in the result that are not
1408 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001409 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001410
Dan Gohman977a76f2008-02-13 22:28:48 +00001411 APInt InSignBit = APInt::getSignBit(EBits);
1412 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001413
1414 // If the sign extended bits are demanded, we know that the sign
1415 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001416 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001417 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 InputDemandedBits |= InSignBit;
1419
1420 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1421 KnownZero, KnownOne, Depth+1);
1422 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1423
1424 // If the sign bit of the input is known set or clear, then we know the
1425 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001426 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001427 KnownZero |= NewBits;
1428 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001429 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001430 KnownOne |= NewBits;
1431 KnownZero &= ~NewBits;
1432 } else { // Input sign bit unknown
1433 KnownZero &= ~NewBits;
1434 KnownOne &= ~NewBits;
1435 }
1436 return;
1437 }
1438 case ISD::CTTZ:
1439 case ISD::CTLZ:
1440 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001441 unsigned LowBits = Log2_32(BitWidth)+1;
1442 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001443 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001444 return;
1445 }
1446 case ISD::LOAD: {
1447 if (ISD::isZEXTLoad(Op.Val)) {
1448 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001449 MVT VT = LD->getMemoryVT();
1450 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001451 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001452 }
1453 return;
1454 }
1455 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001456 MVT InVT = Op.getOperand(0).getValueType();
1457 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001458 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1459 APInt InMask = Mask;
1460 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001461 KnownZero.trunc(InBits);
1462 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001463 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001464 KnownZero.zext(BitWidth);
1465 KnownOne.zext(BitWidth);
1466 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 return;
1468 }
1469 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001470 MVT InVT = Op.getOperand(0).getValueType();
1471 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001472 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001473 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1474 APInt InMask = Mask;
1475 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001476
1477 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001478 // bit is demanded. Temporarily set this bit in the mask for our callee.
1479 if (NewBits.getBoolValue())
1480 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001482 KnownZero.trunc(InBits);
1483 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001484 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1485
1486 // Note if the sign bit is known to be zero or one.
1487 bool SignBitKnownZero = KnownZero.isNegative();
1488 bool SignBitKnownOne = KnownOne.isNegative();
1489 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1490 "Sign bit can't be known to be both zero and one!");
1491
1492 // If the sign bit wasn't actually demanded by our caller, we don't
1493 // want it set in the KnownZero and KnownOne result values. Reset the
1494 // mask and reapply it to the result values.
1495 InMask = Mask;
1496 InMask.trunc(InBits);
1497 KnownZero &= InMask;
1498 KnownOne &= InMask;
1499
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001500 KnownZero.zext(BitWidth);
1501 KnownOne.zext(BitWidth);
1502
Dan Gohman977a76f2008-02-13 22:28:48 +00001503 // If the sign bit is known zero or one, the top bits match.
1504 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001505 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001506 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001507 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001508 return;
1509 }
1510 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001511 MVT InVT = Op.getOperand(0).getValueType();
1512 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001513 APInt InMask = Mask;
1514 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001515 KnownZero.trunc(InBits);
1516 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001517 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001518 KnownZero.zext(BitWidth);
1519 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 return;
1521 }
1522 case ISD::TRUNCATE: {
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.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001527 KnownZero.zext(InBits);
1528 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001529 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001530 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001531 KnownZero.trunc(BitWidth);
1532 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001533 break;
1534 }
1535 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1537 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001538 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1539 KnownOne, Depth+1);
1540 KnownZero |= (~InMask) & Mask;
1541 return;
1542 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001543 case ISD::FGETSIGN:
1544 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001545 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001546 return;
1547
Dan Gohman23e8b712008-04-28 17:02:21 +00001548 case ISD::SUB: {
1549 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1550 // We know that the top bits of C-X are clear if X contains less bits
1551 // than C (i.e. no wrap-around can happen). For example, 20-X is
1552 // positive if we can prove that X is >= 0 and < 16.
1553 if (CLHS->getAPIntValue().isNonNegative()) {
1554 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1555 // NLZ can't be BitWidth with no sign bit
1556 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1557 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1558 Depth+1);
1559
1560 // If all of the MaskV bits are known to be zero, then we know the
1561 // output top bits are zero, because we now know that the output is
1562 // from [0-C].
1563 if ((KnownZero2 & MaskV) == MaskV) {
1564 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1565 // Top bits known zero.
1566 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1567 }
1568 }
1569 }
1570 }
1571 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001572 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 // Output known-0 bits are known if clear or set in both the low clear bits
1574 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1575 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001576 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1577 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1578 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1579 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1580
1581 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1582 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1583 KnownZeroOut = std::min(KnownZeroOut,
1584 KnownZero2.countTrailingOnes());
1585
1586 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001587 return;
1588 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001589 case ISD::SREM:
1590 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001591 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001592 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001593 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001594 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1595 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001596
Dan Gohman23e8b712008-04-28 17:02:21 +00001597 // The sign of a remainder is equal to the sign of the first
1598 // operand (zero being positive).
1599 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1600 KnownZero2 |= ~LowBits;
1601 else if (KnownOne2[BitWidth-1])
1602 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001603
Dan Gohman23e8b712008-04-28 17:02:21 +00001604 KnownZero |= KnownZero2 & Mask;
1605 KnownOne |= KnownOne2 & Mask;
1606
1607 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 }
1609 }
1610 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001611 case ISD::UREM: {
1612 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001613 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001614 if (RA.isPowerOf2()) {
1615 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001616 APInt Mask2 = LowBits & Mask;
1617 KnownZero |= ~LowBits & Mask;
1618 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1619 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1620 break;
1621 }
1622 }
1623
1624 // Since the result is less than or equal to either operand, any leading
1625 // zero bits in either operand must also exist in the result.
1626 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1627 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1628 Depth+1);
1629 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1630 Depth+1);
1631
1632 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1633 KnownZero2.countLeadingOnes());
1634 KnownOne.clear();
1635 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1636 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 }
1638 default:
1639 // Allow the target to implement this method for its nodes.
1640 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1641 case ISD::INTRINSIC_WO_CHAIN:
1642 case ISD::INTRINSIC_W_CHAIN:
1643 case ISD::INTRINSIC_VOID:
1644 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1645 }
1646 return;
1647 }
1648}
1649
1650/// ComputeNumSignBits - Return the number of times the sign bit of the
1651/// register is replicated into the other bits. We know that at least 1 bit
1652/// is always equal to the sign bit (itself), but other cases can give us
1653/// information. For example, immediately after an "SRA X, 2", we know that
1654/// the top 3 bits are all equal to each other, so we return 3.
1655unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001656 MVT VT = Op.getValueType();
1657 assert(VT.isInteger() && "Invalid VT!");
1658 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001659 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001660 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001661
1662 if (Depth == 6)
1663 return 1; // Limit search depth.
1664
1665 switch (Op.getOpcode()) {
1666 default: break;
1667 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 return VTBits-Tmp+1;
1670 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001671 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001672 return VTBits-Tmp;
1673
1674 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001675 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1676 // If negative, return # leading ones.
1677 if (Val.isNegative())
1678 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001679
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001680 // Return # leading zeros.
1681 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 }
1683
1684 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001685 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001686 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1687
1688 case ISD::SIGN_EXTEND_INREG:
1689 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001690 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001691 Tmp = VTBits-Tmp+1;
1692
1693 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1694 return std::max(Tmp, Tmp2);
1695
1696 case ISD::SRA:
1697 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1698 // SRA X, C -> adds C sign bits.
1699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1700 Tmp += C->getValue();
1701 if (Tmp > VTBits) Tmp = VTBits;
1702 }
1703 return Tmp;
1704 case ISD::SHL:
1705 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1706 // shl destroys sign bits.
1707 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1708 if (C->getValue() >= VTBits || // Bad shift.
1709 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1710 return Tmp - C->getValue();
1711 }
1712 break;
1713 case ISD::AND:
1714 case ISD::OR:
1715 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001716 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001717 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001718 if (Tmp != 1) {
1719 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1720 FirstAnswer = std::min(Tmp, Tmp2);
1721 // We computed what we know about the sign bits as our first
1722 // answer. Now proceed to the generic code that uses
1723 // ComputeMaskedBits, and pick whichever answer is better.
1724 }
1725 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001726
1727 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001728 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001729 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001730 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 return std::min(Tmp, Tmp2);
1732
1733 case ISD::SETCC:
1734 // If setcc returns 0/-1, all bits are sign bits.
1735 if (TLI.getSetCCResultContents() ==
1736 TargetLowering::ZeroOrNegativeOneSetCCResult)
1737 return VTBits;
1738 break;
1739 case ISD::ROTL:
1740 case ISD::ROTR:
1741 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1742 unsigned RotAmt = C->getValue() & (VTBits-1);
1743
1744 // Handle rotate right by N like a rotate left by 32-N.
1745 if (Op.getOpcode() == ISD::ROTR)
1746 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1747
1748 // If we aren't rotating out all of the known-in sign bits, return the
1749 // number that are left. This handles rotl(sext(x), 1) for example.
1750 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1751 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1752 }
1753 break;
1754 case ISD::ADD:
1755 // Add can have at most one carry bit. Thus we know that the output
1756 // is, at worst, one more bit than the inputs.
1757 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1758 if (Tmp == 1) return 1; // Early out.
1759
1760 // Special case decrementing a value (ADD X, -1):
1761 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1762 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001763 APInt KnownZero, KnownOne;
1764 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001765 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1766
1767 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1768 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001769 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001770 return VTBits;
1771
1772 // If we are subtracting one from a positive number, there is no carry
1773 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001774 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001775 return Tmp;
1776 }
1777
1778 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1779 if (Tmp2 == 1) return 1;
1780 return std::min(Tmp, Tmp2)-1;
1781 break;
1782
1783 case ISD::SUB:
1784 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1785 if (Tmp2 == 1) return 1;
1786
1787 // Handle NEG.
1788 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001789 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001790 APInt KnownZero, KnownOne;
1791 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1793 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1794 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001795 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001796 return VTBits;
1797
1798 // If the input is known to be positive (the sign bit is known clear),
1799 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001800 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001801 return Tmp2;
1802
1803 // Otherwise, we treat this like a SUB.
1804 }
1805
1806 // Sub can have at most one carry bit. Thus we know that the output
1807 // is, at worst, one more bit than the inputs.
1808 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1809 if (Tmp == 1) return 1; // Early out.
1810 return std::min(Tmp, Tmp2)-1;
1811 break;
1812 case ISD::TRUNCATE:
1813 // FIXME: it's tricky to do anything useful for this, but it is an important
1814 // case for targets like X86.
1815 break;
1816 }
1817
1818 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1819 if (Op.getOpcode() == ISD::LOAD) {
1820 LoadSDNode *LD = cast<LoadSDNode>(Op);
1821 unsigned ExtType = LD->getExtensionType();
1822 switch (ExtType) {
1823 default: break;
1824 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001825 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 return VTBits-Tmp+1;
1827 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001828 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001829 return VTBits-Tmp;
1830 }
1831 }
1832
1833 // Allow the target to implement this method for its nodes.
1834 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1835 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1836 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1837 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1838 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001839 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001840 }
1841
1842 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1843 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001844 APInt KnownZero, KnownOne;
1845 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001846 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1847
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001848 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001849 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001850 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 Mask = KnownOne;
1852 } else {
1853 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001854 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 }
1856
1857 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1858 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001859 Mask = ~Mask;
1860 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 // Return # leading zeros. We use 'min' here in case Val was zero before
1862 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001863 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001864}
1865
Chris Lattner51dabfb2006-10-14 00:41:01 +00001866
Evan Chenga844bde2008-02-02 04:07:54 +00001867bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1868 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1869 if (!GA) return false;
1870 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1871 if (!GV) return false;
1872 MachineModuleInfo *MMI = getMachineModuleInfo();
1873 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1874}
1875
1876
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001877/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1878/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001879SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001880 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001881 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001882 SDOperand Idx = PermMask.getOperand(i);
1883 if (Idx.getOpcode() == ISD::UNDEF)
1884 return getNode(ISD::UNDEF, VT.getVectorElementType());
1885 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001886 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001887 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1888 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001889
1890 if (V.getOpcode() == ISD::BIT_CONVERT) {
1891 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001892 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001893 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001894 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001895 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001896 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001897 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001898 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001899 return V.getOperand(Index);
1900 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1901 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001902 return SDOperand();
1903}
1904
1905
Chris Lattnerc3aae252005-01-07 07:46:32 +00001906/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001907///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001908SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001909 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001910 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001911 void *IP = 0;
1912 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1913 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001914 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001915 CSEMap.InsertNode(N, IP);
1916
1917 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001918 return SDOperand(N, 0);
1919}
1920
Duncan Sands83ec4b62008-06-06 12:08:01 +00001921SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001922 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001924 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001926 switch (Opcode) {
1927 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001928 case ISD::SIGN_EXTEND:
1929 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001930 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001931 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001932 case ISD::TRUNCATE:
1933 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001934 case ISD::UINT_TO_FP:
1935 case ISD::SINT_TO_FP: {
1936 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001937 // No compile time operations on this type.
1938 if (VT==MVT::ppcf128)
1939 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001940 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1941 (void)apf.convertFromAPInt(Val,
1942 Opcode==ISD::SINT_TO_FP,
1943 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001944 return getConstantFP(apf, VT);
1945 }
Chris Lattner94683772005-12-23 05:30:37 +00001946 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001947 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001948 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001949 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001950 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001951 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001952 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001953 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001954 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001955 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001956 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001957 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001958 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001959 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001960 }
1961 }
1962
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001963 // Constant fold unary operations with a floating point constant operand.
1964 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1965 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001966 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001967 switch (Opcode) {
1968 case ISD::FNEG:
1969 V.changeSign();
1970 return getConstantFP(V, VT);
1971 case ISD::FABS:
1972 V.clearSign();
1973 return getConstantFP(V, VT);
1974 case ISD::FP_ROUND:
1975 case ISD::FP_EXTEND:
1976 // This can return overflow, underflow, or inexact; we don't care.
1977 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001978 (void)V.convert(*MVTToAPFloatSemantics(VT),
1979 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001980 return getConstantFP(V, VT);
1981 case ISD::FP_TO_SINT:
1982 case ISD::FP_TO_UINT: {
1983 integerPart x;
1984 assert(integerPartWidth >= 64);
1985 // FIXME need to be more flexible about rounding mode.
1986 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1987 Opcode==ISD::FP_TO_SINT,
1988 APFloat::rmTowardZero);
1989 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1990 break;
1991 return getConstant(x, VT);
1992 }
1993 case ISD::BIT_CONVERT:
1994 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1995 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1996 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1997 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001998 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001999 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002000 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002001 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002002
2003 unsigned OpOpcode = Operand.Val->getOpcode();
2004 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002005 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002006 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002007 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002008 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002009 assert(VT.isFloatingPoint() &&
2010 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002011 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002012 if (Operand.getOpcode() == ISD::UNDEF)
2013 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002014 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002015 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002016 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002017 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002018 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002019 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002020 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002021 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2022 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2023 break;
2024 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002025 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002026 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002027 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002028 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002029 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002030 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002031 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002032 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002033 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002035 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002036 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002037 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002038 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002039 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2040 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2041 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2042 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002044 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002045 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002046 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002047 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002048 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002049 if (OpOpcode == ISD::TRUNCATE)
2050 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002051 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2052 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002053 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002054 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002055 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002056 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002057 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2058 else
2059 return Operand.Val->getOperand(0);
2060 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002061 break;
Chris Lattner35481892005-12-23 00:16:34 +00002062 case ISD::BIT_CONVERT:
2063 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002064 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002065 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002066 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002067 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2068 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002069 if (OpOpcode == ISD::UNDEF)
2070 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002071 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002072 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002073 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2074 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002075 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002076 if (OpOpcode == ISD::UNDEF)
2077 return getNode(ISD::UNDEF, VT);
2078 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2079 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2080 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2081 Operand.getConstantOperandVal(1) == 0 &&
2082 Operand.getOperand(0).getValueType() == VT)
2083 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002084 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002085 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002086 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2087 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002088 Operand.Val->getOperand(0));
2089 if (OpOpcode == ISD::FNEG) // --X -> X
2090 return Operand.Val->getOperand(0);
2091 break;
2092 case ISD::FABS:
2093 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2094 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2095 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002096 }
2097
Chris Lattner43247a12005-08-25 19:12:10 +00002098 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002099 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002100 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002101 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002102 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002103 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002104 void *IP = 0;
2105 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2106 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002107 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002108 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002109 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002110 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002111 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 AllNodes.push_back(N);
2113 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002114}
2115
Chris Lattner36019aa2005-04-18 03:48:41 +00002116
2117
Duncan Sands83ec4b62008-06-06 12:08:01 +00002118SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002120 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2121 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002122 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002123 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002124 case ISD::TokenFactor:
2125 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2126 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002127 // Fold trivial token factors.
2128 if (N1.getOpcode() == ISD::EntryToken) return N2;
2129 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002130 break;
Chris Lattner76365122005-01-16 02:23:22 +00002131 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002132 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002133 N1.getValueType() == VT && "Binary operator types must match!");
2134 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2135 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002136 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002137 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002138 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2139 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002140 break;
Chris Lattner76365122005-01-16 02:23:22 +00002141 case ISD::OR:
2142 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002143 case ISD::ADD:
2144 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002145 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002146 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002147 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2148 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002149 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002150 return N1;
2151 break;
Chris Lattner76365122005-01-16 02:23:22 +00002152 case ISD::UDIV:
2153 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002154 case ISD::MULHU:
2155 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002156 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002157 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002158 case ISD::MUL:
2159 case ISD::SDIV:
2160 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002161 case ISD::FADD:
2162 case ISD::FSUB:
2163 case ISD::FMUL:
2164 case ISD::FDIV:
2165 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002166 assert(N1.getValueType() == N2.getValueType() &&
2167 N1.getValueType() == VT && "Binary operator types must match!");
2168 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002169 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2170 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002171 N1.getValueType().isFloatingPoint() &&
2172 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002173 "Invalid FCOPYSIGN!");
2174 break;
Chris Lattner76365122005-01-16 02:23:22 +00002175 case ISD::SHL:
2176 case ISD::SRA:
2177 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002178 case ISD::ROTL:
2179 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002180 assert(VT == N1.getValueType() &&
2181 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002182 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002183 "Shifts only work on integers");
2184
2185 // Always fold shifts of i1 values so the code generator doesn't need to
2186 // handle them. Since we know the size of the shift has to be less than the
2187 // size of the value, the shift/rotate count is guaranteed to be zero.
2188 if (VT == MVT::i1)
2189 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002190 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002191 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002192 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002193 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002195 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002196 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002197 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002198 break;
2199 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002200 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002201 assert(VT.isFloatingPoint() &&
2202 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002203 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002204 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002205 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002206 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002207 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002208 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002210 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002211 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002212 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002213 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002214 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002215 break;
2216 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002217 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002218 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002219 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002220 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002221 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002222 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002223 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002224
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002225 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002226 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002227 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002228 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002229 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002230 return getConstant(Val, VT);
2231 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002232 break;
2233 }
2234 case ISD::EXTRACT_VECTOR_ELT:
2235 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2236
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002237 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2238 if (N1.getOpcode() == ISD::UNDEF)
2239 return getNode(ISD::UNDEF, VT);
2240
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002241 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2242 // expanding copies of large vectors from registers.
2243 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2244 N1.getNumOperands() > 0) {
2245 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002246 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002247 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2248 N1.getOperand(N2C->getValue() / Factor),
2249 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2250 }
2251
2252 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2253 // expanding large vector constants.
2254 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2255 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002256
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002257 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2258 // operations are lowered to scalars.
2259 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2260 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2261 if (IEC == N2C)
2262 return N1.getOperand(1);
2263 else
2264 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2265 }
2266 break;
2267 case ISD::EXTRACT_ELEMENT:
2268 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002269 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2270 (N1.getValueType().isInteger() == VT.isInteger()) &&
2271 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002272
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002273 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2274 // 64-bit integers into 32-bit parts. Instead of building the extract of
2275 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2276 if (N1.getOpcode() == ISD::BUILD_PAIR)
2277 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002278
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002279 // EXTRACT_ELEMENT of a constant int is also very common.
2280 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002281 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002282 unsigned Shift = ElementSize * N2C->getValue();
2283 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2284 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002285 }
2286 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002287 case ISD::EXTRACT_SUBVECTOR:
2288 if (N1.getValueType() == VT) // Trivial extraction.
2289 return N1;
2290 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002291 }
2292
2293 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002294 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002295 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002296 switch (Opcode) {
2297 case ISD::ADD: return getConstant(C1 + C2, VT);
2298 case ISD::SUB: return getConstant(C1 - C2, VT);
2299 case ISD::MUL: return getConstant(C1 * C2, VT);
2300 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002301 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002302 break;
2303 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002304 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002305 break;
2306 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002307 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002308 break;
2309 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002310 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002311 break;
2312 case ISD::AND : return getConstant(C1 & C2, VT);
2313 case ISD::OR : return getConstant(C1 | C2, VT);
2314 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002315 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002316 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2317 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2318 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2319 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 default: break;
2321 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002322 } else { // Cannonicalize constant to RHS if commutative
2323 if (isCommutativeBinOp(Opcode)) {
2324 std::swap(N1C, N2C);
2325 std::swap(N1, N2);
2326 }
2327 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328 }
2329
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002330 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2332 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002333 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002334 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2335 // Cannonicalize constant to RHS if commutative
2336 std::swap(N1CFP, N2CFP);
2337 std::swap(N1, N2);
2338 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002339 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2340 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002341 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002342 case ISD::FADD:
2343 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002344 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002345 return getConstantFP(V1, VT);
2346 break;
2347 case ISD::FSUB:
2348 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2349 if (s!=APFloat::opInvalidOp)
2350 return getConstantFP(V1, VT);
2351 break;
2352 case ISD::FMUL:
2353 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2354 if (s!=APFloat::opInvalidOp)
2355 return getConstantFP(V1, VT);
2356 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002357 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002358 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2359 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2360 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002361 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002362 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002363 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2364 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2365 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002366 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002367 case ISD::FCOPYSIGN:
2368 V1.copySign(V2);
2369 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002370 default: break;
2371 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002373 }
Chris Lattner62b57722006-04-20 05:39:12 +00002374
2375 // Canonicalize an UNDEF to the RHS, even over a constant.
2376 if (N1.getOpcode() == ISD::UNDEF) {
2377 if (isCommutativeBinOp(Opcode)) {
2378 std::swap(N1, N2);
2379 } else {
2380 switch (Opcode) {
2381 case ISD::FP_ROUND_INREG:
2382 case ISD::SIGN_EXTEND_INREG:
2383 case ISD::SUB:
2384 case ISD::FSUB:
2385 case ISD::FDIV:
2386 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002387 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002388 return N1; // fold op(undef, arg2) -> undef
2389 case ISD::UDIV:
2390 case ISD::SDIV:
2391 case ISD::UREM:
2392 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002393 case ISD::SRL:
2394 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002395 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002396 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2397 // For vectors, we can't easily build an all zero vector, just return
2398 // the LHS.
2399 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002400 }
2401 }
2402 }
2403
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002404 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002405 if (N2.getOpcode() == ISD::UNDEF) {
2406 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002407 case ISD::XOR:
2408 if (N1.getOpcode() == ISD::UNDEF)
2409 // Handle undef ^ undef -> 0 special case. This is a common
2410 // idiom (misuse).
2411 return getConstant(0, VT);
2412 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002413 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002414 case ISD::ADDC:
2415 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002416 case ISD::SUB:
2417 case ISD::FADD:
2418 case ISD::FSUB:
2419 case ISD::FMUL:
2420 case ISD::FDIV:
2421 case ISD::FREM:
2422 case ISD::UDIV:
2423 case ISD::SDIV:
2424 case ISD::UREM:
2425 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002426 return N2; // fold op(arg1, undef) -> undef
2427 case ISD::MUL:
2428 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002429 case ISD::SRL:
2430 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002431 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002432 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2433 // For vectors, we can't easily build an all zero vector, just return
2434 // the LHS.
2435 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002436 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002437 if (!VT.isVector())
2438 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002439 // For vectors, we can't easily build an all one vector, just return
2440 // the LHS.
2441 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002442 case ISD::SRA:
2443 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002444 }
2445 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002446
Chris Lattner27e9b412005-05-11 18:57:39 +00002447 // Memoize this node if possible.
2448 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002449 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002450 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002451 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002452 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002453 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002454 void *IP = 0;
2455 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2456 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002457 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002458 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002459 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002460 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002461 }
2462
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463 AllNodes.push_back(N);
2464 return SDOperand(N, 0);
2465}
2466
Duncan Sands83ec4b62008-06-06 12:08:01 +00002467SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002468 SDOperand N1, SDOperand N2, SDOperand N3) {
2469 // Perform various simplifications.
2470 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2471 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002472 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002473 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002474 // Use FoldSetCC to simplify SETCC's.
2475 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002476 if (Simp.Val) return Simp;
2477 break;
2478 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002479 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002480 if (N1C) {
2481 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002482 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002483 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002485 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002486
2487 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002488 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002489 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002490 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002491 if (N2C->getValue()) // Unconditional branch
2492 return getNode(ISD::BR, MVT::Other, N1, N3);
2493 else
2494 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002495 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002496 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002497 case ISD::VECTOR_SHUFFLE:
2498 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002499 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002500 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002501 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002502 "Illegal VECTOR_SHUFFLE node!");
2503 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002504 case ISD::BIT_CONVERT:
2505 // Fold bit_convert nodes from a type to themselves.
2506 if (N1.getValueType() == VT)
2507 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002508 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002509 }
2510
Chris Lattner43247a12005-08-25 19:12:10 +00002511 // Memoize node if it doesn't produce a flag.
2512 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002513 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002514 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002515 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002516 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002517 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002518 void *IP = 0;
2519 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2520 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002521 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002522 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002523 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002524 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002525 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002526 AllNodes.push_back(N);
2527 return SDOperand(N, 0);
2528}
2529
Duncan Sands83ec4b62008-06-06 12:08:01 +00002530SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002531 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002532 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002533 SDOperand Ops[] = { N1, N2, N3, N4 };
2534 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002535}
2536
Duncan Sands83ec4b62008-06-06 12:08:01 +00002537SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002538 SDOperand N1, SDOperand N2, SDOperand N3,
2539 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002540 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2541 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002542}
2543
Dan Gohman707e0182008-04-12 04:36:06 +00002544/// getMemsetValue - Vectorized representation of the memset value
2545/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002546static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2547 unsigned NumBits = VT.isVector() ?
2548 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002549 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002550 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002551 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002552 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002553 Val = (Val << Shift) | Val;
2554 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002555 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002556 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002557 return DAG.getConstant(Val, VT);
2558 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002559 }
Evan Chengf0df0312008-05-15 08:39:06 +00002560
2561 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2562 unsigned Shift = 8;
2563 for (unsigned i = NumBits; i > 8; i >>= 1) {
2564 Value = DAG.getNode(ISD::OR, VT,
2565 DAG.getNode(ISD::SHL, VT, Value,
2566 DAG.getConstant(Shift, MVT::i8)), Value);
2567 Shift <<= 1;
2568 }
2569
2570 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002571}
2572
Dan Gohman707e0182008-04-12 04:36:06 +00002573/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2574/// used when a memcpy is turned into a memset when the source is a constant
2575/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002576static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002577 const TargetLowering &TLI,
2578 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002579 // Handle vector with all elements zero.
2580 if (Str.empty()) {
2581 if (VT.isInteger())
2582 return DAG.getConstant(0, VT);
2583 unsigned NumElts = VT.getVectorNumElements();
2584 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2585 return DAG.getNode(ISD::BIT_CONVERT, VT,
2586 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2587 }
2588
Duncan Sands83ec4b62008-06-06 12:08:01 +00002589 assert(!VT.isVector() && "Can't handle vector type here!");
2590 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002591 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002592 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002593 if (TLI.isLittleEndian())
2594 Offset = Offset + MSB - 1;
2595 for (unsigned i = 0; i != MSB; ++i) {
2596 Val = (Val << 8) | (unsigned char)Str[Offset];
2597 Offset += TLI.isLittleEndian() ? -1 : 1;
2598 }
2599 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002600}
2601
Dan Gohman707e0182008-04-12 04:36:06 +00002602/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002603///
Dan Gohman707e0182008-04-12 04:36:06 +00002604static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2605 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002607 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2608}
2609
Evan Chengf0df0312008-05-15 08:39:06 +00002610/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2611///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002612static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002613 unsigned SrcDelta = 0;
2614 GlobalAddressSDNode *G = NULL;
2615 if (Src.getOpcode() == ISD::GlobalAddress)
2616 G = cast<GlobalAddressSDNode>(Src);
2617 else if (Src.getOpcode() == ISD::ADD &&
2618 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2619 Src.getOperand(1).getOpcode() == ISD::Constant) {
2620 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2621 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2622 }
2623 if (!G)
2624 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002625
Evan Chengf0df0312008-05-15 08:39:06 +00002626 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002627 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2628 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002629
Evan Chengf0df0312008-05-15 08:39:06 +00002630 return false;
2631}
Dan Gohman707e0182008-04-12 04:36:06 +00002632
Evan Chengf0df0312008-05-15 08:39:06 +00002633/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2634/// to replace the memset / memcpy is below the threshold. It also returns the
2635/// types of the sequence of memory ops to perform memset / memcpy.
2636static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002638 SDOperand Dst, SDOperand Src,
2639 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002640 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002641 SelectionDAG &DAG,
2642 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002643 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002644 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002645 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002646 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002647 if (VT != MVT::iAny) {
2648 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002650 // If source is a string constant, this will require an unaligned load.
2651 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2652 if (Dst.getOpcode() != ISD::FrameIndex) {
2653 // Can't change destination alignment. It requires a unaligned store.
2654 if (AllowUnalign)
2655 VT = MVT::iAny;
2656 } else {
2657 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2658 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2659 if (MFI->isFixedObjectIndex(FI)) {
2660 // Can't change destination alignment. It requires a unaligned store.
2661 if (AllowUnalign)
2662 VT = MVT::iAny;
2663 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002664 // Give the stack frame object a larger alignment if needed.
2665 if (MFI->getObjectAlignment(FI) < NewAlign)
2666 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002667 Align = NewAlign;
2668 }
2669 }
2670 }
2671 }
2672
2673 if (VT == MVT::iAny) {
2674 if (AllowUnalign) {
2675 VT = MVT::i64;
2676 } else {
2677 switch (Align & 7) {
2678 case 0: VT = MVT::i64; break;
2679 case 4: VT = MVT::i32; break;
2680 case 2: VT = MVT::i16; break;
2681 default: VT = MVT::i8; break;
2682 }
2683 }
2684
Duncan Sands83ec4b62008-06-06 12:08:01 +00002685 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002686 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002687 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2688 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002689
Duncan Sands8e4eb092008-06-08 20:54:56 +00002690 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002691 VT = LVT;
2692 }
Dan Gohman707e0182008-04-12 04:36:06 +00002693
2694 unsigned NumMemOps = 0;
2695 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002696 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002697 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002698 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002699 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002700 VT = MVT::i64;
2701 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2703 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002704 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002705 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002706 VTSize >>= 1;
2707 }
Dan Gohman707e0182008-04-12 04:36:06 +00002708 }
Dan Gohman707e0182008-04-12 04:36:06 +00002709
2710 if (++NumMemOps > Limit)
2711 return false;
2712 MemOps.push_back(VT);
2713 Size -= VTSize;
2714 }
2715
2716 return true;
2717}
2718
2719static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2720 SDOperand Chain, SDOperand Dst,
2721 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002722 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002723 const Value *DstSV, uint64_t DstSVOff,
2724 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002725 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2726
Dan Gohman21323f32008-05-29 19:42:22 +00002727 // Expand memcpy to a series of load and store ops if the size operand falls
2728 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002729 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002730 uint64_t Limit = -1;
2731 if (!AlwaysInline)
2732 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002733 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002734 std::string Str;
2735 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002736 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002737 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002738 return SDOperand();
2739
Dan Gohman707e0182008-04-12 04:36:06 +00002740
Evan Cheng0ff39b32008-06-30 07:31:25 +00002741 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002742 SmallVector<SDOperand, 8> OutChains;
2743 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002744 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002745 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 MVT VT = MemOps[i];
2747 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002748 SDOperand Value, Store;
2749
Evan Cheng0ff39b32008-06-30 07:31:25 +00002750 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002751 // It's unlikely a store of a vector immediate can be done in a single
2752 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002753 // We also handle store a vector with all zero's.
2754 // FIXME: Handle other cases where store of vector immediate is done in
2755 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002756 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002757 Store = DAG.getStore(Chain, Value,
2758 getMemBasePlusOffset(Dst, DstOff, DAG),
2759 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002760 } else {
2761 Value = DAG.getLoad(VT, Chain,
2762 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002763 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002764 Store = DAG.getStore(Chain, Value,
2765 getMemBasePlusOffset(Dst, DstOff, DAG),
2766 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002767 }
2768 OutChains.push_back(Store);
2769 SrcOff += VTSize;
2770 DstOff += VTSize;
2771 }
2772
2773 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2774 &OutChains[0], OutChains.size());
2775}
2776
Dan Gohman21323f32008-05-29 19:42:22 +00002777static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2778 SDOperand Chain, SDOperand Dst,
2779 SDOperand Src, uint64_t Size,
2780 unsigned Align, bool AlwaysInline,
2781 const Value *DstSV, uint64_t DstSVOff,
2782 const Value *SrcSV, uint64_t SrcSVOff){
2783 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2784
2785 // Expand memmove to a series of load and store ops if the size operand falls
2786 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002788 uint64_t Limit = -1;
2789 if (!AlwaysInline)
2790 Limit = TLI.getMaxStoresPerMemmove();
2791 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002792 std::string Str;
2793 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002794 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002795 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002796 return SDOperand();
2797
Dan Gohman21323f32008-05-29 19:42:22 +00002798 uint64_t SrcOff = 0, DstOff = 0;
2799
2800 SmallVector<SDOperand, 8> LoadValues;
2801 SmallVector<SDOperand, 8> LoadChains;
2802 SmallVector<SDOperand, 8> OutChains;
2803 unsigned NumMemOps = MemOps.size();
2804 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002805 MVT VT = MemOps[i];
2806 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002807 SDOperand Value, Store;
2808
2809 Value = DAG.getLoad(VT, Chain,
2810 getMemBasePlusOffset(Src, SrcOff, DAG),
2811 SrcSV, SrcSVOff + SrcOff, false, Align);
2812 LoadValues.push_back(Value);
2813 LoadChains.push_back(Value.getValue(1));
2814 SrcOff += VTSize;
2815 }
2816 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2817 &LoadChains[0], LoadChains.size());
2818 OutChains.clear();
2819 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 MVT VT = MemOps[i];
2821 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002822 SDOperand Value, Store;
2823
2824 Store = DAG.getStore(Chain, LoadValues[i],
2825 getMemBasePlusOffset(Dst, DstOff, DAG),
2826 DstSV, DstSVOff + DstOff, false, DstAlign);
2827 OutChains.push_back(Store);
2828 DstOff += VTSize;
2829 }
2830
2831 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2832 &OutChains[0], OutChains.size());
2833}
2834
Dan Gohman707e0182008-04-12 04:36:06 +00002835static SDOperand getMemsetStores(SelectionDAG &DAG,
2836 SDOperand Chain, SDOperand Dst,
2837 SDOperand Src, uint64_t Size,
2838 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002839 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002840 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2841
2842 // Expand memset to a series of load/store ops if the size operand
2843 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002844 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002845 std::string Str;
2846 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002847 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002848 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002849 return SDOperand();
2850
2851 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002852 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002853
2854 unsigned NumMemOps = MemOps.size();
2855 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002856 MVT VT = MemOps[i];
2857 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002858 SDOperand Value = getMemsetValue(Src, VT, DAG);
2859 SDOperand Store = DAG.getStore(Chain, Value,
2860 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002861 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002862 OutChains.push_back(Store);
2863 DstOff += VTSize;
2864 }
2865
2866 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2867 &OutChains[0], OutChains.size());
2868}
2869
2870SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002871 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002872 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002873 const Value *DstSV, uint64_t DstSVOff,
2874 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002875
2876 // Check to see if we should lower the memcpy to loads and stores first.
2877 // For cases within the target-specified limits, this is the best choice.
2878 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2879 if (ConstantSize) {
2880 // Memcpy with size zero? Just return the original chain.
2881 if (ConstantSize->isNullValue())
2882 return Chain;
2883
2884 SDOperand Result =
2885 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002886 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002887 if (Result.Val)
2888 return Result;
2889 }
2890
2891 // Then check to see if we should lower the memcpy with target-specific
2892 // code. If the target chooses to do this, this is the next best.
2893 SDOperand Result =
2894 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2895 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002896 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002897 if (Result.Val)
2898 return Result;
2899
2900 // If we really need inline code and the target declined to provide it,
2901 // use a (potentially long) sequence of loads and stores.
2902 if (AlwaysInline) {
2903 assert(ConstantSize && "AlwaysInline requires a constant size!");
2904 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2905 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002906 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002907 }
2908
2909 // Emit a library call.
2910 TargetLowering::ArgListTy Args;
2911 TargetLowering::ArgListEntry Entry;
2912 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2913 Entry.Node = Dst; Args.push_back(Entry);
2914 Entry.Node = Src; Args.push_back(Entry);
2915 Entry.Node = Size; Args.push_back(Entry);
2916 std::pair<SDOperand,SDOperand> CallResult =
2917 TLI.LowerCallTo(Chain, Type::VoidTy,
2918 false, false, false, CallingConv::C, false,
2919 getExternalSymbol("memcpy", TLI.getPointerTy()),
2920 Args, *this);
2921 return CallResult.second;
2922}
2923
2924SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2925 SDOperand Src, SDOperand Size,
2926 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002927 const Value *DstSV, uint64_t DstSVOff,
2928 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002929
Dan Gohman21323f32008-05-29 19:42:22 +00002930 // Check to see if we should lower the memmove to loads and stores first.
2931 // For cases within the target-specified limits, this is the best choice.
2932 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2933 if (ConstantSize) {
2934 // Memmove with size zero? Just return the original chain.
2935 if (ConstantSize->isNullValue())
2936 return Chain;
2937
2938 SDOperand Result =
2939 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2940 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2941 if (Result.Val)
2942 return Result;
2943 }
Dan Gohman707e0182008-04-12 04:36:06 +00002944
2945 // Then check to see if we should lower the memmove with target-specific
2946 // code. If the target chooses to do this, this is the next best.
2947 SDOperand Result =
2948 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002949 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002950 if (Result.Val)
2951 return Result;
2952
2953 // Emit a library call.
2954 TargetLowering::ArgListTy Args;
2955 TargetLowering::ArgListEntry Entry;
2956 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2957 Entry.Node = Dst; Args.push_back(Entry);
2958 Entry.Node = Src; Args.push_back(Entry);
2959 Entry.Node = Size; Args.push_back(Entry);
2960 std::pair<SDOperand,SDOperand> CallResult =
2961 TLI.LowerCallTo(Chain, Type::VoidTy,
2962 false, false, false, CallingConv::C, false,
2963 getExternalSymbol("memmove", TLI.getPointerTy()),
2964 Args, *this);
2965 return CallResult.second;
2966}
2967
2968SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2969 SDOperand Src, SDOperand Size,
2970 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002971 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002972
2973 // Check to see if we should lower the memset to stores first.
2974 // For cases within the target-specified limits, this is the best choice.
2975 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2976 if (ConstantSize) {
2977 // Memset with size zero? Just return the original chain.
2978 if (ConstantSize->isNullValue())
2979 return Chain;
2980
2981 SDOperand Result =
2982 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002983 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002984 if (Result.Val)
2985 return Result;
2986 }
2987
2988 // Then check to see if we should lower the memset with target-specific
2989 // code. If the target chooses to do this, this is the next best.
2990 SDOperand Result =
2991 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002992 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002993 if (Result.Val)
2994 return Result;
2995
2996 // Emit a library call.
2997 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2998 TargetLowering::ArgListTy Args;
2999 TargetLowering::ArgListEntry Entry;
3000 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3001 Args.push_back(Entry);
3002 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003003 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003004 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3005 else
3006 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3007 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3008 Args.push_back(Entry);
3009 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3010 Args.push_back(Entry);
3011 std::pair<SDOperand,SDOperand> CallResult =
3012 TLI.LowerCallTo(Chain, Type::VoidTy,
3013 false, false, false, CallingConv::C, false,
3014 getExternalSymbol("memset", TLI.getPointerTy()),
3015 Args, *this);
3016 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003017}
3018
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003019SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003020 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003021 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003022 unsigned Alignment) {
3023 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003024 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3025 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003026 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003027 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003028 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003029 void* IP = 0;
3030 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3031 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003032 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003033 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003034 CSEMap.InsertNode(N, IP);
3035 AllNodes.push_back(N);
3036 return SDOperand(N, 0);
3037}
3038
3039SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003040 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003041 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003042 unsigned Alignment) {
3043 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003044 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3045 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003046 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003047 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3048 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003049 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003050 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003051 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003052 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003053 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003054 void* IP = 0;
3055 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3056 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003057 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003058 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003059 CSEMap.InsertNode(N, IP);
3060 AllNodes.push_back(N);
3061 return SDOperand(N, 0);
3062}
3063
Duncan Sands4bdcb612008-07-02 17:40:58 +00003064/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3065/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003066SDOperand SelectionDAG::getMergeValues(const SDOperand *Ops, unsigned NumOps,
Duncan Sands4bdcb612008-07-02 17:40:58 +00003067 bool Simplify) {
3068 if (Simplify && NumOps == 1)
3069 return Ops[0];
3070
3071 SmallVector<MVT, 4> VTs;
3072 VTs.reserve(NumOps);
3073 for (unsigned i = 0; i < NumOps; ++i)
3074 VTs.push_back(Ops[i].getValueType());
3075 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3076}
3077
Duncan Sands14ea39c2008-03-27 20:23:40 +00003078SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003079SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003080 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003081 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003082 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003083 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003084 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3085 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003086 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003088 } else if (SV) {
3089 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3090 assert(PT && "Value for load must be a pointer");
3091 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003092 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003093 assert(Ty && "Could not get type information for load");
3094 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3095 }
Evan Cheng466685d2006-10-09 20:57:25 +00003096
Duncan Sands14ea39c2008-03-27 20:23:40 +00003097 if (VT == EVT) {
3098 ExtType = ISD::NON_EXTLOAD;
3099 } else if (ExtType == ISD::NON_EXTLOAD) {
3100 assert(VT == EVT && "Non-extending load from different memory type!");
3101 } else {
3102 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 if (VT.isVector())
3104 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003106 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003107 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003108 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003109 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003111 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003112 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003113
3114 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003115 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003116 "Unindexed load with an offset!");
3117
3118 SDVTList VTs = Indexed ?
3119 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3120 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003121 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003122 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003123 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003124 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003125 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003126 ID.AddInteger(Alignment);
3127 ID.AddInteger(isVolatile);
3128 void *IP = 0;
3129 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3130 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003131 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3132 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003133 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003134 AllNodes.push_back(N);
3135 return SDOperand(N, 0);
3136}
3137
Duncan Sands83ec4b62008-06-06 12:08:01 +00003138SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003139 SDOperand Chain, SDOperand Ptr,
3140 const Value *SV, int SVOffset,
3141 bool isVolatile, unsigned Alignment) {
3142 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003143 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3144 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003145}
3146
Duncan Sands83ec4b62008-06-06 12:08:01 +00003147SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003148 SDOperand Chain, SDOperand Ptr,
3149 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003150 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003151 bool isVolatile, unsigned Alignment) {
3152 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003153 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3154 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003155}
3156
Evan Cheng144d8f02006-11-09 17:55:04 +00003157SDOperand
3158SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3159 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003160 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003161 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3162 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003163 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3164 LD->getChain(), Base, Offset, LD->getSrcValue(),
3165 LD->getSrcValueOffset(), LD->getMemoryVT(),
3166 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003167}
3168
Jeff Cohend41b30d2006-11-05 19:31:28 +00003169SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003170 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003171 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003172 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003173
Dan Gohman575e2f42007-06-04 15:49:41 +00003174 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3175 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003176 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003177 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003178 } else if (SV) {
3179 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3180 assert(PT && "Value for store must be a pointer");
3181 Ty = PT->getElementType();
3182 }
3183 assert(Ty && "Could not get type information for store");
3184 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3185 }
Evan Chengad071e12006-10-05 22:57:11 +00003186 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003187 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003188 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003189 FoldingSetNodeID ID;
3190 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003191 ID.AddInteger(ISD::UNINDEXED);
3192 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003193 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003194 ID.AddInteger(Alignment);
3195 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003196 void *IP = 0;
3197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3198 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003199 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003200 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003201 CSEMap.InsertNode(N, IP);
3202 AllNodes.push_back(N);
3203 return SDOperand(N, 0);
3204}
3205
Jeff Cohend41b30d2006-11-05 19:31:28 +00003206SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003207 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003208 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003209 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003211
3212 if (VT == SVT)
3213 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003214
Duncan Sands8e4eb092008-06-08 20:54:56 +00003215 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003216 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003217 "Can't do FP-INT conversion!");
3218
Dan Gohman575e2f42007-06-04 15:49:41 +00003219 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3220 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003221 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003222 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003223 } else if (SV) {
3224 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3225 assert(PT && "Value for store must be a pointer");
3226 Ty = PT->getElementType();
3227 }
3228 assert(Ty && "Could not get type information for store");
3229 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3230 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003231 SDVTList VTs = getVTList(MVT::Other);
3232 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003233 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003234 FoldingSetNodeID ID;
3235 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003236 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003237 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003238 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003239 ID.AddInteger(Alignment);
3240 ID.AddInteger(isVolatile);
3241 void *IP = 0;
3242 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3243 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003244 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003245 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003246 CSEMap.InsertNode(N, IP);
3247 AllNodes.push_back(N);
3248 return SDOperand(N, 0);
3249}
3250
Evan Cheng144d8f02006-11-09 17:55:04 +00003251SDOperand
3252SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3253 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003254 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3255 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3256 "Store is already a indexed store!");
3257 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3258 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3259 FoldingSetNodeID ID;
3260 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3261 ID.AddInteger(AM);
3262 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003263 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003264 ID.AddInteger(ST->getAlignment());
3265 ID.AddInteger(ST->isVolatile());
3266 void *IP = 0;
3267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3268 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003269 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003270 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003271 ST->getSrcValue(), ST->getSrcValueOffset(),
3272 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003273 CSEMap.InsertNode(N, IP);
3274 AllNodes.push_back(N);
3275 return SDOperand(N, 0);
3276}
3277
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003279 SDOperand Chain, SDOperand Ptr,
3280 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003281 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003282 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003283}
3284
Duncan Sands83ec4b62008-06-06 12:08:01 +00003285SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003286 const SDUse *Ops, unsigned NumOps) {
3287 switch (NumOps) {
3288 case 0: return getNode(Opcode, VT);
3289 case 1: return getNode(Opcode, VT, Ops[0].getSDOperand());
3290 case 2: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3291 Ops[1].getSDOperand());
3292 case 3: return getNode(Opcode, VT, Ops[0].getSDOperand(),
3293 Ops[1].getSDOperand(), Ops[2].getSDOperand());
3294 default: break;
3295 }
3296
3297 // Copy from an SDUse array into an SDOperand array for use with
3298 // the regular getNode logic.
3299 SmallVector<SDOperand, 8> NewOps;
3300 NewOps.reserve(NumOps);
3301 for (unsigned i = 0; i != NumOps; ++i)
3302 NewOps.push_back(Ops[i].getSDOperand());
3303 return getNode(Opcode, VT, Ops, NumOps);
3304}
3305
3306SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
3307 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003308 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003309 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003310 case 1: return getNode(Opcode, VT, Ops[0]);
3311 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3312 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003313 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003314 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003315
Chris Lattneref847df2005-04-09 03:27:28 +00003316 switch (Opcode) {
3317 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003318 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003319 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003320 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3321 "LHS and RHS of condition must have same type!");
3322 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3323 "True and False arms of SelectCC must have same type!");
3324 assert(Ops[2].getValueType() == VT &&
3325 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003326 break;
3327 }
3328 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003329 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003330 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3331 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003332 break;
3333 }
Chris Lattneref847df2005-04-09 03:27:28 +00003334 }
3335
Chris Lattner385328c2005-05-14 07:42:29 +00003336 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003337 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003338 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003339 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003340 FoldingSetNodeID ID;
3341 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003342 void *IP = 0;
3343 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3344 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003345 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003346 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003347 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003348 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003349 }
Chris Lattneref847df2005-04-09 03:27:28 +00003350 AllNodes.push_back(N);
3351 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003352}
3353
Chris Lattner89c34632005-05-14 06:20:26 +00003354SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003355 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003356 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003357 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3358 Ops, NumOps);
3359}
3360
3361SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003362 const MVT *VTs, unsigned NumVTs,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003363 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003364 if (NumVTs == 1)
3365 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003366 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3367}
3368
3369SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003370 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003371 if (VTList.NumVTs == 1)
3372 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003373
Chris Lattner5f056bf2005-07-10 01:55:33 +00003374 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003375 // FIXME: figure out how to safely handle things like
3376 // int foo(int x) { return 1 << (x & 255); }
3377 // int bar() { return foo(256); }
3378#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003379 case ISD::SRA_PARTS:
3380 case ISD::SRL_PARTS:
3381 case ISD::SHL_PARTS:
3382 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003383 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003384 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3385 else if (N3.getOpcode() == ISD::AND)
3386 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3387 // If the and is only masking out bits that cannot effect the shift,
3388 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003390 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3391 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3392 }
3393 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003394#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003395 }
Chris Lattner89c34632005-05-14 06:20:26 +00003396
Chris Lattner43247a12005-08-25 19:12:10 +00003397 // Memoize the node unless it returns a flag.
3398 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003399 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003400 FoldingSetNodeID ID;
3401 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003402 void *IP = 0;
3403 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3404 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003405 if (NumOps == 1)
3406 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3407 else if (NumOps == 2)
3408 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3409 else if (NumOps == 3)
3410 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3411 else
3412 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003413 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003414 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003415 if (NumOps == 1)
3416 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3417 else if (NumOps == 2)
3418 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3419 else if (NumOps == 3)
3420 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3421 else
3422 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003423 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003424 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003425 return SDOperand(N, 0);
3426}
3427
Dan Gohman08ce9762007-10-08 15:49:58 +00003428SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003429 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003430}
3431
3432SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3433 SDOperand N1) {
3434 SDOperand Ops[] = { N1 };
3435 return getNode(Opcode, VTList, Ops, 1);
3436}
3437
3438SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3439 SDOperand N1, SDOperand N2) {
3440 SDOperand Ops[] = { N1, N2 };
3441 return getNode(Opcode, VTList, Ops, 2);
3442}
3443
3444SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3445 SDOperand N1, SDOperand N2, SDOperand N3) {
3446 SDOperand Ops[] = { N1, N2, N3 };
3447 return getNode(Opcode, VTList, Ops, 3);
3448}
3449
3450SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3451 SDOperand N1, SDOperand N2, SDOperand N3,
3452 SDOperand N4) {
3453 SDOperand Ops[] = { N1, N2, N3, N4 };
3454 return getNode(Opcode, VTList, Ops, 4);
3455}
3456
3457SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3458 SDOperand N1, SDOperand N2, SDOperand N3,
3459 SDOperand N4, SDOperand N5) {
3460 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3461 return getNode(Opcode, VTList, Ops, 5);
3462}
3463
Duncan Sands83ec4b62008-06-06 12:08:01 +00003464SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003465 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003466}
3467
Duncan Sands83ec4b62008-06-06 12:08:01 +00003468SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3469 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003470 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003471 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003472 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003473 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003474 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003475 V.push_back(VT1);
3476 V.push_back(VT2);
3477 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003478 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003479}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3481 MVT VT3) {
3482 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() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3485 (*I)[2] == VT3)
3486 return makeVTList(&(*I)[0], 3);
3487 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003488 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003489 V.push_back(VT1);
3490 V.push_back(VT2);
3491 V.push_back(VT3);
3492 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003493 return makeVTList(&(*VTList.begin())[0], 3);
3494}
3495
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003497 switch (NumVTs) {
3498 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003499 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003500 case 2: return getVTList(VTs[0], VTs[1]);
3501 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3502 default: break;
3503 }
3504
Duncan Sands83ec4b62008-06-06 12:08:01 +00003505 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003506 E = VTList.end(); I != E; ++I) {
3507 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3508
3509 bool NoMatch = false;
3510 for (unsigned i = 2; i != NumVTs; ++i)
3511 if (VTs[i] != (*I)[i]) {
3512 NoMatch = true;
3513 break;
3514 }
3515 if (!NoMatch)
3516 return makeVTList(&*I->begin(), NumVTs);
3517 }
3518
Duncan Sands83ec4b62008-06-06 12:08:01 +00003519 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003520 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003521}
3522
3523
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003524/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3525/// specified operands. If the resultant node already exists in the DAG,
3526/// this does not modify the specified node, instead it returns the node that
3527/// already exists. If the resultant node does not exist in the DAG, the
3528/// input node is returned. As a degenerate case, if you specify the same
3529/// input operands as the node already has, the input node is returned.
3530SDOperand SelectionDAG::
3531UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3532 SDNode *N = InN.Val;
3533 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3534
3535 // Check to see if there is no change.
3536 if (Op == N->getOperand(0)) return InN;
3537
3538 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003539 void *InsertPos = 0;
3540 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3541 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003542
3543 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003544 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003545 RemoveNodeFromCSEMaps(N);
3546
3547 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003548 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003549 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003550 N->OperandList[0].setUser(N);
3551 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003552
3553 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003554 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003555 return InN;
3556}
3557
3558SDOperand SelectionDAG::
3559UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3560 SDNode *N = InN.Val;
3561 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3562
3563 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003564 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3565 return InN; // No operands changed, just return the input node.
3566
3567 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003568 void *InsertPos = 0;
3569 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3570 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003571
3572 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003573 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003574 RemoveNodeFromCSEMaps(N);
3575
3576 // Now we update the operands.
3577 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003578 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003579 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003580 N->OperandList[0].setUser(N);
3581 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003582 }
3583 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003584 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003585 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003586 N->OperandList[1].setUser(N);
3587 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003588 }
3589
3590 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003591 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003592 return InN;
3593}
3594
3595SDOperand SelectionDAG::
3596UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003597 SDOperand Ops[] = { Op1, Op2, Op3 };
3598 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003599}
3600
3601SDOperand SelectionDAG::
3602UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3603 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003604 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3605 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003606}
3607
3608SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003609UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3610 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003611 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3612 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003613}
3614
Chris Lattner809ec112006-01-28 10:09:25 +00003615SDOperand SelectionDAG::
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003616UpdateNodeOperands(SDOperand InN, const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003617 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003618 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003619 "Update with wrong number of operands");
3620
3621 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003622 bool AnyChange = false;
3623 for (unsigned i = 0; i != NumOps; ++i) {
3624 if (Ops[i] != N->getOperand(i)) {
3625 AnyChange = true;
3626 break;
3627 }
3628 }
3629
3630 // No operands changed, just return the input node.
3631 if (!AnyChange) return InN;
3632
3633 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003634 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003635 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003636 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003637
Dan Gohman7ceda162008-05-02 00:05:03 +00003638 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003639 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003640 RemoveNodeFromCSEMaps(N);
3641
3642 // Now we update the operands.
3643 for (unsigned i = 0; i != NumOps; ++i) {
3644 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003645 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003646 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003647 N->OperandList[i].setUser(N);
3648 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003649 }
3650 }
3651
3652 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003653 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003654 return InN;
3655}
3656
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003657/// MorphNodeTo - This frees the operands of the current node, resets the
3658/// opcode, types, and operands to the specified value. This should only be
3659/// used by the SelectionDAG class.
3660void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003661 const SDOperand *Ops, unsigned NumOps,
3662 SmallVectorImpl<SDNode *> &DeadNodes) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003663 NodeType = Opc;
3664 ValueList = L.VTs;
3665 NumValues = L.NumVTs;
3666
3667 // Clear the operands list, updating used nodes to remove this from their
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003668 // use list. Keep track of any operands that become dead as a result.
3669 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3670 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) {
3671 SDNode *N = I->getVal();
3672 N->removeUser(std::distance(op_begin(), I), this);
3673 if (N->use_empty())
3674 DeadNodeSet.insert(N);
3675 }
3676
Chris Lattner63e3f142007-02-04 07:28:00 +00003677 // If NumOps is larger than the # of operands we currently have, reallocate
3678 // the operand list.
3679 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003680 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003681 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003682 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003683 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003684 OperandsNeedDelete = true;
3685 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003686
3687 // Assign the new operands.
3688 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003689
3690 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3691 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003692 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003693 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003694 N->addUser(i, this);
3695 ++N->UsesSize;
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003696 DeadNodeSet.erase(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003697 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003698
3699 // Clean up any nodes that are still dead after adding the uses for the
3700 // new operands.
3701 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
3702 E = DeadNodeSet.end(); I != E; ++I)
3703 DeadNodes.push_back(*I);
3704}
3705
3706/// DropOperands - Release the operands and set this node to have
3707/// zero operands. This should only be used by HandleSDNode to clear
3708/// its operand list.
3709void SDNode::DropOperands() {
3710 assert(NodeType == ISD::HANDLENODE &&
3711 "DropOperands is for HANDLENODE only!");
3712
3713 // Unlike the code in MorphNodeTo that does this, we don't need to
3714 // watch for dead nodes here.
3715 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3716 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3717
3718 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003719}
Chris Lattner149c58c2005-08-16 18:17:10 +00003720
3721/// SelectNodeTo - These are used for target selectors to *mutate* the
3722/// specified node to have the specified return type, Target opcode, and
3723/// operands. Note that target opcodes are stored as
3724/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003725///
3726/// Note that SelectNodeTo returns the resultant node. If there is already a
3727/// node of the specified opcode and operands, it returns that node instead of
3728/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003729SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003730 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003731 SDVTList VTs = getVTList(VT);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003732 return SelectNodeTo(N, TargetOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003733}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003734
Evan Cheng95514ba2006-08-26 08:00:10 +00003735SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003736 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003737 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003738 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003739 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003740}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003741
Evan Cheng95514ba2006-08-26 08:00:10 +00003742SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003743 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003744 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003745 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003746 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003747 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003748}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003749
Evan Cheng95514ba2006-08-26 08:00:10 +00003750SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003751 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003752 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003753 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003754 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003755 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003756}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003757
Evan Cheng95514ba2006-08-26 08:00:10 +00003758SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003759 MVT VT, const SDOperand *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003760 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003761 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003762 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3763}
3764
3765SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003766 MVT VT1, MVT VT2, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003767 unsigned NumOps) {
3768 SDVTList VTs = getVTList(VT1, VT2);
3769 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3770}
3771
3772SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3773 MVT VT1, MVT VT2) {
3774 SDVTList VTs = getVTList(VT1, VT2);
3775 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3776}
3777
3778SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003779 MVT VT1, MVT VT2, MVT VT3,
3780 const SDOperand *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003781 SDVTList VTs = getVTList(VT1, VT2, VT3);
3782 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3783}
3784
3785SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3786 MVT VT1, MVT VT2,
3787 SDOperand Op1) {
3788 SDVTList VTs = getVTList(VT1, VT2);
3789 SDOperand Ops[] = { Op1 };
3790 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003791}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003792
Evan Cheng95514ba2006-08-26 08:00:10 +00003793SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003794 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003795 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003796 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003797 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003798 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003799}
3800
Evan Cheng95514ba2006-08-26 08:00:10 +00003801SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003802 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003803 SDOperand Op1, SDOperand Op2,
3804 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003805 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003806 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003807 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3808}
3809
3810SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003811 SDVTList VTs, const SDOperand *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003812 unsigned NumOps) {
3813 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003814 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003815 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003816 void *IP = 0;
3817 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003818 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003819
Chris Lattner0fb094f2005-11-19 01:44:53 +00003820 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003821
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003822 SmallVector<SDNode *, 16> DeadNodes;
3823 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps, DeadNodes);
3824 RemoveDeadNodes(DeadNodes);
3825
Chris Lattnera5682852006-08-07 23:03:03 +00003826 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003827 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003828}
3829
Chris Lattner0fb094f2005-11-19 01:44:53 +00003830
Evan Cheng6ae46c42006-02-09 07:15:23 +00003831/// getTargetNode - These are used for target selectors to create a new node
3832/// with specified return type(s), target opcode, and operands.
3833///
3834/// Note that getTargetNode returns the resultant node. If there is already a
3835/// node of the specified opcode and operands, it returns that node instead of
3836/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003837SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003838 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3839}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003840SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003841 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3842}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003843SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003844 SDOperand Op1, SDOperand Op2) {
3845 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3846}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003848 SDOperand Op1, SDOperand Op2,
3849 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003850 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3851}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003853 const SDOperand *Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003854 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003855}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003856SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3857 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003858 SDOperand Op;
3859 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3860}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3862 MVT VT2, SDOperand Op1) {
3863 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003864 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003865}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003866SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3867 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003868 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003869 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003870 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003871 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003872}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3874 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003875 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003877 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003878 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003879}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003880SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003881 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003883 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003884}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003885SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003886 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003887 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003888 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003889 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003890}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003892 SDOperand Op1, SDOperand Op2,
3893 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003895 SDOperand Ops[] = { Op1, Op2, Op3 };
3896 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3897}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003899 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003900 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003901 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003902}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3904 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003905 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003906 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003907 VTList.push_back(VT1);
3908 VTList.push_back(VT2);
3909 VTList.push_back(VT3);
3910 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003911 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003912 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3913}
Evan Cheng39305cf2007-10-05 01:10:49 +00003914SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003915 std::vector<MVT> &ResultTys,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003916 const SDOperand *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003917 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003918 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3919 Ops, NumOps).Val;
3920}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003921
Evan Cheng08b11732008-03-22 01:55:50 +00003922/// getNodeIfExists - Get the specified node if it's already available, or
3923/// else return NULL.
3924SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003925 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003926 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3927 FoldingSetNodeID ID;
3928 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3929 void *IP = 0;
3930 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3931 return E;
3932 }
3933 return NULL;
3934}
3935
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003936
Evan Cheng99157a02006-08-07 22:13:29 +00003937/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003938/// This can cause recursive merging of nodes in the DAG.
3939///
Chris Lattner11d049c2008-02-03 03:35:22 +00003940/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003941///
Chris Lattner11d049c2008-02-03 03:35:22 +00003942void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003943 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003944 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003945 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003946 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003947 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003948
Chris Lattner8b8749f2005-08-17 19:00:20 +00003949 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003950 SDNode::use_iterator UI = From->use_begin();
3951 SDNode *U = UI->getUser();
3952
Chris Lattner8b8749f2005-08-17 19:00:20 +00003953 // This node is about to morph, remove its old self from the CSE maps.
3954 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003955 int operandNum = 0;
3956 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3957 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003958 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003959 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003960 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003961 I->setUser(U);
3962 To.Val->addUser(operandNum, U);
3963 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003964
3965 // Now that we have modified U, add it back to the CSE maps. If it already
3966 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003967 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003968 ReplaceAllUsesWith(U, Existing, UpdateListener);
3969 // U is now dead. Inform the listener if it exists and delete it.
3970 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003971 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003972 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003973 } else {
3974 // If the node doesn't already exist, we updated it. Inform a listener if
3975 // it exists.
3976 if (UpdateListener)
3977 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003978 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003979 }
3980}
3981
3982/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3983/// This can cause recursive merging of nodes in the DAG.
3984///
3985/// This version assumes From/To have matching types and numbers of result
3986/// values.
3987///
Chris Lattner1e111c72005-09-07 05:37:01 +00003988void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003989 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003990 assert(From != To && "Cannot replace uses of with self");
3991 assert(From->getNumValues() == To->getNumValues() &&
3992 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003993 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003994 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3995 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003996
3997 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003998 SDNode::use_iterator UI = From->use_begin();
3999 SDNode *U = UI->getUser();
4000
Chris Lattner8b52f212005-08-26 18:36:28 +00004001 // This node is about to morph, remove its old self from the CSE maps.
4002 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004003 int operandNum = 0;
4004 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4005 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004006 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004007 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004008 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004009 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004010 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004011
Chris Lattner8b8749f2005-08-17 19:00:20 +00004012 // Now that we have modified U, add it back to the CSE maps. If it already
4013 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004014 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004015 ReplaceAllUsesWith(U, Existing, UpdateListener);
4016 // U is now dead. Inform the listener if it exists and delete it.
4017 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004018 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004019 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004020 } else {
4021 // If the node doesn't already exist, we updated it. Inform a listener if
4022 // it exists.
4023 if (UpdateListener)
4024 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004025 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004026 }
4027}
4028
Chris Lattner8b52f212005-08-26 18:36:28 +00004029/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4030/// This can cause recursive merging of nodes in the DAG.
4031///
4032/// This version can replace From with any result values. To must match the
4033/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004034void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004035 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004036 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004037 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004038 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004039
4040 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004041 SDNode::use_iterator UI = From->use_begin();
4042 SDNode *U = UI->getUser();
4043
Chris Lattner7b2880c2005-08-24 22:44:39 +00004044 // This node is about to morph, remove its old self from the CSE maps.
4045 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004046 int operandNum = 0;
4047 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4048 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004049 if (I->getVal() == From) {
4050 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004051 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004052 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004053 I->setUser(U);
4054 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004055 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004056
Chris Lattner7b2880c2005-08-24 22:44:39 +00004057 // Now that we have modified U, add it back to the CSE maps. If it already
4058 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004059 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004060 ReplaceAllUsesWith(U, Existing, UpdateListener);
4061 // U is now dead. Inform the listener if it exists and delete it.
4062 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004063 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004064 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004065 } else {
4066 // If the node doesn't already exist, we updated it. Inform a listener if
4067 // it exists.
4068 if (UpdateListener)
4069 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004070 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004071 }
4072}
4073
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004074namespace {
4075 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4076 /// any deleted nodes from the set passed into its constructor and recursively
4077 /// notifies another update listener if specified.
4078 class ChainedSetUpdaterListener :
4079 public SelectionDAG::DAGUpdateListener {
4080 SmallSetVector<SDNode*, 16> &Set;
4081 SelectionDAG::DAGUpdateListener *Chain;
4082 public:
4083 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4084 SelectionDAG::DAGUpdateListener *chain)
4085 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004086
Duncan Sandsedfcf592008-06-11 11:42:12 +00004087 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004088 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004089 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004090 }
4091 virtual void NodeUpdated(SDNode *N) {
4092 if (Chain) Chain->NodeUpdated(N);
4093 }
4094 };
4095}
4096
Chris Lattner012f2412006-02-17 21:58:01 +00004097/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4098/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004099/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004100void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004101 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004102 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004103
Chris Lattner012f2412006-02-17 21:58:01 +00004104 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004105 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004106 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004107 return;
4108 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004109
4110 if (From.use_empty()) return;
4111
Chris Lattnercf5640b2007-02-04 00:14:31 +00004112 // Get all of the users of From.Val. We want these in a nice,
4113 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004114 SmallSetVector<SDNode*, 16> Users;
4115 for (SDNode::use_iterator UI = From.Val->use_begin(),
4116 E = From.Val->use_end(); UI != E; ++UI) {
4117 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004118 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004119 }
Chris Lattner012f2412006-02-17 21:58:01 +00004120
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004121 // When one of the recursive merges deletes nodes from the graph, we need to
4122 // make sure that UpdateListener is notified *and* that the node is removed
4123 // from Users if present. CSUL does this.
4124 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004125
Chris Lattner012f2412006-02-17 21:58:01 +00004126 while (!Users.empty()) {
4127 // We know that this user uses some value of From. If it is the right
4128 // value, update it.
4129 SDNode *User = Users.back();
4130 Users.pop_back();
4131
Chris Lattner01d029b2007-10-15 06:10:22 +00004132 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004133 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004134 for (; Op != E; ++Op)
4135 if (*Op == From) break;
4136
4137 // If there are no matches, the user must use some other result of From.
4138 if (Op == E) continue;
4139
4140 // Okay, we know this user needs to be updated. Remove its old self
4141 // from the CSE maps.
4142 RemoveNodeFromCSEMaps(User);
4143
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004144 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004145 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004146 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004147 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004148 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004149 Op->setUser(User);
4150 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004151 }
4152 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004153
4154 // Now that we have modified User, add it back to the CSE maps. If it
4155 // already exists there, recursively merge the results together.
4156 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004157 if (!Existing) {
4158 if (UpdateListener) UpdateListener->NodeUpdated(User);
4159 continue; // Continue on to next user.
4160 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004161
4162 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004163 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004164 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004165 // can cause deletion of nodes that used the old value. To handle this, we
4166 // use CSUL to remove them from the Users set.
4167 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004168
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004169 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004170 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004171 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004172 }
4173}
4174
Evan Chenge6f35d82006-08-01 08:20:41 +00004175/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4176/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004177unsigned SelectionDAG::AssignNodeIds() {
4178 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004179 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4180 SDNode *N = I;
4181 N->setNodeId(Id++);
4182 }
4183 return Id;
4184}
4185
Evan Chenge6f35d82006-08-01 08:20:41 +00004186/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004187/// based on their topological order. It returns the maximum id and a vector
4188/// of the SDNodes* in assigned order by reference.
4189unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004190 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004191 std::vector<unsigned> InDegree(DAGSize);
4192 std::vector<SDNode*> Sources;
4193
4194 // Use a two pass approach to avoid using a std::map which is slow.
4195 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004196 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4197 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004198 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004199 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004200 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004201 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004202 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004203 }
4204
Evan Cheng99157a02006-08-07 22:13:29 +00004205 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004206 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004207 SDNode *N = Sources.back();
4208 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004209 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004210 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004211 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004212 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004213 if (Degree == 0)
4214 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004215 }
4216 }
4217
Evan Chengc384d6c2006-08-02 22:00:34 +00004218 // Second pass, assign the actual topological order as node ids.
4219 Id = 0;
4220 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4221 TI != TE; ++TI)
4222 (*TI)->setNodeId(Id++);
4223
4224 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004225}
4226
4227
Evan Cheng091cba12006-07-27 06:39:06 +00004228
Jim Laskey58b968b2005-08-17 20:08:02 +00004229//===----------------------------------------------------------------------===//
4230// SDNode Class
4231//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004232
Chris Lattner917d2c92006-07-19 00:00:37 +00004233// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004234void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004235void UnarySDNode::ANCHOR() {}
4236void BinarySDNode::ANCHOR() {}
4237void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004238void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004239void ConstantSDNode::ANCHOR() {}
4240void ConstantFPSDNode::ANCHOR() {}
4241void GlobalAddressSDNode::ANCHOR() {}
4242void FrameIndexSDNode::ANCHOR() {}
4243void JumpTableSDNode::ANCHOR() {}
4244void ConstantPoolSDNode::ANCHOR() {}
4245void BasicBlockSDNode::ANCHOR() {}
4246void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004247void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004248void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004249void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004250void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004251void ExternalSymbolSDNode::ANCHOR() {}
4252void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004253void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004254void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004255void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004256void LoadSDNode::ANCHOR() {}
4257void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004258void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004259
Chris Lattner48b85922007-02-04 02:41:42 +00004260HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004261 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004262}
4263
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004264GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004266 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004267 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004268 // Thread Local
4269 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4270 // Non Thread Local
4271 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4272 getSDVTList(VT)), Offset(o) {
4273 TheGlobal = const_cast<GlobalValue*>(GA);
4274}
Chris Lattner48b85922007-02-04 02:41:42 +00004275
Dan Gohman36b5c132008-04-07 19:35:22 +00004276/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004277/// reference performed by this atomic.
4278MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004279 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004280 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4281 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4282
4283 // Check if the atomic references a frame index
4284 const FrameIndexSDNode *FI =
4285 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4286 if (!getSrcValue() && FI)
4287 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4288 FI->getIndex(), Size, getAlignment());
4289 else
4290 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4291 Size, getAlignment());
4292}
4293
4294/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004295/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004296MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004297 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004298 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004299 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4300 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004301 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004302
4303 // Check if the load references a frame index, and does not have
4304 // an SV attached.
4305 const FrameIndexSDNode *FI =
4306 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4307 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004308 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004309 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004310 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004311 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004312 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004313}
4314
Jim Laskey583bd472006-10-27 23:46:08 +00004315/// Profile - Gather unique data for the node.
4316///
4317void SDNode::Profile(FoldingSetNodeID &ID) {
4318 AddNodeIDNode(ID, this);
4319}
4320
Chris Lattnera3255112005-11-08 23:30:28 +00004321/// getValueTypeList - Return a pointer to the specified value type.
4322///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004323const MVT *SDNode::getValueTypeList(MVT VT) {
4324 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004325 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004326 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004327 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004328 static MVT VTs[MVT::LAST_VALUETYPE];
4329 VTs[VT.getSimpleVT()] = VT;
4330 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004331 }
Chris Lattnera3255112005-11-08 23:30:28 +00004332}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004333
Chris Lattner5c884562005-01-12 18:37:47 +00004334/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4335/// indicated value. This method ignores uses of other values defined by this
4336/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004337bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004338 assert(Value < getNumValues() && "Bad value!");
4339
4340 // If there is only one value, this is easy.
4341 if (getNumValues() == 1)
4342 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004343 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004344
Evan Cheng4ee62112006-02-05 06:29:23 +00004345 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004346
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004347 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004348
Roman Levensteindc1adac2008-04-07 10:06:32 +00004349 // TODO: Only iterate over uses of a given value of the node
4350 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4351 if (*UI == TheValue) {
4352 if (NUses == 0)
4353 return false;
4354 --NUses;
4355 }
Chris Lattner5c884562005-01-12 18:37:47 +00004356 }
4357
4358 // Found exactly the right number of uses?
4359 return NUses == 0;
4360}
4361
4362
Evan Cheng33d55952007-08-02 05:29:38 +00004363/// hasAnyUseOfValue - Return true if there are any use of the indicated
4364/// value. This method ignores uses of other values defined by this operation.
4365bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4366 assert(Value < getNumValues() && "Bad value!");
4367
Dan Gohman30359592008-01-29 13:02:09 +00004368 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004369
4370 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4371
4372 SmallPtrSet<SDNode*, 32> UsersHandled;
4373
Roman Levensteindc1adac2008-04-07 10:06:32 +00004374 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4375 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004376 if (User->getNumOperands() == 1 ||
4377 UsersHandled.insert(User)) // First time we've seen this?
4378 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4379 if (User->getOperand(i) == TheValue) {
4380 return true;
4381 }
4382 }
4383
4384 return false;
4385}
4386
4387
Evan Cheng917be682008-03-04 00:41:45 +00004388/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004389///
Evan Cheng917be682008-03-04 00:41:45 +00004390bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004391 bool Seen = false;
4392 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004393 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004394 if (User == this)
4395 Seen = true;
4396 else
4397 return false;
4398 }
4399
4400 return Seen;
4401}
4402
Evan Chenge6e97e62006-11-03 07:31:32 +00004403/// isOperand - Return true if this node is an operand of N.
4404///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004405bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004406 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4407 if (*this == N->getOperand(i))
4408 return true;
4409 return false;
4410}
4411
Evan Cheng917be682008-03-04 00:41:45 +00004412bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004413 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004414 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004415 return true;
4416 return false;
4417}
Evan Cheng4ee62112006-02-05 06:29:23 +00004418
Chris Lattner572dee72008-01-16 05:49:24 +00004419/// reachesChainWithoutSideEffects - Return true if this operand (which must
4420/// be a chain) reaches the specified operand without crossing any
4421/// side-effecting instructions. In practice, this looks through token
4422/// factors and non-volatile loads. In order to remain efficient, this only
4423/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004424bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004425 unsigned Depth) const {
4426 if (*this == Dest) return true;
4427
4428 // Don't search too deeply, we just want to be able to see through
4429 // TokenFactor's etc.
4430 if (Depth == 0) return false;
4431
4432 // If this is a token factor, all inputs to the TF happen in parallel. If any
4433 // of the operands of the TF reach dest, then we can do the xform.
4434 if (getOpcode() == ISD::TokenFactor) {
4435 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4436 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4437 return true;
4438 return false;
4439 }
4440
4441 // Loads don't have side effects, look through them.
4442 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4443 if (!Ld->isVolatile())
4444 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4445 }
4446 return false;
4447}
4448
4449
Evan Chengc5fc57d2006-11-03 03:05:24 +00004450static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004451 SmallPtrSet<SDNode *, 32> &Visited) {
4452 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004453 return;
4454
4455 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4456 SDNode *Op = N->getOperand(i).Val;
4457 if (Op == P) {
4458 found = true;
4459 return;
4460 }
4461 findPredecessor(Op, P, found, Visited);
4462 }
4463}
4464
Evan Cheng917be682008-03-04 00:41:45 +00004465/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004466/// is either an operand of N or it can be reached by recursively traversing
4467/// up the operands.
4468/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004469bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004470 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004471 bool found = false;
4472 findPredecessor(N, this, found, Visited);
4473 return found;
4474}
4475
Evan Chengc5484282006-10-04 00:56:09 +00004476uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4477 assert(Num < NumOperands && "Invalid child # of SDNode!");
4478 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4479}
4480
Reid Spencer577cc322007-04-01 07:32:19 +00004481std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004482 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004483 default:
4484 if (getOpcode() < ISD::BUILTIN_OP_END)
4485 return "<<Unknown DAG Node>>";
4486 else {
Evan Cheng72261582005-12-20 06:22:03 +00004487 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004488 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004489 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004490 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004491
Evan Cheng72261582005-12-20 06:22:03 +00004492 TargetLowering &TLI = G->getTargetLoweringInfo();
4493 const char *Name =
4494 TLI.getTargetNodeName(getOpcode());
4495 if (Name) return Name;
4496 }
4497
4498 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004499 }
4500
Evan Cheng27b7db52008-03-08 00:58:38 +00004501 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004502 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004503 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4504 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4505 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004506 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4507 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4508 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004509 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004510 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4511 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4512 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4513 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4514 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004515 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004516 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004517 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004518 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004519 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004520 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004521 case ISD::AssertSext: return "AssertSext";
4522 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004523
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004524 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004525 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004526 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004527 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004528
4529 case ISD::Constant: return "Constant";
4530 case ISD::ConstantFP: return "ConstantFP";
4531 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004532 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004533 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004534 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004535 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004536 case ISD::RETURNADDR: return "RETURNADDR";
4537 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004538 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004539 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4540 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004541 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004542 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004543 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004544 case ISD::INTRINSIC_WO_CHAIN: {
4545 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4546 return Intrinsic::getName((Intrinsic::ID)IID);
4547 }
4548 case ISD::INTRINSIC_VOID:
4549 case ISD::INTRINSIC_W_CHAIN: {
4550 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004551 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004552 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004553
Chris Lattnerb2827b02006-03-19 00:52:58 +00004554 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004555 case ISD::TargetConstant: return "TargetConstant";
4556 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004557 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004558 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004559 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004560 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004561 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004562 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004563
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004564 case ISD::CopyToReg: return "CopyToReg";
4565 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004566 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004567 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004568 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004569 case ISD::DBG_LABEL: return "dbg_label";
4570 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004571 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004572 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004573 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004574 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004575
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004576 // Unary operators
4577 case ISD::FABS: return "fabs";
4578 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004579 case ISD::FSQRT: return "fsqrt";
4580 case ISD::FSIN: return "fsin";
4581 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004582 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004583 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004584
4585 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004586 case ISD::ADD: return "add";
4587 case ISD::SUB: return "sub";
4588 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004589 case ISD::MULHU: return "mulhu";
4590 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004591 case ISD::SDIV: return "sdiv";
4592 case ISD::UDIV: return "udiv";
4593 case ISD::SREM: return "srem";
4594 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004595 case ISD::SMUL_LOHI: return "smul_lohi";
4596 case ISD::UMUL_LOHI: return "umul_lohi";
4597 case ISD::SDIVREM: return "sdivrem";
4598 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004599 case ISD::AND: return "and";
4600 case ISD::OR: return "or";
4601 case ISD::XOR: return "xor";
4602 case ISD::SHL: return "shl";
4603 case ISD::SRA: return "sra";
4604 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004605 case ISD::ROTL: return "rotl";
4606 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004607 case ISD::FADD: return "fadd";
4608 case ISD::FSUB: return "fsub";
4609 case ISD::FMUL: return "fmul";
4610 case ISD::FDIV: return "fdiv";
4611 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004612 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004613 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004614
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004615 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004616 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004617 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004618 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004619 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004620 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004621 case ISD::CONCAT_VECTORS: return "concat_vectors";
4622 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004623 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004624 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004625 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004626 case ISD::ADDC: return "addc";
4627 case ISD::ADDE: return "adde";
4628 case ISD::SUBC: return "subc";
4629 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004630 case ISD::SHL_PARTS: return "shl_parts";
4631 case ISD::SRA_PARTS: return "sra_parts";
4632 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004633
4634 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4635 case ISD::INSERT_SUBREG: return "insert_subreg";
4636
Chris Lattner7f644642005-04-28 21:44:03 +00004637 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004638 case ISD::SIGN_EXTEND: return "sign_extend";
4639 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004640 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004641 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004642 case ISD::TRUNCATE: return "truncate";
4643 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004644 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004645 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004646 case ISD::FP_EXTEND: return "fp_extend";
4647
4648 case ISD::SINT_TO_FP: return "sint_to_fp";
4649 case ISD::UINT_TO_FP: return "uint_to_fp";
4650 case ISD::FP_TO_SINT: return "fp_to_sint";
4651 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004652 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004653
4654 // Control flow instructions
4655 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004656 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004657 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004658 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004659 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004660 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004661 case ISD::CALLSEQ_START: return "callseq_start";
4662 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004663
4664 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004665 case ISD::LOAD: return "load";
4666 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004667 case ISD::VAARG: return "vaarg";
4668 case ISD::VACOPY: return "vacopy";
4669 case ISD::VAEND: return "vaend";
4670 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004671 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004672 case ISD::EXTRACT_ELEMENT: return "extract_element";
4673 case ISD::BUILD_PAIR: return "build_pair";
4674 case ISD::STACKSAVE: return "stacksave";
4675 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004676 case ISD::TRAP: return "trap";
4677
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004678 // Bit manipulation
4679 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004680 case ISD::CTPOP: return "ctpop";
4681 case ISD::CTTZ: return "cttz";
4682 case ISD::CTLZ: return "ctlz";
4683
Chris Lattner36ce6912005-11-29 06:21:05 +00004684 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004685 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004686 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004687
Duncan Sands36397f52007-07-27 12:58:54 +00004688 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004689 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004690
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004691 case ISD::CONDCODE:
4692 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004693 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004694 case ISD::SETOEQ: return "setoeq";
4695 case ISD::SETOGT: return "setogt";
4696 case ISD::SETOGE: return "setoge";
4697 case ISD::SETOLT: return "setolt";
4698 case ISD::SETOLE: return "setole";
4699 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004700
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004701 case ISD::SETO: return "seto";
4702 case ISD::SETUO: return "setuo";
4703 case ISD::SETUEQ: return "setue";
4704 case ISD::SETUGT: return "setugt";
4705 case ISD::SETUGE: return "setuge";
4706 case ISD::SETULT: return "setult";
4707 case ISD::SETULE: return "setule";
4708 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004709
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004710 case ISD::SETEQ: return "seteq";
4711 case ISD::SETGT: return "setgt";
4712 case ISD::SETGE: return "setge";
4713 case ISD::SETLT: return "setlt";
4714 case ISD::SETLE: return "setle";
4715 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004716 }
4717 }
4718}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004719
Evan Cheng144d8f02006-11-09 17:55:04 +00004720const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004721 switch (AM) {
4722 default:
4723 return "";
4724 case ISD::PRE_INC:
4725 return "<pre-inc>";
4726 case ISD::PRE_DEC:
4727 return "<pre-dec>";
4728 case ISD::POST_INC:
4729 return "<post-inc>";
4730 case ISD::POST_DEC:
4731 return "<post-dec>";
4732 }
4733}
4734
Duncan Sands276dcbd2008-03-21 09:14:45 +00004735std::string ISD::ArgFlagsTy::getArgFlagsString() {
4736 std::string S = "< ";
4737
4738 if (isZExt())
4739 S += "zext ";
4740 if (isSExt())
4741 S += "sext ";
4742 if (isInReg())
4743 S += "inreg ";
4744 if (isSRet())
4745 S += "sret ";
4746 if (isByVal())
4747 S += "byval ";
4748 if (isNest())
4749 S += "nest ";
4750 if (getByValAlign())
4751 S += "byval-align:" + utostr(getByValAlign()) + " ";
4752 if (getOrigAlign())
4753 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4754 if (getByValSize())
4755 S += "byval-size:" + utostr(getByValSize()) + " ";
4756 return S + ">";
4757}
4758
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004759void SDNode::dump() const { dump(0); }
4760void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004761 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004762
4763 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004764 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004765 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004766 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004767 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004768 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004769 }
Bill Wendling832171c2006-12-07 20:04:42 +00004770 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004771
Bill Wendling832171c2006-12-07 20:04:42 +00004772 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004773 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004774 if (i) cerr << ", ";
4775 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004776 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004777 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004778 }
4779
Evan Chengce254432007-12-11 02:08:35 +00004780 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4781 SDNode *Mask = getOperand(2).Val;
4782 cerr << "<";
4783 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4784 if (i) cerr << ",";
4785 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4786 cerr << "u";
4787 else
4788 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4789 }
4790 cerr << ">";
4791 }
4792
Chris Lattnerc3aae252005-01-07 07:46:32 +00004793 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004794 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004795 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004796 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4797 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4798 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4799 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4800 else {
4801 cerr << "<APFloat(";
4802 CSDN->getValueAPF().convertToAPInt().dump();
4803 cerr << ")>";
4804 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004805 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004806 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004807 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004808 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004809 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004810 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004811 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004812 else
Bill Wendling832171c2006-12-07 20:04:42 +00004813 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004814 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004815 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004816 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004817 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004818 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004819 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004820 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004821 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004822 else
Bill Wendling832171c2006-12-07 20:04:42 +00004823 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004824 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004825 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004826 else
Bill Wendling832171c2006-12-07 20:04:42 +00004827 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004828 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004829 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004830 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4831 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004832 cerr << LBB->getName() << " ";
4833 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004834 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004835 if (G && R->getReg() &&
4836 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004837 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004838 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004839 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004840 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004841 } else if (const ExternalSymbolSDNode *ES =
4842 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004843 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004844 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4845 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004846 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004847 else
Dan Gohman69de1932008-02-06 22:27:42 +00004848 cerr << "<null>";
4849 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4850 if (M->MO.getValue())
4851 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4852 else
4853 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004854 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4855 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004856 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004857 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004858 }
4859 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004860 const Value *SrcValue = LD->getSrcValue();
4861 int SrcOffset = LD->getSrcValueOffset();
4862 cerr << " <";
4863 if (SrcValue)
4864 cerr << SrcValue;
4865 else
4866 cerr << "null";
4867 cerr << ":" << SrcOffset << ">";
4868
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004869 bool doExt = true;
4870 switch (LD->getExtensionType()) {
4871 default: doExt = false; break;
4872 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004873 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004874 break;
4875 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004876 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004877 break;
4878 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004879 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004880 break;
4881 }
4882 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004883 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004884
Evan Cheng144d8f02006-11-09 17:55:04 +00004885 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004886 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004887 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004888 if (LD->isVolatile())
4889 cerr << " <volatile>";
4890 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004891 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004892 const Value *SrcValue = ST->getSrcValue();
4893 int SrcOffset = ST->getSrcValueOffset();
4894 cerr << " <";
4895 if (SrcValue)
4896 cerr << SrcValue;
4897 else
4898 cerr << "null";
4899 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004900
4901 if (ST->isTruncatingStore())
4902 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004903 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004904
4905 const char *AM = getIndexedModeName(ST->getAddressingMode());
4906 if (*AM)
4907 cerr << " " << AM;
4908 if (ST->isVolatile())
4909 cerr << " <volatile>";
4910 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004911 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4912 const Value *SrcValue = AT->getSrcValue();
4913 int SrcOffset = AT->getSrcValueOffset();
4914 cerr << " <";
4915 if (SrcValue)
4916 cerr << SrcValue;
4917 else
4918 cerr << "null";
4919 cerr << ":" << SrcOffset << ">";
4920 if (AT->isVolatile())
4921 cerr << " <volatile>";
4922 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004923 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004924}
4925
Chris Lattnerde202b32005-11-09 23:47:37 +00004926static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004927 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4928 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004929 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004930 else
Bill Wendling832171c2006-12-07 20:04:42 +00004931 cerr << "\n" << std::string(indent+2, ' ')
4932 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004933
Chris Lattnerea946cd2005-01-09 20:38:33 +00004934
Bill Wendling832171c2006-12-07 20:04:42 +00004935 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004936 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004937}
4938
Chris Lattnerc3aae252005-01-07 07:46:32 +00004939void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004940 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004941 std::vector<const SDNode*> Nodes;
4942 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4943 I != E; ++I)
4944 Nodes.push_back(I);
4945
Chris Lattner49d24712005-01-09 20:26:36 +00004946 std::sort(Nodes.begin(), Nodes.end());
4947
4948 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004949 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004950 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004951 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004952
Jim Laskey26f7fa72006-10-17 19:33:52 +00004953 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004954
Bill Wendling832171c2006-12-07 20:04:42 +00004955 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004956}
4957
Evan Chengd6594ae2006-09-12 21:00:35 +00004958const Type *ConstantPoolSDNode::getType() const {
4959 if (isMachineConstantPoolEntry())
4960 return Val.MachineCPVal->getType();
4961 return Val.ConstVal->getType();
4962}