blob: 6456fa30e3f1a4ea78a94b78efc849312b0a4a5c [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
Evan Chengfc718542008-02-04 23:10:38 +0000195/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000196/// is 0).
197bool ISD::isDebugLabel(const SDNode *N) {
198 SDOperand Zero;
199 if (N->getOpcode() == ISD::LABEL)
200 Zero = N->getOperand(2);
201 else if (N->isTargetOpcode() &&
202 N->getTargetOpcode() == TargetInstrInfo::LABEL)
203 // Chain moved to last operand.
204 Zero = N->getOperand(1);
205 else
206 return false;
207 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
208}
209
Chris Lattnerc3aae252005-01-07 07:46:32 +0000210/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
211/// when given the operation for (X op Y).
212ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
213 // To perform this operation, we just need to swap the L and G bits of the
214 // operation.
215 unsigned OldL = (Operation >> 2) & 1;
216 unsigned OldG = (Operation >> 1) & 1;
217 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
218 (OldL << 1) | // New G bit
219 (OldG << 2)); // New L bit.
220}
221
222/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
223/// 'op' is a valid SetCC operation.
224ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
225 unsigned Operation = Op;
226 if (isInteger)
227 Operation ^= 7; // Flip L, G, E bits, but not U.
228 else
229 Operation ^= 15; // Flip all of the condition bits.
230 if (Operation > ISD::SETTRUE2)
231 Operation &= ~8; // Don't let N and U bits get set.
232 return ISD::CondCode(Operation);
233}
234
235
236/// isSignedOp - For an integer comparison, return 1 if the comparison is a
237/// signed operation and 2 if the result is an unsigned comparison. Return zero
238/// if the operation does not depend on the sign of the input (setne and seteq).
239static int isSignedOp(ISD::CondCode Opcode) {
240 switch (Opcode) {
241 default: assert(0 && "Illegal integer setcc operation!");
242 case ISD::SETEQ:
243 case ISD::SETNE: return 0;
244 case ISD::SETLT:
245 case ISD::SETLE:
246 case ISD::SETGT:
247 case ISD::SETGE: return 1;
248 case ISD::SETULT:
249 case ISD::SETULE:
250 case ISD::SETUGT:
251 case ISD::SETUGE: return 2;
252 }
253}
254
255/// getSetCCOrOperation - Return the result of a logical OR between different
256/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
257/// returns SETCC_INVALID if it is not possible to represent the resultant
258/// comparison.
259ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
260 bool isInteger) {
261 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
262 // Cannot fold a signed integer setcc with an unsigned integer setcc.
263 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000264
Chris Lattnerc3aae252005-01-07 07:46:32 +0000265 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267 // If the N and U bits get set then the resultant comparison DOES suddenly
268 // care about orderedness, and is true when ordered.
269 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000270 Op &= ~16; // Clear the U bit if the N bit is set.
271
272 // Canonicalize illegal integer setcc's.
273 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
274 Op = ISD::SETNE;
275
Chris Lattnerc3aae252005-01-07 07:46:32 +0000276 return ISD::CondCode(Op);
277}
278
279/// getSetCCAndOperation - Return the result of a logical AND between different
280/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
281/// function returns zero if it is not possible to represent the resultant
282/// comparison.
283ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
284 bool isInteger) {
285 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
286 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000287 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000288
289 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000290 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
291
292 // Canonicalize illegal integer setcc's.
293 if (isInteger) {
294 switch (Result) {
295 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000296 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000297 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000298 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
299 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
300 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000301 }
302 }
303
304 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000305}
306
Chris Lattnerb48da392005-01-23 04:39:44 +0000307const TargetMachine &SelectionDAG::getTarget() const {
308 return TLI.getTargetMachine();
309}
310
Jim Laskey58b968b2005-08-17 20:08:02 +0000311//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000312// SDNode Profile Support
313//===----------------------------------------------------------------------===//
314
Jim Laskeydef69b92006-10-27 23:52:51 +0000315/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
316///
Jim Laskey583bd472006-10-27 23:46:08 +0000317static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
318 ID.AddInteger(OpC);
319}
320
321/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
322/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000323static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000324 ID.AddPointer(VTList.VTs);
325}
326
Jim Laskeydef69b92006-10-27 23:52:51 +0000327/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
328///
Jim Laskey583bd472006-10-27 23:46:08 +0000329static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000330 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 for (; NumOps; --NumOps, ++Ops) {
332 ID.AddPointer(Ops->Val);
333 ID.AddInteger(Ops->ResNo);
334 }
Jim Laskey583bd472006-10-27 23:46:08 +0000335}
336
Jim Laskey583bd472006-10-27 23:46:08 +0000337static void AddNodeIDNode(FoldingSetNodeID &ID,
338 unsigned short OpC, SDVTList VTList,
Dan Gohman35b31be2008-04-17 23:02:12 +0000339 SDOperandPtr OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000340 AddNodeIDOpcode(ID, OpC);
341 AddNodeIDValueTypes(ID, VTList);
342 AddNodeIDOperands(ID, OpList, N);
343}
344
Roman Levenstein9cac5252008-04-16 16:15:27 +0000345
Jim Laskeydef69b92006-10-27 23:52:51 +0000346/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
347/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000348static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
354
355 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000358 case ISD::ARG_FLAGS:
359 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
360 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 case ISD::TargetConstant:
362 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000363 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 break;
365 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000366 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000367 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000369 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000371 case ISD::GlobalAddress:
372 case ISD::TargetGlobalTLSAddress:
373 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
375 ID.AddPointer(GA->getGlobal());
376 ID.AddInteger(GA->getOffset());
377 break;
378 }
379 case ISD::BasicBlock:
380 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
381 break;
382 case ISD::Register:
383 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
384 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000385 case ISD::SRCVALUE:
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
387 break;
388 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000389 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000390 ID.AddPointer(MO.getValue());
391 ID.AddInteger(MO.getFlags());
392 ID.AddInteger(MO.getOffset());
393 ID.AddInteger(MO.getSize());
394 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000395 break;
396 }
397 case ISD::FrameIndex:
398 case ISD::TargetFrameIndex:
399 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
400 break;
401 case ISD::JumpTable:
402 case ISD::TargetJumpTable:
403 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
404 break;
405 case ISD::ConstantPool:
406 case ISD::TargetConstantPool: {
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
408 ID.AddInteger(CP->getAlignment());
409 ID.AddInteger(CP->getOffset());
410 if (CP->isMachineConstantPoolEntry())
411 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
412 else
413 ID.AddPointer(CP->getConstVal());
414 break;
415 }
416 case ISD::LOAD: {
417 LoadSDNode *LD = cast<LoadSDNode>(N);
418 ID.AddInteger(LD->getAddressingMode());
419 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000420 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 ID.AddInteger(LD->getAlignment());
422 ID.AddInteger(LD->isVolatile());
423 break;
424 }
425 case ISD::STORE: {
426 StoreSDNode *ST = cast<StoreSDNode>(N);
427 ID.AddInteger(ST->getAddressingMode());
428 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000429 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 ID.AddInteger(ST->getAlignment());
431 ID.AddInteger(ST->isVolatile());
432 break;
433 }
Mon P Wang28873102008-06-25 08:15:39 +0000434 case ISD::ATOMIC_CMP_SWAP:
435 case ISD::ATOMIC_LOAD_ADD:
436 case ISD::ATOMIC_SWAP:
437 case ISD::ATOMIC_LOAD_SUB:
438 case ISD::ATOMIC_LOAD_AND:
439 case ISD::ATOMIC_LOAD_OR:
440 case ISD::ATOMIC_LOAD_XOR:
441 case ISD::ATOMIC_LOAD_NAND:
442 case ISD::ATOMIC_LOAD_MIN:
443 case ISD::ATOMIC_LOAD_MAX:
444 case ISD::ATOMIC_LOAD_UMIN:
445 case ISD::ATOMIC_LOAD_UMAX: {
446 AtomicSDNode *AT = cast<AtomicSDNode>(N);
447 ID.AddInteger(AT->getAlignment());
448 ID.AddInteger(AT->isVolatile());
449 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000450 }
Mon P Wang28873102008-06-25 08:15:39 +0000451 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000452}
453
454//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000455// SelectionDAG Class
456//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000457
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000458/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000459/// SelectionDAG.
460void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000461 // Create a dummy node (which is not added to allnodes), that adds a reference
462 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000463 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000464
Chris Lattner190a4182006-08-04 17:45:20 +0000465 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000466
Chris Lattner190a4182006-08-04 17:45:20 +0000467 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000468 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000469 if (I->use_empty())
470 DeadNodes.push_back(I);
471
472 // Process the worklist, deleting the nodes and adding their uses to the
473 // worklist.
474 while (!DeadNodes.empty()) {
475 SDNode *N = DeadNodes.back();
476 DeadNodes.pop_back();
477
478 // Take the node out of the appropriate CSE map.
479 RemoveNodeFromCSEMaps(N);
480
481 // Next, brutally remove the operand list. This is safe to do, as there are
482 // no cycles in the graph.
483 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000484 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000485 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000486
487 // Now that we removed this operand, see if there are no uses of it left.
488 if (Operand->use_empty())
489 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000490 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000491 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000492 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000493 }
Chris Lattner190a4182006-08-04 17:45:20 +0000494 N->OperandList = 0;
495 N->NumOperands = 0;
496
497 // Finally, remove N itself.
498 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000499 }
500
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000501 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000502 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000503}
504
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000505void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000506 SmallVector<SDNode*, 16> DeadNodes;
507 DeadNodes.push_back(N);
508
509 // Process the worklist, deleting the nodes and adding their uses to the
510 // worklist.
511 while (!DeadNodes.empty()) {
512 SDNode *N = DeadNodes.back();
513 DeadNodes.pop_back();
514
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000515 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +0000516 UpdateListener->NodeDeleted(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000517
Evan Cheng130a6472006-10-12 20:34:05 +0000518 // Take the node out of the appropriate CSE map.
519 RemoveNodeFromCSEMaps(N);
520
521 // Next, brutally remove the operand list. This is safe to do, as there are
522 // no cycles in the graph.
523 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000524 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000525 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000526
527 // Now that we removed this operand, see if there are no uses of it left.
528 if (Operand->use_empty())
529 DeadNodes.push_back(Operand);
530 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000531 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000532 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000533 }
Evan Cheng130a6472006-10-12 20:34:05 +0000534 N->OperandList = 0;
535 N->NumOperands = 0;
536
537 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000538 AllNodes.erase(N);
539 }
540}
541
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000542void SelectionDAG::DeleteNode(SDNode *N) {
543 assert(N->use_empty() && "Cannot delete a node that is not dead!");
544
545 // First take this out of the appropriate CSE map.
546 RemoveNodeFromCSEMaps(N);
547
Chris Lattner1e111c72005-09-07 05:37:01 +0000548 // Finally, remove uses due to operands of this node, remove from the
549 // AllNodes list, and delete the node.
550 DeleteNodeNotInCSEMaps(N);
551}
552
553void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
554
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000555 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000556 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000557
558 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000559 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000560 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000561 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000562 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000563 }
Chris Lattner65113b22005-11-08 22:07:03 +0000564 N->OperandList = 0;
565 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000566
567 delete N;
568}
569
Chris Lattner149c58c2005-08-16 18:17:10 +0000570/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
571/// correspond to it. This is useful when we're about to delete or repurpose
572/// the node. We don't want future request for structurally identical nodes
573/// to return N anymore.
574void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000575 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000576 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000577 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000578 case ISD::STRING:
579 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
580 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000581 case ISD::CONDCODE:
582 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
583 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000584 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000585 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
586 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000587 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000588 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000589 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000590 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000591 Erased =
592 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000593 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000594 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000595 MVT VT = cast<VTSDNode>(N)->getVT();
596 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000597 Erased = ExtendedValueTypeNodes.erase(VT);
598 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000599 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
600 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000601 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000602 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000603 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000604 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000605 // Remove it from the CSE Map.
606 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000607 break;
608 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000609#ifndef NDEBUG
610 // Verify that the node was actually in one of the CSE maps, unless it has a
611 // flag result (which cannot be CSE'd) or is one of the special cases that are
612 // not subject to CSE.
613 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000614 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000615 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000616 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000617 assert(0 && "Node is not in map!");
618 }
619#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000620}
621
Chris Lattner8b8749f2005-08-17 19:00:20 +0000622/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
623/// has been taken out and modified in some way. If the specified node already
624/// exists in the CSE maps, do not modify the maps, but return the existing node
625/// instead. If it doesn't exist, add it and return null.
626///
627SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
628 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000629 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000630 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000631
Chris Lattner9f8cc692005-12-19 22:21:21 +0000632 // Check that remaining values produced are not flags.
633 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
634 if (N->getValueType(i) == MVT::Flag)
635 return 0; // Never CSE anything that produces a flag.
636
Chris Lattnera5682852006-08-07 23:03:03 +0000637 SDNode *New = CSEMap.GetOrInsertNode(N);
638 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000639 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000640}
641
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000642/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
643/// were replaced with those specified. If this node is never memoized,
644/// return null, otherwise return a pointer to the slot it would take. If a
645/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000646SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
647 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000648 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649 return 0; // Never add these nodes.
650
651 // Check that remaining values produced are not flags.
652 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
653 if (N->getValueType(i) == MVT::Flag)
654 return 0; // Never CSE anything that produces a flag.
655
Chris Lattner63e3f142007-02-04 07:28:00 +0000656 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000657 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000658 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000659 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000660}
661
662/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
663/// were replaced with those specified. If this node is never memoized,
664/// return null, otherwise return a pointer to the slot it would take. If a
665/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000666SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
667 SDOperand Op1, SDOperand Op2,
668 void *&InsertPos) {
669 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
670 return 0; // Never add these nodes.
671
672 // Check that remaining values produced are not flags.
673 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
674 if (N->getValueType(i) == MVT::Flag)
675 return 0; // Never CSE anything that produces a flag.
676
Chris Lattner63e3f142007-02-04 07:28:00 +0000677 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000678 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000679 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000680 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
681}
682
683
684/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
685/// were replaced with those specified. If this node is never memoized,
686/// return null, otherwise return a pointer to the slot it would take. If a
687/// node already exists with these operands, the slot will be non-null.
688SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000689 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000690 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000691 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000692 return 0; // Never add these nodes.
693
694 // Check that remaining values produced are not flags.
695 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
696 if (N->getValueType(i) == MVT::Flag)
697 return 0; // Never CSE anything that produces a flag.
698
Jim Laskey583bd472006-10-27 23:46:08 +0000699 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000700 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000701
Evan Cheng9629aba2006-10-11 01:47:58 +0000702 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
703 ID.AddInteger(LD->getAddressingMode());
704 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000705 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000706 ID.AddInteger(LD->getAlignment());
707 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000708 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
709 ID.AddInteger(ST->getAddressingMode());
710 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000711 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000712 ID.AddInteger(ST->getAlignment());
713 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000714 }
Jim Laskey583bd472006-10-27 23:46:08 +0000715
Chris Lattnera5682852006-08-07 23:03:03 +0000716 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000717}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000718
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000719
Chris Lattner78ec3112003-08-11 14:57:33 +0000720SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000721 while (!AllNodes.empty()) {
722 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000723 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000724 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000725 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000726 }
Chris Lattner65113b22005-11-08 22:07:03 +0000727 N->OperandList = 0;
728 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000729 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000730 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000731}
732
Duncan Sands83ec4b62008-06-06 12:08:01 +0000733SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000734 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000735 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000736 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000737 return getNode(ISD::AND, Op.getValueType(), Op,
738 getConstant(Imm, Op.getValueType()));
739}
740
Chris Lattner36ce6912005-11-29 06:21:05 +0000741SDOperand SelectionDAG::getString(const std::string &Val) {
742 StringSDNode *&N = StringNodes[Val];
743 if (!N) {
744 N = new StringSDNode(Val);
745 AllNodes.push_back(N);
746 }
747 return SDOperand(N, 0);
748}
749
Duncan Sands83ec4b62008-06-06 12:08:01 +0000750SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000751 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000752 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000753}
754
Duncan Sands83ec4b62008-06-06 12:08:01 +0000755SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
756 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000757
Evan Cheng0ff39b32008-06-30 07:31:25 +0000758 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000759 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000760 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000761
762 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000763 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000764 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000765 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000766 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000767 SDNode *N = NULL;
768 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000769 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000770 return SDOperand(N, 0);
771 if (!N) {
772 N = new ConstantSDNode(isT, Val, EltVT);
773 CSEMap.InsertNode(N, IP);
774 AllNodes.push_back(N);
775 }
776
777 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000779 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000780 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000781 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
782 }
783 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000784}
785
Chris Lattner0bd48932008-01-17 07:00:52 +0000786SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
787 return getConstant(Val, TLI.getPointerTy(), isTarget);
788}
789
790
Duncan Sands83ec4b62008-06-06 12:08:01 +0000791SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
792 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000793
Duncan Sands83ec4b62008-06-06 12:08:01 +0000794 MVT EltVT =
795 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000796
Chris Lattnerd8658612005-02-17 20:17:32 +0000797 // Do the map lookup using the actual bit pattern for the floating point
798 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
799 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000800 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000801 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000802 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000803 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000804 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000805 SDNode *N = NULL;
806 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000807 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000808 return SDOperand(N, 0);
809 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000810 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000811 CSEMap.InsertNode(N, IP);
812 AllNodes.push_back(N);
813 }
814
Dan Gohman7f321562007-06-25 16:23:39 +0000815 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000817 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000818 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000819 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
820 }
821 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822}
823
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
825 MVT EltVT =
826 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000827 if (EltVT==MVT::f32)
828 return getConstantFP(APFloat((float)Val), VT, isTarget);
829 else
830 return getConstantFP(APFloat(Val), VT, isTarget);
831}
832
Chris Lattnerc3aae252005-01-07 07:46:32 +0000833SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000834 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000835 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000836 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000837
838 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
839 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000840 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000841 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
842 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
843 }
844
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000845 if (GVar && GVar->isThreadLocal())
846 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
847 else
848 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000849
Jim Laskey583bd472006-10-27 23:46:08 +0000850 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000851 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000852 ID.AddPointer(GV);
853 ID.AddInteger(Offset);
854 void *IP = 0;
855 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
856 return SDOperand(E, 0);
857 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
858 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859 AllNodes.push_back(N);
860 return SDOperand(N, 0);
861}
862
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000864 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000865 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000866 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000867 ID.AddInteger(FI);
868 void *IP = 0;
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
872 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000878 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000879 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000880 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000881 ID.AddInteger(JTI);
882 void *IP = 0;
883 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
884 return SDOperand(E, 0);
885 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
886 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000887 AllNodes.push_back(N);
888 return SDOperand(N, 0);
889}
890
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000892 unsigned Alignment, int Offset,
893 bool isTarget) {
894 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000896 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000897 ID.AddInteger(Alignment);
898 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000899 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000900 void *IP = 0;
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
903 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
904 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
907}
908
Chris Lattnerc3aae252005-01-07 07:46:32 +0000909
Duncan Sands83ec4b62008-06-06 12:08:01 +0000910SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000911 unsigned Alignment, int Offset,
912 bool isTarget) {
913 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000914 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000915 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000916 ID.AddInteger(Alignment);
917 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000918 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000919 void *IP = 0;
920 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
921 return SDOperand(E, 0);
922 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
923 CSEMap.InsertNode(N, IP);
924 AllNodes.push_back(N);
925 return SDOperand(N, 0);
926}
927
928
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000930 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000931 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000932 ID.AddPointer(MBB);
933 void *IP = 0;
934 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
935 return SDOperand(E, 0);
936 SDNode *N = new BasicBlockSDNode(MBB);
937 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 AllNodes.push_back(N);
939 return SDOperand(N, 0);
940}
941
Duncan Sands276dcbd2008-03-21 09:14:45 +0000942SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
943 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000944 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000945 ID.AddInteger(Flags.getRawBits());
946 void *IP = 0;
947 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
948 return SDOperand(E, 0);
949 SDNode *N = new ARG_FLAGSSDNode(Flags);
950 CSEMap.InsertNode(N, IP);
951 AllNodes.push_back(N);
952 return SDOperand(N, 0);
953}
954
Duncan Sands83ec4b62008-06-06 12:08:01 +0000955SDOperand SelectionDAG::getValueType(MVT VT) {
956 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
957 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000958
Duncan Sands83ec4b62008-06-06 12:08:01 +0000959 SDNode *&N = VT.isExtended() ?
960 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000961
962 if (N) return SDOperand(N, 0);
963 N = new VTSDNode(VT);
964 AllNodes.push_back(N);
965 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000966}
967
Duncan Sands83ec4b62008-06-06 12:08:01 +0000968SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969 SDNode *&N = ExternalSymbols[Sym];
970 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000971 N = new ExternalSymbolSDNode(false, Sym, VT);
972 AllNodes.push_back(N);
973 return SDOperand(N, 0);
974}
975
Duncan Sands83ec4b62008-06-06 12:08:01 +0000976SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000977 SDNode *&N = TargetExternalSymbols[Sym];
978 if (N) return SDOperand(N, 0);
979 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980 AllNodes.push_back(N);
981 return SDOperand(N, 0);
982}
983
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000984SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
985 if ((unsigned)Cond >= CondCodeNodes.size())
986 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000987
Chris Lattner079a27a2005-08-09 20:40:02 +0000988 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000989 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000990 AllNodes.push_back(CondCodeNodes[Cond]);
991 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000992 return SDOperand(CondCodeNodes[Cond], 0);
993}
994
Duncan Sands83ec4b62008-06-06 12:08:01 +0000995SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000996 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000997 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000998 ID.AddInteger(RegNo);
999 void *IP = 0;
1000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1001 return SDOperand(E, 0);
1002 SDNode *N = new RegisterSDNode(RegNo, VT);
1003 CSEMap.InsertNode(N, IP);
1004 AllNodes.push_back(N);
1005 return SDOperand(N, 0);
1006}
1007
Dan Gohman69de1932008-02-06 22:27:42 +00001008SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001009 assert((!V || isa<PointerType>(V->getType())) &&
1010 "SrcValue is not a pointer?");
1011
Jim Laskey583bd472006-10-27 23:46:08 +00001012 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001013 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001014 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001015
Chris Lattner61b09412006-08-11 21:01:22 +00001016 void *IP = 0;
1017 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1018 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001019
1020 SDNode *N = new SrcValueSDNode(V);
1021 CSEMap.InsertNode(N, IP);
1022 AllNodes.push_back(N);
1023 return SDOperand(N, 0);
1024}
1025
Dan Gohman36b5c132008-04-07 19:35:22 +00001026SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001027 const Value *v = MO.getValue();
1028 assert((!v || isa<PointerType>(v->getType())) &&
1029 "SrcValue is not a pointer?");
1030
1031 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001032 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001033 ID.AddPointer(v);
1034 ID.AddInteger(MO.getFlags());
1035 ID.AddInteger(MO.getOffset());
1036 ID.AddInteger(MO.getSize());
1037 ID.AddInteger(MO.getAlignment());
1038
1039 void *IP = 0;
1040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1041 return SDOperand(E, 0);
1042
1043 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001044 CSEMap.InsertNode(N, IP);
1045 AllNodes.push_back(N);
1046 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001047}
1048
Chris Lattner37ce9df2007-10-15 17:47:20 +00001049/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1050/// specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001051SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001052 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 unsigned ByteSize = VT.getSizeInBits()/8;
1054 const Type *Ty = VT.getTypeForMVT();
Chris Lattner37ce9df2007-10-15 17:47:20 +00001055 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1056 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1057 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1058}
1059
1060
Duncan Sands83ec4b62008-06-06 12:08:01 +00001061SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001062 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063 // These setcc operations always fold.
1064 switch (Cond) {
1065 default: break;
1066 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001067 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001069 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001070
1071 case ISD::SETOEQ:
1072 case ISD::SETOGT:
1073 case ISD::SETOGE:
1074 case ISD::SETOLT:
1075 case ISD::SETOLE:
1076 case ISD::SETONE:
1077 case ISD::SETO:
1078 case ISD::SETUO:
1079 case ISD::SETUEQ:
1080 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001082 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001084
Chris Lattner67255a12005-04-07 18:14:58 +00001085 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001086 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001087 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001088 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001089
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090 switch (Cond) {
1091 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001092 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1093 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001094 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1095 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1096 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1097 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1098 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1099 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1100 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1101 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 }
Chris Lattner67255a12005-04-07 18:14:58 +00001104 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001105 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001107 // No compile time operations on this type yet.
1108 if (N1C->getValueType(0) == MVT::ppcf128)
1109 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001110
1111 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001113 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001114 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1115 return getNode(ISD::UNDEF, VT);
1116 // fall through
1117 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1118 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1119 return getNode(ISD::UNDEF, VT);
1120 // fall through
1121 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001122 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001123 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1124 return getNode(ISD::UNDEF, VT);
1125 // fall through
1126 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1127 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1128 return getNode(ISD::UNDEF, VT);
1129 // fall through
1130 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1131 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1132 return getNode(ISD::UNDEF, VT);
1133 // fall through
1134 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001135 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001136 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1137 return getNode(ISD::UNDEF, VT);
1138 // fall through
1139 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001140 R==APFloat::cmpEqual, VT);
1141 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1142 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1143 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1144 R==APFloat::cmpEqual, VT);
1145 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1146 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1147 R==APFloat::cmpLessThan, VT);
1148 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1149 R==APFloat::cmpUnordered, VT);
1150 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1151 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152 }
1153 } else {
1154 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001155 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001156 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001157 }
1158
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001159 // Could not fold it.
1160 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001161}
1162
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001163/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1164/// use this predicate to simplify operations downstream.
1165bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1166 unsigned BitWidth = Op.getValueSizeInBits();
1167 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1168}
1169
Dan Gohmanea859be2007-06-22 14:59:07 +00001170/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1171/// this predicate to simplify operations downstream. Mask is known to be zero
1172/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001173bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001174 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001175 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001176 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1177 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1178 return (KnownZero & Mask) == Mask;
1179}
1180
1181/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1182/// known to be either zero or one and return them in the KnownZero/KnownOne
1183/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1184/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001185void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001186 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001187 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001188 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001189 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001190 "Mask size mismatches value type size!");
1191
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001192 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001193 if (Depth == 6 || Mask == 0)
1194 return; // Limit search depth.
1195
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001196 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001197
1198 switch (Op.getOpcode()) {
1199 case ISD::Constant:
1200 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001201 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001202 KnownZero = ~KnownOne & Mask;
1203 return;
1204 case ISD::AND:
1205 // If either the LHS or the RHS are Zero, the result is zero.
1206 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001207 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1208 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001209 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1210 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1211
1212 // Output known-1 bits are only known if set in both the LHS & RHS.
1213 KnownOne &= KnownOne2;
1214 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1215 KnownZero |= KnownZero2;
1216 return;
1217 case ISD::OR:
1218 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001219 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1220 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001221 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1222 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1223
1224 // Output known-0 bits are only known if clear in both the LHS & RHS.
1225 KnownZero &= KnownZero2;
1226 // Output known-1 are known to be set if set in either the LHS | RHS.
1227 KnownOne |= KnownOne2;
1228 return;
1229 case ISD::XOR: {
1230 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1231 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1232 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1233 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1234
1235 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001236 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001237 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1238 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1239 KnownZero = KnownZeroOut;
1240 return;
1241 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001242 case ISD::MUL: {
1243 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1244 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1245 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1246 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1247 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1248
1249 // If low bits are zero in either operand, output low known-0 bits.
1250 // Also compute a conserative estimate for high known-0 bits.
1251 // More trickiness is possible, but this is sufficient for the
1252 // interesting case of alignment computation.
1253 KnownOne.clear();
1254 unsigned TrailZ = KnownZero.countTrailingOnes() +
1255 KnownZero2.countTrailingOnes();
1256 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001257 KnownZero2.countLeadingOnes(),
1258 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001259
1260 TrailZ = std::min(TrailZ, BitWidth);
1261 LeadZ = std::min(LeadZ, BitWidth);
1262 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1263 APInt::getHighBitsSet(BitWidth, LeadZ);
1264 KnownZero &= Mask;
1265 return;
1266 }
1267 case ISD::UDIV: {
1268 // For the purposes of computing leading zeros we can conservatively
1269 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001270 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001271 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1272 ComputeMaskedBits(Op.getOperand(0),
1273 AllOnes, KnownZero2, KnownOne2, Depth+1);
1274 unsigned LeadZ = KnownZero2.countLeadingOnes();
1275
1276 KnownOne2.clear();
1277 KnownZero2.clear();
1278 ComputeMaskedBits(Op.getOperand(1),
1279 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001280 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1281 if (RHSUnknownLeadingOnes != BitWidth)
1282 LeadZ = std::min(BitWidth,
1283 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001284
1285 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1286 return;
1287 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001288 case ISD::SELECT:
1289 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1290 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1292 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1293
1294 // Only known if known in both the LHS and RHS.
1295 KnownOne &= KnownOne2;
1296 KnownZero &= KnownZero2;
1297 return;
1298 case ISD::SELECT_CC:
1299 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1300 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1301 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1302 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1303
1304 // Only known if known in both the LHS and RHS.
1305 KnownOne &= KnownOne2;
1306 KnownZero &= KnownZero2;
1307 return;
1308 case ISD::SETCC:
1309 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001310 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1311 BitWidth > 1)
1312 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001313 return;
1314 case ISD::SHL:
1315 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1316 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001317 unsigned ShAmt = SA->getValue();
1318
1319 // If the shift count is an invalid immediate, don't do anything.
1320 if (ShAmt >= BitWidth)
1321 return;
1322
1323 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001324 KnownZero, KnownOne, Depth+1);
1325 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001326 KnownZero <<= ShAmt;
1327 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001328 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001329 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001330 }
1331 return;
1332 case ISD::SRL:
1333 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1334 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001335 unsigned ShAmt = SA->getValue();
1336
Dan Gohmand4cf9922008-02-26 18:50:50 +00001337 // If the shift count is an invalid immediate, don't do anything.
1338 if (ShAmt >= BitWidth)
1339 return;
1340
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001341 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001342 KnownZero, KnownOne, Depth+1);
1343 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001344 KnownZero = KnownZero.lshr(ShAmt);
1345 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001346
Dan Gohman72d2fd52008-02-13 22:43:25 +00001347 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001348 KnownZero |= HighBits; // High bits known zero.
1349 }
1350 return;
1351 case ISD::SRA:
1352 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001353 unsigned ShAmt = SA->getValue();
1354
Dan Gohmand4cf9922008-02-26 18:50:50 +00001355 // If the shift count is an invalid immediate, don't do anything.
1356 if (ShAmt >= BitWidth)
1357 return;
1358
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001359 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001360 // If any of the demanded bits are produced by the sign extension, we also
1361 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001362 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1363 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001364 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001365
1366 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1367 Depth+1);
1368 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001369 KnownZero = KnownZero.lshr(ShAmt);
1370 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001371
1372 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001373 APInt SignBit = APInt::getSignBit(BitWidth);
1374 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001375
Dan Gohmanca93a432008-02-20 16:30:17 +00001376 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001377 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001378 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001379 KnownOne |= HighBits; // New bits are known one.
1380 }
1381 }
1382 return;
1383 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001384 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1385 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001386
1387 // Sign extension. Compute the demanded bits in the result that are not
1388 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001389 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001390
Dan Gohman977a76f2008-02-13 22:28:48 +00001391 APInt InSignBit = APInt::getSignBit(EBits);
1392 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001393
1394 // If the sign extended bits are demanded, we know that the sign
1395 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001396 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001397 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001398 InputDemandedBits |= InSignBit;
1399
1400 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1401 KnownZero, KnownOne, Depth+1);
1402 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1403
1404 // If the sign bit of the input is known set or clear, then we know the
1405 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001406 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001407 KnownZero |= NewBits;
1408 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001409 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001410 KnownOne |= NewBits;
1411 KnownZero &= ~NewBits;
1412 } else { // Input sign bit unknown
1413 KnownZero &= ~NewBits;
1414 KnownOne &= ~NewBits;
1415 }
1416 return;
1417 }
1418 case ISD::CTTZ:
1419 case ISD::CTLZ:
1420 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001421 unsigned LowBits = Log2_32(BitWidth)+1;
1422 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001423 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001424 return;
1425 }
1426 case ISD::LOAD: {
1427 if (ISD::isZEXTLoad(Op.Val)) {
1428 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001429 MVT VT = LD->getMemoryVT();
1430 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001431 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 }
1433 return;
1434 }
1435 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001436 MVT InVT = Op.getOperand(0).getValueType();
1437 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001438 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1439 APInt InMask = Mask;
1440 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001441 KnownZero.trunc(InBits);
1442 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001443 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001444 KnownZero.zext(BitWidth);
1445 KnownOne.zext(BitWidth);
1446 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001447 return;
1448 }
1449 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001450 MVT InVT = Op.getOperand(0).getValueType();
1451 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001453 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1454 APInt InMask = Mask;
1455 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001456
1457 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001458 // bit is demanded. Temporarily set this bit in the mask for our callee.
1459 if (NewBits.getBoolValue())
1460 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001461
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001462 KnownZero.trunc(InBits);
1463 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001464 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1465
1466 // Note if the sign bit is known to be zero or one.
1467 bool SignBitKnownZero = KnownZero.isNegative();
1468 bool SignBitKnownOne = KnownOne.isNegative();
1469 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1470 "Sign bit can't be known to be both zero and one!");
1471
1472 // If the sign bit wasn't actually demanded by our caller, we don't
1473 // want it set in the KnownZero and KnownOne result values. Reset the
1474 // mask and reapply it to the result values.
1475 InMask = Mask;
1476 InMask.trunc(InBits);
1477 KnownZero &= InMask;
1478 KnownOne &= InMask;
1479
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001480 KnownZero.zext(BitWidth);
1481 KnownOne.zext(BitWidth);
1482
Dan Gohman977a76f2008-02-13 22:28:48 +00001483 // If the sign bit is known zero or one, the top bits match.
1484 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001485 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001486 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001487 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001488 return;
1489 }
1490 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001491 MVT InVT = Op.getOperand(0).getValueType();
1492 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001493 APInt InMask = Mask;
1494 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001495 KnownZero.trunc(InBits);
1496 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001497 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001498 KnownZero.zext(BitWidth);
1499 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001500 return;
1501 }
1502 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001503 MVT InVT = Op.getOperand(0).getValueType();
1504 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001505 APInt InMask = Mask;
1506 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001507 KnownZero.zext(InBits);
1508 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001509 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001510 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001511 KnownZero.trunc(BitWidth);
1512 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001513 break;
1514 }
1515 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001516 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1517 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001518 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1519 KnownOne, Depth+1);
1520 KnownZero |= (~InMask) & Mask;
1521 return;
1522 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001523 case ISD::FGETSIGN:
1524 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001525 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001526 return;
1527
Dan Gohman23e8b712008-04-28 17:02:21 +00001528 case ISD::SUB: {
1529 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1530 // We know that the top bits of C-X are clear if X contains less bits
1531 // than C (i.e. no wrap-around can happen). For example, 20-X is
1532 // positive if we can prove that X is >= 0 and < 16.
1533 if (CLHS->getAPIntValue().isNonNegative()) {
1534 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1535 // NLZ can't be BitWidth with no sign bit
1536 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1537 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1538 Depth+1);
1539
1540 // If all of the MaskV bits are known to be zero, then we know the
1541 // output top bits are zero, because we now know that the output is
1542 // from [0-C].
1543 if ((KnownZero2 & MaskV) == MaskV) {
1544 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1545 // Top bits known zero.
1546 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1547 }
1548 }
1549 }
1550 }
1551 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001552 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001553 // Output known-0 bits are known if clear or set in both the low clear bits
1554 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1555 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001556 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1557 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1558 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1559 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1560
1561 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1562 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1563 KnownZeroOut = std::min(KnownZeroOut,
1564 KnownZero2.countTrailingOnes());
1565
1566 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001567 return;
1568 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001569 case ISD::SREM:
1570 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1571 APInt RA = Rem->getAPIntValue();
1572 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001573 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001574 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1575 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001576
Dan Gohman23e8b712008-04-28 17:02:21 +00001577 // The sign of a remainder is equal to the sign of the first
1578 // operand (zero being positive).
1579 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1580 KnownZero2 |= ~LowBits;
1581 else if (KnownOne2[BitWidth-1])
1582 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001583
Dan Gohman23e8b712008-04-28 17:02:21 +00001584 KnownZero |= KnownZero2 & Mask;
1585 KnownOne |= KnownOne2 & Mask;
1586
1587 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 }
1589 }
1590 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001591 case ISD::UREM: {
1592 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1593 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001594 if (RA.isPowerOf2()) {
1595 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001596 APInt Mask2 = LowBits & Mask;
1597 KnownZero |= ~LowBits & Mask;
1598 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1599 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1600 break;
1601 }
1602 }
1603
1604 // Since the result is less than or equal to either operand, any leading
1605 // zero bits in either operand must also exist in the result.
1606 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1607 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1608 Depth+1);
1609 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1610 Depth+1);
1611
1612 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1613 KnownZero2.countLeadingOnes());
1614 KnownOne.clear();
1615 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1616 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001617 }
1618 default:
1619 // Allow the target to implement this method for its nodes.
1620 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1621 case ISD::INTRINSIC_WO_CHAIN:
1622 case ISD::INTRINSIC_W_CHAIN:
1623 case ISD::INTRINSIC_VOID:
1624 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1625 }
1626 return;
1627 }
1628}
1629
1630/// ComputeNumSignBits - Return the number of times the sign bit of the
1631/// register is replicated into the other bits. We know that at least 1 bit
1632/// is always equal to the sign bit (itself), but other cases can give us
1633/// information. For example, immediately after an "SRA X, 2", we know that
1634/// the top 3 bits are all equal to each other, so we return 3.
1635unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001636 MVT VT = Op.getValueType();
1637 assert(VT.isInteger() && "Invalid VT!");
1638 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001639 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001640 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001641
1642 if (Depth == 6)
1643 return 1; // Limit search depth.
1644
1645 switch (Op.getOpcode()) {
1646 default: break;
1647 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001648 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001649 return VTBits-Tmp+1;
1650 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001652 return VTBits-Tmp;
1653
1654 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001655 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1656 // If negative, return # leading ones.
1657 if (Val.isNegative())
1658 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001659
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001660 // Return # leading zeros.
1661 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001662 }
1663
1664 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001666 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1667
1668 case ISD::SIGN_EXTEND_INREG:
1669 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001670 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001671 Tmp = VTBits-Tmp+1;
1672
1673 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1674 return std::max(Tmp, Tmp2);
1675
1676 case ISD::SRA:
1677 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1678 // SRA X, C -> adds C sign bits.
1679 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1680 Tmp += C->getValue();
1681 if (Tmp > VTBits) Tmp = VTBits;
1682 }
1683 return Tmp;
1684 case ISD::SHL:
1685 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1686 // shl destroys sign bits.
1687 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1688 if (C->getValue() >= VTBits || // Bad shift.
1689 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1690 return Tmp - C->getValue();
1691 }
1692 break;
1693 case ISD::AND:
1694 case ISD::OR:
1695 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001696 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001697 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001698 if (Tmp != 1) {
1699 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1700 FirstAnswer = std::min(Tmp, Tmp2);
1701 // We computed what we know about the sign bits as our first
1702 // answer. Now proceed to the generic code that uses
1703 // ComputeMaskedBits, and pick whichever answer is better.
1704 }
1705 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001706
1707 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001708 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001709 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001710 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001711 return std::min(Tmp, Tmp2);
1712
1713 case ISD::SETCC:
1714 // If setcc returns 0/-1, all bits are sign bits.
1715 if (TLI.getSetCCResultContents() ==
1716 TargetLowering::ZeroOrNegativeOneSetCCResult)
1717 return VTBits;
1718 break;
1719 case ISD::ROTL:
1720 case ISD::ROTR:
1721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1722 unsigned RotAmt = C->getValue() & (VTBits-1);
1723
1724 // Handle rotate right by N like a rotate left by 32-N.
1725 if (Op.getOpcode() == ISD::ROTR)
1726 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1727
1728 // If we aren't rotating out all of the known-in sign bits, return the
1729 // number that are left. This handles rotl(sext(x), 1) for example.
1730 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1731 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1732 }
1733 break;
1734 case ISD::ADD:
1735 // Add can have at most one carry bit. Thus we know that the output
1736 // is, at worst, one more bit than the inputs.
1737 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1738 if (Tmp == 1) return 1; // Early out.
1739
1740 // Special case decrementing a value (ADD X, -1):
1741 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1742 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001743 APInt KnownZero, KnownOne;
1744 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1746
1747 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1748 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001749 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001750 return VTBits;
1751
1752 // If we are subtracting one from a positive number, there is no carry
1753 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001754 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001755 return Tmp;
1756 }
1757
1758 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1759 if (Tmp2 == 1) return 1;
1760 return std::min(Tmp, Tmp2)-1;
1761 break;
1762
1763 case ISD::SUB:
1764 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1765 if (Tmp2 == 1) return 1;
1766
1767 // Handle NEG.
1768 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001769 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001770 APInt KnownZero, KnownOne;
1771 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1773 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1774 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001775 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001776 return VTBits;
1777
1778 // If the input is known to be positive (the sign bit is known clear),
1779 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001780 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001781 return Tmp2;
1782
1783 // Otherwise, we treat this like a SUB.
1784 }
1785
1786 // Sub can have at most one carry bit. Thus we know that the output
1787 // is, at worst, one more bit than the inputs.
1788 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1789 if (Tmp == 1) return 1; // Early out.
1790 return std::min(Tmp, Tmp2)-1;
1791 break;
1792 case ISD::TRUNCATE:
1793 // FIXME: it's tricky to do anything useful for this, but it is an important
1794 // case for targets like X86.
1795 break;
1796 }
1797
1798 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1799 if (Op.getOpcode() == ISD::LOAD) {
1800 LoadSDNode *LD = cast<LoadSDNode>(Op);
1801 unsigned ExtType = LD->getExtensionType();
1802 switch (ExtType) {
1803 default: break;
1804 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001805 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001806 return VTBits-Tmp+1;
1807 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001808 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001809 return VTBits-Tmp;
1810 }
1811 }
1812
1813 // Allow the target to implement this method for its nodes.
1814 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1815 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1816 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1817 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1818 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001819 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001820 }
1821
1822 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1823 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001824 APInt KnownZero, KnownOne;
1825 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1827
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001828 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001829 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001830 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001831 Mask = KnownOne;
1832 } else {
1833 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001834 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 }
1836
1837 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1838 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001839 Mask = ~Mask;
1840 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 // Return # leading zeros. We use 'min' here in case Val was zero before
1842 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001843 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001844}
1845
Chris Lattner51dabfb2006-10-14 00:41:01 +00001846
Evan Chenga844bde2008-02-02 04:07:54 +00001847bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1848 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1849 if (!GA) return false;
1850 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1851 if (!GV) return false;
1852 MachineModuleInfo *MMI = getMachineModuleInfo();
1853 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1854}
1855
1856
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001857/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1858/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001859SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001860 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001861 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001862 SDOperand Idx = PermMask.getOperand(i);
1863 if (Idx.getOpcode() == ISD::UNDEF)
1864 return getNode(ISD::UNDEF, VT.getVectorElementType());
1865 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001866 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001867 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1868 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001869
1870 if (V.getOpcode() == ISD::BIT_CONVERT) {
1871 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001872 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001873 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001874 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001875 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001876 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001877 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001878 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001879 return V.getOperand(Index);
1880 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1881 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001882 return SDOperand();
1883}
1884
1885
Chris Lattnerc3aae252005-01-07 07:46:32 +00001886/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001887///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001888SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001889 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001890 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001891 void *IP = 0;
1892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1893 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001894 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001895 CSEMap.InsertNode(N, IP);
1896
1897 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001898 return SDOperand(N, 0);
1899}
1900
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001902 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001904 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001905 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001906 switch (Opcode) {
1907 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001908 case ISD::SIGN_EXTEND:
1909 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001910 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001911 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001912 case ISD::TRUNCATE:
1913 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001914 case ISD::UINT_TO_FP:
1915 case ISD::SINT_TO_FP: {
1916 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001917 // No compile time operations on this type.
1918 if (VT==MVT::ppcf128)
1919 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001920 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1921 (void)apf.convertFromAPInt(Val,
1922 Opcode==ISD::SINT_TO_FP,
1923 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001924 return getConstantFP(apf, VT);
1925 }
Chris Lattner94683772005-12-23 05:30:37 +00001926 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001927 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001928 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001929 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001930 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001931 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001932 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001933 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001934 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001935 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001936 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001937 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001938 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001939 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001940 }
1941 }
1942
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001943 // Constant fold unary operations with a floating point constant operand.
1944 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1945 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001946 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001947 switch (Opcode) {
1948 case ISD::FNEG:
1949 V.changeSign();
1950 return getConstantFP(V, VT);
1951 case ISD::FABS:
1952 V.clearSign();
1953 return getConstantFP(V, VT);
1954 case ISD::FP_ROUND:
1955 case ISD::FP_EXTEND:
1956 // This can return overflow, underflow, or inexact; we don't care.
1957 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001958 (void)V.convert(*MVTToAPFloatSemantics(VT),
1959 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001960 return getConstantFP(V, VT);
1961 case ISD::FP_TO_SINT:
1962 case ISD::FP_TO_UINT: {
1963 integerPart x;
1964 assert(integerPartWidth >= 64);
1965 // FIXME need to be more flexible about rounding mode.
1966 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1967 Opcode==ISD::FP_TO_SINT,
1968 APFloat::rmTowardZero);
1969 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1970 break;
1971 return getConstant(x, VT);
1972 }
1973 case ISD::BIT_CONVERT:
1974 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1975 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1976 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1977 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001978 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001979 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001980 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001981 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001982
1983 unsigned OpOpcode = Operand.Val->getOpcode();
1984 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001985 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00001986 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001987 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001988 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 assert(VT.isFloatingPoint() &&
1990 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001991 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001992 if (Operand.getOpcode() == ISD::UNDEF)
1993 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001994 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001995 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001996 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00001997 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001998 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00001999 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002000 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002001 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2002 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2003 break;
2004 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002005 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002006 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002007 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002008 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002009 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002010 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002011 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002012 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002013 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002014 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002015 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002016 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002017 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002018 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002019 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2020 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2021 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2022 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002023 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002024 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002025 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002026 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002027 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002028 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002029 if (OpOpcode == ISD::TRUNCATE)
2030 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002031 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2032 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002033 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002034 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002035 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002036 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002037 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2038 else
2039 return Operand.Val->getOperand(0);
2040 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002041 break;
Chris Lattner35481892005-12-23 00:16:34 +00002042 case ISD::BIT_CONVERT:
2043 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002044 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002045 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002046 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002047 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2048 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002049 if (OpOpcode == ISD::UNDEF)
2050 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002051 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002052 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2054 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002055 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002056 if (OpOpcode == ISD::UNDEF)
2057 return getNode(ISD::UNDEF, VT);
2058 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2059 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2060 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2061 Operand.getConstantOperandVal(1) == 0 &&
2062 Operand.getOperand(0).getValueType() == VT)
2063 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002064 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002065 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002066 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2067 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002068 Operand.Val->getOperand(0));
2069 if (OpOpcode == ISD::FNEG) // --X -> X
2070 return Operand.Val->getOperand(0);
2071 break;
2072 case ISD::FABS:
2073 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2074 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2075 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002076 }
2077
Chris Lattner43247a12005-08-25 19:12:10 +00002078 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002079 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002080 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002081 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002082 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002083 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002084 void *IP = 0;
2085 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2086 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002087 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002088 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002089 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002090 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002091 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002092 AllNodes.push_back(N);
2093 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002094}
2095
Chris Lattner36019aa2005-04-18 03:48:41 +00002096
2097
Duncan Sands83ec4b62008-06-06 12:08:01 +00002098SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2101 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002102 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002103 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002104 case ISD::TokenFactor:
2105 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2106 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002107 // Fold trivial token factors.
2108 if (N1.getOpcode() == ISD::EntryToken) return N2;
2109 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002110 break;
Chris Lattner76365122005-01-16 02:23:22 +00002111 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002112 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002113 N1.getValueType() == VT && "Binary operator types must match!");
2114 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2115 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002116 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002117 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002118 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2119 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002120 break;
Chris Lattner76365122005-01-16 02:23:22 +00002121 case ISD::OR:
2122 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002123 case ISD::ADD:
2124 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002125 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002126 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002127 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2128 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002129 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002130 return N1;
2131 break;
Chris Lattner76365122005-01-16 02:23:22 +00002132 case ISD::UDIV:
2133 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002134 case ISD::MULHU:
2135 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002136 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002137 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002138 case ISD::MUL:
2139 case ISD::SDIV:
2140 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002141 case ISD::FADD:
2142 case ISD::FSUB:
2143 case ISD::FMUL:
2144 case ISD::FDIV:
2145 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002146 assert(N1.getValueType() == N2.getValueType() &&
2147 N1.getValueType() == VT && "Binary operator types must match!");
2148 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002149 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2150 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002151 N1.getValueType().isFloatingPoint() &&
2152 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002153 "Invalid FCOPYSIGN!");
2154 break;
Chris Lattner76365122005-01-16 02:23:22 +00002155 case ISD::SHL:
2156 case ISD::SRA:
2157 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002158 case ISD::ROTL:
2159 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002160 assert(VT == N1.getValueType() &&
2161 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002162 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002163 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002164 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002165 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002166 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002167 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002168 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002169 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002170 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002171 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002172 break;
2173 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002174 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002175 assert(VT.isFloatingPoint() &&
2176 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002177 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002178 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002179 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002180 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002181 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002182 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002184 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002185 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002186 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002187 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002188 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002189 break;
2190 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002191 case ISD::SIGN_EXTEND_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 extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002195 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002196 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002197 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002198
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002199 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002200 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002201 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002202 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002203 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002204 return getConstant(Val, VT);
2205 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002206 break;
2207 }
2208 case ISD::EXTRACT_VECTOR_ELT:
2209 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2210
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002211 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2212 if (N1.getOpcode() == ISD::UNDEF)
2213 return getNode(ISD::UNDEF, VT);
2214
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002215 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2216 // expanding copies of large vectors from registers.
2217 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2218 N1.getNumOperands() > 0) {
2219 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002220 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002221 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2222 N1.getOperand(N2C->getValue() / Factor),
2223 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2224 }
2225
2226 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2227 // expanding large vector constants.
2228 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2229 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002230
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002231 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2232 // operations are lowered to scalars.
2233 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2234 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2235 if (IEC == N2C)
2236 return N1.getOperand(1);
2237 else
2238 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2239 }
2240 break;
2241 case ISD::EXTRACT_ELEMENT:
2242 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002243 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2244 (N1.getValueType().isInteger() == VT.isInteger()) &&
2245 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002246
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002247 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2248 // 64-bit integers into 32-bit parts. Instead of building the extract of
2249 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2250 if (N1.getOpcode() == ISD::BUILD_PAIR)
2251 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002252
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002253 // EXTRACT_ELEMENT of a constant int is also very common.
2254 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002255 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002256 unsigned Shift = ElementSize * N2C->getValue();
2257 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2258 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002259 }
2260 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002261 case ISD::EXTRACT_SUBVECTOR:
2262 if (N1.getValueType() == VT) // Trivial extraction.
2263 return N1;
2264 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002265 }
2266
2267 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002268 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002269 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002270 switch (Opcode) {
2271 case ISD::ADD: return getConstant(C1 + C2, VT);
2272 case ISD::SUB: return getConstant(C1 - C2, VT);
2273 case ISD::MUL: return getConstant(C1 * C2, VT);
2274 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002275 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002276 break;
2277 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002278 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002279 break;
2280 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002281 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002282 break;
2283 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002284 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002285 break;
2286 case ISD::AND : return getConstant(C1 & C2, VT);
2287 case ISD::OR : return getConstant(C1 | C2, VT);
2288 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002289 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002290 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2291 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2292 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2293 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002294 default: break;
2295 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002296 } else { // Cannonicalize constant to RHS if commutative
2297 if (isCommutativeBinOp(Opcode)) {
2298 std::swap(N1C, N2C);
2299 std::swap(N1, N2);
2300 }
2301 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002302 }
2303
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002304 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002305 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2306 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002307 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002308 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2309 // Cannonicalize constant to RHS if commutative
2310 std::swap(N1CFP, N2CFP);
2311 std::swap(N1, N2);
2312 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002313 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2314 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002315 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002316 case ISD::FADD:
2317 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002318 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002319 return getConstantFP(V1, VT);
2320 break;
2321 case ISD::FSUB:
2322 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2323 if (s!=APFloat::opInvalidOp)
2324 return getConstantFP(V1, VT);
2325 break;
2326 case ISD::FMUL:
2327 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2328 if (s!=APFloat::opInvalidOp)
2329 return getConstantFP(V1, VT);
2330 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002331 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002332 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2333 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2334 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002335 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002336 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002337 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2338 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2339 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002340 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002341 case ISD::FCOPYSIGN:
2342 V1.copySign(V2);
2343 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 default: break;
2345 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002346 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002347 }
Chris Lattner62b57722006-04-20 05:39:12 +00002348
2349 // Canonicalize an UNDEF to the RHS, even over a constant.
2350 if (N1.getOpcode() == ISD::UNDEF) {
2351 if (isCommutativeBinOp(Opcode)) {
2352 std::swap(N1, N2);
2353 } else {
2354 switch (Opcode) {
2355 case ISD::FP_ROUND_INREG:
2356 case ISD::SIGN_EXTEND_INREG:
2357 case ISD::SUB:
2358 case ISD::FSUB:
2359 case ISD::FDIV:
2360 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002361 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002362 return N1; // fold op(undef, arg2) -> undef
2363 case ISD::UDIV:
2364 case ISD::SDIV:
2365 case ISD::UREM:
2366 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002367 case ISD::SRL:
2368 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002369 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002370 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2371 // For vectors, we can't easily build an all zero vector, just return
2372 // the LHS.
2373 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002374 }
2375 }
2376 }
2377
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002378 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002379 if (N2.getOpcode() == ISD::UNDEF) {
2380 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002381 case ISD::XOR:
2382 if (N1.getOpcode() == ISD::UNDEF)
2383 // Handle undef ^ undef -> 0 special case. This is a common
2384 // idiom (misuse).
2385 return getConstant(0, VT);
2386 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002387 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002388 case ISD::ADDC:
2389 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002390 case ISD::SUB:
2391 case ISD::FADD:
2392 case ISD::FSUB:
2393 case ISD::FMUL:
2394 case ISD::FDIV:
2395 case ISD::FREM:
2396 case ISD::UDIV:
2397 case ISD::SDIV:
2398 case ISD::UREM:
2399 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002400 return N2; // fold op(arg1, undef) -> undef
2401 case ISD::MUL:
2402 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002403 case ISD::SRL:
2404 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002406 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2407 // For vectors, we can't easily build an all zero vector, just return
2408 // the LHS.
2409 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002410 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002411 if (!VT.isVector())
2412 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002413 // For vectors, we can't easily build an all one vector, just return
2414 // the LHS.
2415 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002416 case ISD::SRA:
2417 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002418 }
2419 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002420
Chris Lattner27e9b412005-05-11 18:57:39 +00002421 // Memoize this node if possible.
2422 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002423 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002424 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002425 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002426 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002427 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002428 void *IP = 0;
2429 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2430 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002431 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002432 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002433 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002434 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002435 }
2436
Chris Lattnerc3aae252005-01-07 07:46:32 +00002437 AllNodes.push_back(N);
2438 return SDOperand(N, 0);
2439}
2440
Duncan Sands83ec4b62008-06-06 12:08:01 +00002441SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002442 SDOperand N1, SDOperand N2, SDOperand N3) {
2443 // Perform various simplifications.
2444 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2445 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002446 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002447 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002448 // Use FoldSetCC to simplify SETCC's.
2449 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002450 if (Simp.Val) return Simp;
2451 break;
2452 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002453 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002454 if (N1C) {
2455 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002456 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002457 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002458 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002459 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460
2461 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002462 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002463 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002464 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002465 if (N2C->getValue()) // Unconditional branch
2466 return getNode(ISD::BR, MVT::Other, N1, N3);
2467 else
2468 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002469 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002470 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002471 case ISD::VECTOR_SHUFFLE:
2472 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002473 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002474 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002475 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002476 "Illegal VECTOR_SHUFFLE node!");
2477 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002478 case ISD::BIT_CONVERT:
2479 // Fold bit_convert nodes from a type to themselves.
2480 if (N1.getValueType() == VT)
2481 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002482 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002483 }
2484
Chris Lattner43247a12005-08-25 19:12:10 +00002485 // Memoize node if it doesn't produce a flag.
2486 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002487 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002488 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002489 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002490 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002491 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002492 void *IP = 0;
2493 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2494 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002495 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002496 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002497 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002498 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002499 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 AllNodes.push_back(N);
2501 return SDOperand(N, 0);
2502}
2503
Duncan Sands83ec4b62008-06-06 12:08:01 +00002504SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002505 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002506 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002507 SDOperand Ops[] = { N1, N2, N3, N4 };
2508 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002509}
2510
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002512 SDOperand N1, SDOperand N2, SDOperand N3,
2513 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002514 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2515 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002516}
2517
Dan Gohman707e0182008-04-12 04:36:06 +00002518/// getMemsetValue - Vectorized representation of the memset value
2519/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002520static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2521 unsigned NumBits = VT.isVector() ?
2522 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002524 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002525 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002526 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002527 Val = (Val << Shift) | Val;
2528 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002529 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002530 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002531 return DAG.getConstant(Val, VT);
2532 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002533 }
Evan Chengf0df0312008-05-15 08:39:06 +00002534
2535 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2536 unsigned Shift = 8;
2537 for (unsigned i = NumBits; i > 8; i >>= 1) {
2538 Value = DAG.getNode(ISD::OR, VT,
2539 DAG.getNode(ISD::SHL, VT, Value,
2540 DAG.getConstant(Shift, MVT::i8)), Value);
2541 Shift <<= 1;
2542 }
2543
2544 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002545}
2546
Dan Gohman707e0182008-04-12 04:36:06 +00002547/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2548/// used when a memcpy is turned into a memset when the source is a constant
2549/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002550static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002551 const TargetLowering &TLI,
2552 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002553 // Handle vector with all elements zero.
2554 if (Str.empty()) {
2555 if (VT.isInteger())
2556 return DAG.getConstant(0, VT);
2557 unsigned NumElts = VT.getVectorNumElements();
2558 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2559 return DAG.getNode(ISD::BIT_CONVERT, VT,
2560 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2561 }
2562
Duncan Sands83ec4b62008-06-06 12:08:01 +00002563 assert(!VT.isVector() && "Can't handle vector type here!");
2564 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002565 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002566 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002567 if (TLI.isLittleEndian())
2568 Offset = Offset + MSB - 1;
2569 for (unsigned i = 0; i != MSB; ++i) {
2570 Val = (Val << 8) | (unsigned char)Str[Offset];
2571 Offset += TLI.isLittleEndian() ? -1 : 1;
2572 }
2573 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002574}
2575
Dan Gohman707e0182008-04-12 04:36:06 +00002576/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002577///
Dan Gohman707e0182008-04-12 04:36:06 +00002578static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2579 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002581 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2582}
2583
Evan Chengf0df0312008-05-15 08:39:06 +00002584/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2585///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002586static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002587 unsigned SrcDelta = 0;
2588 GlobalAddressSDNode *G = NULL;
2589 if (Src.getOpcode() == ISD::GlobalAddress)
2590 G = cast<GlobalAddressSDNode>(Src);
2591 else if (Src.getOpcode() == ISD::ADD &&
2592 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2593 Src.getOperand(1).getOpcode() == ISD::Constant) {
2594 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2595 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2596 }
2597 if (!G)
2598 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002599
Evan Chengf0df0312008-05-15 08:39:06 +00002600 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002601 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2602 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002603
Evan Chengf0df0312008-05-15 08:39:06 +00002604 return false;
2605}
Dan Gohman707e0182008-04-12 04:36:06 +00002606
Evan Chengf0df0312008-05-15 08:39:06 +00002607/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2608/// to replace the memset / memcpy is below the threshold. It also returns the
2609/// types of the sequence of memory ops to perform memset / memcpy.
2610static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002611bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002612 SDOperand Dst, SDOperand Src,
2613 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002614 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002615 SelectionDAG &DAG,
2616 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002617 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002618 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002619 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002620 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002621 if (VT != MVT::iAny) {
2622 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002624 // If source is a string constant, this will require an unaligned load.
2625 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2626 if (Dst.getOpcode() != ISD::FrameIndex) {
2627 // Can't change destination alignment. It requires a unaligned store.
2628 if (AllowUnalign)
2629 VT = MVT::iAny;
2630 } else {
2631 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2632 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2633 if (MFI->isFixedObjectIndex(FI)) {
2634 // Can't change destination alignment. It requires a unaligned store.
2635 if (AllowUnalign)
2636 VT = MVT::iAny;
2637 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002638 // Give the stack frame object a larger alignment if needed.
2639 if (MFI->getObjectAlignment(FI) < NewAlign)
2640 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002641 Align = NewAlign;
2642 }
2643 }
2644 }
2645 }
2646
2647 if (VT == MVT::iAny) {
2648 if (AllowUnalign) {
2649 VT = MVT::i64;
2650 } else {
2651 switch (Align & 7) {
2652 case 0: VT = MVT::i64; break;
2653 case 4: VT = MVT::i32; break;
2654 case 2: VT = MVT::i16; break;
2655 default: VT = MVT::i8; break;
2656 }
2657 }
2658
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002660 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2662 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002663
Duncan Sands8e4eb092008-06-08 20:54:56 +00002664 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002665 VT = LVT;
2666 }
Dan Gohman707e0182008-04-12 04:36:06 +00002667
2668 unsigned NumMemOps = 0;
2669 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002670 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002671 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002672 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002674 VT = MVT::i64;
2675 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2677 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002678 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002680 VTSize >>= 1;
2681 }
Dan Gohman707e0182008-04-12 04:36:06 +00002682 }
Dan Gohman707e0182008-04-12 04:36:06 +00002683
2684 if (++NumMemOps > Limit)
2685 return false;
2686 MemOps.push_back(VT);
2687 Size -= VTSize;
2688 }
2689
2690 return true;
2691}
2692
2693static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2694 SDOperand Chain, SDOperand Dst,
2695 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002696 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002697 const Value *DstSV, uint64_t DstSVOff,
2698 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002699 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2700
Dan Gohman21323f32008-05-29 19:42:22 +00002701 // Expand memcpy to a series of load and store ops if the size operand falls
2702 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002703 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002704 uint64_t Limit = -1;
2705 if (!AlwaysInline)
2706 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002707 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002708 std::string Str;
2709 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002710 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002711 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002712 return SDOperand();
2713
Dan Gohman707e0182008-04-12 04:36:06 +00002714
Evan Cheng0ff39b32008-06-30 07:31:25 +00002715 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002716 SmallVector<SDOperand, 8> OutChains;
2717 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002718 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002719 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002720 MVT VT = MemOps[i];
2721 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002722 SDOperand Value, Store;
2723
Evan Cheng0ff39b32008-06-30 07:31:25 +00002724 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002725 // It's unlikely a store of a vector immediate can be done in a single
2726 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002727 // We also handle store a vector with all zero's.
2728 // FIXME: Handle other cases where store of vector immediate is done in
2729 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002730 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002731 Store = DAG.getStore(Chain, Value,
2732 getMemBasePlusOffset(Dst, DstOff, DAG),
2733 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002734 } else {
2735 Value = DAG.getLoad(VT, Chain,
2736 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002737 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002738 Store = DAG.getStore(Chain, Value,
2739 getMemBasePlusOffset(Dst, DstOff, DAG),
2740 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002741 }
2742 OutChains.push_back(Store);
2743 SrcOff += VTSize;
2744 DstOff += VTSize;
2745 }
2746
2747 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2748 &OutChains[0], OutChains.size());
2749}
2750
Dan Gohman21323f32008-05-29 19:42:22 +00002751static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2752 SDOperand Chain, SDOperand Dst,
2753 SDOperand Src, uint64_t Size,
2754 unsigned Align, bool AlwaysInline,
2755 const Value *DstSV, uint64_t DstSVOff,
2756 const Value *SrcSV, uint64_t SrcSVOff){
2757 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2758
2759 // Expand memmove to a series of load and store ops if the size operand falls
2760 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002762 uint64_t Limit = -1;
2763 if (!AlwaysInline)
2764 Limit = TLI.getMaxStoresPerMemmove();
2765 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002766 std::string Str;
2767 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002768 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002769 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002770 return SDOperand();
2771
Dan Gohman21323f32008-05-29 19:42:22 +00002772 uint64_t SrcOff = 0, DstOff = 0;
2773
2774 SmallVector<SDOperand, 8> LoadValues;
2775 SmallVector<SDOperand, 8> LoadChains;
2776 SmallVector<SDOperand, 8> OutChains;
2777 unsigned NumMemOps = MemOps.size();
2778 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002779 MVT VT = MemOps[i];
2780 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002781 SDOperand Value, Store;
2782
2783 Value = DAG.getLoad(VT, Chain,
2784 getMemBasePlusOffset(Src, SrcOff, DAG),
2785 SrcSV, SrcSVOff + SrcOff, false, Align);
2786 LoadValues.push_back(Value);
2787 LoadChains.push_back(Value.getValue(1));
2788 SrcOff += VTSize;
2789 }
2790 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2791 &LoadChains[0], LoadChains.size());
2792 OutChains.clear();
2793 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002794 MVT VT = MemOps[i];
2795 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002796 SDOperand Value, Store;
2797
2798 Store = DAG.getStore(Chain, LoadValues[i],
2799 getMemBasePlusOffset(Dst, DstOff, DAG),
2800 DstSV, DstSVOff + DstOff, false, DstAlign);
2801 OutChains.push_back(Store);
2802 DstOff += VTSize;
2803 }
2804
2805 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2806 &OutChains[0], OutChains.size());
2807}
2808
Dan Gohman707e0182008-04-12 04:36:06 +00002809static SDOperand getMemsetStores(SelectionDAG &DAG,
2810 SDOperand Chain, SDOperand Dst,
2811 SDOperand Src, uint64_t Size,
2812 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002813 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002814 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2815
2816 // Expand memset to a series of load/store ops if the size operand
2817 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002818 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002819 std::string Str;
2820 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002821 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002822 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002823 return SDOperand();
2824
2825 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002826 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002827
2828 unsigned NumMemOps = MemOps.size();
2829 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002830 MVT VT = MemOps[i];
2831 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002832 SDOperand Value = getMemsetValue(Src, VT, DAG);
2833 SDOperand Store = DAG.getStore(Chain, Value,
2834 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002835 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002836 OutChains.push_back(Store);
2837 DstOff += VTSize;
2838 }
2839
2840 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2841 &OutChains[0], OutChains.size());
2842}
2843
2844SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002845 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002846 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002847 const Value *DstSV, uint64_t DstSVOff,
2848 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002849
2850 // Check to see if we should lower the memcpy to loads and stores first.
2851 // For cases within the target-specified limits, this is the best choice.
2852 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2853 if (ConstantSize) {
2854 // Memcpy with size zero? Just return the original chain.
2855 if (ConstantSize->isNullValue())
2856 return Chain;
2857
2858 SDOperand Result =
2859 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002860 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002861 if (Result.Val)
2862 return Result;
2863 }
2864
2865 // Then check to see if we should lower the memcpy with target-specific
2866 // code. If the target chooses to do this, this is the next best.
2867 SDOperand Result =
2868 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2869 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002870 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002871 if (Result.Val)
2872 return Result;
2873
2874 // If we really need inline code and the target declined to provide it,
2875 // use a (potentially long) sequence of loads and stores.
2876 if (AlwaysInline) {
2877 assert(ConstantSize && "AlwaysInline requires a constant size!");
2878 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2879 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002880 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002881 }
2882
2883 // Emit a library call.
2884 TargetLowering::ArgListTy Args;
2885 TargetLowering::ArgListEntry Entry;
2886 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2887 Entry.Node = Dst; Args.push_back(Entry);
2888 Entry.Node = Src; Args.push_back(Entry);
2889 Entry.Node = Size; Args.push_back(Entry);
2890 std::pair<SDOperand,SDOperand> CallResult =
2891 TLI.LowerCallTo(Chain, Type::VoidTy,
2892 false, false, false, CallingConv::C, false,
2893 getExternalSymbol("memcpy", TLI.getPointerTy()),
2894 Args, *this);
2895 return CallResult.second;
2896}
2897
2898SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2899 SDOperand Src, SDOperand Size,
2900 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002901 const Value *DstSV, uint64_t DstSVOff,
2902 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002903
Dan Gohman21323f32008-05-29 19:42:22 +00002904 // Check to see if we should lower the memmove to loads and stores first.
2905 // For cases within the target-specified limits, this is the best choice.
2906 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2907 if (ConstantSize) {
2908 // Memmove with size zero? Just return the original chain.
2909 if (ConstantSize->isNullValue())
2910 return Chain;
2911
2912 SDOperand Result =
2913 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2914 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2915 if (Result.Val)
2916 return Result;
2917 }
Dan Gohman707e0182008-04-12 04:36:06 +00002918
2919 // Then check to see if we should lower the memmove with target-specific
2920 // code. If the target chooses to do this, this is the next best.
2921 SDOperand Result =
2922 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002923 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002924 if (Result.Val)
2925 return Result;
2926
2927 // Emit a library call.
2928 TargetLowering::ArgListTy Args;
2929 TargetLowering::ArgListEntry Entry;
2930 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2931 Entry.Node = Dst; Args.push_back(Entry);
2932 Entry.Node = Src; Args.push_back(Entry);
2933 Entry.Node = Size; Args.push_back(Entry);
2934 std::pair<SDOperand,SDOperand> CallResult =
2935 TLI.LowerCallTo(Chain, Type::VoidTy,
2936 false, false, false, CallingConv::C, false,
2937 getExternalSymbol("memmove", TLI.getPointerTy()),
2938 Args, *this);
2939 return CallResult.second;
2940}
2941
2942SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2943 SDOperand Src, SDOperand Size,
2944 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002945 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002946
2947 // Check to see if we should lower the memset to stores first.
2948 // For cases within the target-specified limits, this is the best choice.
2949 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2950 if (ConstantSize) {
2951 // Memset with size zero? Just return the original chain.
2952 if (ConstantSize->isNullValue())
2953 return Chain;
2954
2955 SDOperand Result =
2956 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002957 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002958 if (Result.Val)
2959 return Result;
2960 }
2961
2962 // Then check to see if we should lower the memset with target-specific
2963 // code. If the target chooses to do this, this is the next best.
2964 SDOperand Result =
2965 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002966 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002967 if (Result.Val)
2968 return Result;
2969
2970 // Emit a library call.
2971 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2972 TargetLowering::ArgListTy Args;
2973 TargetLowering::ArgListEntry Entry;
2974 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2975 Args.push_back(Entry);
2976 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002977 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00002978 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2979 else
2980 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2981 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2982 Args.push_back(Entry);
2983 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2984 Args.push_back(Entry);
2985 std::pair<SDOperand,SDOperand> CallResult =
2986 TLI.LowerCallTo(Chain, Type::VoidTy,
2987 false, false, false, CallingConv::C, false,
2988 getExternalSymbol("memset", TLI.getPointerTy()),
2989 Args, *this);
2990 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002991}
2992
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002993SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002994 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00002995 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00002996 unsigned Alignment) {
2997 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002998 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2999 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003000 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003001 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003002 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003003 void* IP = 0;
3004 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3005 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003006 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003007 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003008 CSEMap.InsertNode(N, IP);
3009 AllNodes.push_back(N);
3010 return SDOperand(N, 0);
3011}
3012
3013SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003014 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003015 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003016 unsigned Alignment) {
3017 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003018 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3019 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003020 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003021 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3022 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003023 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003024 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003025 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003026 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003027 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003028 void* IP = 0;
3029 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3030 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003031 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003032 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003033 CSEMap.InsertNode(N, IP);
3034 AllNodes.push_back(N);
3035 return SDOperand(N, 0);
3036}
3037
Duncan Sands14ea39c2008-03-27 20:23:40 +00003038SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003039SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003040 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003041 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003042 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003043 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003044 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3045 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003046 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003048 } else if (SV) {
3049 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3050 assert(PT && "Value for load must be a pointer");
3051 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003052 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003053 assert(Ty && "Could not get type information for load");
3054 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3055 }
Evan Cheng466685d2006-10-09 20:57:25 +00003056
Duncan Sands14ea39c2008-03-27 20:23:40 +00003057 if (VT == EVT) {
3058 ExtType = ISD::NON_EXTLOAD;
3059 } else if (ExtType == ISD::NON_EXTLOAD) {
3060 assert(VT == EVT && "Non-extending load from different memory type!");
3061 } else {
3062 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 if (VT.isVector())
3064 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003065 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003066 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003067 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003068 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003069 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003070 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003071 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003072 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003073
3074 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003075 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003076 "Unindexed load with an offset!");
3077
3078 SDVTList VTs = Indexed ?
3079 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3080 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003081 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003082 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003083 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003084 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003085 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003086 ID.AddInteger(Alignment);
3087 ID.AddInteger(isVolatile);
3088 void *IP = 0;
3089 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3090 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003091 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3092 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003093 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003094 AllNodes.push_back(N);
3095 return SDOperand(N, 0);
3096}
3097
Duncan Sands83ec4b62008-06-06 12:08:01 +00003098SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003099 SDOperand Chain, SDOperand Ptr,
3100 const Value *SV, int SVOffset,
3101 bool isVolatile, unsigned Alignment) {
3102 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003103 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3104 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105}
3106
Duncan Sands83ec4b62008-06-06 12:08:01 +00003107SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003108 SDOperand Chain, SDOperand Ptr,
3109 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003111 bool isVolatile, unsigned Alignment) {
3112 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003113 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3114 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003115}
3116
Evan Cheng144d8f02006-11-09 17:55:04 +00003117SDOperand
3118SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3119 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003120 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003121 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3122 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003123 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3124 LD->getChain(), Base, Offset, LD->getSrcValue(),
3125 LD->getSrcValueOffset(), LD->getMemoryVT(),
3126 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003127}
3128
Jeff Cohend41b30d2006-11-05 19:31:28 +00003129SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003130 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003131 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003132 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003133
Dan Gohman575e2f42007-06-04 15:49:41 +00003134 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3135 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003136 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003137 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003138 } else if (SV) {
3139 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3140 assert(PT && "Value for store must be a pointer");
3141 Ty = PT->getElementType();
3142 }
3143 assert(Ty && "Could not get type information for store");
3144 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3145 }
Evan Chengad071e12006-10-05 22:57:11 +00003146 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003147 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003148 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003149 FoldingSetNodeID ID;
3150 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003151 ID.AddInteger(ISD::UNINDEXED);
3152 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003153 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003154 ID.AddInteger(Alignment);
3155 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003156 void *IP = 0;
3157 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3158 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003159 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003160 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003161 CSEMap.InsertNode(N, IP);
3162 AllNodes.push_back(N);
3163 return SDOperand(N, 0);
3164}
3165
Jeff Cohend41b30d2006-11-05 19:31:28 +00003166SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003167 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003168 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003169 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003170 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003171
3172 if (VT == SVT)
3173 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003174
Duncan Sands8e4eb092008-06-08 20:54:56 +00003175 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003176 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003177 "Can't do FP-INT conversion!");
3178
Dan Gohman575e2f42007-06-04 15:49:41 +00003179 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3180 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003181 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003182 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003183 } else if (SV) {
3184 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3185 assert(PT && "Value for store must be a pointer");
3186 Ty = PT->getElementType();
3187 }
3188 assert(Ty && "Could not get type information for store");
3189 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3190 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003191 SDVTList VTs = getVTList(MVT::Other);
3192 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003193 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003194 FoldingSetNodeID ID;
3195 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003196 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003197 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003198 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003199 ID.AddInteger(Alignment);
3200 ID.AddInteger(isVolatile);
3201 void *IP = 0;
3202 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3203 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003204 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003205 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003206 CSEMap.InsertNode(N, IP);
3207 AllNodes.push_back(N);
3208 return SDOperand(N, 0);
3209}
3210
Evan Cheng144d8f02006-11-09 17:55:04 +00003211SDOperand
3212SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3213 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003214 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3215 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3216 "Store is already a indexed store!");
3217 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3218 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3219 FoldingSetNodeID ID;
3220 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3221 ID.AddInteger(AM);
3222 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003223 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003224 ID.AddInteger(ST->getAlignment());
3225 ID.AddInteger(ST->isVolatile());
3226 void *IP = 0;
3227 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3228 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003229 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003230 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003231 ST->getSrcValue(), ST->getSrcValueOffset(),
3232 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003233 CSEMap.InsertNode(N, IP);
3234 AllNodes.push_back(N);
3235 return SDOperand(N, 0);
3236}
3237
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003239 SDOperand Chain, SDOperand Ptr,
3240 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003241 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003242 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003243}
3244
Duncan Sands83ec4b62008-06-06 12:08:01 +00003245SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003246 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003247 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003248 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003249 case 1: return getNode(Opcode, VT, Ops[0]);
3250 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3251 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003252 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003253 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003254
Chris Lattneref847df2005-04-09 03:27:28 +00003255 switch (Opcode) {
3256 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003257 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003258 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003259 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3260 "LHS and RHS of condition must have same type!");
3261 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3262 "True and False arms of SelectCC must have same type!");
3263 assert(Ops[2].getValueType() == VT &&
3264 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003265 break;
3266 }
3267 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003268 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003269 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3270 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003271 break;
3272 }
Chris Lattneref847df2005-04-09 03:27:28 +00003273 }
3274
Chris Lattner385328c2005-05-14 07:42:29 +00003275 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003276 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003277 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003278 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003279 FoldingSetNodeID ID;
3280 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003281 void *IP = 0;
3282 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3283 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003284 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003285 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003286 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003287 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003288 }
Chris Lattneref847df2005-04-09 03:27:28 +00003289 AllNodes.push_back(N);
3290 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003291}
3292
Chris Lattner89c34632005-05-14 06:20:26 +00003293SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003294 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003295 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003296 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3297 Ops, NumOps);
3298}
3299
3300SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003301 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003302 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003303 if (NumVTs == 1)
3304 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003305 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3306}
3307
3308SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003309 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003310 if (VTList.NumVTs == 1)
3311 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003312
Chris Lattner5f056bf2005-07-10 01:55:33 +00003313 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003314 // FIXME: figure out how to safely handle things like
3315 // int foo(int x) { return 1 << (x & 255); }
3316 // int bar() { return foo(256); }
3317#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003318 case ISD::SRA_PARTS:
3319 case ISD::SRL_PARTS:
3320 case ISD::SHL_PARTS:
3321 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003322 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003323 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3324 else if (N3.getOpcode() == ISD::AND)
3325 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3326 // If the and is only masking out bits that cannot effect the shift,
3327 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003329 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3330 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3331 }
3332 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003333#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003334 }
Chris Lattner89c34632005-05-14 06:20:26 +00003335
Chris Lattner43247a12005-08-25 19:12:10 +00003336 // Memoize the node unless it returns a flag.
3337 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003338 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003339 FoldingSetNodeID ID;
3340 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003341 void *IP = 0;
3342 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3343 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003344 if (NumOps == 1)
3345 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3346 else if (NumOps == 2)
3347 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3348 else if (NumOps == 3)
3349 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3350 else
3351 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003352 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003353 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003354 if (NumOps == 1)
3355 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3356 else if (NumOps == 2)
3357 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3358 else if (NumOps == 3)
3359 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3360 else
3361 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003362 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003363 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003364 return SDOperand(N, 0);
3365}
3366
Dan Gohman08ce9762007-10-08 15:49:58 +00003367SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003368 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003369}
3370
3371SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3372 SDOperand N1) {
3373 SDOperand Ops[] = { N1 };
3374 return getNode(Opcode, VTList, Ops, 1);
3375}
3376
3377SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3378 SDOperand N1, SDOperand N2) {
3379 SDOperand Ops[] = { N1, N2 };
3380 return getNode(Opcode, VTList, Ops, 2);
3381}
3382
3383SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3384 SDOperand N1, SDOperand N2, SDOperand N3) {
3385 SDOperand Ops[] = { N1, N2, N3 };
3386 return getNode(Opcode, VTList, Ops, 3);
3387}
3388
3389SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3390 SDOperand N1, SDOperand N2, SDOperand N3,
3391 SDOperand N4) {
3392 SDOperand Ops[] = { N1, N2, N3, N4 };
3393 return getNode(Opcode, VTList, Ops, 4);
3394}
3395
3396SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3397 SDOperand N1, SDOperand N2, SDOperand N3,
3398 SDOperand N4, SDOperand N5) {
3399 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3400 return getNode(Opcode, VTList, Ops, 5);
3401}
3402
Duncan Sands83ec4b62008-06-06 12:08:01 +00003403SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003404 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003405}
3406
Duncan Sands83ec4b62008-06-06 12:08:01 +00003407SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3408 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003409 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003410 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003411 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003412 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003413 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003414 V.push_back(VT1);
3415 V.push_back(VT2);
3416 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003417 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003418}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003419SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3420 MVT VT3) {
3421 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003422 E = VTList.end(); I != E; ++I) {
3423 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3424 (*I)[2] == VT3)
3425 return makeVTList(&(*I)[0], 3);
3426 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003427 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003428 V.push_back(VT1);
3429 V.push_back(VT2);
3430 V.push_back(VT3);
3431 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003432 return makeVTList(&(*VTList.begin())[0], 3);
3433}
3434
Duncan Sands83ec4b62008-06-06 12:08:01 +00003435SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003436 switch (NumVTs) {
3437 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003438 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003439 case 2: return getVTList(VTs[0], VTs[1]);
3440 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3441 default: break;
3442 }
3443
Duncan Sands83ec4b62008-06-06 12:08:01 +00003444 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003445 E = VTList.end(); I != E; ++I) {
3446 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3447
3448 bool NoMatch = false;
3449 for (unsigned i = 2; i != NumVTs; ++i)
3450 if (VTs[i] != (*I)[i]) {
3451 NoMatch = true;
3452 break;
3453 }
3454 if (!NoMatch)
3455 return makeVTList(&*I->begin(), NumVTs);
3456 }
3457
Duncan Sands83ec4b62008-06-06 12:08:01 +00003458 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003459 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003460}
3461
3462
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003463/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3464/// specified operands. If the resultant node already exists in the DAG,
3465/// this does not modify the specified node, instead it returns the node that
3466/// already exists. If the resultant node does not exist in the DAG, the
3467/// input node is returned. As a degenerate case, if you specify the same
3468/// input operands as the node already has, the input node is returned.
3469SDOperand SelectionDAG::
3470UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3471 SDNode *N = InN.Val;
3472 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3473
3474 // Check to see if there is no change.
3475 if (Op == N->getOperand(0)) return InN;
3476
3477 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003478 void *InsertPos = 0;
3479 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3480 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003481
3482 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003483 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003484 RemoveNodeFromCSEMaps(N);
3485
3486 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003487 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003488 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003489 N->OperandList[0].setUser(N);
3490 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003491
3492 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003493 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003494 return InN;
3495}
3496
3497SDOperand SelectionDAG::
3498UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3499 SDNode *N = InN.Val;
3500 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3501
3502 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003503 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3504 return InN; // No operands changed, just return the input node.
3505
3506 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003507 void *InsertPos = 0;
3508 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3509 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003510
3511 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003512 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003513 RemoveNodeFromCSEMaps(N);
3514
3515 // Now we update the operands.
3516 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003517 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003518 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003519 N->OperandList[0].setUser(N);
3520 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003521 }
3522 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003523 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003524 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003525 N->OperandList[1].setUser(N);
3526 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003527 }
3528
3529 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003530 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003531 return InN;
3532}
3533
3534SDOperand SelectionDAG::
3535UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003536 SDOperand Ops[] = { Op1, Op2, Op3 };
3537 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003538}
3539
3540SDOperand SelectionDAG::
3541UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3542 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003543 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3544 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003545}
3546
3547SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003548UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3549 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003550 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3551 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003552}
3553
Chris Lattner809ec112006-01-28 10:09:25 +00003554SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003555UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003556 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003557 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003558 "Update with wrong number of operands");
3559
3560 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003561 bool AnyChange = false;
3562 for (unsigned i = 0; i != NumOps; ++i) {
3563 if (Ops[i] != N->getOperand(i)) {
3564 AnyChange = true;
3565 break;
3566 }
3567 }
3568
3569 // No operands changed, just return the input node.
3570 if (!AnyChange) return InN;
3571
3572 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003573 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003574 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003575 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003576
Dan Gohman7ceda162008-05-02 00:05:03 +00003577 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003578 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003579 RemoveNodeFromCSEMaps(N);
3580
3581 // Now we update the operands.
3582 for (unsigned i = 0; i != NumOps; ++i) {
3583 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003584 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003585 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003586 N->OperandList[i].setUser(N);
3587 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003588 }
3589 }
3590
3591 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003592 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003593 return InN;
3594}
3595
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003596/// MorphNodeTo - This frees the operands of the current node, resets the
3597/// opcode, types, and operands to the specified value. This should only be
3598/// used by the SelectionDAG class.
3599void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003600 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003601 NodeType = Opc;
3602 ValueList = L.VTs;
3603 NumValues = L.NumVTs;
3604
3605 // Clear the operands list, updating used nodes to remove this from their
3606 // use list.
3607 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003608 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003609
3610 // If NumOps is larger than the # of operands we currently have, reallocate
3611 // the operand list.
3612 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003613 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003614 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003615 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003616 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003617 OperandsNeedDelete = true;
3618 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003619
3620 // Assign the new operands.
3621 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003622
3623 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3624 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003625 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003626 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003627 N->addUser(i, this);
3628 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003629 }
3630}
Chris Lattner149c58c2005-08-16 18:17:10 +00003631
3632/// SelectNodeTo - These are used for target selectors to *mutate* the
3633/// specified node to have the specified return type, Target opcode, and
3634/// operands. Note that target opcodes are stored as
3635/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003636///
3637/// Note that SelectNodeTo returns the resultant node. If there is already a
3638/// node of the specified opcode and operands, it returns that node instead of
3639/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003640SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003641 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003642 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003643 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003644 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003645 void *IP = 0;
3646 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003647 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003648
Chris Lattner7651fa42005-08-24 23:00:29 +00003649 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003650
Dan Gohman35b31be2008-04-17 23:02:12 +00003651 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003652
Chris Lattner4a283e92006-08-11 18:38:11 +00003653 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003654 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003655}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003656
Evan Cheng95514ba2006-08-26 08:00:10 +00003657SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003658 MVT VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003659 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003660 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003661 SDOperand Ops[] = { Op1 };
3662
Jim Laskey583bd472006-10-27 23:46:08 +00003663 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003664 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003665 void *IP = 0;
3666 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003667 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003668
Chris Lattner149c58c2005-08-16 18:17:10 +00003669 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003670 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003671 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003672 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003673}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003674
Evan Cheng95514ba2006-08-26 08:00:10 +00003675SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003676 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003677 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003678 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003679 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003680 SDOperand Ops[] = { Op1, Op2 };
3681
Jim Laskey583bd472006-10-27 23:46:08 +00003682 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003683 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003684 void *IP = 0;
3685 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003686 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003687
Chris Lattner149c58c2005-08-16 18:17:10 +00003688 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003689
Chris Lattner63e3f142007-02-04 07:28:00 +00003690 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003691
Chris Lattnera5682852006-08-07 23:03:03 +00003692 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003693 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003694}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003695
Evan Cheng95514ba2006-08-26 08:00:10 +00003696SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003697 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003698 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003699 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003700 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003701 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003702 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003703 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003704 void *IP = 0;
3705 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003706 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003707
Chris Lattner149c58c2005-08-16 18:17:10 +00003708 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003709
Chris Lattner63e3f142007-02-04 07:28:00 +00003710 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003711
Chris Lattnera5682852006-08-07 23:03:03 +00003712 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003713 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003714}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003715
Evan Cheng95514ba2006-08-26 08:00:10 +00003716SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003717 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003718 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003719 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003720 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003721 FoldingSetNodeID ID;
3722 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003723 void *IP = 0;
3724 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003725 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003726
Chris Lattner6b09a292005-08-21 18:49:33 +00003727 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003728 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003729
Chris Lattnera5682852006-08-07 23:03:03 +00003730 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003731 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003732}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003733
Evan Cheng95514ba2006-08-26 08:00:10 +00003734SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003735 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003736 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003737 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003738 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003739 SDOperand Ops[] = { Op1, Op2 };
3740 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003741 void *IP = 0;
3742 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003743 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003744
Chris Lattner0fb094f2005-11-19 01:44:53 +00003745 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003746 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003747 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003748 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003749}
3750
Evan Cheng95514ba2006-08-26 08:00:10 +00003751SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003752 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003753 SDOperand Op1, SDOperand Op2,
3754 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003755 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003756 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003757 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003758 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003759 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003760 void *IP = 0;
3761 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003762 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003763
Chris Lattner0fb094f2005-11-19 01:44:53 +00003764 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003765
Chris Lattner63e3f142007-02-04 07:28:00 +00003766 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003767 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003768 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003769}
3770
Chris Lattner0fb094f2005-11-19 01:44:53 +00003771
Evan Cheng6ae46c42006-02-09 07:15:23 +00003772/// getTargetNode - These are used for target selectors to create a new node
3773/// with specified return type(s), target opcode, and operands.
3774///
3775/// Note that getTargetNode returns the resultant node. If there is already a
3776/// node of the specified opcode and operands, it returns that node instead of
3777/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003778SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003779 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3780}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003781SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003782 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3783}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003784SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003785 SDOperand Op1, SDOperand Op2) {
3786 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3787}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003789 SDOperand Op1, SDOperand Op2,
3790 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003791 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3792}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003793SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003794 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003795 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003796}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003797SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3798 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003799 SDOperand Op;
3800 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3801}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003802SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3803 MVT VT2, SDOperand Op1) {
3804 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003805 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003806}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3808 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003809 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003810 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003811 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003812 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003813}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3815 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003816 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003818 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003819 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003820}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003821SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003822 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003823 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003824 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003825}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003826SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003827 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003828 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003829 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003830 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003831}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003833 SDOperand Op1, SDOperand Op2,
3834 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003836 SDOperand Ops[] = { Op1, Op2, Op3 };
3837 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3838}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003839SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003840 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003841 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003842 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003843}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003844SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3845 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003846 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003848 VTList.push_back(VT1);
3849 VTList.push_back(VT2);
3850 VTList.push_back(VT3);
3851 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003853 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3854}
Evan Cheng39305cf2007-10-05 01:10:49 +00003855SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003856 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003857 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003858 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003859 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3860 Ops, NumOps).Val;
3861}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003862
Evan Cheng08b11732008-03-22 01:55:50 +00003863/// getNodeIfExists - Get the specified node if it's already available, or
3864/// else return NULL.
3865SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003866 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003867 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3868 FoldingSetNodeID ID;
3869 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3870 void *IP = 0;
3871 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3872 return E;
3873 }
3874 return NULL;
3875}
3876
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003877
Evan Cheng99157a02006-08-07 22:13:29 +00003878/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003879/// This can cause recursive merging of nodes in the DAG.
3880///
Chris Lattner11d049c2008-02-03 03:35:22 +00003881/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003882///
Chris Lattner11d049c2008-02-03 03:35:22 +00003883void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003884 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003885 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003886 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003887 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003888 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003889
Chris Lattner8b8749f2005-08-17 19:00:20 +00003890 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003891 SDNode::use_iterator UI = From->use_begin();
3892 SDNode *U = UI->getUser();
3893
Chris Lattner8b8749f2005-08-17 19:00:20 +00003894 // This node is about to morph, remove its old self from the CSE maps.
3895 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003896 int operandNum = 0;
3897 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3898 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003899 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003900 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003901 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003902 I->setUser(U);
3903 To.Val->addUser(operandNum, U);
3904 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003905
3906 // Now that we have modified U, add it back to the CSE maps. If it already
3907 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003908 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003909 ReplaceAllUsesWith(U, Existing, UpdateListener);
3910 // U is now dead. Inform the listener if it exists and delete it.
3911 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003912 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003913 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003914 } else {
3915 // If the node doesn't already exist, we updated it. Inform a listener if
3916 // it exists.
3917 if (UpdateListener)
3918 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003919 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003920 }
3921}
3922
3923/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3924/// This can cause recursive merging of nodes in the DAG.
3925///
3926/// This version assumes From/To have matching types and numbers of result
3927/// values.
3928///
Chris Lattner1e111c72005-09-07 05:37:01 +00003929void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003930 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003931 assert(From != To && "Cannot replace uses of with self");
3932 assert(From->getNumValues() == To->getNumValues() &&
3933 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003934 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003935 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3936 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003937
3938 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003939 SDNode::use_iterator UI = From->use_begin();
3940 SDNode *U = UI->getUser();
3941
Chris Lattner8b52f212005-08-26 18:36:28 +00003942 // This node is about to morph, remove its old self from the CSE maps.
3943 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003944 int operandNum = 0;
3945 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3946 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003947 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003948 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003949 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003950 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003951 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003952
Chris Lattner8b8749f2005-08-17 19:00:20 +00003953 // Now that we have modified U, add it back to the CSE maps. If it already
3954 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003955 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003956 ReplaceAllUsesWith(U, Existing, UpdateListener);
3957 // U is now dead. Inform the listener if it exists and delete it.
3958 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003959 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003960 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003961 } else {
3962 // If the node doesn't already exist, we updated it. Inform a listener if
3963 // it exists.
3964 if (UpdateListener)
3965 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003966 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003967 }
3968}
3969
Chris Lattner8b52f212005-08-26 18:36:28 +00003970/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3971/// This can cause recursive merging of nodes in the DAG.
3972///
3973/// This version can replace From with any result values. To must match the
3974/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003975void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003976 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003977 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003978 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003979 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003980
3981 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003982 SDNode::use_iterator UI = From->use_begin();
3983 SDNode *U = UI->getUser();
3984
Chris Lattner7b2880c2005-08-24 22:44:39 +00003985 // This node is about to morph, remove its old self from the CSE maps.
3986 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003987 int operandNum = 0;
3988 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3989 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003990 if (I->getVal() == From) {
3991 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003992 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003993 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003994 I->setUser(U);
3995 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003996 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003997
Chris Lattner7b2880c2005-08-24 22:44:39 +00003998 // Now that we have modified U, add it back to the CSE maps. If it already
3999 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004000 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004001 ReplaceAllUsesWith(U, Existing, UpdateListener);
4002 // U is now dead. Inform the listener if it exists and delete it.
4003 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004004 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004005 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004006 } else {
4007 // If the node doesn't already exist, we updated it. Inform a listener if
4008 // it exists.
4009 if (UpdateListener)
4010 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004011 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004012 }
4013}
4014
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004015namespace {
4016 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4017 /// any deleted nodes from the set passed into its constructor and recursively
4018 /// notifies another update listener if specified.
4019 class ChainedSetUpdaterListener :
4020 public SelectionDAG::DAGUpdateListener {
4021 SmallSetVector<SDNode*, 16> &Set;
4022 SelectionDAG::DAGUpdateListener *Chain;
4023 public:
4024 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4025 SelectionDAG::DAGUpdateListener *chain)
4026 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004027
Duncan Sandsedfcf592008-06-11 11:42:12 +00004028 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004029 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004030 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004031 }
4032 virtual void NodeUpdated(SDNode *N) {
4033 if (Chain) Chain->NodeUpdated(N);
4034 }
4035 };
4036}
4037
Chris Lattner012f2412006-02-17 21:58:01 +00004038/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4039/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004040/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004041void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004042 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004043 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004044
Chris Lattner012f2412006-02-17 21:58:01 +00004045 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004046 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004047 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004048 return;
4049 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004050
4051 if (From.use_empty()) return;
4052
Chris Lattnercf5640b2007-02-04 00:14:31 +00004053 // Get all of the users of From.Val. We want these in a nice,
4054 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004055 SmallSetVector<SDNode*, 16> Users;
4056 for (SDNode::use_iterator UI = From.Val->use_begin(),
4057 E = From.Val->use_end(); UI != E; ++UI) {
4058 SDNode *User = UI->getUser();
4059 if (!Users.count(User))
4060 Users.insert(User);
4061 }
Chris Lattner012f2412006-02-17 21:58:01 +00004062
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004063 // When one of the recursive merges deletes nodes from the graph, we need to
4064 // make sure that UpdateListener is notified *and* that the node is removed
4065 // from Users if present. CSUL does this.
4066 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004067
Chris Lattner012f2412006-02-17 21:58:01 +00004068 while (!Users.empty()) {
4069 // We know that this user uses some value of From. If it is the right
4070 // value, update it.
4071 SDNode *User = Users.back();
4072 Users.pop_back();
4073
Chris Lattner01d029b2007-10-15 06:10:22 +00004074 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004075 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004076 for (; Op != E; ++Op)
4077 if (*Op == From) break;
4078
4079 // If there are no matches, the user must use some other result of From.
4080 if (Op == E) continue;
4081
4082 // Okay, we know this user needs to be updated. Remove its old self
4083 // from the CSE maps.
4084 RemoveNodeFromCSEMaps(User);
4085
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004086 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004087 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004088 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004089 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004090 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004091 Op->setUser(User);
4092 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004093 }
4094 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004095
4096 // Now that we have modified User, add it back to the CSE maps. If it
4097 // already exists there, recursively merge the results together.
4098 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004099 if (!Existing) {
4100 if (UpdateListener) UpdateListener->NodeUpdated(User);
4101 continue; // Continue on to next user.
4102 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004103
4104 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004105 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004106 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004107 // can cause deletion of nodes that used the old value. To handle this, we
4108 // use CSUL to remove them from the Users set.
4109 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004110
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004111 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004112 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004113 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004114 }
4115}
4116
Evan Chenge6f35d82006-08-01 08:20:41 +00004117/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4118/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004119unsigned SelectionDAG::AssignNodeIds() {
4120 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004121 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4122 SDNode *N = I;
4123 N->setNodeId(Id++);
4124 }
4125 return Id;
4126}
4127
Evan Chenge6f35d82006-08-01 08:20:41 +00004128/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004129/// based on their topological order. It returns the maximum id and a vector
4130/// of the SDNodes* in assigned order by reference.
4131unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004132 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004133 std::vector<unsigned> InDegree(DAGSize);
4134 std::vector<SDNode*> Sources;
4135
4136 // Use a two pass approach to avoid using a std::map which is slow.
4137 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004138 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4139 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004140 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004141 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004142 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004143 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004144 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004145 }
4146
Evan Cheng99157a02006-08-07 22:13:29 +00004147 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004148 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004149 SDNode *N = Sources.back();
4150 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004151 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004152 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004153 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004154 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004155 if (Degree == 0)
4156 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004157 }
4158 }
4159
Evan Chengc384d6c2006-08-02 22:00:34 +00004160 // Second pass, assign the actual topological order as node ids.
4161 Id = 0;
4162 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4163 TI != TE; ++TI)
4164 (*TI)->setNodeId(Id++);
4165
4166 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004167}
4168
4169
Evan Cheng091cba12006-07-27 06:39:06 +00004170
Jim Laskey58b968b2005-08-17 20:08:02 +00004171//===----------------------------------------------------------------------===//
4172// SDNode Class
4173//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004174
Chris Lattner917d2c92006-07-19 00:00:37 +00004175// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004176void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004177void UnarySDNode::ANCHOR() {}
4178void BinarySDNode::ANCHOR() {}
4179void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004180void HandleSDNode::ANCHOR() {}
4181void StringSDNode::ANCHOR() {}
4182void ConstantSDNode::ANCHOR() {}
4183void ConstantFPSDNode::ANCHOR() {}
4184void GlobalAddressSDNode::ANCHOR() {}
4185void FrameIndexSDNode::ANCHOR() {}
4186void JumpTableSDNode::ANCHOR() {}
4187void ConstantPoolSDNode::ANCHOR() {}
4188void BasicBlockSDNode::ANCHOR() {}
4189void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004190void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004191void RegisterSDNode::ANCHOR() {}
4192void ExternalSymbolSDNode::ANCHOR() {}
4193void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004194void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004195void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004196void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004197void LoadSDNode::ANCHOR() {}
4198void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004199void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004200
Chris Lattner48b85922007-02-04 02:41:42 +00004201HandleSDNode::~HandleSDNode() {
4202 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004203 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004204}
4205
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004206GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004207 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004208 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004209 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004210 // Thread Local
4211 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4212 // Non Thread Local
4213 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4214 getSDVTList(VT)), Offset(o) {
4215 TheGlobal = const_cast<GlobalValue*>(GA);
4216}
Chris Lattner48b85922007-02-04 02:41:42 +00004217
Dan Gohman36b5c132008-04-07 19:35:22 +00004218/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004219/// reference performed by this atomic.
4220MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004221 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004222 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4223 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4224
4225 // Check if the atomic references a frame index
4226 const FrameIndexSDNode *FI =
4227 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4228 if (!getSrcValue() && FI)
4229 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4230 FI->getIndex(), Size, getAlignment());
4231 else
4232 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4233 Size, getAlignment());
4234}
4235
4236/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004237/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004238MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004239 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004240 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004241 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4242 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004243 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004244
4245 // Check if the load references a frame index, and does not have
4246 // an SV attached.
4247 const FrameIndexSDNode *FI =
4248 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4249 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004250 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004251 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004252 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004253 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004254 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004255}
4256
Jim Laskey583bd472006-10-27 23:46:08 +00004257/// Profile - Gather unique data for the node.
4258///
4259void SDNode::Profile(FoldingSetNodeID &ID) {
4260 AddNodeIDNode(ID, this);
4261}
4262
Chris Lattnera3255112005-11-08 23:30:28 +00004263/// getValueTypeList - Return a pointer to the specified value type.
4264///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265const MVT *SDNode::getValueTypeList(MVT VT) {
4266 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004267 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004268 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004269 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004270 static MVT VTs[MVT::LAST_VALUETYPE];
4271 VTs[VT.getSimpleVT()] = VT;
4272 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004273 }
Chris Lattnera3255112005-11-08 23:30:28 +00004274}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004275
Chris Lattner5c884562005-01-12 18:37:47 +00004276/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4277/// indicated value. This method ignores uses of other values defined by this
4278/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004279bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004280 assert(Value < getNumValues() && "Bad value!");
4281
4282 // If there is only one value, this is easy.
4283 if (getNumValues() == 1)
4284 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004285 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004286
Evan Cheng4ee62112006-02-05 06:29:23 +00004287 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004288
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004289 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004290
Roman Levensteindc1adac2008-04-07 10:06:32 +00004291 // TODO: Only iterate over uses of a given value of the node
4292 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4293 if (*UI == TheValue) {
4294 if (NUses == 0)
4295 return false;
4296 --NUses;
4297 }
Chris Lattner5c884562005-01-12 18:37:47 +00004298 }
4299
4300 // Found exactly the right number of uses?
4301 return NUses == 0;
4302}
4303
4304
Evan Cheng33d55952007-08-02 05:29:38 +00004305/// hasAnyUseOfValue - Return true if there are any use of the indicated
4306/// value. This method ignores uses of other values defined by this operation.
4307bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4308 assert(Value < getNumValues() && "Bad value!");
4309
Dan Gohman30359592008-01-29 13:02:09 +00004310 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004311
4312 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4313
4314 SmallPtrSet<SDNode*, 32> UsersHandled;
4315
Roman Levensteindc1adac2008-04-07 10:06:32 +00004316 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4317 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004318 if (User->getNumOperands() == 1 ||
4319 UsersHandled.insert(User)) // First time we've seen this?
4320 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4321 if (User->getOperand(i) == TheValue) {
4322 return true;
4323 }
4324 }
4325
4326 return false;
4327}
4328
4329
Evan Cheng917be682008-03-04 00:41:45 +00004330/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004331///
Evan Cheng917be682008-03-04 00:41:45 +00004332bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004333 bool Seen = false;
4334 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004335 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004336 if (User == this)
4337 Seen = true;
4338 else
4339 return false;
4340 }
4341
4342 return Seen;
4343}
4344
Evan Chenge6e97e62006-11-03 07:31:32 +00004345/// isOperand - Return true if this node is an operand of N.
4346///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004347bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004348 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4349 if (*this == N->getOperand(i))
4350 return true;
4351 return false;
4352}
4353
Evan Cheng917be682008-03-04 00:41:45 +00004354bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004355 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004356 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004357 return true;
4358 return false;
4359}
Evan Cheng4ee62112006-02-05 06:29:23 +00004360
Chris Lattner572dee72008-01-16 05:49:24 +00004361/// reachesChainWithoutSideEffects - Return true if this operand (which must
4362/// be a chain) reaches the specified operand without crossing any
4363/// side-effecting instructions. In practice, this looks through token
4364/// factors and non-volatile loads. In order to remain efficient, this only
4365/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004366bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004367 unsigned Depth) const {
4368 if (*this == Dest) return true;
4369
4370 // Don't search too deeply, we just want to be able to see through
4371 // TokenFactor's etc.
4372 if (Depth == 0) return false;
4373
4374 // If this is a token factor, all inputs to the TF happen in parallel. If any
4375 // of the operands of the TF reach dest, then we can do the xform.
4376 if (getOpcode() == ISD::TokenFactor) {
4377 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4378 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4379 return true;
4380 return false;
4381 }
4382
4383 // Loads don't have side effects, look through them.
4384 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4385 if (!Ld->isVolatile())
4386 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4387 }
4388 return false;
4389}
4390
4391
Evan Chengc5fc57d2006-11-03 03:05:24 +00004392static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004393 SmallPtrSet<SDNode *, 32> &Visited) {
4394 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004395 return;
4396
4397 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4398 SDNode *Op = N->getOperand(i).Val;
4399 if (Op == P) {
4400 found = true;
4401 return;
4402 }
4403 findPredecessor(Op, P, found, Visited);
4404 }
4405}
4406
Evan Cheng917be682008-03-04 00:41:45 +00004407/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004408/// is either an operand of N or it can be reached by recursively traversing
4409/// up the operands.
4410/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004411bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004412 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004413 bool found = false;
4414 findPredecessor(N, this, found, Visited);
4415 return found;
4416}
4417
Evan Chengc5484282006-10-04 00:56:09 +00004418uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4419 assert(Num < NumOperands && "Invalid child # of SDNode!");
4420 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4421}
4422
Reid Spencer577cc322007-04-01 07:32:19 +00004423std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004424 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004425 default:
4426 if (getOpcode() < ISD::BUILTIN_OP_END)
4427 return "<<Unknown DAG Node>>";
4428 else {
Evan Cheng72261582005-12-20 06:22:03 +00004429 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004430 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004431 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004432 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004433
Evan Cheng72261582005-12-20 06:22:03 +00004434 TargetLowering &TLI = G->getTargetLoweringInfo();
4435 const char *Name =
4436 TLI.getTargetNodeName(getOpcode());
4437 if (Name) return Name;
4438 }
4439
4440 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004441 }
4442
Evan Cheng27b7db52008-03-08 00:58:38 +00004443 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004444 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004445 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4446 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4447 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004448 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4449 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4450 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004451 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004452 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4453 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4454 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4455 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4456 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004457 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004458 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004459 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004460 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004461 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004462 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004463 case ISD::AssertSext: return "AssertSext";
4464 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004465
4466 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004467 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004468 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004469 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004470 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004471
4472 case ISD::Constant: return "Constant";
4473 case ISD::ConstantFP: return "ConstantFP";
4474 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004475 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004476 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004477 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004478 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004479 case ISD::RETURNADDR: return "RETURNADDR";
4480 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004481 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004482 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4483 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004484 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004485 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004486 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004487 case ISD::INTRINSIC_WO_CHAIN: {
4488 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4489 return Intrinsic::getName((Intrinsic::ID)IID);
4490 }
4491 case ISD::INTRINSIC_VOID:
4492 case ISD::INTRINSIC_W_CHAIN: {
4493 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004494 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004495 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004496
Chris Lattnerb2827b02006-03-19 00:52:58 +00004497 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004498 case ISD::TargetConstant: return "TargetConstant";
4499 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004500 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004501 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004502 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004503 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004504 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004505 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004506
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004507 case ISD::CopyToReg: return "CopyToReg";
4508 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004509 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004510 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004511 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004512 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004513 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004514 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004515 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004516 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004517
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004518 // Unary operators
4519 case ISD::FABS: return "fabs";
4520 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004521 case ISD::FSQRT: return "fsqrt";
4522 case ISD::FSIN: return "fsin";
4523 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004524 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004525 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004526
4527 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004528 case ISD::ADD: return "add";
4529 case ISD::SUB: return "sub";
4530 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004531 case ISD::MULHU: return "mulhu";
4532 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004533 case ISD::SDIV: return "sdiv";
4534 case ISD::UDIV: return "udiv";
4535 case ISD::SREM: return "srem";
4536 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004537 case ISD::SMUL_LOHI: return "smul_lohi";
4538 case ISD::UMUL_LOHI: return "umul_lohi";
4539 case ISD::SDIVREM: return "sdivrem";
4540 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004541 case ISD::AND: return "and";
4542 case ISD::OR: return "or";
4543 case ISD::XOR: return "xor";
4544 case ISD::SHL: return "shl";
4545 case ISD::SRA: return "sra";
4546 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004547 case ISD::ROTL: return "rotl";
4548 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004549 case ISD::FADD: return "fadd";
4550 case ISD::FSUB: return "fsub";
4551 case ISD::FMUL: return "fmul";
4552 case ISD::FDIV: return "fdiv";
4553 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004554 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004555 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004556
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004557 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004558 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004559 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004560 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004561 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004562 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004563 case ISD::CONCAT_VECTORS: return "concat_vectors";
4564 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004565 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004566 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004567 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004568 case ISD::ADDC: return "addc";
4569 case ISD::ADDE: return "adde";
4570 case ISD::SUBC: return "subc";
4571 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004572 case ISD::SHL_PARTS: return "shl_parts";
4573 case ISD::SRA_PARTS: return "sra_parts";
4574 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004575
4576 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4577 case ISD::INSERT_SUBREG: return "insert_subreg";
4578
Chris Lattner7f644642005-04-28 21:44:03 +00004579 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004580 case ISD::SIGN_EXTEND: return "sign_extend";
4581 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004582 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004583 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004584 case ISD::TRUNCATE: return "truncate";
4585 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004586 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004587 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004588 case ISD::FP_EXTEND: return "fp_extend";
4589
4590 case ISD::SINT_TO_FP: return "sint_to_fp";
4591 case ISD::UINT_TO_FP: return "uint_to_fp";
4592 case ISD::FP_TO_SINT: return "fp_to_sint";
4593 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004594 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004595
4596 // Control flow instructions
4597 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004598 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004599 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004600 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004601 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004602 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004603 case ISD::CALLSEQ_START: return "callseq_start";
4604 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004605
4606 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004607 case ISD::LOAD: return "load";
4608 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004609 case ISD::VAARG: return "vaarg";
4610 case ISD::VACOPY: return "vacopy";
4611 case ISD::VAEND: return "vaend";
4612 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004613 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004614 case ISD::EXTRACT_ELEMENT: return "extract_element";
4615 case ISD::BUILD_PAIR: return "build_pair";
4616 case ISD::STACKSAVE: return "stacksave";
4617 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004618 case ISD::TRAP: return "trap";
4619
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004620 // Bit manipulation
4621 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004622 case ISD::CTPOP: return "ctpop";
4623 case ISD::CTTZ: return "cttz";
4624 case ISD::CTLZ: return "ctlz";
4625
Chris Lattner36ce6912005-11-29 06:21:05 +00004626 // Debug info
4627 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004628 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004629
Duncan Sands36397f52007-07-27 12:58:54 +00004630 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004631 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004632
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004633 case ISD::CONDCODE:
4634 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004635 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004636 case ISD::SETOEQ: return "setoeq";
4637 case ISD::SETOGT: return "setogt";
4638 case ISD::SETOGE: return "setoge";
4639 case ISD::SETOLT: return "setolt";
4640 case ISD::SETOLE: return "setole";
4641 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004642
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004643 case ISD::SETO: return "seto";
4644 case ISD::SETUO: return "setuo";
4645 case ISD::SETUEQ: return "setue";
4646 case ISD::SETUGT: return "setugt";
4647 case ISD::SETUGE: return "setuge";
4648 case ISD::SETULT: return "setult";
4649 case ISD::SETULE: return "setule";
4650 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004651
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004652 case ISD::SETEQ: return "seteq";
4653 case ISD::SETGT: return "setgt";
4654 case ISD::SETGE: return "setge";
4655 case ISD::SETLT: return "setlt";
4656 case ISD::SETLE: return "setle";
4657 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004658 }
4659 }
4660}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004661
Evan Cheng144d8f02006-11-09 17:55:04 +00004662const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004663 switch (AM) {
4664 default:
4665 return "";
4666 case ISD::PRE_INC:
4667 return "<pre-inc>";
4668 case ISD::PRE_DEC:
4669 return "<pre-dec>";
4670 case ISD::POST_INC:
4671 return "<post-inc>";
4672 case ISD::POST_DEC:
4673 return "<post-dec>";
4674 }
4675}
4676
Duncan Sands276dcbd2008-03-21 09:14:45 +00004677std::string ISD::ArgFlagsTy::getArgFlagsString() {
4678 std::string S = "< ";
4679
4680 if (isZExt())
4681 S += "zext ";
4682 if (isSExt())
4683 S += "sext ";
4684 if (isInReg())
4685 S += "inreg ";
4686 if (isSRet())
4687 S += "sret ";
4688 if (isByVal())
4689 S += "byval ";
4690 if (isNest())
4691 S += "nest ";
4692 if (getByValAlign())
4693 S += "byval-align:" + utostr(getByValAlign()) + " ";
4694 if (getOrigAlign())
4695 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4696 if (getByValSize())
4697 S += "byval-size:" + utostr(getByValSize()) + " ";
4698 return S + ">";
4699}
4700
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004701void SDNode::dump() const { dump(0); }
4702void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004703 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004704
4705 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004706 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004707 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004708 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004709 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004710 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004711 }
Bill Wendling832171c2006-12-07 20:04:42 +00004712 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004713
Bill Wendling832171c2006-12-07 20:04:42 +00004714 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004715 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004716 if (i) cerr << ", ";
4717 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004718 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004719 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004720 }
4721
Evan Chengce254432007-12-11 02:08:35 +00004722 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4723 SDNode *Mask = getOperand(2).Val;
4724 cerr << "<";
4725 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4726 if (i) cerr << ",";
4727 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4728 cerr << "u";
4729 else
4730 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4731 }
4732 cerr << ">";
4733 }
4734
Chris Lattnerc3aae252005-01-07 07:46:32 +00004735 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004736 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004737 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004738 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4739 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4740 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4741 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4742 else {
4743 cerr << "<APFloat(";
4744 CSDN->getValueAPF().convertToAPInt().dump();
4745 cerr << ")>";
4746 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004747 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004748 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004749 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004750 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004751 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004752 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004753 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004754 else
Bill Wendling832171c2006-12-07 20:04:42 +00004755 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004756 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004757 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004758 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004759 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004760 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004761 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004762 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004763 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004764 else
Bill Wendling832171c2006-12-07 20:04:42 +00004765 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004766 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004767 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004768 else
Bill Wendling832171c2006-12-07 20:04:42 +00004769 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004770 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004771 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004772 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4773 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004774 cerr << LBB->getName() << " ";
4775 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004776 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004777 if (G && R->getReg() &&
4778 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004779 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004780 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004781 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004782 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004783 } else if (const ExternalSymbolSDNode *ES =
4784 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004785 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004786 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4787 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004788 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004789 else
Dan Gohman69de1932008-02-06 22:27:42 +00004790 cerr << "<null>";
4791 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4792 if (M->MO.getValue())
4793 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4794 else
4795 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004796 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4797 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004798 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004799 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004800 }
4801 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004802 const Value *SrcValue = LD->getSrcValue();
4803 int SrcOffset = LD->getSrcValueOffset();
4804 cerr << " <";
4805 if (SrcValue)
4806 cerr << SrcValue;
4807 else
4808 cerr << "null";
4809 cerr << ":" << SrcOffset << ">";
4810
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004811 bool doExt = true;
4812 switch (LD->getExtensionType()) {
4813 default: doExt = false; break;
4814 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004815 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004816 break;
4817 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004818 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004819 break;
4820 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004821 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004822 break;
4823 }
4824 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004825 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004826
Evan Cheng144d8f02006-11-09 17:55:04 +00004827 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004828 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004829 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004830 if (LD->isVolatile())
4831 cerr << " <volatile>";
4832 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004833 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004834 const Value *SrcValue = ST->getSrcValue();
4835 int SrcOffset = ST->getSrcValueOffset();
4836 cerr << " <";
4837 if (SrcValue)
4838 cerr << SrcValue;
4839 else
4840 cerr << "null";
4841 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004842
4843 if (ST->isTruncatingStore())
4844 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004845 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004846
4847 const char *AM = getIndexedModeName(ST->getAddressingMode());
4848 if (*AM)
4849 cerr << " " << AM;
4850 if (ST->isVolatile())
4851 cerr << " <volatile>";
4852 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004853 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4854 const Value *SrcValue = AT->getSrcValue();
4855 int SrcOffset = AT->getSrcValueOffset();
4856 cerr << " <";
4857 if (SrcValue)
4858 cerr << SrcValue;
4859 else
4860 cerr << "null";
4861 cerr << ":" << SrcOffset << ">";
4862 if (AT->isVolatile())
4863 cerr << " <volatile>";
4864 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004865 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004866}
4867
Chris Lattnerde202b32005-11-09 23:47:37 +00004868static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004869 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4870 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004871 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004872 else
Bill Wendling832171c2006-12-07 20:04:42 +00004873 cerr << "\n" << std::string(indent+2, ' ')
4874 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004875
Chris Lattnerea946cd2005-01-09 20:38:33 +00004876
Bill Wendling832171c2006-12-07 20:04:42 +00004877 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004878 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004879}
4880
Chris Lattnerc3aae252005-01-07 07:46:32 +00004881void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004882 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004883 std::vector<const SDNode*> Nodes;
4884 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4885 I != E; ++I)
4886 Nodes.push_back(I);
4887
Chris Lattner49d24712005-01-09 20:26:36 +00004888 std::sort(Nodes.begin(), Nodes.end());
4889
4890 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004891 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004892 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004893 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004894
Jim Laskey26f7fa72006-10-17 19:33:52 +00004895 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004896
Bill Wendling832171c2006-12-07 20:04:42 +00004897 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004898}
4899
Evan Chengd6594ae2006-09-12 21:00:35 +00004900const Type *ConstantPoolSDNode::getType() const {
4901 if (isMachineConstantPoolEntry())
4902 return Val.MachineCPVal->getType();
4903 return Val.ConstVal->getType();
4904}