blob: b3acc9a39ecb24f477a532f7a3a741650b2e37c4 [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"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000015#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000020#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000026#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000032#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000033#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000034#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000035#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000036#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000037#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000038#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner0b3e5252006-08-15 19:11:05 +000041/// makeVTList - Return an instance of the SDVTList struct initialized with the
42/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000043static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000044 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Duncan Sands83ec4b62008-06-06 12:08:01 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
49 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000050 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
56 }
57}
58
Chris Lattnerf8dc0612008-02-03 06:49:24 +000059SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000069bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000070 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000071}
72
Duncan Sands83ec4b62008-06-06 12:08:01 +000073bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000074 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000075 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000076
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000077 // PPC long double cannot be converted to any other type.
78 if (VT == MVT::ppcf128 ||
79 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000080 return false;
81
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000084 return Val2.convert(*MVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000086}
87
Jim Laskey58b968b2005-08-17 20:08:02 +000088//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000089// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000091
Evan Chenga8df1662006-03-27 06:58:47 +000092/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000093/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000094bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000095 // Look through a bit convert.
96 if (N->getOpcode() == ISD::BIT_CONVERT)
97 N = N->getOperand(0).Val;
98
Evan Chenga8df1662006-03-27 06:58:47 +000099 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000100
101 unsigned i = 0, e = N->getNumOperands();
102
103 // Skip over all of the undef values.
104 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
105 ++i;
106
107 // Do not accept an all-undef vector.
108 if (i == e) return false;
109
110 // Do not accept build_vectors that aren't all constants or which have non-~0
111 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000112 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000113 if (isa<ConstantSDNode>(NotZero)) {
114 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
115 return false;
116 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000117 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
118 convertToAPInt().isAllOnesValue())
119 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000120 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000121 return false;
122
123 // Okay, we have at least one ~0 value, check to see if the rest match or are
124 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000125 for (++i; i != e; ++i)
126 if (N->getOperand(i) != NotZero &&
127 N->getOperand(i).getOpcode() != ISD::UNDEF)
128 return false;
129 return true;
130}
131
132
Evan Cheng4a147842006-03-26 09:50:58 +0000133/// isBuildVectorAllZeros - Return true if the specified node is a
134/// BUILD_VECTOR where all of the elements are 0 or undef.
135bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000136 // Look through a bit convert.
137 if (N->getOpcode() == ISD::BIT_CONVERT)
138 N = N->getOperand(0).Val;
139
Evan Cheng4a147842006-03-26 09:50:58 +0000140 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000141
142 unsigned i = 0, e = N->getNumOperands();
143
144 // Skip over all of the undef values.
145 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
146 ++i;
147
148 // Do not accept an all-undef vector.
149 if (i == e) return false;
150
151 // Do not accept build_vectors that aren't all constants or which have non-~0
152 // elements.
153 SDOperand Zero = N->getOperand(i);
154 if (isa<ConstantSDNode>(Zero)) {
155 if (!cast<ConstantSDNode>(Zero)->isNullValue())
156 return false;
157 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000158 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000159 return false;
160 } else
161 return false;
162
163 // Okay, we have at least one ~0 value, check to see if the rest match or are
164 // undefs.
165 for (++i; i != e; ++i)
166 if (N->getOperand(i) != Zero &&
167 N->getOperand(i).getOpcode() != ISD::UNDEF)
168 return false;
169 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000170}
171
Evan Chengefec7512008-02-18 23:04:32 +0000172/// isScalarToVector - Return true if the specified node is a
173/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
174/// element is not an undef.
175bool ISD::isScalarToVector(const SDNode *N) {
176 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
177 return true;
178
179 if (N->getOpcode() != ISD::BUILD_VECTOR)
180 return false;
181 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
182 return false;
183 unsigned NumElems = N->getNumOperands();
184 for (unsigned i = 1; i < NumElems; ++i) {
185 SDOperand V = N->getOperand(i);
186 if (V.getOpcode() != ISD::UNDEF)
187 return false;
188 }
189 return true;
190}
191
192
Evan Chengbb81d972008-01-31 09:59:15 +0000193/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000194/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// is 0).
196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
198 if (N->getOpcode() == ISD::LABEL)
199 Zero = N->getOperand(2);
200 else if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::LABEL)
202 // Chain moved to last operand.
203 Zero = N->getOperand(1);
204 else
205 return false;
206 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
207}
208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
210/// when given the operation for (X op Y).
211ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
212 // To perform this operation, we just need to swap the L and G bits of the
213 // operation.
214 unsigned OldL = (Operation >> 2) & 1;
215 unsigned OldG = (Operation >> 1) & 1;
216 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
217 (OldL << 1) | // New G bit
218 (OldG << 2)); // New L bit.
219}
220
221/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
222/// 'op' is a valid SetCC operation.
223ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
224 unsigned Operation = Op;
225 if (isInteger)
226 Operation ^= 7; // Flip L, G, E bits, but not U.
227 else
228 Operation ^= 15; // Flip all of the condition bits.
229 if (Operation > ISD::SETTRUE2)
230 Operation &= ~8; // Don't let N and U bits get set.
231 return ISD::CondCode(Operation);
232}
233
234
235/// isSignedOp - For an integer comparison, return 1 if the comparison is a
236/// signed operation and 2 if the result is an unsigned comparison. Return zero
237/// if the operation does not depend on the sign of the input (setne and seteq).
238static int isSignedOp(ISD::CondCode Opcode) {
239 switch (Opcode) {
240 default: assert(0 && "Illegal integer setcc operation!");
241 case ISD::SETEQ:
242 case ISD::SETNE: return 0;
243 case ISD::SETLT:
244 case ISD::SETLE:
245 case ISD::SETGT:
246 case ISD::SETGE: return 1;
247 case ISD::SETULT:
248 case ISD::SETULE:
249 case ISD::SETUGT:
250 case ISD::SETUGE: return 2;
251 }
252}
253
254/// getSetCCOrOperation - Return the result of a logical OR between different
255/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
256/// returns SETCC_INVALID if it is not possible to represent the resultant
257/// comparison.
258ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
259 bool isInteger) {
260 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
261 // Cannot fold a signed integer setcc with an unsigned integer setcc.
262 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000265
Chris Lattnerc3aae252005-01-07 07:46:32 +0000266 // If the N and U bits get set then the resultant comparison DOES suddenly
267 // care about orderedness, and is true when ordered.
268 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000269 Op &= ~16; // Clear the U bit if the N bit is set.
270
271 // Canonicalize illegal integer setcc's.
272 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
273 Op = ISD::SETNE;
274
Chris Lattnerc3aae252005-01-07 07:46:32 +0000275 return ISD::CondCode(Op);
276}
277
278/// getSetCCAndOperation - Return the result of a logical AND between different
279/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
280/// function returns zero if it is not possible to represent the resultant
281/// comparison.
282ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
283 bool isInteger) {
284 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
285 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000286 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000287
288 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000289 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
290
291 // Canonicalize illegal integer setcc's.
292 if (isInteger) {
293 switch (Result) {
294 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000296 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000297 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
298 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
299 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000300 }
301 }
302
303 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000304}
305
Chris Lattnerb48da392005-01-23 04:39:44 +0000306const TargetMachine &SelectionDAG::getTarget() const {
307 return TLI.getTargetMachine();
308}
309
Jim Laskey58b968b2005-08-17 20:08:02 +0000310//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000311// SDNode Profile Support
312//===----------------------------------------------------------------------===//
313
Jim Laskeydef69b92006-10-27 23:52:51 +0000314/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
315///
Jim Laskey583bd472006-10-27 23:46:08 +0000316static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
317 ID.AddInteger(OpC);
318}
319
320/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
321/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000322static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000323 ID.AddPointer(VTList.VTs);
324}
325
Jim Laskeydef69b92006-10-27 23:52:51 +0000326/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
327///
Jim Laskey583bd472006-10-27 23:46:08 +0000328static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000329 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000330 for (; NumOps; --NumOps, ++Ops) {
331 ID.AddPointer(Ops->Val);
332 ID.AddInteger(Ops->ResNo);
333 }
Jim Laskey583bd472006-10-27 23:46:08 +0000334}
335
Jim Laskey583bd472006-10-27 23:46:08 +0000336static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
Dan Gohman35b31be2008-04-17 23:02:12 +0000338 SDOperandPtr OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
342}
343
Roman Levenstein9cac5252008-04-16 16:15:27 +0000344
Jim Laskeydef69b92006-10-27 23:52:51 +0000345/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
346/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000347static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
348 AddNodeIDOpcode(ID, N->getOpcode());
349 // Add the return value info.
350 AddNodeIDValueTypes(ID, N->getVTList());
351 // Add the operand info.
352 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
353
354 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000357 case ISD::ARG_FLAGS:
358 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
359 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 case ISD::TargetConstant:
361 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000362 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 break;
364 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000365 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000366 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000368 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000370 case ISD::GlobalAddress:
371 case ISD::TargetGlobalTLSAddress:
372 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
374 ID.AddPointer(GA->getGlobal());
375 ID.AddInteger(GA->getOffset());
376 break;
377 }
378 case ISD::BasicBlock:
379 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 break;
381 case ISD::Register:
382 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000384 case ISD::SRCVALUE:
385 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
386 break;
387 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000388 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000389 ID.AddPointer(MO.getValue());
390 ID.AddInteger(MO.getFlags());
391 ID.AddInteger(MO.getOffset());
392 ID.AddInteger(MO.getSize());
393 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000394 break;
395 }
396 case ISD::FrameIndex:
397 case ISD::TargetFrameIndex:
398 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
399 break;
400 case ISD::JumpTable:
401 case ISD::TargetJumpTable:
402 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
403 break;
404 case ISD::ConstantPool:
405 case ISD::TargetConstantPool: {
406 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
407 ID.AddInteger(CP->getAlignment());
408 ID.AddInteger(CP->getOffset());
409 if (CP->isMachineConstantPoolEntry())
410 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
411 else
412 ID.AddPointer(CP->getConstVal());
413 break;
414 }
415 case ISD::LOAD: {
416 LoadSDNode *LD = cast<LoadSDNode>(N);
417 ID.AddInteger(LD->getAddressingMode());
418 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000419 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 ID.AddInteger(LD->getAlignment());
421 ID.AddInteger(LD->isVolatile());
422 break;
423 }
424 case ISD::STORE: {
425 StoreSDNode *ST = cast<StoreSDNode>(N);
426 ID.AddInteger(ST->getAddressingMode());
427 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000428 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 ID.AddInteger(ST->getAlignment());
430 ID.AddInteger(ST->isVolatile());
431 break;
432 }
Jim Laskey583bd472006-10-27 23:46:08 +0000433 }
434}
435
436//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000437// SelectionDAG Class
438//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000439
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000440/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000441/// SelectionDAG.
442void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000443 // Create a dummy node (which is not added to allnodes), that adds a reference
444 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000445 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000446
Chris Lattner190a4182006-08-04 17:45:20 +0000447 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000448
Chris Lattner190a4182006-08-04 17:45:20 +0000449 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000450 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000451 if (I->use_empty())
452 DeadNodes.push_back(I);
453
454 // Process the worklist, deleting the nodes and adding their uses to the
455 // worklist.
456 while (!DeadNodes.empty()) {
457 SDNode *N = DeadNodes.back();
458 DeadNodes.pop_back();
459
460 // Take the node out of the appropriate CSE map.
461 RemoveNodeFromCSEMaps(N);
462
463 // Next, brutally remove the operand list. This is safe to do, as there are
464 // no cycles in the graph.
465 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000466 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000467 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000468
469 // Now that we removed this operand, see if there are no uses of it left.
470 if (Operand->use_empty())
471 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000472 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000473 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000474 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000475 }
Chris Lattner190a4182006-08-04 17:45:20 +0000476 N->OperandList = 0;
477 N->NumOperands = 0;
478
479 // Finally, remove N itself.
480 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000481 }
482
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000483 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000484 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000485}
486
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000487void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000488 SmallVector<SDNode*, 16> DeadNodes;
489 DeadNodes.push_back(N);
490
491 // Process the worklist, deleting the nodes and adding their uses to the
492 // worklist.
493 while (!DeadNodes.empty()) {
494 SDNode *N = DeadNodes.back();
495 DeadNodes.pop_back();
496
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000497 if (UpdateListener)
498 UpdateListener->NodeDeleted(N);
499
Evan Cheng130a6472006-10-12 20:34:05 +0000500 // Take the node out of the appropriate CSE map.
501 RemoveNodeFromCSEMaps(N);
502
503 // Next, brutally remove the operand list. This is safe to do, as there are
504 // no cycles in the graph.
505 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000506 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000507 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000508
509 // Now that we removed this operand, see if there are no uses of it left.
510 if (Operand->use_empty())
511 DeadNodes.push_back(Operand);
512 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000513 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000514 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000515 }
Evan Cheng130a6472006-10-12 20:34:05 +0000516 N->OperandList = 0;
517 N->NumOperands = 0;
518
519 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000520 AllNodes.erase(N);
521 }
522}
523
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000524void SelectionDAG::DeleteNode(SDNode *N) {
525 assert(N->use_empty() && "Cannot delete a node that is not dead!");
526
527 // First take this out of the appropriate CSE map.
528 RemoveNodeFromCSEMaps(N);
529
Chris Lattner1e111c72005-09-07 05:37:01 +0000530 // Finally, remove uses due to operands of this node, remove from the
531 // AllNodes list, and delete the node.
532 DeleteNodeNotInCSEMaps(N);
533}
534
535void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
536
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000537 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000538 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000539
540 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000541 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000542 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000543 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000544 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000545 }
Chris Lattner65113b22005-11-08 22:07:03 +0000546 N->OperandList = 0;
547 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000548
549 delete N;
550}
551
Chris Lattner149c58c2005-08-16 18:17:10 +0000552/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
553/// correspond to it. This is useful when we're about to delete or repurpose
554/// the node. We don't want future request for structurally identical nodes
555/// to return N anymore.
556void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000557 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000558 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000559 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000560 case ISD::STRING:
561 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
562 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000563 case ISD::CONDCODE:
564 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
565 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000566 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000567 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
568 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000571 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000572 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000573 Erased =
574 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000575 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000576 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000577 MVT VT = cast<VTSDNode>(N)->getVT();
578 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000579 Erased = ExtendedValueTypeNodes.erase(VT);
580 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000581 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
582 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000583 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000584 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000585 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000586 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000587 // Remove it from the CSE Map.
588 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000589 break;
590 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000591#ifndef NDEBUG
592 // Verify that the node was actually in one of the CSE maps, unless it has a
593 // flag result (which cannot be CSE'd) or is one of the special cases that are
594 // not subject to CSE.
595 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000596 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000597 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000598 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000599 assert(0 && "Node is not in map!");
600 }
601#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000602}
603
Chris Lattner8b8749f2005-08-17 19:00:20 +0000604/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
605/// has been taken out and modified in some way. If the specified node already
606/// exists in the CSE maps, do not modify the maps, but return the existing node
607/// instead. If it doesn't exist, add it and return null.
608///
609SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
610 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000611 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000612 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000613
Chris Lattner9f8cc692005-12-19 22:21:21 +0000614 // Check that remaining values produced are not flags.
615 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
616 if (N->getValueType(i) == MVT::Flag)
617 return 0; // Never CSE anything that produces a flag.
618
Chris Lattnera5682852006-08-07 23:03:03 +0000619 SDNode *New = CSEMap.GetOrInsertNode(N);
620 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000621 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000622}
623
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000624/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
625/// were replaced with those specified. If this node is never memoized,
626/// return null, otherwise return a pointer to the slot it would take. If a
627/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000628SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
629 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000630 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000631 return 0; // Never add these nodes.
632
633 // Check that remaining values produced are not flags.
634 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
635 if (N->getValueType(i) == MVT::Flag)
636 return 0; // Never CSE anything that produces a flag.
637
Chris Lattner63e3f142007-02-04 07:28:00 +0000638 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000639 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000640 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000641 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000642}
643
644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
649 SDOperand Op1, SDOperand Op2,
650 void *&InsertPos) {
651 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
652 return 0; // Never add these nodes.
653
654 // Check that remaining values produced are not flags.
655 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
656 if (N->getValueType(i) == MVT::Flag)
657 return 0; // Never CSE anything that produces a flag.
658
Chris Lattner63e3f142007-02-04 07:28:00 +0000659 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000660 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000661 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000662 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
663}
664
665
666/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
667/// were replaced with those specified. If this node is never memoized,
668/// return null, otherwise return a pointer to the slot it would take. If a
669/// node already exists with these operands, the slot will be non-null.
670SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000671 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000672 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000673 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000674 return 0; // Never add these nodes.
675
676 // Check that remaining values produced are not flags.
677 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
678 if (N->getValueType(i) == MVT::Flag)
679 return 0; // Never CSE anything that produces a flag.
680
Jim Laskey583bd472006-10-27 23:46:08 +0000681 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000682 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000683
Evan Cheng9629aba2006-10-11 01:47:58 +0000684 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
685 ID.AddInteger(LD->getAddressingMode());
686 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000687 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000688 ID.AddInteger(LD->getAlignment());
689 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000690 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
691 ID.AddInteger(ST->getAddressingMode());
692 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000693 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000694 ID.AddInteger(ST->getAlignment());
695 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000696 }
Jim Laskey583bd472006-10-27 23:46:08 +0000697
Chris Lattnera5682852006-08-07 23:03:03 +0000698 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000700
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000701
Chris Lattner78ec3112003-08-11 14:57:33 +0000702SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000703 while (!AllNodes.empty()) {
704 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000705 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000706 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000707 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000708 }
Chris Lattner65113b22005-11-08 22:07:03 +0000709 N->OperandList = 0;
710 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000711 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000712 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000713}
714
Duncan Sands83ec4b62008-06-06 12:08:01 +0000715SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000716 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000717 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000718 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000719 return getNode(ISD::AND, Op.getValueType(), Op,
720 getConstant(Imm, Op.getValueType()));
721}
722
Chris Lattner36ce6912005-11-29 06:21:05 +0000723SDOperand SelectionDAG::getString(const std::string &Val) {
724 StringSDNode *&N = StringNodes[Val];
725 if (!N) {
726 N = new StringSDNode(Val);
727 AllNodes.push_back(N);
728 }
729 return SDOperand(N, 0);
730}
731
Duncan Sands83ec4b62008-06-06 12:08:01 +0000732SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
733 MVT EltVT =
734 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohman6394b092008-02-08 22:59:30 +0000735
Duncan Sands83ec4b62008-06-06 12:08:01 +0000736 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000737}
738
Duncan Sands83ec4b62008-06-06 12:08:01 +0000739SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
740 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000741
Duncan Sands83ec4b62008-06-06 12:08:01 +0000742 MVT EltVT =
743 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000744
Duncan Sands83ec4b62008-06-06 12:08:01 +0000745 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000746 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000747
748 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000749 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000750 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000751 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000752 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000753 SDNode *N = NULL;
754 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000755 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000756 return SDOperand(N, 0);
757 if (!N) {
758 N = new ConstantSDNode(isT, Val, EltVT);
759 CSEMap.InsertNode(N, IP);
760 AllNodes.push_back(N);
761 }
762
763 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000765 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000767 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
768 }
769 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000770}
771
Chris Lattner0bd48932008-01-17 07:00:52 +0000772SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
773 return getConstant(Val, TLI.getPointerTy(), isTarget);
774}
775
776
Duncan Sands83ec4b62008-06-06 12:08:01 +0000777SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
778 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000779
Duncan Sands83ec4b62008-06-06 12:08:01 +0000780 MVT EltVT =
781 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000782
Chris Lattnerd8658612005-02-17 20:17:32 +0000783 // Do the map lookup using the actual bit pattern for the floating point
784 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
785 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000786 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000787 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000788 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000789 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000790 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000791 SDNode *N = NULL;
792 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000793 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000794 return SDOperand(N, 0);
795 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000796 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000797 CSEMap.InsertNode(N, IP);
798 AllNodes.push_back(N);
799 }
800
Dan Gohman7f321562007-06-25 16:23:39 +0000801 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000802 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000803 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000804 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000805 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
806 }
807 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808}
809
Duncan Sands83ec4b62008-06-06 12:08:01 +0000810SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
811 MVT EltVT =
812 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000813 if (EltVT==MVT::f32)
814 return getConstantFP(APFloat((float)Val), VT, isTarget);
815 else
816 return getConstantFP(APFloat(Val), VT, isTarget);
817}
818
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000820 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000821 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000822 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000823
824 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
825 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000826 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000827 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
828 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
829 }
830
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000831 if (GVar && GVar->isThreadLocal())
832 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
833 else
834 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000835
Jim Laskey583bd472006-10-27 23:46:08 +0000836 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000837 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000838 ID.AddPointer(GV);
839 ID.AddInteger(Offset);
840 void *IP = 0;
841 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
842 return SDOperand(E, 0);
843 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
844 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845 AllNodes.push_back(N);
846 return SDOperand(N, 0);
847}
848
Duncan Sands83ec4b62008-06-06 12:08:01 +0000849SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000850 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000851 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000852 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000853 ID.AddInteger(FI);
854 void *IP = 0;
855 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
856 return SDOperand(E, 0);
857 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
858 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000859 AllNodes.push_back(N);
860 return SDOperand(N, 0);
861}
862
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000864 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
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(JTI);
868 void *IP = 0;
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
872 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000878 unsigned Alignment, int Offset,
879 bool isTarget) {
880 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000881 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000882 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000883 ID.AddInteger(Alignment);
884 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000885 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 void *IP = 0;
887 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
888 return SDOperand(E, 0);
889 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
890 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
893}
894
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895
Duncan Sands83ec4b62008-06-06 12:08:01 +0000896SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000897 unsigned Alignment, int Offset,
898 bool isTarget) {
899 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000900 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000901 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000902 ID.AddInteger(Alignment);
903 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000904 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000905 void *IP = 0;
906 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
907 return SDOperand(E, 0);
908 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
909 CSEMap.InsertNode(N, IP);
910 AllNodes.push_back(N);
911 return SDOperand(N, 0);
912}
913
914
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000916 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000917 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000918 ID.AddPointer(MBB);
919 void *IP = 0;
920 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
921 return SDOperand(E, 0);
922 SDNode *N = new BasicBlockSDNode(MBB);
923 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000924 AllNodes.push_back(N);
925 return SDOperand(N, 0);
926}
927
Duncan Sands276dcbd2008-03-21 09:14:45 +0000928SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
929 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000930 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000931 ID.AddInteger(Flags.getRawBits());
932 void *IP = 0;
933 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
934 return SDOperand(E, 0);
935 SDNode *N = new ARG_FLAGSSDNode(Flags);
936 CSEMap.InsertNode(N, IP);
937 AllNodes.push_back(N);
938 return SDOperand(N, 0);
939}
940
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941SDOperand SelectionDAG::getValueType(MVT VT) {
942 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
943 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000944
Duncan Sands83ec4b62008-06-06 12:08:01 +0000945 SDNode *&N = VT.isExtended() ?
946 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000947
948 if (N) return SDOperand(N, 0);
949 N = new VTSDNode(VT);
950 AllNodes.push_back(N);
951 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000952}
953
Duncan Sands83ec4b62008-06-06 12:08:01 +0000954SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 SDNode *&N = ExternalSymbols[Sym];
956 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000957 N = new ExternalSymbolSDNode(false, Sym, VT);
958 AllNodes.push_back(N);
959 return SDOperand(N, 0);
960}
961
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000963 SDNode *&N = TargetExternalSymbols[Sym];
964 if (N) return SDOperand(N, 0);
965 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966 AllNodes.push_back(N);
967 return SDOperand(N, 0);
968}
969
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000970SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
971 if ((unsigned)Cond >= CondCodeNodes.size())
972 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000973
Chris Lattner079a27a2005-08-09 20:40:02 +0000974 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000975 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000976 AllNodes.push_back(CondCodeNodes[Cond]);
977 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000978 return SDOperand(CondCodeNodes[Cond], 0);
979}
980
Duncan Sands83ec4b62008-06-06 12:08:01 +0000981SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000982 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000983 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000984 ID.AddInteger(RegNo);
985 void *IP = 0;
986 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
987 return SDOperand(E, 0);
988 SDNode *N = new RegisterSDNode(RegNo, VT);
989 CSEMap.InsertNode(N, IP);
990 AllNodes.push_back(N);
991 return SDOperand(N, 0);
992}
993
Dan Gohman69de1932008-02-06 22:27:42 +0000994SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000995 assert((!V || isa<PointerType>(V->getType())) &&
996 "SrcValue is not a pointer?");
997
Jim Laskey583bd472006-10-27 23:46:08 +0000998 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000999 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001000 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001001
Chris Lattner61b09412006-08-11 21:01:22 +00001002 void *IP = 0;
1003 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1004 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001005
1006 SDNode *N = new SrcValueSDNode(V);
1007 CSEMap.InsertNode(N, IP);
1008 AllNodes.push_back(N);
1009 return SDOperand(N, 0);
1010}
1011
Dan Gohman36b5c132008-04-07 19:35:22 +00001012SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001013 const Value *v = MO.getValue();
1014 assert((!v || isa<PointerType>(v->getType())) &&
1015 "SrcValue is not a pointer?");
1016
1017 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001018 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001019 ID.AddPointer(v);
1020 ID.AddInteger(MO.getFlags());
1021 ID.AddInteger(MO.getOffset());
1022 ID.AddInteger(MO.getSize());
1023 ID.AddInteger(MO.getAlignment());
1024
1025 void *IP = 0;
1026 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1027 return SDOperand(E, 0);
1028
1029 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001030 CSEMap.InsertNode(N, IP);
1031 AllNodes.push_back(N);
1032 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001033}
1034
Chris Lattner37ce9df2007-10-15 17:47:20 +00001035/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1036/// specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001037SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001038 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 unsigned ByteSize = VT.getSizeInBits()/8;
1040 const Type *Ty = VT.getTypeForMVT();
Chris Lattner37ce9df2007-10-15 17:47:20 +00001041 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1042 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1043 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1044}
1045
1046
Duncan Sands83ec4b62008-06-06 12:08:01 +00001047SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001048 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 // These setcc operations always fold.
1050 switch (Cond) {
1051 default: break;
1052 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001053 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001055 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001056
1057 case ISD::SETOEQ:
1058 case ISD::SETOGT:
1059 case ISD::SETOGE:
1060 case ISD::SETOLT:
1061 case ISD::SETOLE:
1062 case ISD::SETONE:
1063 case ISD::SETO:
1064 case ISD::SETUO:
1065 case ISD::SETUEQ:
1066 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001067 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001068 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001070
Chris Lattner67255a12005-04-07 18:14:58 +00001071 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001072 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001073 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001074 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001075
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 switch (Cond) {
1077 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001078 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1079 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001080 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1081 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1082 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1083 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1084 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1085 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1086 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1087 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089 }
Chris Lattner67255a12005-04-07 18:14:58 +00001090 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001091 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001093 // No compile time operations on this type yet.
1094 if (N1C->getValueType(0) == MVT::ppcf128)
1095 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001096
1097 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001099 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001100 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1101 return getNode(ISD::UNDEF, VT);
1102 // fall through
1103 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1104 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1105 return getNode(ISD::UNDEF, VT);
1106 // fall through
1107 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001108 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001109 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1110 return getNode(ISD::UNDEF, VT);
1111 // fall through
1112 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1113 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1114 return getNode(ISD::UNDEF, VT);
1115 // fall through
1116 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1117 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1118 return getNode(ISD::UNDEF, VT);
1119 // fall through
1120 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001121 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001122 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1123 return getNode(ISD::UNDEF, VT);
1124 // fall through
1125 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001126 R==APFloat::cmpEqual, VT);
1127 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1128 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1129 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1130 R==APFloat::cmpEqual, VT);
1131 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1132 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1133 R==APFloat::cmpLessThan, VT);
1134 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1135 R==APFloat::cmpUnordered, VT);
1136 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1137 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 }
1139 } else {
1140 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001141 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001142 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001143 }
1144
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001145 // Could not fold it.
1146 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147}
1148
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001149/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1150/// use this predicate to simplify operations downstream.
1151bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1152 unsigned BitWidth = Op.getValueSizeInBits();
1153 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1154}
1155
Dan Gohmanea859be2007-06-22 14:59:07 +00001156/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1157/// this predicate to simplify operations downstream. Mask is known to be zero
1158/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001159bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001160 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001161 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001162 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1163 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1164 return (KnownZero & Mask) == Mask;
1165}
1166
1167/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1168/// known to be either zero or one and return them in the KnownZero/KnownOne
1169/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1170/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001171void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001172 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001173 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001174 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001175 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001176 "Mask size mismatches value type size!");
1177
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001178 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001179 if (Depth == 6 || Mask == 0)
1180 return; // Limit search depth.
1181
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001182 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001183
1184 switch (Op.getOpcode()) {
1185 case ISD::Constant:
1186 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001187 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001188 KnownZero = ~KnownOne & Mask;
1189 return;
1190 case ISD::AND:
1191 // If either the LHS or the RHS are Zero, the result is zero.
1192 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001193 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1194 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001195 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1196 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1197
1198 // Output known-1 bits are only known if set in both the LHS & RHS.
1199 KnownOne &= KnownOne2;
1200 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1201 KnownZero |= KnownZero2;
1202 return;
1203 case ISD::OR:
1204 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001205 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1206 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1208 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1209
1210 // Output known-0 bits are only known if clear in both the LHS & RHS.
1211 KnownZero &= KnownZero2;
1212 // Output known-1 are known to be set if set in either the LHS | RHS.
1213 KnownOne |= KnownOne2;
1214 return;
1215 case ISD::XOR: {
1216 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1217 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1218 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1219 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1220
1221 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001222 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001223 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1224 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1225 KnownZero = KnownZeroOut;
1226 return;
1227 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001228 case ISD::MUL: {
1229 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1230 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1231 ComputeMaskedBits(Op.getOperand(0), Mask2, 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 // If low bits are zero in either operand, output low known-0 bits.
1236 // Also compute a conserative estimate for high known-0 bits.
1237 // More trickiness is possible, but this is sufficient for the
1238 // interesting case of alignment computation.
1239 KnownOne.clear();
1240 unsigned TrailZ = KnownZero.countTrailingOnes() +
1241 KnownZero2.countTrailingOnes();
1242 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001243 KnownZero2.countLeadingOnes(),
1244 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001245
1246 TrailZ = std::min(TrailZ, BitWidth);
1247 LeadZ = std::min(LeadZ, BitWidth);
1248 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1249 APInt::getHighBitsSet(BitWidth, LeadZ);
1250 KnownZero &= Mask;
1251 return;
1252 }
1253 case ISD::UDIV: {
1254 // For the purposes of computing leading zeros we can conservatively
1255 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001256 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001257 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1258 ComputeMaskedBits(Op.getOperand(0),
1259 AllOnes, KnownZero2, KnownOne2, Depth+1);
1260 unsigned LeadZ = KnownZero2.countLeadingOnes();
1261
1262 KnownOne2.clear();
1263 KnownZero2.clear();
1264 ComputeMaskedBits(Op.getOperand(1),
1265 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001266 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1267 if (RHSUnknownLeadingOnes != BitWidth)
1268 LeadZ = std::min(BitWidth,
1269 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001270
1271 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1272 return;
1273 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001274 case ISD::SELECT:
1275 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1276 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1277 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1278 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1279
1280 // Only known if known in both the LHS and RHS.
1281 KnownOne &= KnownOne2;
1282 KnownZero &= KnownZero2;
1283 return;
1284 case ISD::SELECT_CC:
1285 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1286 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1287 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1288 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1289
1290 // Only known if known in both the LHS and RHS.
1291 KnownOne &= KnownOne2;
1292 KnownZero &= KnownZero2;
1293 return;
1294 case ISD::SETCC:
1295 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001296 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1297 BitWidth > 1)
1298 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001299 return;
1300 case ISD::SHL:
1301 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1302 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001303 unsigned ShAmt = SA->getValue();
1304
1305 // If the shift count is an invalid immediate, don't do anything.
1306 if (ShAmt >= BitWidth)
1307 return;
1308
1309 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001310 KnownZero, KnownOne, Depth+1);
1311 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001312 KnownZero <<= ShAmt;
1313 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001314 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001315 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001316 }
1317 return;
1318 case ISD::SRL:
1319 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1320 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001321 unsigned ShAmt = SA->getValue();
1322
Dan Gohmand4cf9922008-02-26 18:50:50 +00001323 // If the shift count is an invalid immediate, don't do anything.
1324 if (ShAmt >= BitWidth)
1325 return;
1326
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001327 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001328 KnownZero, KnownOne, Depth+1);
1329 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001330 KnownZero = KnownZero.lshr(ShAmt);
1331 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001332
Dan Gohman72d2fd52008-02-13 22:43:25 +00001333 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001334 KnownZero |= HighBits; // High bits known zero.
1335 }
1336 return;
1337 case ISD::SRA:
1338 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001339 unsigned ShAmt = SA->getValue();
1340
Dan Gohmand4cf9922008-02-26 18:50:50 +00001341 // If the shift count is an invalid immediate, don't do anything.
1342 if (ShAmt >= BitWidth)
1343 return;
1344
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001345 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001346 // If any of the demanded bits are produced by the sign extension, we also
1347 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001348 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1349 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001350 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001351
1352 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1353 Depth+1);
1354 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001355 KnownZero = KnownZero.lshr(ShAmt);
1356 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001357
1358 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001359 APInt SignBit = APInt::getSignBit(BitWidth);
1360 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001361
Dan Gohmanca93a432008-02-20 16:30:17 +00001362 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001363 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001364 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001365 KnownOne |= HighBits; // New bits are known one.
1366 }
1367 }
1368 return;
1369 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001370 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1371 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001372
1373 // Sign extension. Compute the demanded bits in the result that are not
1374 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001375 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001376
Dan Gohman977a76f2008-02-13 22:28:48 +00001377 APInt InSignBit = APInt::getSignBit(EBits);
1378 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001379
1380 // If the sign extended bits are demanded, we know that the sign
1381 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001382 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001383 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001384 InputDemandedBits |= InSignBit;
1385
1386 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1387 KnownZero, KnownOne, Depth+1);
1388 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1389
1390 // If the sign bit of the input is known set or clear, then we know the
1391 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001392 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001393 KnownZero |= NewBits;
1394 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001395 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001396 KnownOne |= NewBits;
1397 KnownZero &= ~NewBits;
1398 } else { // Input sign bit unknown
1399 KnownZero &= ~NewBits;
1400 KnownOne &= ~NewBits;
1401 }
1402 return;
1403 }
1404 case ISD::CTTZ:
1405 case ISD::CTLZ:
1406 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001407 unsigned LowBits = Log2_32(BitWidth)+1;
1408 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1409 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001410 return;
1411 }
1412 case ISD::LOAD: {
1413 if (ISD::isZEXTLoad(Op.Val)) {
1414 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001415 MVT VT = LD->getMemoryVT();
1416 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001417 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 }
1419 return;
1420 }
1421 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001422 MVT InVT = Op.getOperand(0).getValueType();
1423 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001424 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1425 APInt InMask = Mask;
1426 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001427 KnownZero.trunc(InBits);
1428 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001429 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001430 KnownZero.zext(BitWidth);
1431 KnownOne.zext(BitWidth);
1432 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001433 return;
1434 }
1435 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001436 MVT InVT = Op.getOperand(0).getValueType();
1437 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001438 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001439 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1440 APInt InMask = Mask;
1441 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001442
1443 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001444 // bit is demanded. Temporarily set this bit in the mask for our callee.
1445 if (NewBits.getBoolValue())
1446 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001447
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001448 KnownZero.trunc(InBits);
1449 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001450 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1451
1452 // Note if the sign bit is known to be zero or one.
1453 bool SignBitKnownZero = KnownZero.isNegative();
1454 bool SignBitKnownOne = KnownOne.isNegative();
1455 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1456 "Sign bit can't be known to be both zero and one!");
1457
1458 // If the sign bit wasn't actually demanded by our caller, we don't
1459 // want it set in the KnownZero and KnownOne result values. Reset the
1460 // mask and reapply it to the result values.
1461 InMask = Mask;
1462 InMask.trunc(InBits);
1463 KnownZero &= InMask;
1464 KnownOne &= InMask;
1465
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001466 KnownZero.zext(BitWidth);
1467 KnownOne.zext(BitWidth);
1468
Dan Gohman977a76f2008-02-13 22:28:48 +00001469 // If the sign bit is known zero or one, the top bits match.
1470 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001471 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001472 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001473 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001474 return;
1475 }
1476 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001477 MVT InVT = Op.getOperand(0).getValueType();
1478 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001479 APInt InMask = Mask;
1480 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481 KnownZero.trunc(InBits);
1482 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001483 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001484 KnownZero.zext(BitWidth);
1485 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001486 return;
1487 }
1488 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001489 MVT InVT = Op.getOperand(0).getValueType();
1490 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001491 APInt InMask = Mask;
1492 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001493 KnownZero.zext(InBits);
1494 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001495 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001496 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001497 KnownZero.trunc(BitWidth);
1498 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001499 break;
1500 }
1501 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001502 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1503 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001504 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1505 KnownOne, Depth+1);
1506 KnownZero |= (~InMask) & Mask;
1507 return;
1508 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001509 case ISD::FGETSIGN:
1510 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001511 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001512 return;
1513
Dan Gohman23e8b712008-04-28 17:02:21 +00001514 case ISD::SUB: {
1515 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1516 // We know that the top bits of C-X are clear if X contains less bits
1517 // than C (i.e. no wrap-around can happen). For example, 20-X is
1518 // positive if we can prove that X is >= 0 and < 16.
1519 if (CLHS->getAPIntValue().isNonNegative()) {
1520 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1521 // NLZ can't be BitWidth with no sign bit
1522 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1523 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1524 Depth+1);
1525
1526 // If all of the MaskV bits are known to be zero, then we know the
1527 // output top bits are zero, because we now know that the output is
1528 // from [0-C].
1529 if ((KnownZero2 & MaskV) == MaskV) {
1530 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1531 // Top bits known zero.
1532 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1533 }
1534 }
1535 }
1536 }
1537 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001538 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001539 // Output known-0 bits are known if clear or set in both the low clear bits
1540 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1541 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001542 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1543 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1544 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1545 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1546
1547 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1548 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1549 KnownZeroOut = std::min(KnownZeroOut,
1550 KnownZero2.countTrailingOnes());
1551
1552 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001553 return;
1554 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001555 case ISD::SREM:
1556 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1557 APInt RA = Rem->getAPIntValue();
1558 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001559 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001560 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1561 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001562
Dan Gohman23e8b712008-04-28 17:02:21 +00001563 // The sign of a remainder is equal to the sign of the first
1564 // operand (zero being positive).
1565 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1566 KnownZero2 |= ~LowBits;
1567 else if (KnownOne2[BitWidth-1])
1568 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001569
Dan Gohman23e8b712008-04-28 17:02:21 +00001570 KnownZero |= KnownZero2 & Mask;
1571 KnownOne |= KnownOne2 & Mask;
1572
1573 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001574 }
1575 }
1576 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001577 case ISD::UREM: {
1578 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1579 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001580 if (RA.isPowerOf2()) {
1581 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001582 APInt Mask2 = LowBits & Mask;
1583 KnownZero |= ~LowBits & Mask;
1584 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1585 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1586 break;
1587 }
1588 }
1589
1590 // Since the result is less than or equal to either operand, any leading
1591 // zero bits in either operand must also exist in the result.
1592 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1593 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1594 Depth+1);
1595 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1596 Depth+1);
1597
1598 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1599 KnownZero2.countLeadingOnes());
1600 KnownOne.clear();
1601 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1602 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001603 }
1604 default:
1605 // Allow the target to implement this method for its nodes.
1606 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1607 case ISD::INTRINSIC_WO_CHAIN:
1608 case ISD::INTRINSIC_W_CHAIN:
1609 case ISD::INTRINSIC_VOID:
1610 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1611 }
1612 return;
1613 }
1614}
1615
1616/// ComputeNumSignBits - Return the number of times the sign bit of the
1617/// register is replicated into the other bits. We know that at least 1 bit
1618/// is always equal to the sign bit (itself), but other cases can give us
1619/// information. For example, immediately after an "SRA X, 2", we know that
1620/// the top 3 bits are all equal to each other, so we return 3.
1621unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001622 MVT VT = Op.getValueType();
1623 assert(VT.isInteger() && "Invalid VT!");
1624 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001625 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001626 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001627
1628 if (Depth == 6)
1629 return 1; // Limit search depth.
1630
1631 switch (Op.getOpcode()) {
1632 default: break;
1633 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001634 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 return VTBits-Tmp+1;
1636 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001637 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001638 return VTBits-Tmp;
1639
1640 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001641 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1642 // If negative, return # leading ones.
1643 if (Val.isNegative())
1644 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001645
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001646 // Return # leading zeros.
1647 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001648 }
1649
1650 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001652 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1653
1654 case ISD::SIGN_EXTEND_INREG:
1655 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001656 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001657 Tmp = VTBits-Tmp+1;
1658
1659 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1660 return std::max(Tmp, Tmp2);
1661
1662 case ISD::SRA:
1663 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1664 // SRA X, C -> adds C sign bits.
1665 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1666 Tmp += C->getValue();
1667 if (Tmp > VTBits) Tmp = VTBits;
1668 }
1669 return Tmp;
1670 case ISD::SHL:
1671 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1672 // shl destroys sign bits.
1673 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1674 if (C->getValue() >= VTBits || // Bad shift.
1675 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1676 return Tmp - C->getValue();
1677 }
1678 break;
1679 case ISD::AND:
1680 case ISD::OR:
1681 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001682 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001683 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001684 if (Tmp != 1) {
1685 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1686 FirstAnswer = std::min(Tmp, Tmp2);
1687 // We computed what we know about the sign bits as our first
1688 // answer. Now proceed to the generic code that uses
1689 // ComputeMaskedBits, and pick whichever answer is better.
1690 }
1691 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001692
1693 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001694 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001696 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001697 return std::min(Tmp, Tmp2);
1698
1699 case ISD::SETCC:
1700 // If setcc returns 0/-1, all bits are sign bits.
1701 if (TLI.getSetCCResultContents() ==
1702 TargetLowering::ZeroOrNegativeOneSetCCResult)
1703 return VTBits;
1704 break;
1705 case ISD::ROTL:
1706 case ISD::ROTR:
1707 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1708 unsigned RotAmt = C->getValue() & (VTBits-1);
1709
1710 // Handle rotate right by N like a rotate left by 32-N.
1711 if (Op.getOpcode() == ISD::ROTR)
1712 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1713
1714 // If we aren't rotating out all of the known-in sign bits, return the
1715 // number that are left. This handles rotl(sext(x), 1) for example.
1716 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1717 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1718 }
1719 break;
1720 case ISD::ADD:
1721 // Add can have at most one carry bit. Thus we know that the output
1722 // is, at worst, one more bit than the inputs.
1723 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1724 if (Tmp == 1) return 1; // Early out.
1725
1726 // Special case decrementing a value (ADD X, -1):
1727 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1728 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001729 APInt KnownZero, KnownOne;
1730 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1732
1733 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1734 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001735 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001736 return VTBits;
1737
1738 // If we are subtracting one from a positive number, there is no carry
1739 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001740 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 return Tmp;
1742 }
1743
1744 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1745 if (Tmp2 == 1) return 1;
1746 return std::min(Tmp, Tmp2)-1;
1747 break;
1748
1749 case ISD::SUB:
1750 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1751 if (Tmp2 == 1) return 1;
1752
1753 // Handle NEG.
1754 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001755 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001756 APInt KnownZero, KnownOne;
1757 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001758 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1759 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1760 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001761 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001762 return VTBits;
1763
1764 // If the input is known to be positive (the sign bit is known clear),
1765 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001766 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001767 return Tmp2;
1768
1769 // Otherwise, we treat this like a SUB.
1770 }
1771
1772 // Sub can have at most one carry bit. Thus we know that the output
1773 // is, at worst, one more bit than the inputs.
1774 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1775 if (Tmp == 1) return 1; // Early out.
1776 return std::min(Tmp, Tmp2)-1;
1777 break;
1778 case ISD::TRUNCATE:
1779 // FIXME: it's tricky to do anything useful for this, but it is an important
1780 // case for targets like X86.
1781 break;
1782 }
1783
1784 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1785 if (Op.getOpcode() == ISD::LOAD) {
1786 LoadSDNode *LD = cast<LoadSDNode>(Op);
1787 unsigned ExtType = LD->getExtensionType();
1788 switch (ExtType) {
1789 default: break;
1790 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001791 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 return VTBits-Tmp+1;
1793 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001794 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001795 return VTBits-Tmp;
1796 }
1797 }
1798
1799 // Allow the target to implement this method for its nodes.
1800 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1801 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1802 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1803 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1804 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001805 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001806 }
1807
1808 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1809 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001810 APInt KnownZero, KnownOne;
1811 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001812 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1813
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001814 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001816 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 Mask = KnownOne;
1818 } else {
1819 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001820 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001821 }
1822
1823 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1824 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001825 Mask = ~Mask;
1826 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 // Return # leading zeros. We use 'min' here in case Val was zero before
1828 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001829 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001830}
1831
Chris Lattner51dabfb2006-10-14 00:41:01 +00001832
Evan Chenga844bde2008-02-02 04:07:54 +00001833bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1834 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1835 if (!GA) return false;
1836 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1837 if (!GV) return false;
1838 MachineModuleInfo *MMI = getMachineModuleInfo();
1839 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1840}
1841
1842
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001843/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1844/// element of the result of the vector shuffle.
1845SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned Idx) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001846 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001847 SDOperand PermMask = N->getOperand(2);
1848 unsigned NumElems = PermMask.getNumOperands();
1849 SDOperand V = (Idx < NumElems) ? N->getOperand(0) : N->getOperand(1);
1850 Idx %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001851
1852 if (V.getOpcode() == ISD::BIT_CONVERT) {
1853 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001854 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001855 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001856 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001857 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
1858 return (Idx == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001860 if (V.getOpcode() == ISD::BUILD_VECTOR)
1861 return V.getOperand(Idx);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001862 if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
1863 SDOperand Elt = PermMask.getOperand(Idx);
1864 if (Elt.getOpcode() == ISD::UNDEF)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001865 return getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001866 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Elt)->getValue());
1867 }
1868 return SDOperand();
1869}
1870
1871
Chris Lattnerc3aae252005-01-07 07:46:32 +00001872/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001873///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001874SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001875 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001876 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001877 void *IP = 0;
1878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1879 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001880 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001881 CSEMap.InsertNode(N, IP);
1882
1883 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001884 return SDOperand(N, 0);
1885}
1886
Duncan Sands83ec4b62008-06-06 12:08:01 +00001887SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001888 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001890 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001891 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001892 switch (Opcode) {
1893 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001894 case ISD::SIGN_EXTEND:
1895 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001896 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001897 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001898 case ISD::TRUNCATE:
1899 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001900 case ISD::UINT_TO_FP:
1901 case ISD::SINT_TO_FP: {
1902 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001903 // No compile time operations on this type.
1904 if (VT==MVT::ppcf128)
1905 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001906 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1907 (void)apf.convertFromAPInt(Val,
1908 Opcode==ISD::SINT_TO_FP,
1909 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001910 return getConstantFP(apf, VT);
1911 }
Chris Lattner94683772005-12-23 05:30:37 +00001912 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001913 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001914 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001915 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001916 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001917 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001918 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001919 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001920 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001921 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001922 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001923 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001924 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001925 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001926 }
1927 }
1928
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001929 // Constant fold unary operations with a floating point constant operand.
1930 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1931 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001932 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001933 switch (Opcode) {
1934 case ISD::FNEG:
1935 V.changeSign();
1936 return getConstantFP(V, VT);
1937 case ISD::FABS:
1938 V.clearSign();
1939 return getConstantFP(V, VT);
1940 case ISD::FP_ROUND:
1941 case ISD::FP_EXTEND:
1942 // This can return overflow, underflow, or inexact; we don't care.
1943 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001944 (void)V.convert(*MVTToAPFloatSemantics(VT),
1945 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001946 return getConstantFP(V, VT);
1947 case ISD::FP_TO_SINT:
1948 case ISD::FP_TO_UINT: {
1949 integerPart x;
1950 assert(integerPartWidth >= 64);
1951 // FIXME need to be more flexible about rounding mode.
1952 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1953 Opcode==ISD::FP_TO_SINT,
1954 APFloat::rmTowardZero);
1955 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1956 break;
1957 return getConstant(x, VT);
1958 }
1959 case ISD::BIT_CONVERT:
1960 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1961 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1962 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1963 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001964 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001965 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001966 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001967 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001968
1969 unsigned OpOpcode = Operand.Val->getOpcode();
1970 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001971 case ISD::TokenFactor:
Dan Gohmanb91c89d2008-04-14 18:43:25 +00001972 case ISD::MERGE_VALUES:
1973 return Operand; // Factor or merge of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001974 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001975 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001976 assert(VT.isFloatingPoint() &&
1977 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001978 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001979 if (Operand.getOpcode() == ISD::UNDEF)
1980 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001981 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001982 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001983 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00001984 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001985 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00001986 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00001987 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1989 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1990 break;
1991 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001992 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00001993 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001994 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00001995 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00001996 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001997 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001998 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001999 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002000 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002002 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002003 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002004 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002005 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002006 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2007 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2008 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2009 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002010 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002011 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002012 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002013 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002014 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002015 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 if (OpOpcode == ISD::TRUNCATE)
2017 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002018 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2019 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002020 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002021 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002022 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002023 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002024 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2025 else
2026 return Operand.Val->getOperand(0);
2027 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002028 break;
Chris Lattner35481892005-12-23 00:16:34 +00002029 case ISD::BIT_CONVERT:
2030 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002032 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002033 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002034 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2035 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002036 if (OpOpcode == ISD::UNDEF)
2037 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002038 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002039 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002040 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2041 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002042 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002043 if (OpOpcode == ISD::UNDEF)
2044 return getNode(ISD::UNDEF, VT);
2045 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2046 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2047 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2048 Operand.getConstantOperandVal(1) == 0 &&
2049 Operand.getOperand(0).getValueType() == VT)
2050 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002051 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002052 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002053 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2054 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002055 Operand.Val->getOperand(0));
2056 if (OpOpcode == ISD::FNEG) // --X -> X
2057 return Operand.Val->getOperand(0);
2058 break;
2059 case ISD::FABS:
2060 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2061 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2062 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002063 }
2064
Chris Lattner43247a12005-08-25 19:12:10 +00002065 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002066 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002067 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002068 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002069 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002070 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002071 void *IP = 0;
2072 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2073 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002074 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002075 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002076 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002077 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002078 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002079 AllNodes.push_back(N);
2080 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002081}
2082
Chris Lattner36019aa2005-04-18 03:48:41 +00002083
2084
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002086 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002087 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2088 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002089 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002090 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002091 case ISD::TokenFactor:
2092 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2093 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002094 // Fold trivial token factors.
2095 if (N1.getOpcode() == ISD::EntryToken) return N2;
2096 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002097 break;
Chris Lattner76365122005-01-16 02:23:22 +00002098 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002100 N1.getValueType() == VT && "Binary operator types must match!");
2101 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2102 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002103 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002104 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002105 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2106 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002107 break;
Chris Lattner76365122005-01-16 02:23:22 +00002108 case ISD::OR:
2109 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002110 case ISD::ADD:
2111 case ISD::SUB:
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!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002114 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2115 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002116 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002117 return N1;
2118 break;
Chris Lattner76365122005-01-16 02:23:22 +00002119 case ISD::UDIV:
2120 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002121 case ISD::MULHU:
2122 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002123 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002124 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002125 case ISD::MUL:
2126 case ISD::SDIV:
2127 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002128 case ISD::FADD:
2129 case ISD::FSUB:
2130 case ISD::FMUL:
2131 case ISD::FDIV:
2132 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002133 assert(N1.getValueType() == N2.getValueType() &&
2134 N1.getValueType() == VT && "Binary operator types must match!");
2135 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002136 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2137 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 N1.getValueType().isFloatingPoint() &&
2139 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002140 "Invalid FCOPYSIGN!");
2141 break;
Chris Lattner76365122005-01-16 02:23:22 +00002142 case ISD::SHL:
2143 case ISD::SRA:
2144 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002145 case ISD::ROTL:
2146 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002147 assert(VT == N1.getValueType() &&
2148 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002149 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002150 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002151 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002152 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002154 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002156 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002157 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002158 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002159 break;
2160 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002161 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002162 assert(VT.isFloatingPoint() &&
2163 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002164 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002165 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002166 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002167 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002168 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002169 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002170 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002171 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002172 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002173 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002174 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002175 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002176 break;
2177 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002178 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002179 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002180 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002181 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002182 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002183 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002184 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002185
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002186 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002187 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002189 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002190 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002191 return getConstant(Val, VT);
2192 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002193 break;
2194 }
2195 case ISD::EXTRACT_VECTOR_ELT:
2196 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2197
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002198 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2199 if (N1.getOpcode() == ISD::UNDEF)
2200 return getNode(ISD::UNDEF, VT);
2201
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002202 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2203 // expanding copies of large vectors from registers.
2204 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2205 N1.getNumOperands() > 0) {
2206 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002207 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002208 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2209 N1.getOperand(N2C->getValue() / Factor),
2210 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2211 }
2212
2213 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2214 // expanding large vector constants.
2215 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2216 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002217
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002218 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2219 // operations are lowered to scalars.
2220 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2221 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2222 if (IEC == N2C)
2223 return N1.getOperand(1);
2224 else
2225 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2226 }
2227 break;
2228 case ISD::EXTRACT_ELEMENT:
2229 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002230 assert(!N1.getValueType().isVector() &&
2231 N1.getValueType().isInteger() &&
2232 !VT.isVector() && VT.isInteger() &&
Duncan Sands25eb0432008-03-12 20:30:08 +00002233 "EXTRACT_ELEMENT only applies to integers!");
2234
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002235 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2236 // 64-bit integers into 32-bit parts. Instead of building the extract of
2237 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2238 if (N1.getOpcode() == ISD::BUILD_PAIR)
2239 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002240
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002241 // EXTRACT_ELEMENT of a constant int is also very common.
2242 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002243 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002244 unsigned Shift = ElementSize * N2C->getValue();
2245 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2246 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002247 }
2248 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002249 case ISD::EXTRACT_SUBVECTOR:
2250 if (N1.getValueType() == VT) // Trivial extraction.
2251 return N1;
2252 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002253 }
2254
2255 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002256 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002257 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002258 switch (Opcode) {
2259 case ISD::ADD: return getConstant(C1 + C2, VT);
2260 case ISD::SUB: return getConstant(C1 - C2, VT);
2261 case ISD::MUL: return getConstant(C1 * C2, VT);
2262 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002263 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002264 break;
2265 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002266 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002267 break;
2268 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002269 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002270 break;
2271 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002272 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002273 break;
2274 case ISD::AND : return getConstant(C1 & C2, VT);
2275 case ISD::OR : return getConstant(C1 | C2, VT);
2276 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002277 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002278 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2279 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2280 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2281 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002282 default: break;
2283 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002284 } else { // Cannonicalize constant to RHS if commutative
2285 if (isCommutativeBinOp(Opcode)) {
2286 std::swap(N1C, N2C);
2287 std::swap(N1, N2);
2288 }
2289 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002290 }
2291
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002292 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002293 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2294 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002295 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002296 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2297 // Cannonicalize constant to RHS if commutative
2298 std::swap(N1CFP, N2CFP);
2299 std::swap(N1, N2);
2300 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002301 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2302 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002303 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002304 case ISD::FADD:
2305 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002306 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002307 return getConstantFP(V1, VT);
2308 break;
2309 case ISD::FSUB:
2310 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2311 if (s!=APFloat::opInvalidOp)
2312 return getConstantFP(V1, VT);
2313 break;
2314 case ISD::FMUL:
2315 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2316 if (s!=APFloat::opInvalidOp)
2317 return getConstantFP(V1, VT);
2318 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002319 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002320 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2321 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2322 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002324 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002325 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2326 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2327 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002329 case ISD::FCOPYSIGN:
2330 V1.copySign(V2);
2331 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002332 default: break;
2333 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002335 }
Chris Lattner62b57722006-04-20 05:39:12 +00002336
2337 // Canonicalize an UNDEF to the RHS, even over a constant.
2338 if (N1.getOpcode() == ISD::UNDEF) {
2339 if (isCommutativeBinOp(Opcode)) {
2340 std::swap(N1, N2);
2341 } else {
2342 switch (Opcode) {
2343 case ISD::FP_ROUND_INREG:
2344 case ISD::SIGN_EXTEND_INREG:
2345 case ISD::SUB:
2346 case ISD::FSUB:
2347 case ISD::FDIV:
2348 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002349 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002350 return N1; // fold op(undef, arg2) -> undef
2351 case ISD::UDIV:
2352 case ISD::SDIV:
2353 case ISD::UREM:
2354 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002355 case ISD::SRL:
2356 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002358 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2359 // For vectors, we can't easily build an all zero vector, just return
2360 // the LHS.
2361 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002362 }
2363 }
2364 }
2365
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002366 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002367 if (N2.getOpcode() == ISD::UNDEF) {
2368 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002369 case ISD::XOR:
2370 if (N1.getOpcode() == ISD::UNDEF)
2371 // Handle undef ^ undef -> 0 special case. This is a common
2372 // idiom (misuse).
2373 return getConstant(0, VT);
2374 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002375 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002376 case ISD::ADDC:
2377 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002378 case ISD::SUB:
2379 case ISD::FADD:
2380 case ISD::FSUB:
2381 case ISD::FMUL:
2382 case ISD::FDIV:
2383 case ISD::FREM:
2384 case ISD::UDIV:
2385 case ISD::SDIV:
2386 case ISD::UREM:
2387 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002388 return N2; // fold op(arg1, undef) -> undef
2389 case ISD::MUL:
2390 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002391 case ISD::SRL:
2392 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002393 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002394 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2395 // For vectors, we can't easily build an all zero vector, just return
2396 // the LHS.
2397 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002398 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002399 if (!VT.isVector())
2400 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002401 // For vectors, we can't easily build an all one vector, just return
2402 // the LHS.
2403 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002404 case ISD::SRA:
2405 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002406 }
2407 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002408
Chris Lattner27e9b412005-05-11 18:57:39 +00002409 // Memoize this node if possible.
2410 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002411 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002412 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002413 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002414 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002415 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002416 void *IP = 0;
2417 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2418 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002419 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002420 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002421 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002422 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002423 }
2424
Chris Lattnerc3aae252005-01-07 07:46:32 +00002425 AllNodes.push_back(N);
2426 return SDOperand(N, 0);
2427}
2428
Duncan Sands83ec4b62008-06-06 12:08:01 +00002429SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002430 SDOperand N1, SDOperand N2, SDOperand N3) {
2431 // Perform various simplifications.
2432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2433 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002434 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002435 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002436 // Use FoldSetCC to simplify SETCC's.
2437 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002438 if (Simp.Val) return Simp;
2439 break;
2440 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002441 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002442 if (N1C) {
2443 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002444 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002445 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002446 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002447 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448
2449 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002450 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002451 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002452 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002453 if (N2C->getValue()) // Unconditional branch
2454 return getNode(ISD::BR, MVT::Other, N1, N3);
2455 else
2456 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002457 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002458 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002459 case ISD::VECTOR_SHUFFLE:
2460 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002461 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002462 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002463 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002464 "Illegal VECTOR_SHUFFLE node!");
2465 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002466 case ISD::BIT_CONVERT:
2467 // Fold bit_convert nodes from a type to themselves.
2468 if (N1.getValueType() == VT)
2469 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002470 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002471 }
2472
Chris Lattner43247a12005-08-25 19:12:10 +00002473 // Memoize node if it doesn't produce a flag.
2474 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002475 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002476 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002477 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002478 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002479 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002480 void *IP = 0;
2481 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2482 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002483 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002484 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002485 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002486 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002487 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002488 AllNodes.push_back(N);
2489 return SDOperand(N, 0);
2490}
2491
Duncan Sands83ec4b62008-06-06 12:08:01 +00002492SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002493 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002494 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002495 SDOperand Ops[] = { N1, N2, N3, N4 };
2496 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002497}
2498
Duncan Sands83ec4b62008-06-06 12:08:01 +00002499SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002500 SDOperand N1, SDOperand N2, SDOperand N3,
2501 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002502 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2503 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002504}
2505
Dan Gohman707e0182008-04-12 04:36:06 +00002506/// getMemsetValue - Vectorized representation of the memset value
2507/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002508static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2509 unsigned NumBits = VT.isVector() ?
2510 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002511 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002512 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002513 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002514 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002515 Val = (Val << Shift) | Val;
2516 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002517 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002518 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002519 return DAG.getConstant(Val, VT);
2520 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002521 }
Evan Chengf0df0312008-05-15 08:39:06 +00002522
2523 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2524 unsigned Shift = 8;
2525 for (unsigned i = NumBits; i > 8; i >>= 1) {
2526 Value = DAG.getNode(ISD::OR, VT,
2527 DAG.getNode(ISD::SHL, VT, Value,
2528 DAG.getConstant(Shift, MVT::i8)), Value);
2529 Shift <<= 1;
2530 }
2531
2532 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002533}
2534
Dan Gohman707e0182008-04-12 04:36:06 +00002535/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2536/// used when a memcpy is turned into a memset when the source is a constant
2537/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002538static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002539 const TargetLowering &TLI,
2540 std::string &Str, unsigned Offset) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002541 assert(!VT.isVector() && "Can't handle vector type here!");
2542 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002543 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002544 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002545 if (TLI.isLittleEndian())
2546 Offset = Offset + MSB - 1;
2547 for (unsigned i = 0; i != MSB; ++i) {
2548 Val = (Val << 8) | (unsigned char)Str[Offset];
2549 Offset += TLI.isLittleEndian() ? -1 : 1;
2550 }
2551 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002552}
2553
Dan Gohman707e0182008-04-12 04:36:06 +00002554/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002555///
Dan Gohman707e0182008-04-12 04:36:06 +00002556static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2557 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002559 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2560}
2561
Evan Chengf0df0312008-05-15 08:39:06 +00002562/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2563///
2564static bool isMemSrcFromString(SDOperand Src, std::string &Str,
2565 uint64_t &SrcOff) {
2566 unsigned SrcDelta = 0;
2567 GlobalAddressSDNode *G = NULL;
2568 if (Src.getOpcode() == ISD::GlobalAddress)
2569 G = cast<GlobalAddressSDNode>(Src);
2570 else if (Src.getOpcode() == ISD::ADD &&
2571 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2572 Src.getOperand(1).getOpcode() == ISD::Constant) {
2573 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2574 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2575 }
2576 if (!G)
2577 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002578
Evan Chengf0df0312008-05-15 08:39:06 +00002579 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2580 if (GV && GV->isConstant()) {
2581 Str = GV->getStringValue(false);
2582 if (!Str.empty()) {
2583 SrcOff += SrcDelta;
2584 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002585 }
2586 }
2587
Evan Chengf0df0312008-05-15 08:39:06 +00002588 return false;
2589}
Dan Gohman707e0182008-04-12 04:36:06 +00002590
Evan Chengf0df0312008-05-15 08:39:06 +00002591/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2592/// to replace the memset / memcpy is below the threshold. It also returns the
2593/// types of the sequence of memory ops to perform memset / memcpy.
2594static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002595bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002596 SDOperand Dst, SDOperand Src,
2597 unsigned Limit, uint64_t Size, unsigned &Align,
2598 SelectionDAG &DAG,
2599 const TargetLowering &TLI) {
2600 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
2601
2602 std::string Str;
2603 uint64_t SrcOff = 0;
2604 bool isSrcStr = isMemSrcFromString(Src, Str, SrcOff);
2605 bool isSrcConst = isa<ConstantSDNode>(Src);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002607 if (VT != MVT::iAny) {
2608 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002609 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002610 // If source is a string constant, this will require an unaligned load.
2611 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2612 if (Dst.getOpcode() != ISD::FrameIndex) {
2613 // Can't change destination alignment. It requires a unaligned store.
2614 if (AllowUnalign)
2615 VT = MVT::iAny;
2616 } else {
2617 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2618 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2619 if (MFI->isFixedObjectIndex(FI)) {
2620 // Can't change destination alignment. It requires a unaligned store.
2621 if (AllowUnalign)
2622 VT = MVT::iAny;
2623 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002624 // Give the stack frame object a larger alignment if needed.
2625 if (MFI->getObjectAlignment(FI) < NewAlign)
2626 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002627 Align = NewAlign;
2628 }
2629 }
2630 }
2631 }
2632
2633 if (VT == MVT::iAny) {
2634 if (AllowUnalign) {
2635 VT = MVT::i64;
2636 } else {
2637 switch (Align & 7) {
2638 case 0: VT = MVT::i64; break;
2639 case 4: VT = MVT::i32; break;
2640 case 2: VT = MVT::i16; break;
2641 default: VT = MVT::i8; break;
2642 }
2643 }
2644
Duncan Sands83ec4b62008-06-06 12:08:01 +00002645 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002646 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002647 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2648 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002649
Duncan Sands8e4eb092008-06-08 20:54:56 +00002650 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002651 VT = LVT;
2652 }
Dan Gohman707e0182008-04-12 04:36:06 +00002653
2654 unsigned NumMemOps = 0;
2655 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002656 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002657 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002658 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002660 VT = MVT::i64;
2661 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2663 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002664 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002665 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002666 VTSize >>= 1;
2667 }
Dan Gohman707e0182008-04-12 04:36:06 +00002668 }
Dan Gohman707e0182008-04-12 04:36:06 +00002669
2670 if (++NumMemOps > Limit)
2671 return false;
2672 MemOps.push_back(VT);
2673 Size -= VTSize;
2674 }
2675
2676 return true;
2677}
2678
2679static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2680 SDOperand Chain, SDOperand Dst,
2681 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002682 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002683 const Value *DstSV, uint64_t DstSVOff,
2684 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002685 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2686
Dan Gohman21323f32008-05-29 19:42:22 +00002687 // Expand memcpy to a series of load and store ops if the size operand falls
2688 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002689 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002690 uint64_t Limit = -1;
2691 if (!AlwaysInline)
2692 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002693 unsigned DstAlign = Align; // Destination alignment can change.
2694 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2695 DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002696 return SDOperand();
2697
Dan Gohman707e0182008-04-12 04:36:06 +00002698 std::string Str;
Dan Gohman1f13c682008-04-28 17:15:20 +00002699 uint64_t SrcOff = 0, DstOff = 0;
Evan Chengf0df0312008-05-15 08:39:06 +00002700 bool CopyFromStr = isMemSrcFromString(Src, Str, SrcOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002701
Evan Chengf0df0312008-05-15 08:39:06 +00002702 SmallVector<SDOperand, 8> OutChains;
2703 unsigned NumMemOps = MemOps.size();
Dan Gohman707e0182008-04-12 04:36:06 +00002704 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002705 MVT VT = MemOps[i];
2706 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002707 SDOperand Value, Store;
2708
Duncan Sands83ec4b62008-06-06 12:08:01 +00002709 if (CopyFromStr && !VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002710 // It's unlikely a store of a vector immediate can be done in a single
2711 // instruction. It would require a load from a constantpool first.
2712 // FIXME: Handle cases where store of vector immediate is done in a
2713 // single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002714 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002715 Store = DAG.getStore(Chain, Value,
2716 getMemBasePlusOffset(Dst, DstOff, DAG),
2717 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002718 } else {
2719 Value = DAG.getLoad(VT, Chain,
2720 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002721 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002722 Store = DAG.getStore(Chain, Value,
2723 getMemBasePlusOffset(Dst, DstOff, DAG),
2724 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002725 }
2726 OutChains.push_back(Store);
2727 SrcOff += VTSize;
2728 DstOff += VTSize;
2729 }
2730
2731 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2732 &OutChains[0], OutChains.size());
2733}
2734
Dan Gohman21323f32008-05-29 19:42:22 +00002735static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2736 SDOperand Chain, SDOperand Dst,
2737 SDOperand Src, uint64_t Size,
2738 unsigned Align, bool AlwaysInline,
2739 const Value *DstSV, uint64_t DstSVOff,
2740 const Value *SrcSV, uint64_t SrcSVOff){
2741 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2742
2743 // Expand memmove to a series of load and store ops if the size operand falls
2744 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002746 uint64_t Limit = -1;
2747 if (!AlwaysInline)
2748 Limit = TLI.getMaxStoresPerMemmove();
2749 unsigned DstAlign = Align; // Destination alignment can change.
2750 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2751 DAG, TLI))
2752 return SDOperand();
2753
Dan Gohman21323f32008-05-29 19:42:22 +00002754 uint64_t SrcOff = 0, DstOff = 0;
2755
2756 SmallVector<SDOperand, 8> LoadValues;
2757 SmallVector<SDOperand, 8> LoadChains;
2758 SmallVector<SDOperand, 8> OutChains;
2759 unsigned NumMemOps = MemOps.size();
2760 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 MVT VT = MemOps[i];
2762 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002763 SDOperand Value, Store;
2764
2765 Value = DAG.getLoad(VT, Chain,
2766 getMemBasePlusOffset(Src, SrcOff, DAG),
2767 SrcSV, SrcSVOff + SrcOff, false, Align);
2768 LoadValues.push_back(Value);
2769 LoadChains.push_back(Value.getValue(1));
2770 SrcOff += VTSize;
2771 }
2772 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2773 &LoadChains[0], LoadChains.size());
2774 OutChains.clear();
2775 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002776 MVT VT = MemOps[i];
2777 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002778 SDOperand Value, Store;
2779
2780 Store = DAG.getStore(Chain, LoadValues[i],
2781 getMemBasePlusOffset(Dst, DstOff, DAG),
2782 DstSV, DstSVOff + DstOff, false, DstAlign);
2783 OutChains.push_back(Store);
2784 DstOff += VTSize;
2785 }
2786
2787 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2788 &OutChains[0], OutChains.size());
2789}
2790
Dan Gohman707e0182008-04-12 04:36:06 +00002791static SDOperand getMemsetStores(SelectionDAG &DAG,
2792 SDOperand Chain, SDOperand Dst,
2793 SDOperand Src, uint64_t Size,
2794 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002795 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002796 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2797
2798 // Expand memset to a series of load/store ops if the size operand
2799 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002800 std::vector<MVT> MemOps;
Evan Chengf0df0312008-05-15 08:39:06 +00002801 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
2802 Size, Align, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002803 return SDOperand();
2804
2805 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002806 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002807
2808 unsigned NumMemOps = MemOps.size();
2809 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002810 MVT VT = MemOps[i];
2811 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002812 SDOperand Value = getMemsetValue(Src, VT, DAG);
2813 SDOperand Store = DAG.getStore(Chain, Value,
2814 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002815 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002816 OutChains.push_back(Store);
2817 DstOff += VTSize;
2818 }
2819
2820 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2821 &OutChains[0], OutChains.size());
2822}
2823
2824SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002825 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002826 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002827 const Value *DstSV, uint64_t DstSVOff,
2828 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002829
2830 // Check to see if we should lower the memcpy to loads and stores first.
2831 // For cases within the target-specified limits, this is the best choice.
2832 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2833 if (ConstantSize) {
2834 // Memcpy with size zero? Just return the original chain.
2835 if (ConstantSize->isNullValue())
2836 return Chain;
2837
2838 SDOperand Result =
2839 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002840 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002841 if (Result.Val)
2842 return Result;
2843 }
2844
2845 // Then check to see if we should lower the memcpy with target-specific
2846 // code. If the target chooses to do this, this is the next best.
2847 SDOperand Result =
2848 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2849 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002850 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002851 if (Result.Val)
2852 return Result;
2853
2854 // If we really need inline code and the target declined to provide it,
2855 // use a (potentially long) sequence of loads and stores.
2856 if (AlwaysInline) {
2857 assert(ConstantSize && "AlwaysInline requires a constant size!");
2858 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2859 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002860 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002861 }
2862
2863 // Emit a library call.
2864 TargetLowering::ArgListTy Args;
2865 TargetLowering::ArgListEntry Entry;
2866 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2867 Entry.Node = Dst; Args.push_back(Entry);
2868 Entry.Node = Src; Args.push_back(Entry);
2869 Entry.Node = Size; Args.push_back(Entry);
2870 std::pair<SDOperand,SDOperand> CallResult =
2871 TLI.LowerCallTo(Chain, Type::VoidTy,
2872 false, false, false, CallingConv::C, false,
2873 getExternalSymbol("memcpy", TLI.getPointerTy()),
2874 Args, *this);
2875 return CallResult.second;
2876}
2877
2878SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2879 SDOperand Src, SDOperand Size,
2880 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002881 const Value *DstSV, uint64_t DstSVOff,
2882 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002883
Dan Gohman21323f32008-05-29 19:42:22 +00002884 // Check to see if we should lower the memmove to loads and stores first.
2885 // For cases within the target-specified limits, this is the best choice.
2886 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2887 if (ConstantSize) {
2888 // Memmove with size zero? Just return the original chain.
2889 if (ConstantSize->isNullValue())
2890 return Chain;
2891
2892 SDOperand Result =
2893 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2894 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2895 if (Result.Val)
2896 return Result;
2897 }
Dan Gohman707e0182008-04-12 04:36:06 +00002898
2899 // Then check to see if we should lower the memmove with target-specific
2900 // code. If the target chooses to do this, this is the next best.
2901 SDOperand Result =
2902 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002903 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002904 if (Result.Val)
2905 return Result;
2906
2907 // Emit a library call.
2908 TargetLowering::ArgListTy Args;
2909 TargetLowering::ArgListEntry Entry;
2910 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2911 Entry.Node = Dst; Args.push_back(Entry);
2912 Entry.Node = Src; Args.push_back(Entry);
2913 Entry.Node = Size; Args.push_back(Entry);
2914 std::pair<SDOperand,SDOperand> CallResult =
2915 TLI.LowerCallTo(Chain, Type::VoidTy,
2916 false, false, false, CallingConv::C, false,
2917 getExternalSymbol("memmove", TLI.getPointerTy()),
2918 Args, *this);
2919 return CallResult.second;
2920}
2921
2922SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2923 SDOperand Src, SDOperand Size,
2924 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002925 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002926
2927 // Check to see if we should lower the memset to stores first.
2928 // For cases within the target-specified limits, this is the best choice.
2929 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2930 if (ConstantSize) {
2931 // Memset with size zero? Just return the original chain.
2932 if (ConstantSize->isNullValue())
2933 return Chain;
2934
2935 SDOperand Result =
2936 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002937 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002938 if (Result.Val)
2939 return Result;
2940 }
2941
2942 // Then check to see if we should lower the memset with target-specific
2943 // code. If the target chooses to do this, this is the next best.
2944 SDOperand Result =
2945 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002946 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002947 if (Result.Val)
2948 return Result;
2949
2950 // Emit a library call.
2951 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2952 TargetLowering::ArgListTy Args;
2953 TargetLowering::ArgListEntry Entry;
2954 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2955 Args.push_back(Entry);
2956 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002957 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00002958 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2959 else
2960 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2961 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2962 Args.push_back(Entry);
2963 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2964 Args.push_back(Entry);
2965 std::pair<SDOperand,SDOperand> CallResult =
2966 TLI.LowerCallTo(Chain, Type::VoidTy,
2967 false, false, false, CallingConv::C, false,
2968 getExternalSymbol("memset", TLI.getPointerTy()),
2969 Args, *this);
2970 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002971}
2972
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002973SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002974 SDOperand Ptr, SDOperand Cmp,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 SDOperand Swp, MVT VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002976 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002977 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2978 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002979 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002980 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002981 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00002982 ID.AddInteger(VT.getRawBits());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002983 void* IP = 0;
2984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2985 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002986 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002987 CSEMap.InsertNode(N, IP);
2988 AllNodes.push_back(N);
2989 return SDOperand(N, 0);
2990}
2991
2992SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002993 SDOperand Ptr, SDOperand Val,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002994 MVT VT) {
Mon P Wang63307c32008-05-05 19:05:59 +00002995 assert(( Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_LSS
2996 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
2997 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
2998 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
2999 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003000 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003001 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003002 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003003 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003004 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003005 ID.AddInteger(VT.getRawBits());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003006 void* IP = 0;
3007 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3008 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003009 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003010 CSEMap.InsertNode(N, IP);
3011 AllNodes.push_back(N);
3012 return SDOperand(N, 0);
3013}
3014
Duncan Sands14ea39c2008-03-27 20:23:40 +00003015SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003016SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003017 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003018 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003019 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003020 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003021 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3022 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003023 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003024 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003025 } else if (SV) {
3026 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3027 assert(PT && "Value for load must be a pointer");
3028 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003029 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003030 assert(Ty && "Could not get type information for load");
3031 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3032 }
Evan Cheng466685d2006-10-09 20:57:25 +00003033
Duncan Sands14ea39c2008-03-27 20:23:40 +00003034 if (VT == EVT) {
3035 ExtType = ISD::NON_EXTLOAD;
3036 } else if (ExtType == ISD::NON_EXTLOAD) {
3037 assert(VT == EVT && "Non-extending load from different memory type!");
3038 } else {
3039 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003040 if (VT.isVector())
3041 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003042 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003043 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003044 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003045 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003046 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003048 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003049 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003050
3051 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003052 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003053 "Unindexed load with an offset!");
3054
3055 SDVTList VTs = Indexed ?
3056 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3057 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003058 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003059 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003060 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003061 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003062 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003063 ID.AddInteger(Alignment);
3064 ID.AddInteger(isVolatile);
3065 void *IP = 0;
3066 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3067 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003068 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3069 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003070 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003071 AllNodes.push_back(N);
3072 return SDOperand(N, 0);
3073}
3074
Duncan Sands83ec4b62008-06-06 12:08:01 +00003075SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003076 SDOperand Chain, SDOperand Ptr,
3077 const Value *SV, int SVOffset,
3078 bool isVolatile, unsigned Alignment) {
3079 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003080 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3081 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003082}
3083
Duncan Sands83ec4b62008-06-06 12:08:01 +00003084SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003085 SDOperand Chain, SDOperand Ptr,
3086 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003088 bool isVolatile, unsigned Alignment) {
3089 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003090 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3091 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003092}
3093
Evan Cheng144d8f02006-11-09 17:55:04 +00003094SDOperand
3095SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3096 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003097 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003098 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3099 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003100 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3101 LD->getChain(), Base, Offset, LD->getSrcValue(),
3102 LD->getSrcValueOffset(), LD->getMemoryVT(),
3103 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003104}
3105
Jeff Cohend41b30d2006-11-05 19:31:28 +00003106SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003107 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003108 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003109 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003110
Dan Gohman575e2f42007-06-04 15:49:41 +00003111 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3112 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003113 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003114 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003115 } else if (SV) {
3116 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3117 assert(PT && "Value for store must be a pointer");
3118 Ty = PT->getElementType();
3119 }
3120 assert(Ty && "Could not get type information for store");
3121 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3122 }
Evan Chengad071e12006-10-05 22:57:11 +00003123 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003124 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003125 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003126 FoldingSetNodeID ID;
3127 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003128 ID.AddInteger(ISD::UNINDEXED);
3129 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003130 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003131 ID.AddInteger(Alignment);
3132 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003133 void *IP = 0;
3134 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3135 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003136 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003137 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003138 CSEMap.InsertNode(N, IP);
3139 AllNodes.push_back(N);
3140 return SDOperand(N, 0);
3141}
3142
Jeff Cohend41b30d2006-11-05 19:31:28 +00003143SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003144 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003145 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003146 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003147 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003148
3149 if (VT == SVT)
3150 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003151
Duncan Sands8e4eb092008-06-08 20:54:56 +00003152 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003153 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003154 "Can't do FP-INT conversion!");
3155
Dan Gohman575e2f42007-06-04 15:49:41 +00003156 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3157 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003158 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003159 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003160 } else if (SV) {
3161 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3162 assert(PT && "Value for store must be a pointer");
3163 Ty = PT->getElementType();
3164 }
3165 assert(Ty && "Could not get type information for store");
3166 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3167 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003168 SDVTList VTs = getVTList(MVT::Other);
3169 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003170 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003171 FoldingSetNodeID ID;
3172 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003173 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003174 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003175 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003176 ID.AddInteger(Alignment);
3177 ID.AddInteger(isVolatile);
3178 void *IP = 0;
3179 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3180 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003181 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003182 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003183 CSEMap.InsertNode(N, IP);
3184 AllNodes.push_back(N);
3185 return SDOperand(N, 0);
3186}
3187
Evan Cheng144d8f02006-11-09 17:55:04 +00003188SDOperand
3189SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3190 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003191 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3192 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3193 "Store is already a indexed store!");
3194 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3195 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3196 FoldingSetNodeID ID;
3197 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3198 ID.AddInteger(AM);
3199 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003200 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003201 ID.AddInteger(ST->getAlignment());
3202 ID.AddInteger(ST->isVolatile());
3203 void *IP = 0;
3204 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3205 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003206 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003207 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003208 ST->getSrcValue(), ST->getSrcValueOffset(),
3209 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003210 CSEMap.InsertNode(N, IP);
3211 AllNodes.push_back(N);
3212 return SDOperand(N, 0);
3213}
3214
Duncan Sands83ec4b62008-06-06 12:08:01 +00003215SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003216 SDOperand Chain, SDOperand Ptr,
3217 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003218 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003219 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003220}
3221
Duncan Sands83ec4b62008-06-06 12:08:01 +00003222SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003223 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003224 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003225 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003226 case 1: return getNode(Opcode, VT, Ops[0]);
3227 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3228 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003229 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003230 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003231
Chris Lattneref847df2005-04-09 03:27:28 +00003232 switch (Opcode) {
3233 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003234 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003235 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003236 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3237 "LHS and RHS of condition must have same type!");
3238 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3239 "True and False arms of SelectCC must have same type!");
3240 assert(Ops[2].getValueType() == VT &&
3241 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003242 break;
3243 }
3244 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003245 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003246 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3247 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003248 break;
3249 }
Chris Lattneref847df2005-04-09 03:27:28 +00003250 }
3251
Chris Lattner385328c2005-05-14 07:42:29 +00003252 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003253 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003254 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003255 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003256 FoldingSetNodeID ID;
3257 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003258 void *IP = 0;
3259 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3260 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003261 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003262 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003263 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003264 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003265 }
Chris Lattneref847df2005-04-09 03:27:28 +00003266 AllNodes.push_back(N);
3267 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003268}
3269
Chris Lattner89c34632005-05-14 06:20:26 +00003270SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003271 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003272 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003273 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3274 Ops, NumOps);
3275}
3276
3277SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003279 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003280 if (NumVTs == 1)
3281 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003282 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3283}
3284
3285SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003286 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003287 if (VTList.NumVTs == 1)
3288 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003289
Chris Lattner5f056bf2005-07-10 01:55:33 +00003290 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003291 // FIXME: figure out how to safely handle things like
3292 // int foo(int x) { return 1 << (x & 255); }
3293 // int bar() { return foo(256); }
3294#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003295 case ISD::SRA_PARTS:
3296 case ISD::SRL_PARTS:
3297 case ISD::SHL_PARTS:
3298 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003299 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003300 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3301 else if (N3.getOpcode() == ISD::AND)
3302 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3303 // If the and is only masking out bits that cannot effect the shift,
3304 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003305 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003306 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3307 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3308 }
3309 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003310#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003311 }
Chris Lattner89c34632005-05-14 06:20:26 +00003312
Chris Lattner43247a12005-08-25 19:12:10 +00003313 // Memoize the node unless it returns a flag.
3314 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003315 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003316 FoldingSetNodeID ID;
3317 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003318 void *IP = 0;
3319 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3320 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003321 if (NumOps == 1)
3322 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3323 else if (NumOps == 2)
3324 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3325 else if (NumOps == 3)
3326 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3327 else
3328 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003329 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003330 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003331 if (NumOps == 1)
3332 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3333 else if (NumOps == 2)
3334 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3335 else if (NumOps == 3)
3336 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3337 else
3338 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003339 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003340 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003341 return SDOperand(N, 0);
3342}
3343
Dan Gohman08ce9762007-10-08 15:49:58 +00003344SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003345 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003346}
3347
3348SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3349 SDOperand N1) {
3350 SDOperand Ops[] = { N1 };
3351 return getNode(Opcode, VTList, Ops, 1);
3352}
3353
3354SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3355 SDOperand N1, SDOperand N2) {
3356 SDOperand Ops[] = { N1, N2 };
3357 return getNode(Opcode, VTList, Ops, 2);
3358}
3359
3360SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3361 SDOperand N1, SDOperand N2, SDOperand N3) {
3362 SDOperand Ops[] = { N1, N2, N3 };
3363 return getNode(Opcode, VTList, Ops, 3);
3364}
3365
3366SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3367 SDOperand N1, SDOperand N2, SDOperand N3,
3368 SDOperand N4) {
3369 SDOperand Ops[] = { N1, N2, N3, N4 };
3370 return getNode(Opcode, VTList, Ops, 4);
3371}
3372
3373SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3374 SDOperand N1, SDOperand N2, SDOperand N3,
3375 SDOperand N4, SDOperand N5) {
3376 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3377 return getNode(Opcode, VTList, Ops, 5);
3378}
3379
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003381 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003382}
3383
Duncan Sands83ec4b62008-06-06 12:08:01 +00003384SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3385 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003386 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003387 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003388 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003389 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003390 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003391 V.push_back(VT1);
3392 V.push_back(VT2);
3393 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003394 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003395}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3397 MVT VT3) {
3398 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003399 E = VTList.end(); I != E; ++I) {
3400 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3401 (*I)[2] == VT3)
3402 return makeVTList(&(*I)[0], 3);
3403 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003404 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003405 V.push_back(VT1);
3406 V.push_back(VT2);
3407 V.push_back(VT3);
3408 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003409 return makeVTList(&(*VTList.begin())[0], 3);
3410}
3411
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003413 switch (NumVTs) {
3414 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003415 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003416 case 2: return getVTList(VTs[0], VTs[1]);
3417 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3418 default: break;
3419 }
3420
Duncan Sands83ec4b62008-06-06 12:08:01 +00003421 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() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3424
3425 bool NoMatch = false;
3426 for (unsigned i = 2; i != NumVTs; ++i)
3427 if (VTs[i] != (*I)[i]) {
3428 NoMatch = true;
3429 break;
3430 }
3431 if (!NoMatch)
3432 return makeVTList(&*I->begin(), NumVTs);
3433 }
3434
Duncan Sands83ec4b62008-06-06 12:08:01 +00003435 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003436 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003437}
3438
3439
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003440/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3441/// specified operands. If the resultant node already exists in the DAG,
3442/// this does not modify the specified node, instead it returns the node that
3443/// already exists. If the resultant node does not exist in the DAG, the
3444/// input node is returned. As a degenerate case, if you specify the same
3445/// input operands as the node already has, the input node is returned.
3446SDOperand SelectionDAG::
3447UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3448 SDNode *N = InN.Val;
3449 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3450
3451 // Check to see if there is no change.
3452 if (Op == N->getOperand(0)) return InN;
3453
3454 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003455 void *InsertPos = 0;
3456 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3457 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003458
3459 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003460 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003461 RemoveNodeFromCSEMaps(N);
3462
3463 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003464 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003465 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003466 N->OperandList[0].setUser(N);
3467 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003468
3469 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003470 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003471 return InN;
3472}
3473
3474SDOperand SelectionDAG::
3475UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3476 SDNode *N = InN.Val;
3477 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3478
3479 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003480 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3481 return InN; // No operands changed, just return the input node.
3482
3483 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003484 void *InsertPos = 0;
3485 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3486 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003487
3488 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003489 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003490 RemoveNodeFromCSEMaps(N);
3491
3492 // Now we update the operands.
3493 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003494 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003495 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003496 N->OperandList[0].setUser(N);
3497 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003498 }
3499 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003500 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003501 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003502 N->OperandList[1].setUser(N);
3503 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003504 }
3505
3506 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003507 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003508 return InN;
3509}
3510
3511SDOperand SelectionDAG::
3512UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003513 SDOperand Ops[] = { Op1, Op2, Op3 };
3514 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003515}
3516
3517SDOperand SelectionDAG::
3518UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3519 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003520 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3521 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003522}
3523
3524SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003525UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3526 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003527 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3528 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003529}
3530
Chris Lattner809ec112006-01-28 10:09:25 +00003531SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003532UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003533 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003534 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003535 "Update with wrong number of operands");
3536
3537 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003538 bool AnyChange = false;
3539 for (unsigned i = 0; i != NumOps; ++i) {
3540 if (Ops[i] != N->getOperand(i)) {
3541 AnyChange = true;
3542 break;
3543 }
3544 }
3545
3546 // No operands changed, just return the input node.
3547 if (!AnyChange) return InN;
3548
3549 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003550 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003551 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003552 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003553
Dan Gohman7ceda162008-05-02 00:05:03 +00003554 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003555 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003556 RemoveNodeFromCSEMaps(N);
3557
3558 // Now we update the operands.
3559 for (unsigned i = 0; i != NumOps; ++i) {
3560 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003561 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003562 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003563 N->OperandList[i].setUser(N);
3564 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003565 }
3566 }
3567
3568 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003569 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003570 return InN;
3571}
3572
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003573/// MorphNodeTo - This frees the operands of the current node, resets the
3574/// opcode, types, and operands to the specified value. This should only be
3575/// used by the SelectionDAG class.
3576void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003577 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003578 NodeType = Opc;
3579 ValueList = L.VTs;
3580 NumValues = L.NumVTs;
3581
3582 // Clear the operands list, updating used nodes to remove this from their
3583 // use list.
3584 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003585 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003586
3587 // If NumOps is larger than the # of operands we currently have, reallocate
3588 // the operand list.
3589 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003590 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003591 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003592 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003593 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003594 OperandsNeedDelete = true;
3595 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003596
3597 // Assign the new operands.
3598 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003599
3600 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3601 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003602 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003603 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003604 N->addUser(i, this);
3605 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003606 }
3607}
Chris Lattner149c58c2005-08-16 18:17:10 +00003608
3609/// SelectNodeTo - These are used for target selectors to *mutate* the
3610/// specified node to have the specified return type, Target opcode, and
3611/// operands. Note that target opcodes are stored as
3612/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003613///
3614/// Note that SelectNodeTo returns the resultant node. If there is already a
3615/// node of the specified opcode and operands, it returns that node instead of
3616/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003617SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003618 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003619 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003620 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003621 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003622 void *IP = 0;
3623 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003624 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003625
Chris Lattner7651fa42005-08-24 23:00:29 +00003626 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003627
Dan Gohman35b31be2008-04-17 23:02:12 +00003628 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003629
Chris Lattner4a283e92006-08-11 18:38:11 +00003630 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003631 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003632}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003633
Evan Cheng95514ba2006-08-26 08:00:10 +00003634SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003635 MVT VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003636 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003637 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003638 SDOperand Ops[] = { Op1 };
3639
Jim Laskey583bd472006-10-27 23:46:08 +00003640 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003641 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003642 void *IP = 0;
3643 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003644 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003645
Chris Lattner149c58c2005-08-16 18:17:10 +00003646 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003647 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003648 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003649 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003650}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003651
Evan Cheng95514ba2006-08-26 08:00:10 +00003652SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003654 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003655 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003656 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003657 SDOperand Ops[] = { Op1, Op2 };
3658
Jim Laskey583bd472006-10-27 23:46:08 +00003659 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003660 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003661 void *IP = 0;
3662 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003663 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003664
Chris Lattner149c58c2005-08-16 18:17:10 +00003665 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003666
Chris Lattner63e3f142007-02-04 07:28:00 +00003667 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003668
Chris Lattnera5682852006-08-07 23:03:03 +00003669 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003670 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003671}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003672
Evan Cheng95514ba2006-08-26 08:00:10 +00003673SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003674 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003675 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003676 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003677 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003678 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003679 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003680 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003681 void *IP = 0;
3682 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003683 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003684
Chris Lattner149c58c2005-08-16 18:17:10 +00003685 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003686
Chris Lattner63e3f142007-02-04 07:28:00 +00003687 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003688
Chris Lattnera5682852006-08-07 23:03:03 +00003689 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003690 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003691}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003692
Evan Cheng95514ba2006-08-26 08:00:10 +00003693SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003694 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003695 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003696 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003697 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003698 FoldingSetNodeID ID;
3699 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003700 void *IP = 0;
3701 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003702 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003703
Chris Lattner6b09a292005-08-21 18:49:33 +00003704 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003705 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003706
Chris Lattnera5682852006-08-07 23:03:03 +00003707 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003708 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003709}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003710
Evan Cheng95514ba2006-08-26 08:00:10 +00003711SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003713 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003714 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003715 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003716 SDOperand Ops[] = { Op1, Op2 };
3717 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003718 void *IP = 0;
3719 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003720 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003721
Chris Lattner0fb094f2005-11-19 01:44:53 +00003722 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003723 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003724 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003725 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003726}
3727
Evan Cheng95514ba2006-08-26 08:00:10 +00003728SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003729 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003730 SDOperand Op1, SDOperand Op2,
3731 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003732 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003733 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003734 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003735 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003736 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003737 void *IP = 0;
3738 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003739 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003740
Chris Lattner0fb094f2005-11-19 01:44:53 +00003741 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003742
Chris Lattner63e3f142007-02-04 07:28:00 +00003743 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003744 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003745 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003746}
3747
Chris Lattner0fb094f2005-11-19 01:44:53 +00003748
Evan Cheng6ae46c42006-02-09 07:15:23 +00003749/// getTargetNode - These are used for target selectors to create a new node
3750/// with specified return type(s), target opcode, and operands.
3751///
3752/// Note that getTargetNode returns the resultant node. If there is already a
3753/// node of the specified opcode and operands, it returns that node instead of
3754/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003756 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3757}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003758SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003759 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3760}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003761SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003762 SDOperand Op1, SDOperand Op2) {
3763 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3764}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003765SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003766 SDOperand Op1, SDOperand Op2,
3767 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003768 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3769}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003770SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003771 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003772 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003773}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003774SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3775 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003776 SDOperand Op;
3777 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3778}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003779SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3780 MVT VT2, SDOperand Op1) {
3781 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003782 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003783}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003784SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3785 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003786 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003787 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003788 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003789 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003790}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003791SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3792 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003793 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003794 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003795 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003796 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003797}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003798SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003799 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003800 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003801 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003802}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003804 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003805 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003806 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003807 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003808}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003809SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003810 SDOperand Op1, SDOperand Op2,
3811 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003812 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003813 SDOperand Ops[] = { Op1, Op2, Op3 };
3814 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3815}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003816SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003817 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003818 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003819 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003820}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003821SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3822 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003823 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003824 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003825 VTList.push_back(VT1);
3826 VTList.push_back(VT2);
3827 VTList.push_back(VT3);
3828 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003830 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3831}
Evan Cheng39305cf2007-10-05 01:10:49 +00003832SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003833 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003834 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003836 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3837 Ops, NumOps).Val;
3838}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003839
Evan Cheng08b11732008-03-22 01:55:50 +00003840/// getNodeIfExists - Get the specified node if it's already available, or
3841/// else return NULL.
3842SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003843 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003844 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3845 FoldingSetNodeID ID;
3846 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3847 void *IP = 0;
3848 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3849 return E;
3850 }
3851 return NULL;
3852}
3853
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003854
Evan Cheng99157a02006-08-07 22:13:29 +00003855/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003856/// This can cause recursive merging of nodes in the DAG.
3857///
Chris Lattner11d049c2008-02-03 03:35:22 +00003858/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003859///
Chris Lattner11d049c2008-02-03 03:35:22 +00003860void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003861 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003862 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003863 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003864 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003865 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003866
Chris Lattner8b8749f2005-08-17 19:00:20 +00003867 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003868 SDNode::use_iterator UI = From->use_begin();
3869 SDNode *U = UI->getUser();
3870
Chris Lattner8b8749f2005-08-17 19:00:20 +00003871 // This node is about to morph, remove its old self from the CSE maps.
3872 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003873 int operandNum = 0;
3874 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3875 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003876 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003877 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003878 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003879 I->setUser(U);
3880 To.Val->addUser(operandNum, U);
3881 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003882
3883 // Now that we have modified U, add it back to the CSE maps. If it already
3884 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003885 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003886 ReplaceAllUsesWith(U, Existing, UpdateListener);
3887 // U is now dead. Inform the listener if it exists and delete it.
3888 if (UpdateListener)
3889 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003890 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003891 } else {
3892 // If the node doesn't already exist, we updated it. Inform a listener if
3893 // it exists.
3894 if (UpdateListener)
3895 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003896 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003897 }
3898}
3899
3900/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3901/// This can cause recursive merging of nodes in the DAG.
3902///
3903/// This version assumes From/To have matching types and numbers of result
3904/// values.
3905///
Chris Lattner1e111c72005-09-07 05:37:01 +00003906void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003907 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003908 assert(From != To && "Cannot replace uses of with self");
3909 assert(From->getNumValues() == To->getNumValues() &&
3910 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003911 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003912 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3913 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003914
3915 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003916 SDNode::use_iterator UI = From->use_begin();
3917 SDNode *U = UI->getUser();
3918
Chris Lattner8b52f212005-08-26 18:36:28 +00003919 // This node is about to morph, remove its old self from the CSE maps.
3920 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003921 int operandNum = 0;
3922 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3923 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003924 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003925 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003926 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003927 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003928 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003929
Chris Lattner8b8749f2005-08-17 19:00:20 +00003930 // Now that we have modified U, add it back to the CSE maps. If it already
3931 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003932 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003933 ReplaceAllUsesWith(U, Existing, UpdateListener);
3934 // U is now dead. Inform the listener if it exists and delete it.
3935 if (UpdateListener)
3936 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003937 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003938 } else {
3939 // If the node doesn't already exist, we updated it. Inform a listener if
3940 // it exists.
3941 if (UpdateListener)
3942 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003943 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003944 }
3945}
3946
Chris Lattner8b52f212005-08-26 18:36:28 +00003947/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3948/// This can cause recursive merging of nodes in the DAG.
3949///
3950/// This version can replace From with any result values. To must match the
3951/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003952void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003953 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003954 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003955 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003956 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003957
3958 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003959 SDNode::use_iterator UI = From->use_begin();
3960 SDNode *U = UI->getUser();
3961
Chris Lattner7b2880c2005-08-24 22:44:39 +00003962 // This node is about to morph, remove its old self from the CSE maps.
3963 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003964 int operandNum = 0;
3965 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3966 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003967 if (I->getVal() == From) {
3968 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003969 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003970 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003971 I->setUser(U);
3972 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003973 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003974
Chris Lattner7b2880c2005-08-24 22:44:39 +00003975 // Now that we have modified U, add it back to the CSE maps. If it already
3976 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003977 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003978 ReplaceAllUsesWith(U, Existing, UpdateListener);
3979 // U is now dead. Inform the listener if it exists and delete it.
3980 if (UpdateListener)
3981 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003982 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003983 } else {
3984 // If the node doesn't already exist, we updated it. Inform a listener if
3985 // it exists.
3986 if (UpdateListener)
3987 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003988 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003989 }
3990}
3991
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003992namespace {
3993 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3994 /// any deleted nodes from the set passed into its constructor and recursively
3995 /// notifies another update listener if specified.
3996 class ChainedSetUpdaterListener :
3997 public SelectionDAG::DAGUpdateListener {
3998 SmallSetVector<SDNode*, 16> &Set;
3999 SelectionDAG::DAGUpdateListener *Chain;
4000 public:
4001 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4002 SelectionDAG::DAGUpdateListener *chain)
4003 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004004
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004005 virtual void NodeDeleted(SDNode *N) {
4006 Set.remove(N);
4007 if (Chain) Chain->NodeDeleted(N);
4008 }
4009 virtual void NodeUpdated(SDNode *N) {
4010 if (Chain) Chain->NodeUpdated(N);
4011 }
4012 };
4013}
4014
Chris Lattner012f2412006-02-17 21:58:01 +00004015/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4016/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004017/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004018void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004019 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004020 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004021
Chris Lattner012f2412006-02-17 21:58:01 +00004022 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004023 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004024 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004025 return;
4026 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004027
4028 if (From.use_empty()) return;
4029
Chris Lattnercf5640b2007-02-04 00:14:31 +00004030 // Get all of the users of From.Val. We want these in a nice,
4031 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004032 SmallSetVector<SDNode*, 16> Users;
4033 for (SDNode::use_iterator UI = From.Val->use_begin(),
4034 E = From.Val->use_end(); UI != E; ++UI) {
4035 SDNode *User = UI->getUser();
4036 if (!Users.count(User))
4037 Users.insert(User);
4038 }
Chris Lattner012f2412006-02-17 21:58:01 +00004039
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004040 // When one of the recursive merges deletes nodes from the graph, we need to
4041 // make sure that UpdateListener is notified *and* that the node is removed
4042 // from Users if present. CSUL does this.
4043 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004044
Chris Lattner012f2412006-02-17 21:58:01 +00004045 while (!Users.empty()) {
4046 // We know that this user uses some value of From. If it is the right
4047 // value, update it.
4048 SDNode *User = Users.back();
4049 Users.pop_back();
4050
Chris Lattner01d029b2007-10-15 06:10:22 +00004051 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004052 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004053 for (; Op != E; ++Op)
4054 if (*Op == From) break;
4055
4056 // If there are no matches, the user must use some other result of From.
4057 if (Op == E) continue;
4058
4059 // Okay, we know this user needs to be updated. Remove its old self
4060 // from the CSE maps.
4061 RemoveNodeFromCSEMaps(User);
4062
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004063 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004064 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004065 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004066 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004067 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004068 Op->setUser(User);
4069 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004070 }
4071 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004072
4073 // Now that we have modified User, add it back to the CSE maps. If it
4074 // already exists there, recursively merge the results together.
4075 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004076 if (!Existing) {
4077 if (UpdateListener) UpdateListener->NodeUpdated(User);
4078 continue; // Continue on to next user.
4079 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004080
4081 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004083 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004084 // can cause deletion of nodes that used the old value. To handle this, we
4085 // use CSUL to remove them from the Users set.
4086 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004087
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004088 // User is now dead. Notify a listener if present.
4089 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00004090 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004091 }
4092}
4093
Evan Chenge6f35d82006-08-01 08:20:41 +00004094/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4095/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004096unsigned SelectionDAG::AssignNodeIds() {
4097 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004098 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4099 SDNode *N = I;
4100 N->setNodeId(Id++);
4101 }
4102 return Id;
4103}
4104
Evan Chenge6f35d82006-08-01 08:20:41 +00004105/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004106/// based on their topological order. It returns the maximum id and a vector
4107/// of the SDNodes* in assigned order by reference.
4108unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004109 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004110 std::vector<unsigned> InDegree(DAGSize);
4111 std::vector<SDNode*> Sources;
4112
4113 // Use a two pass approach to avoid using a std::map which is slow.
4114 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004115 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4116 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004117 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004118 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004119 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004120 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004121 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004122 }
4123
Evan Cheng99157a02006-08-07 22:13:29 +00004124 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004125 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004126 SDNode *N = Sources.back();
4127 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004128 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004129 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004130 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004131 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004132 if (Degree == 0)
4133 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004134 }
4135 }
4136
Evan Chengc384d6c2006-08-02 22:00:34 +00004137 // Second pass, assign the actual topological order as node ids.
4138 Id = 0;
4139 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4140 TI != TE; ++TI)
4141 (*TI)->setNodeId(Id++);
4142
4143 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004144}
4145
4146
Evan Cheng091cba12006-07-27 06:39:06 +00004147
Jim Laskey58b968b2005-08-17 20:08:02 +00004148//===----------------------------------------------------------------------===//
4149// SDNode Class
4150//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004151
Chris Lattner917d2c92006-07-19 00:00:37 +00004152// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004153void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004154void UnarySDNode::ANCHOR() {}
4155void BinarySDNode::ANCHOR() {}
4156void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004157void HandleSDNode::ANCHOR() {}
4158void StringSDNode::ANCHOR() {}
4159void ConstantSDNode::ANCHOR() {}
4160void ConstantFPSDNode::ANCHOR() {}
4161void GlobalAddressSDNode::ANCHOR() {}
4162void FrameIndexSDNode::ANCHOR() {}
4163void JumpTableSDNode::ANCHOR() {}
4164void ConstantPoolSDNode::ANCHOR() {}
4165void BasicBlockSDNode::ANCHOR() {}
4166void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004167void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004168void RegisterSDNode::ANCHOR() {}
4169void ExternalSymbolSDNode::ANCHOR() {}
4170void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004171void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004172void VTSDNode::ANCHOR() {}
4173void LoadSDNode::ANCHOR() {}
4174void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004175void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004176
Chris Lattner48b85922007-02-04 02:41:42 +00004177HandleSDNode::~HandleSDNode() {
4178 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004179 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004180}
4181
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004182GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004184 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004185 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004186 // Thread Local
4187 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4188 // Non Thread Local
4189 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4190 getSDVTList(VT)), Offset(o) {
4191 TheGlobal = const_cast<GlobalValue*>(GA);
4192}
Chris Lattner48b85922007-02-04 02:41:42 +00004193
Dan Gohman36b5c132008-04-07 19:35:22 +00004194/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004195/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004196MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004198 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004199 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4200 MachineMemOperand::MOStore;
4201 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004202
4203 // Check if the load references a frame index, and does not have
4204 // an SV attached.
4205 const FrameIndexSDNode *FI =
4206 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4207 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004208 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4209 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00004210 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004211 return MachineMemOperand(getSrcValue(), Flags,
4212 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00004213}
4214
Jim Laskey583bd472006-10-27 23:46:08 +00004215/// Profile - Gather unique data for the node.
4216///
4217void SDNode::Profile(FoldingSetNodeID &ID) {
4218 AddNodeIDNode(ID, this);
4219}
4220
Chris Lattnera3255112005-11-08 23:30:28 +00004221/// getValueTypeList - Return a pointer to the specified value type.
4222///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223const MVT *SDNode::getValueTypeList(MVT VT) {
4224 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004225 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004226 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004227 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004228 static MVT VTs[MVT::LAST_VALUETYPE];
4229 VTs[VT.getSimpleVT()] = VT;
4230 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004231 }
Chris Lattnera3255112005-11-08 23:30:28 +00004232}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004233
Chris Lattner5c884562005-01-12 18:37:47 +00004234/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4235/// indicated value. This method ignores uses of other values defined by this
4236/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004237bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004238 assert(Value < getNumValues() && "Bad value!");
4239
4240 // If there is only one value, this is easy.
4241 if (getNumValues() == 1)
4242 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004243 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004244
Evan Cheng4ee62112006-02-05 06:29:23 +00004245 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004246
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004247 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004248
Roman Levensteindc1adac2008-04-07 10:06:32 +00004249 // TODO: Only iterate over uses of a given value of the node
4250 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4251 if (*UI == TheValue) {
4252 if (NUses == 0)
4253 return false;
4254 --NUses;
4255 }
Chris Lattner5c884562005-01-12 18:37:47 +00004256 }
4257
4258 // Found exactly the right number of uses?
4259 return NUses == 0;
4260}
4261
4262
Evan Cheng33d55952007-08-02 05:29:38 +00004263/// hasAnyUseOfValue - Return true if there are any use of the indicated
4264/// value. This method ignores uses of other values defined by this operation.
4265bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4266 assert(Value < getNumValues() && "Bad value!");
4267
Dan Gohman30359592008-01-29 13:02:09 +00004268 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004269
4270 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4271
4272 SmallPtrSet<SDNode*, 32> UsersHandled;
4273
Roman Levensteindc1adac2008-04-07 10:06:32 +00004274 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4275 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004276 if (User->getNumOperands() == 1 ||
4277 UsersHandled.insert(User)) // First time we've seen this?
4278 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4279 if (User->getOperand(i) == TheValue) {
4280 return true;
4281 }
4282 }
4283
4284 return false;
4285}
4286
4287
Evan Cheng917be682008-03-04 00:41:45 +00004288/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004289///
Evan Cheng917be682008-03-04 00:41:45 +00004290bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004291 bool Seen = false;
4292 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004293 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004294 if (User == this)
4295 Seen = true;
4296 else
4297 return false;
4298 }
4299
4300 return Seen;
4301}
4302
Evan Chenge6e97e62006-11-03 07:31:32 +00004303/// isOperand - Return true if this node is an operand of N.
4304///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004305bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004306 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4307 if (*this == N->getOperand(i))
4308 return true;
4309 return false;
4310}
4311
Evan Cheng917be682008-03-04 00:41:45 +00004312bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004313 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004314 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004315 return true;
4316 return false;
4317}
Evan Cheng4ee62112006-02-05 06:29:23 +00004318
Chris Lattner572dee72008-01-16 05:49:24 +00004319/// reachesChainWithoutSideEffects - Return true if this operand (which must
4320/// be a chain) reaches the specified operand without crossing any
4321/// side-effecting instructions. In practice, this looks through token
4322/// factors and non-volatile loads. In order to remain efficient, this only
4323/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004324bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004325 unsigned Depth) const {
4326 if (*this == Dest) return true;
4327
4328 // Don't search too deeply, we just want to be able to see through
4329 // TokenFactor's etc.
4330 if (Depth == 0) return false;
4331
4332 // If this is a token factor, all inputs to the TF happen in parallel. If any
4333 // of the operands of the TF reach dest, then we can do the xform.
4334 if (getOpcode() == ISD::TokenFactor) {
4335 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4336 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4337 return true;
4338 return false;
4339 }
4340
4341 // Loads don't have side effects, look through them.
4342 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4343 if (!Ld->isVolatile())
4344 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4345 }
4346 return false;
4347}
4348
4349
Evan Chengc5fc57d2006-11-03 03:05:24 +00004350static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004351 SmallPtrSet<SDNode *, 32> &Visited) {
4352 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004353 return;
4354
4355 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4356 SDNode *Op = N->getOperand(i).Val;
4357 if (Op == P) {
4358 found = true;
4359 return;
4360 }
4361 findPredecessor(Op, P, found, Visited);
4362 }
4363}
4364
Evan Cheng917be682008-03-04 00:41:45 +00004365/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004366/// is either an operand of N or it can be reached by recursively traversing
4367/// up the operands.
4368/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004369bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004370 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004371 bool found = false;
4372 findPredecessor(N, this, found, Visited);
4373 return found;
4374}
4375
Evan Chengc5484282006-10-04 00:56:09 +00004376uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4377 assert(Num < NumOperands && "Invalid child # of SDNode!");
4378 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4379}
4380
Reid Spencer577cc322007-04-01 07:32:19 +00004381std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004382 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004383 default:
4384 if (getOpcode() < ISD::BUILTIN_OP_END)
4385 return "<<Unknown DAG Node>>";
4386 else {
Evan Cheng72261582005-12-20 06:22:03 +00004387 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004388 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004389 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004390 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004391
Evan Cheng72261582005-12-20 06:22:03 +00004392 TargetLowering &TLI = G->getTargetLoweringInfo();
4393 const char *Name =
4394 TLI.getTargetNodeName(getOpcode());
4395 if (Name) return Name;
4396 }
4397
4398 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004399 }
4400
Evan Cheng27b7db52008-03-08 00:58:38 +00004401 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004402 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004403 case ISD::ATOMIC_LCS: return "AtomicLCS";
4404 case ISD::ATOMIC_LAS: return "AtomicLAS";
Mon P Wang63307c32008-05-05 19:05:59 +00004405 case ISD::ATOMIC_LSS: return "AtomicLSS";
4406 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4407 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4408 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
4409 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4410 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4411 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4412 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4413 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004414 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004415 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004416 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004417 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004418 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004419 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004420 case ISD::AssertSext: return "AssertSext";
4421 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004422
4423 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004424 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004425 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004426 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004427 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004428
4429 case ISD::Constant: return "Constant";
4430 case ISD::ConstantFP: return "ConstantFP";
4431 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004432 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004433 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004434 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004435 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004436 case ISD::RETURNADDR: return "RETURNADDR";
4437 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004438 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004439 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4440 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004441 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004442 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004443 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004444 case ISD::INTRINSIC_WO_CHAIN: {
4445 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4446 return Intrinsic::getName((Intrinsic::ID)IID);
4447 }
4448 case ISD::INTRINSIC_VOID:
4449 case ISD::INTRINSIC_W_CHAIN: {
4450 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004451 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004452 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004453
Chris Lattnerb2827b02006-03-19 00:52:58 +00004454 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004455 case ISD::TargetConstant: return "TargetConstant";
4456 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004457 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004458 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004459 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004460 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004461 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004462 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004463
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004464 case ISD::CopyToReg: return "CopyToReg";
4465 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004466 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004467 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004468 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004469 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004470 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004471 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004472 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004473 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004474
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004475 // Unary operators
4476 case ISD::FABS: return "fabs";
4477 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004478 case ISD::FSQRT: return "fsqrt";
4479 case ISD::FSIN: return "fsin";
4480 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004481 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004482 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004483
4484 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004485 case ISD::ADD: return "add";
4486 case ISD::SUB: return "sub";
4487 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004488 case ISD::MULHU: return "mulhu";
4489 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004490 case ISD::SDIV: return "sdiv";
4491 case ISD::UDIV: return "udiv";
4492 case ISD::SREM: return "srem";
4493 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004494 case ISD::SMUL_LOHI: return "smul_lohi";
4495 case ISD::UMUL_LOHI: return "umul_lohi";
4496 case ISD::SDIVREM: return "sdivrem";
4497 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004498 case ISD::AND: return "and";
4499 case ISD::OR: return "or";
4500 case ISD::XOR: return "xor";
4501 case ISD::SHL: return "shl";
4502 case ISD::SRA: return "sra";
4503 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004504 case ISD::ROTL: return "rotl";
4505 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004506 case ISD::FADD: return "fadd";
4507 case ISD::FSUB: return "fsub";
4508 case ISD::FMUL: return "fmul";
4509 case ISD::FDIV: return "fdiv";
4510 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004511 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004512 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004513
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004514 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004515 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004516 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004517 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004518 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004519 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004520 case ISD::CONCAT_VECTORS: return "concat_vectors";
4521 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004522 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004523 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004524 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004525 case ISD::ADDC: return "addc";
4526 case ISD::ADDE: return "adde";
4527 case ISD::SUBC: return "subc";
4528 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004529 case ISD::SHL_PARTS: return "shl_parts";
4530 case ISD::SRA_PARTS: return "sra_parts";
4531 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004532
4533 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4534 case ISD::INSERT_SUBREG: return "insert_subreg";
4535
Chris Lattner7f644642005-04-28 21:44:03 +00004536 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004537 case ISD::SIGN_EXTEND: return "sign_extend";
4538 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004539 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004540 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004541 case ISD::TRUNCATE: return "truncate";
4542 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004543 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004544 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004545 case ISD::FP_EXTEND: return "fp_extend";
4546
4547 case ISD::SINT_TO_FP: return "sint_to_fp";
4548 case ISD::UINT_TO_FP: return "uint_to_fp";
4549 case ISD::FP_TO_SINT: return "fp_to_sint";
4550 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004551 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004552
4553 // Control flow instructions
4554 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004555 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004556 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004557 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004558 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004559 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004560 case ISD::CALLSEQ_START: return "callseq_start";
4561 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004562
4563 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004564 case ISD::LOAD: return "load";
4565 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004566 case ISD::VAARG: return "vaarg";
4567 case ISD::VACOPY: return "vacopy";
4568 case ISD::VAEND: return "vaend";
4569 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004570 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004571 case ISD::EXTRACT_ELEMENT: return "extract_element";
4572 case ISD::BUILD_PAIR: return "build_pair";
4573 case ISD::STACKSAVE: return "stacksave";
4574 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004575 case ISD::TRAP: return "trap";
4576
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004577 // Bit manipulation
4578 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004579 case ISD::CTPOP: return "ctpop";
4580 case ISD::CTTZ: return "cttz";
4581 case ISD::CTLZ: return "ctlz";
4582
Chris Lattner36ce6912005-11-29 06:21:05 +00004583 // Debug info
4584 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004585 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004586
Duncan Sands36397f52007-07-27 12:58:54 +00004587 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004588 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004589
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004590 case ISD::CONDCODE:
4591 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004592 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004593 case ISD::SETOEQ: return "setoeq";
4594 case ISD::SETOGT: return "setogt";
4595 case ISD::SETOGE: return "setoge";
4596 case ISD::SETOLT: return "setolt";
4597 case ISD::SETOLE: return "setole";
4598 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004599
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004600 case ISD::SETO: return "seto";
4601 case ISD::SETUO: return "setuo";
4602 case ISD::SETUEQ: return "setue";
4603 case ISD::SETUGT: return "setugt";
4604 case ISD::SETUGE: return "setuge";
4605 case ISD::SETULT: return "setult";
4606 case ISD::SETULE: return "setule";
4607 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004608
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004609 case ISD::SETEQ: return "seteq";
4610 case ISD::SETGT: return "setgt";
4611 case ISD::SETGE: return "setge";
4612 case ISD::SETLT: return "setlt";
4613 case ISD::SETLE: return "setle";
4614 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004615 }
4616 }
4617}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004618
Evan Cheng144d8f02006-11-09 17:55:04 +00004619const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004620 switch (AM) {
4621 default:
4622 return "";
4623 case ISD::PRE_INC:
4624 return "<pre-inc>";
4625 case ISD::PRE_DEC:
4626 return "<pre-dec>";
4627 case ISD::POST_INC:
4628 return "<post-inc>";
4629 case ISD::POST_DEC:
4630 return "<post-dec>";
4631 }
4632}
4633
Duncan Sands276dcbd2008-03-21 09:14:45 +00004634std::string ISD::ArgFlagsTy::getArgFlagsString() {
4635 std::string S = "< ";
4636
4637 if (isZExt())
4638 S += "zext ";
4639 if (isSExt())
4640 S += "sext ";
4641 if (isInReg())
4642 S += "inreg ";
4643 if (isSRet())
4644 S += "sret ";
4645 if (isByVal())
4646 S += "byval ";
4647 if (isNest())
4648 S += "nest ";
4649 if (getByValAlign())
4650 S += "byval-align:" + utostr(getByValAlign()) + " ";
4651 if (getOrigAlign())
4652 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4653 if (getByValSize())
4654 S += "byval-size:" + utostr(getByValSize()) + " ";
4655 return S + ">";
4656}
4657
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004658void SDNode::dump() const { dump(0); }
4659void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004660 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004661
4662 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004663 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004664 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004665 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004666 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004667 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004668 }
Bill Wendling832171c2006-12-07 20:04:42 +00004669 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004670
Bill Wendling832171c2006-12-07 20:04:42 +00004671 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004672 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004673 if (i) cerr << ", ";
4674 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004675 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004676 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004677 }
4678
Evan Chengce254432007-12-11 02:08:35 +00004679 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4680 SDNode *Mask = getOperand(2).Val;
4681 cerr << "<";
4682 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4683 if (i) cerr << ",";
4684 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4685 cerr << "u";
4686 else
4687 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4688 }
4689 cerr << ">";
4690 }
4691
Chris Lattnerc3aae252005-01-07 07:46:32 +00004692 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004693 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004694 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004695 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4696 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4697 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4698 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4699 else {
4700 cerr << "<APFloat(";
4701 CSDN->getValueAPF().convertToAPInt().dump();
4702 cerr << ")>";
4703 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004704 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004705 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004706 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004707 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004708 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004709 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004710 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004711 else
Bill Wendling832171c2006-12-07 20:04:42 +00004712 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004713 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004714 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004715 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004716 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004717 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004718 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004719 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004720 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004721 else
Bill Wendling832171c2006-12-07 20:04:42 +00004722 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004723 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004724 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004725 else
Bill Wendling832171c2006-12-07 20:04:42 +00004726 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004727 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004728 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004729 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4730 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004731 cerr << LBB->getName() << " ";
4732 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004733 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004734 if (G && R->getReg() &&
4735 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004736 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004737 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004738 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004739 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004740 } else if (const ExternalSymbolSDNode *ES =
4741 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004742 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004743 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4744 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004745 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004746 else
Dan Gohman69de1932008-02-06 22:27:42 +00004747 cerr << "<null>";
4748 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4749 if (M->MO.getValue())
4750 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4751 else
4752 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004753 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4754 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004755 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004756 cerr << ":" << N->getVT().getMVTString();
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004757 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004758 const Value *SrcValue = LD->getSrcValue();
4759 int SrcOffset = LD->getSrcValueOffset();
4760 cerr << " <";
4761 if (SrcValue)
4762 cerr << SrcValue;
4763 else
4764 cerr << "null";
4765 cerr << ":" << SrcOffset << ">";
4766
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004767 bool doExt = true;
4768 switch (LD->getExtensionType()) {
4769 default: doExt = false; break;
4770 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004771 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004772 break;
4773 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004774 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004775 break;
4776 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004777 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004778 break;
4779 }
4780 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004781 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004782
Evan Cheng144d8f02006-11-09 17:55:04 +00004783 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004784 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004785 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004786 if (LD->isVolatile())
4787 cerr << " <volatile>";
4788 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004789 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004790 const Value *SrcValue = ST->getSrcValue();
4791 int SrcOffset = ST->getSrcValueOffset();
4792 cerr << " <";
4793 if (SrcValue)
4794 cerr << SrcValue;
4795 else
4796 cerr << "null";
4797 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004798
4799 if (ST->isTruncatingStore())
4800 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004801 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004802
4803 const char *AM = getIndexedModeName(ST->getAddressingMode());
4804 if (*AM)
4805 cerr << " " << AM;
4806 if (ST->isVolatile())
4807 cerr << " <volatile>";
4808 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004809 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004810}
4811
Chris Lattnerde202b32005-11-09 23:47:37 +00004812static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004813 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4814 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004815 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004816 else
Bill Wendling832171c2006-12-07 20:04:42 +00004817 cerr << "\n" << std::string(indent+2, ' ')
4818 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004819
Chris Lattnerea946cd2005-01-09 20:38:33 +00004820
Bill Wendling832171c2006-12-07 20:04:42 +00004821 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004822 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004823}
4824
Chris Lattnerc3aae252005-01-07 07:46:32 +00004825void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004826 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004827 std::vector<const SDNode*> Nodes;
4828 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4829 I != E; ++I)
4830 Nodes.push_back(I);
4831
Chris Lattner49d24712005-01-09 20:26:36 +00004832 std::sort(Nodes.begin(), Nodes.end());
4833
4834 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004835 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004836 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004837 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004838
Jim Laskey26f7fa72006-10-17 19:33:52 +00004839 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004840
Bill Wendling832171c2006-12-07 20:04:42 +00004841 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004842}
4843
Evan Chengd6594ae2006-09-12 21:00:35 +00004844const Type *ConstantPoolSDNode::getType() const {
4845 if (isMachineConstantPoolEntry())
4846 return Val.MachineCPVal->getType();
4847 return Val.ConstVal->getType();
4848}