blob: a9b7381a096c75d32c88211bf5c8c4ed2911c5a3 [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.
43static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
44 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Chris Lattnerec4a5672008-03-05 06:48:13 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
49 switch (VT) {
50 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
Dale Johannesenf04afdb2007-08-30 00:23:21 +000073bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
74 const APFloat& Val) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000075 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
76
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());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000419 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
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());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000428 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
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: {
577 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
578 if (MVT::isExtendedVT(VT)) {
579 Erased = ExtendedValueTypeNodes.erase(VT);
580 } else {
581 Erased = ValueTypeNodes[VT] != 0;
582 ValueTypeNodes[VT] = 0;
583 }
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());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000687 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
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());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000693 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
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
Chris Lattner0f2287b2005-04-13 02:38:18 +0000715SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType 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(),
718 MVT::getSizeInBits(VT));
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
Chris Lattner61b09412006-08-11 21:01:22 +0000732SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000733 MVT::ValueType EltVT =
734 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
735
736 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
737}
738
739SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000740 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000741
742 MVT::ValueType EltVT =
743 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000744
Dan Gohman6394b092008-02-08 22:59:30 +0000745 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
746 "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)))
755 if (!MVT::isVector(VT))
756 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);
764 if (MVT::isVector(VT)) {
765 SmallVector<SDOperand, 8> Ops;
766 Ops.assign(MVT::getVectorNumElements(VT), Result);
767 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
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000777SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000778 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000780
Dan Gohman7f321562007-06-25 16:23:39 +0000781 MVT::ValueType EltVT =
782 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783
Chris Lattnerd8658612005-02-17 20:17:32 +0000784 // Do the map lookup using the actual bit pattern for the floating point
785 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
786 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000787 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000789 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000790 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000791 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000792 SDNode *N = NULL;
793 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
794 if (!MVT::isVector(VT))
795 return SDOperand(N, 0);
796 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000797 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000798 CSEMap.InsertNode(N, IP);
799 AllNodes.push_back(N);
800 }
801
Dan Gohman7f321562007-06-25 16:23:39 +0000802 SDOperand Result(N, 0);
803 if (MVT::isVector(VT)) {
804 SmallVector<SDOperand, 8> Ops;
805 Ops.assign(MVT::getVectorNumElements(VT), Result);
806 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
807 }
808 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000809}
810
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000811SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
812 bool isTarget) {
813 MVT::ValueType EltVT =
814 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
815 if (EltVT==MVT::f32)
816 return getConstantFP(APFloat((float)Val), VT, isTarget);
817 else
818 return getConstantFP(APFloat(Val), VT, isTarget);
819}
820
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000822 MVT::ValueType VT, int Offset,
823 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000824 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000825
826 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
827 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000828 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000829 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
830 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
831 }
832
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000833 if (GVar && GVar->isThreadLocal())
834 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
835 else
836 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000837
Jim Laskey583bd472006-10-27 23:46:08 +0000838 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000839 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000840 ID.AddPointer(GV);
841 ID.AddInteger(Offset);
842 void *IP = 0;
843 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
844 return SDOperand(E, 0);
845 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
846 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000847 AllNodes.push_back(N);
848 return SDOperand(N, 0);
849}
850
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000851SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
852 bool isTarget) {
853 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000854 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000855 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000856 ID.AddInteger(FI);
857 void *IP = 0;
858 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
859 return SDOperand(E, 0);
860 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
861 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
864}
865
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000866SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
867 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000868 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000869 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000870 ID.AddInteger(JTI);
871 void *IP = 0;
872 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
873 return SDOperand(E, 0);
874 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
875 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000876 AllNodes.push_back(N);
877 return SDOperand(N, 0);
878}
879
Evan Chengb8973bd2006-01-31 22:23:14 +0000880SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000881 unsigned Alignment, int Offset,
882 bool isTarget) {
883 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000884 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000885 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000886 ID.AddInteger(Alignment);
887 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000888 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000889 void *IP = 0;
890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
891 return SDOperand(E, 0);
892 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
893 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000894 AllNodes.push_back(N);
895 return SDOperand(N, 0);
896}
897
Chris Lattnerc3aae252005-01-07 07:46:32 +0000898
Evan Chengd6594ae2006-09-12 21:00:35 +0000899SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
900 MVT::ValueType VT,
901 unsigned Alignment, int Offset,
902 bool isTarget) {
903 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000904 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000905 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000906 ID.AddInteger(Alignment);
907 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000908 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000909 void *IP = 0;
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
911 return SDOperand(E, 0);
912 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
913 CSEMap.InsertNode(N, IP);
914 AllNodes.push_back(N);
915 return SDOperand(N, 0);
916}
917
918
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000920 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000921 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000922 ID.AddPointer(MBB);
923 void *IP = 0;
924 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
925 return SDOperand(E, 0);
926 SDNode *N = new BasicBlockSDNode(MBB);
927 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 AllNodes.push_back(N);
929 return SDOperand(N, 0);
930}
931
Duncan Sands276dcbd2008-03-21 09:14:45 +0000932SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
933 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000934 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000935 ID.AddInteger(Flags.getRawBits());
936 void *IP = 0;
937 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
938 return SDOperand(E, 0);
939 SDNode *N = new ARG_FLAGSSDNode(Flags);
940 CSEMap.InsertNode(N, IP);
941 AllNodes.push_back(N);
942 return SDOperand(N, 0);
943}
944
Chris Lattner15e4b012005-07-10 00:07:11 +0000945SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000946 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000947 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000948
Duncan Sandsf411b832007-10-17 13:49:58 +0000949 SDNode *&N = MVT::isExtendedVT(VT) ?
950 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
951
952 if (N) return SDOperand(N, 0);
953 N = new VTSDNode(VT);
954 AllNodes.push_back(N);
955 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000956}
957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
959 SDNode *&N = ExternalSymbols[Sym];
960 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000961 N = new ExternalSymbolSDNode(false, Sym, VT);
962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
964}
965
Chris Lattner809ec112006-01-28 10:09:25 +0000966SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
967 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000968 SDNode *&N = TargetExternalSymbols[Sym];
969 if (N) return SDOperand(N, 0);
970 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 AllNodes.push_back(N);
972 return SDOperand(N, 0);
973}
974
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000975SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
976 if ((unsigned)Cond >= CondCodeNodes.size())
977 CondCodeNodes.resize(Cond+1);
978
Chris Lattner079a27a2005-08-09 20:40:02 +0000979 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000980 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000981 AllNodes.push_back(CondCodeNodes[Cond]);
982 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000983 return SDOperand(CondCodeNodes[Cond], 0);
984}
985
Chris Lattner0fdd7682005-08-30 22:38:38 +0000986SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000987 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000988 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000989 ID.AddInteger(RegNo);
990 void *IP = 0;
991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
992 return SDOperand(E, 0);
993 SDNode *N = new RegisterSDNode(RegNo, VT);
994 CSEMap.InsertNode(N, IP);
995 AllNodes.push_back(N);
996 return SDOperand(N, 0);
997}
998
Dan Gohman69de1932008-02-06 22:27:42 +0000999SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001000 assert((!V || isa<PointerType>(V->getType())) &&
1001 "SrcValue is not a pointer?");
1002
Jim Laskey583bd472006-10-27 23:46:08 +00001003 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001004 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001005 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001006
Chris Lattner61b09412006-08-11 21:01:22 +00001007 void *IP = 0;
1008 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1009 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001010
1011 SDNode *N = new SrcValueSDNode(V);
1012 CSEMap.InsertNode(N, IP);
1013 AllNodes.push_back(N);
1014 return SDOperand(N, 0);
1015}
1016
Dan Gohman36b5c132008-04-07 19:35:22 +00001017SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001018 const Value *v = MO.getValue();
1019 assert((!v || isa<PointerType>(v->getType())) &&
1020 "SrcValue is not a pointer?");
1021
1022 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001023 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001024 ID.AddPointer(v);
1025 ID.AddInteger(MO.getFlags());
1026 ID.AddInteger(MO.getOffset());
1027 ID.AddInteger(MO.getSize());
1028 ID.AddInteger(MO.getAlignment());
1029
1030 void *IP = 0;
1031 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1032 return SDOperand(E, 0);
1033
1034 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001035 CSEMap.InsertNode(N, IP);
1036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001038}
1039
Chris Lattner37ce9df2007-10-15 17:47:20 +00001040/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1041/// specified value type.
1042SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1043 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1044 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1045 const Type *Ty = MVT::getTypeForValueType(VT);
1046 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1047 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1048 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1049}
1050
1051
Chris Lattner51dabfb2006-10-14 00:41:01 +00001052SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1053 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 // These setcc operations always fold.
1055 switch (Cond) {
1056 default: break;
1057 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001058 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001060 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001061
1062 case ISD::SETOEQ:
1063 case ISD::SETOGT:
1064 case ISD::SETOGE:
1065 case ISD::SETOLT:
1066 case ISD::SETOLE:
1067 case ISD::SETONE:
1068 case ISD::SETO:
1069 case ISD::SETUO:
1070 case ISD::SETUEQ:
1071 case ISD::SETUNE:
1072 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1073 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001075
Chris Lattner67255a12005-04-07 18:14:58 +00001076 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001077 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001078 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001079 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001080
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081 switch (Cond) {
1082 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001083 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1084 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001085 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1086 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1087 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1088 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1089 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1090 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1091 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1092 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 }
Chris Lattner67255a12005-04-07 18:14:58 +00001095 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001096 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001098 // No compile time operations on this type yet.
1099 if (N1C->getValueType(0) == MVT::ppcf128)
1100 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001101
1102 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001103 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001104 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001105 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1106 return getNode(ISD::UNDEF, VT);
1107 // fall through
1108 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1109 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1110 return getNode(ISD::UNDEF, VT);
1111 // fall through
1112 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001113 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001114 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1115 return getNode(ISD::UNDEF, VT);
1116 // fall through
1117 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1118 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1119 return getNode(ISD::UNDEF, VT);
1120 // fall through
1121 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1122 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1123 return getNode(ISD::UNDEF, VT);
1124 // fall through
1125 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001126 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001127 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1128 return getNode(ISD::UNDEF, VT);
1129 // fall through
1130 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001131 R==APFloat::cmpEqual, VT);
1132 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1133 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1134 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1135 R==APFloat::cmpEqual, VT);
1136 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1137 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1138 R==APFloat::cmpLessThan, VT);
1139 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1140 R==APFloat::cmpUnordered, VT);
1141 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1142 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001143 }
1144 } else {
1145 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001146 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001148 }
1149
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001150 // Could not fold it.
1151 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152}
1153
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001154/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1155/// use this predicate to simplify operations downstream.
1156bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1157 unsigned BitWidth = Op.getValueSizeInBits();
1158 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1159}
1160
Dan Gohmanea859be2007-06-22 14:59:07 +00001161/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1162/// this predicate to simplify operations downstream. Mask is known to be zero
1163/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001164bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001165 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001166 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001167 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1168 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1169 return (KnownZero & Mask) == Mask;
1170}
1171
1172/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1173/// known to be either zero or one and return them in the KnownZero/KnownOne
1174/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1175/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001176void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001177 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001178 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001179 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001180 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1181 "Mask size mismatches value type size!");
1182
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001183 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001184 if (Depth == 6 || Mask == 0)
1185 return; // Limit search depth.
1186
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001187 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001188
1189 switch (Op.getOpcode()) {
1190 case ISD::Constant:
1191 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001192 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001193 KnownZero = ~KnownOne & Mask;
1194 return;
1195 case ISD::AND:
1196 // If either the LHS or the RHS are Zero, the result is zero.
1197 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001198 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1199 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001200 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1201 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1202
1203 // Output known-1 bits are only known if set in both the LHS & RHS.
1204 KnownOne &= KnownOne2;
1205 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1206 KnownZero |= KnownZero2;
1207 return;
1208 case ISD::OR:
1209 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001210 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1211 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001212 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1213 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1214
1215 // Output known-0 bits are only known if clear in both the LHS & RHS.
1216 KnownZero &= KnownZero2;
1217 // Output known-1 are known to be set if set in either the LHS | RHS.
1218 KnownOne |= KnownOne2;
1219 return;
1220 case ISD::XOR: {
1221 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1222 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1223 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1224 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1225
1226 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001227 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001228 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1229 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1230 KnownZero = KnownZeroOut;
1231 return;
1232 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001233 case ISD::MUL: {
1234 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1235 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1236 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1237 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1238 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1239
1240 // If low bits are zero in either operand, output low known-0 bits.
1241 // Also compute a conserative estimate for high known-0 bits.
1242 // More trickiness is possible, but this is sufficient for the
1243 // interesting case of alignment computation.
1244 KnownOne.clear();
1245 unsigned TrailZ = KnownZero.countTrailingOnes() +
1246 KnownZero2.countTrailingOnes();
1247 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001248 KnownZero2.countLeadingOnes(),
1249 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001250
1251 TrailZ = std::min(TrailZ, BitWidth);
1252 LeadZ = std::min(LeadZ, BitWidth);
1253 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1254 APInt::getHighBitsSet(BitWidth, LeadZ);
1255 KnownZero &= Mask;
1256 return;
1257 }
1258 case ISD::UDIV: {
1259 // For the purposes of computing leading zeros we can conservatively
1260 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001261 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001262 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1263 ComputeMaskedBits(Op.getOperand(0),
1264 AllOnes, KnownZero2, KnownOne2, Depth+1);
1265 unsigned LeadZ = KnownZero2.countLeadingOnes();
1266
1267 KnownOne2.clear();
1268 KnownZero2.clear();
1269 ComputeMaskedBits(Op.getOperand(1),
1270 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001271 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1272 if (RHSUnknownLeadingOnes != BitWidth)
1273 LeadZ = std::min(BitWidth,
1274 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001275
1276 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1277 return;
1278 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001279 case ISD::SELECT:
1280 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1281 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1282 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1283 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1284
1285 // Only known if known in both the LHS and RHS.
1286 KnownOne &= KnownOne2;
1287 KnownZero &= KnownZero2;
1288 return;
1289 case ISD::SELECT_CC:
1290 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1291 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1294
1295 // Only known if known in both the LHS and RHS.
1296 KnownOne &= KnownOne2;
1297 KnownZero &= KnownZero2;
1298 return;
1299 case ISD::SETCC:
1300 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001301 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1302 BitWidth > 1)
1303 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001304 return;
1305 case ISD::SHL:
1306 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1307 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001308 unsigned ShAmt = SA->getValue();
1309
1310 // If the shift count is an invalid immediate, don't do anything.
1311 if (ShAmt >= BitWidth)
1312 return;
1313
1314 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001315 KnownZero, KnownOne, Depth+1);
1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001317 KnownZero <<= ShAmt;
1318 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001319 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001320 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001321 }
1322 return;
1323 case ISD::SRL:
1324 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1325 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001326 unsigned ShAmt = SA->getValue();
1327
Dan Gohmand4cf9922008-02-26 18:50:50 +00001328 // If the shift count is an invalid immediate, don't do anything.
1329 if (ShAmt >= BitWidth)
1330 return;
1331
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001332 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 KnownZero, KnownOne, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001335 KnownZero = KnownZero.lshr(ShAmt);
1336 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001337
Dan Gohman72d2fd52008-02-13 22:43:25 +00001338 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001339 KnownZero |= HighBits; // High bits known zero.
1340 }
1341 return;
1342 case ISD::SRA:
1343 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001344 unsigned ShAmt = SA->getValue();
1345
Dan Gohmand4cf9922008-02-26 18:50:50 +00001346 // If the shift count is an invalid immediate, don't do anything.
1347 if (ShAmt >= BitWidth)
1348 return;
1349
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001350 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001351 // If any of the demanded bits are produced by the sign extension, we also
1352 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001353 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1354 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001355 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001356
1357 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1358 Depth+1);
1359 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001360 KnownZero = KnownZero.lshr(ShAmt);
1361 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001362
1363 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001364 APInt SignBit = APInt::getSignBit(BitWidth);
1365 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001366
Dan Gohmanca93a432008-02-20 16:30:17 +00001367 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001368 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001369 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001370 KnownOne |= HighBits; // New bits are known one.
1371 }
1372 }
1373 return;
1374 case ISD::SIGN_EXTEND_INREG: {
1375 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001376 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001377
1378 // Sign extension. Compute the demanded bits in the result that are not
1379 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001380 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001381
Dan Gohman977a76f2008-02-13 22:28:48 +00001382 APInt InSignBit = APInt::getSignBit(EBits);
1383 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001384
1385 // If the sign extended bits are demanded, we know that the sign
1386 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001387 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001388 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001389 InputDemandedBits |= InSignBit;
1390
1391 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1392 KnownZero, KnownOne, Depth+1);
1393 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1394
1395 // If the sign bit of the input is known set or clear, then we know the
1396 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001397 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001398 KnownZero |= NewBits;
1399 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001400 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001401 KnownOne |= NewBits;
1402 KnownZero &= ~NewBits;
1403 } else { // Input sign bit unknown
1404 KnownZero &= ~NewBits;
1405 KnownOne &= ~NewBits;
1406 }
1407 return;
1408 }
1409 case ISD::CTTZ:
1410 case ISD::CTLZ:
1411 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001412 unsigned LowBits = Log2_32(BitWidth)+1;
1413 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1414 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001415 return;
1416 }
1417 case ISD::LOAD: {
1418 if (ISD::isZEXTLoad(Op.Val)) {
1419 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001420 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001421 unsigned MemBits = MVT::getSizeInBits(VT);
1422 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001423 }
1424 return;
1425 }
1426 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001427 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1428 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001429 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1430 APInt InMask = Mask;
1431 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001432 KnownZero.trunc(InBits);
1433 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001434 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001435 KnownZero.zext(BitWidth);
1436 KnownOne.zext(BitWidth);
1437 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001438 return;
1439 }
1440 case ISD::SIGN_EXTEND: {
1441 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001442 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001443 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001444 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1445 APInt InMask = Mask;
1446 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001447
1448 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001449 // bit is demanded. Temporarily set this bit in the mask for our callee.
1450 if (NewBits.getBoolValue())
1451 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001453 KnownZero.trunc(InBits);
1454 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001455 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1456
1457 // Note if the sign bit is known to be zero or one.
1458 bool SignBitKnownZero = KnownZero.isNegative();
1459 bool SignBitKnownOne = KnownOne.isNegative();
1460 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1461 "Sign bit can't be known to be both zero and one!");
1462
1463 // If the sign bit wasn't actually demanded by our caller, we don't
1464 // want it set in the KnownZero and KnownOne result values. Reset the
1465 // mask and reapply it to the result values.
1466 InMask = Mask;
1467 InMask.trunc(InBits);
1468 KnownZero &= InMask;
1469 KnownOne &= InMask;
1470
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001471 KnownZero.zext(BitWidth);
1472 KnownOne.zext(BitWidth);
1473
Dan Gohman977a76f2008-02-13 22:28:48 +00001474 // If the sign bit is known zero or one, the top bits match.
1475 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001476 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001477 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001478 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001479 return;
1480 }
1481 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001482 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1483 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001484 APInt InMask = Mask;
1485 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001486 KnownZero.trunc(InBits);
1487 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001488 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001489 KnownZero.zext(BitWidth);
1490 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001491 return;
1492 }
1493 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001494 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1495 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001496 APInt InMask = Mask;
1497 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001498 KnownZero.zext(InBits);
1499 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001500 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001501 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001502 KnownZero.trunc(BitWidth);
1503 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001504 break;
1505 }
1506 case ISD::AssertZext: {
1507 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001508 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001509 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1510 KnownOne, Depth+1);
1511 KnownZero |= (~InMask) & Mask;
1512 return;
1513 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001514 case ISD::FGETSIGN:
1515 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001516 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001517 return;
1518
Dan Gohman23e8b712008-04-28 17:02:21 +00001519 case ISD::SUB: {
1520 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1521 // We know that the top bits of C-X are clear if X contains less bits
1522 // than C (i.e. no wrap-around can happen). For example, 20-X is
1523 // positive if we can prove that X is >= 0 and < 16.
1524 if (CLHS->getAPIntValue().isNonNegative()) {
1525 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1526 // NLZ can't be BitWidth with no sign bit
1527 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1528 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1529 Depth+1);
1530
1531 // If all of the MaskV bits are known to be zero, then we know the
1532 // output top bits are zero, because we now know that the output is
1533 // from [0-C].
1534 if ((KnownZero2 & MaskV) == MaskV) {
1535 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1536 // Top bits known zero.
1537 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1538 }
1539 }
1540 }
1541 }
1542 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001543 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001544 // Output known-0 bits are known if clear or set in both the low clear bits
1545 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1546 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001547 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1548 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1549 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1550 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1551
1552 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1553 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1554 KnownZeroOut = std::min(KnownZeroOut,
1555 KnownZero2.countTrailingOnes());
1556
1557 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001558 return;
1559 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001560 case ISD::SREM:
1561 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1562 APInt RA = Rem->getAPIntValue();
1563 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001564 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001565 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1566 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001567
Dan Gohman23e8b712008-04-28 17:02:21 +00001568 // The sign of a remainder is equal to the sign of the first
1569 // operand (zero being positive).
1570 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1571 KnownZero2 |= ~LowBits;
1572 else if (KnownOne2[BitWidth-1])
1573 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001574
Dan Gohman23e8b712008-04-28 17:02:21 +00001575 KnownZero |= KnownZero2 & Mask;
1576 KnownOne |= KnownOne2 & Mask;
1577
1578 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001579 }
1580 }
1581 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001582 case ISD::UREM: {
1583 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1584 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001585 if (RA.isPowerOf2()) {
1586 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001587 APInt Mask2 = LowBits & Mask;
1588 KnownZero |= ~LowBits & Mask;
1589 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1590 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1591 break;
1592 }
1593 }
1594
1595 // Since the result is less than or equal to either operand, any leading
1596 // zero bits in either operand must also exist in the result.
1597 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1598 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1599 Depth+1);
1600 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1601 Depth+1);
1602
1603 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1604 KnownZero2.countLeadingOnes());
1605 KnownOne.clear();
1606 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1607 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 }
1609 default:
1610 // Allow the target to implement this method for its nodes.
1611 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1612 case ISD::INTRINSIC_WO_CHAIN:
1613 case ISD::INTRINSIC_W_CHAIN:
1614 case ISD::INTRINSIC_VOID:
1615 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1616 }
1617 return;
1618 }
1619}
1620
1621/// ComputeNumSignBits - Return the number of times the sign bit of the
1622/// register is replicated into the other bits. We know that at least 1 bit
1623/// is always equal to the sign bit (itself), but other cases can give us
1624/// information. For example, immediately after an "SRA X, 2", we know that
1625/// the top 3 bits are all equal to each other, so we return 3.
1626unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1627 MVT::ValueType VT = Op.getValueType();
1628 assert(MVT::isInteger(VT) && "Invalid VT!");
1629 unsigned VTBits = MVT::getSizeInBits(VT);
1630 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001631 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001632
1633 if (Depth == 6)
1634 return 1; // Limit search depth.
1635
1636 switch (Op.getOpcode()) {
1637 default: break;
1638 case ISD::AssertSext:
1639 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1640 return VTBits-Tmp+1;
1641 case ISD::AssertZext:
1642 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1643 return VTBits-Tmp;
1644
1645 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001646 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1647 // If negative, return # leading ones.
1648 if (Val.isNegative())
1649 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001650
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001651 // Return # leading zeros.
1652 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001653 }
1654
1655 case ISD::SIGN_EXTEND:
1656 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1657 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1658
1659 case ISD::SIGN_EXTEND_INREG:
1660 // Max of the input and what this extends.
1661 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1662 Tmp = VTBits-Tmp+1;
1663
1664 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1665 return std::max(Tmp, Tmp2);
1666
1667 case ISD::SRA:
1668 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1669 // SRA X, C -> adds C sign bits.
1670 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1671 Tmp += C->getValue();
1672 if (Tmp > VTBits) Tmp = VTBits;
1673 }
1674 return Tmp;
1675 case ISD::SHL:
1676 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1677 // shl destroys sign bits.
1678 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1679 if (C->getValue() >= VTBits || // Bad shift.
1680 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1681 return Tmp - C->getValue();
1682 }
1683 break;
1684 case ISD::AND:
1685 case ISD::OR:
1686 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001687 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001688 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001689 if (Tmp != 1) {
1690 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1691 FirstAnswer = std::min(Tmp, Tmp2);
1692 // We computed what we know about the sign bits as our first
1693 // answer. Now proceed to the generic code that uses
1694 // ComputeMaskedBits, and pick whichever answer is better.
1695 }
1696 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001697
1698 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001699 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001701 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001702 return std::min(Tmp, Tmp2);
1703
1704 case ISD::SETCC:
1705 // If setcc returns 0/-1, all bits are sign bits.
1706 if (TLI.getSetCCResultContents() ==
1707 TargetLowering::ZeroOrNegativeOneSetCCResult)
1708 return VTBits;
1709 break;
1710 case ISD::ROTL:
1711 case ISD::ROTR:
1712 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1713 unsigned RotAmt = C->getValue() & (VTBits-1);
1714
1715 // Handle rotate right by N like a rotate left by 32-N.
1716 if (Op.getOpcode() == ISD::ROTR)
1717 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1718
1719 // If we aren't rotating out all of the known-in sign bits, return the
1720 // number that are left. This handles rotl(sext(x), 1) for example.
1721 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1722 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1723 }
1724 break;
1725 case ISD::ADD:
1726 // Add can have at most one carry bit. Thus we know that the output
1727 // is, at worst, one more bit than the inputs.
1728 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1729 if (Tmp == 1) return 1; // Early out.
1730
1731 // Special case decrementing a value (ADD X, -1):
1732 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1733 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001734 APInt KnownZero, KnownOne;
1735 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001736 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1737
1738 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1739 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001740 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 return VTBits;
1742
1743 // If we are subtracting one from a positive number, there is no carry
1744 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001745 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001746 return Tmp;
1747 }
1748
1749 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1750 if (Tmp2 == 1) return 1;
1751 return std::min(Tmp, Tmp2)-1;
1752 break;
1753
1754 case ISD::SUB:
1755 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1756 if (Tmp2 == 1) return 1;
1757
1758 // Handle NEG.
1759 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001760 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001761 APInt KnownZero, KnownOne;
1762 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001763 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1764 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1765 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001766 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001767 return VTBits;
1768
1769 // If the input is known to be positive (the sign bit is known clear),
1770 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001771 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 return Tmp2;
1773
1774 // Otherwise, we treat this like a SUB.
1775 }
1776
1777 // Sub can have at most one carry bit. Thus we know that the output
1778 // is, at worst, one more bit than the inputs.
1779 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1780 if (Tmp == 1) return 1; // Early out.
1781 return std::min(Tmp, Tmp2)-1;
1782 break;
1783 case ISD::TRUNCATE:
1784 // FIXME: it's tricky to do anything useful for this, but it is an important
1785 // case for targets like X86.
1786 break;
1787 }
1788
1789 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1790 if (Op.getOpcode() == ISD::LOAD) {
1791 LoadSDNode *LD = cast<LoadSDNode>(Op);
1792 unsigned ExtType = LD->getExtensionType();
1793 switch (ExtType) {
1794 default: break;
1795 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001796 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 return VTBits-Tmp+1;
1798 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001799 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001800 return VTBits-Tmp;
1801 }
1802 }
1803
1804 // Allow the target to implement this method for its nodes.
1805 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1806 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1807 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1808 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1809 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001810 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 }
1812
1813 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1814 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001815 APInt KnownZero, KnownOne;
1816 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1818
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001819 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001820 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001821 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001822 Mask = KnownOne;
1823 } else {
1824 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001825 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 }
1827
1828 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1829 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001830 Mask = ~Mask;
1831 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 // Return # leading zeros. We use 'min' here in case Val was zero before
1833 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001834 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001835}
1836
Chris Lattner51dabfb2006-10-14 00:41:01 +00001837
Evan Chenga844bde2008-02-02 04:07:54 +00001838bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1839 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1840 if (!GA) return false;
1841 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1842 if (!GV) return false;
1843 MachineModuleInfo *MMI = getMachineModuleInfo();
1844 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1845}
1846
1847
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001848/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1849/// element of the result of the vector shuffle.
1850SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned Idx) {
1851 MVT::ValueType VT = N->getValueType(0);
1852 SDOperand PermMask = N->getOperand(2);
1853 unsigned NumElems = PermMask.getNumOperands();
1854 SDOperand V = (Idx < NumElems) ? N->getOperand(0) : N->getOperand(1);
1855 Idx %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001856
1857 if (V.getOpcode() == ISD::BIT_CONVERT) {
1858 V = V.getOperand(0);
1859 if (MVT::getVectorNumElements(V.getValueType()) != NumElems)
1860 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001861 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001862 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
1863 return (Idx == 0) ? V.getOperand(0)
1864 : getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
1865 if (V.getOpcode() == ISD::BUILD_VECTOR)
1866 return V.getOperand(Idx);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001867 if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
1868 SDOperand Elt = PermMask.getOperand(Idx);
1869 if (Elt.getOpcode() == ISD::UNDEF)
1870 return getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
1871 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Elt)->getValue());
1872 }
1873 return SDOperand();
1874}
1875
1876
Chris Lattnerc3aae252005-01-07 07:46:32 +00001877/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001878///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001879SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001880 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001881 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001882 void *IP = 0;
1883 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1884 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001885 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001886 CSEMap.InsertNode(N, IP);
1887
1888 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889 return SDOperand(N, 0);
1890}
1891
Chris Lattnerc3aae252005-01-07 07:46:32 +00001892SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1893 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001894 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001895 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001896 const APInt &Val = C->getAPIntValue();
1897 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001898 switch (Opcode) {
1899 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001900 case ISD::SIGN_EXTEND:
1901 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001902 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001903 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001904 case ISD::TRUNCATE:
1905 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001906 case ISD::UINT_TO_FP:
1907 case ISD::SINT_TO_FP: {
1908 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001909 // No compile time operations on this type.
1910 if (VT==MVT::ppcf128)
1911 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001912 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1913 (void)apf.convertFromAPInt(Val,
1914 Opcode==ISD::SINT_TO_FP,
1915 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001916 return getConstantFP(apf, VT);
1917 }
Chris Lattner94683772005-12-23 05:30:37 +00001918 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001919 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001920 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001921 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001922 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001923 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001924 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001925 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001926 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001927 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001928 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001929 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001930 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001931 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001932 }
1933 }
1934
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001935 // Constant fold unary operations with a floating point constant operand.
1936 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1937 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001938 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001939 switch (Opcode) {
1940 case ISD::FNEG:
1941 V.changeSign();
1942 return getConstantFP(V, VT);
1943 case ISD::FABS:
1944 V.clearSign();
1945 return getConstantFP(V, VT);
1946 case ISD::FP_ROUND:
1947 case ISD::FP_EXTEND:
1948 // This can return overflow, underflow, or inexact; we don't care.
1949 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001950 (void)V.convert(*MVTToAPFloatSemantics(VT),
1951 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001952 return getConstantFP(V, VT);
1953 case ISD::FP_TO_SINT:
1954 case ISD::FP_TO_UINT: {
1955 integerPart x;
1956 assert(integerPartWidth >= 64);
1957 // FIXME need to be more flexible about rounding mode.
1958 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1959 Opcode==ISD::FP_TO_SINT,
1960 APFloat::rmTowardZero);
1961 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1962 break;
1963 return getConstant(x, VT);
1964 }
1965 case ISD::BIT_CONVERT:
1966 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1967 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1968 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1969 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001970 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001971 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001972 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001973 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001974
1975 unsigned OpOpcode = Operand.Val->getOpcode();
1976 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001977 case ISD::TokenFactor:
Dan Gohmanb91c89d2008-04-14 18:43:25 +00001978 case ISD::MERGE_VALUES:
1979 return Operand; // Factor or merge of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001980 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001981 case ISD::FP_EXTEND:
1982 assert(MVT::isFloatingPoint(VT) &&
1983 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001984 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001985 if (Operand.getOpcode() == ISD::UNDEF)
1986 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001987 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001988 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001989 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1990 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001991 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001992 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1993 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001994 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1995 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1996 break;
1997 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001998 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1999 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002000 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00002001 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
2002 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002003 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002004 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002005 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002006 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00002007 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
2008 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002009 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00002010 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
2011 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002012 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2013 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2014 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2015 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00002017 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
2018 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002019 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00002020 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
2021 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002022 if (OpOpcode == ISD::TRUNCATE)
2023 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002024 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2025 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002026 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00002027 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
2028 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002029 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00002030 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
2031 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002032 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2033 else
2034 return Operand.Val->getOperand(0);
2035 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002036 break;
Chris Lattner35481892005-12-23 00:16:34 +00002037 case ISD::BIT_CONVERT:
2038 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00002039 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00002040 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002041 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002042 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2043 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002044 if (OpOpcode == ISD::UNDEF)
2045 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002046 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002047 case ISD::SCALAR_TO_VECTOR:
2048 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00002049 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002050 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002051 if (OpOpcode == ISD::UNDEF)
2052 return getNode(ISD::UNDEF, VT);
2053 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2054 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2055 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2056 Operand.getConstantOperandVal(1) == 0 &&
2057 Operand.getOperand(0).getValueType() == VT)
2058 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002059 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002060 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002061 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2062 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002063 Operand.Val->getOperand(0));
2064 if (OpOpcode == ISD::FNEG) // --X -> X
2065 return Operand.Val->getOperand(0);
2066 break;
2067 case ISD::FABS:
2068 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2069 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2070 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002071 }
2072
Chris Lattner43247a12005-08-25 19:12:10 +00002073 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002074 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002075 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002076 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002077 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002078 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002079 void *IP = 0;
2080 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2081 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002082 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002083 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002084 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002085 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002086 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002087 AllNodes.push_back(N);
2088 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002089}
2090
Chris Lattner36019aa2005-04-18 03:48:41 +00002091
2092
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2094 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2096 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002097 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002098 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002099 case ISD::TokenFactor:
2100 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2101 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002102 // Fold trivial token factors.
2103 if (N1.getOpcode() == ISD::EntryToken) return N2;
2104 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002105 break;
Chris Lattner76365122005-01-16 02:23:22 +00002106 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002107 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
2108 N1.getValueType() == VT && "Binary operator types must match!");
2109 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2110 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002111 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002112 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002113 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2114 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002115 break;
Chris Lattner76365122005-01-16 02:23:22 +00002116 case ISD::OR:
2117 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002118 case ISD::ADD:
2119 case ISD::SUB:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002120 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
2121 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002122 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2123 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002124 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002125 return N1;
2126 break;
Chris Lattner76365122005-01-16 02:23:22 +00002127 case ISD::UDIV:
2128 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002129 case ISD::MULHU:
2130 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002131 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2132 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002133 case ISD::MUL:
2134 case ISD::SDIV:
2135 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002136 case ISD::FADD:
2137 case ISD::FSUB:
2138 case ISD::FMUL:
2139 case ISD::FDIV:
2140 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002141 assert(N1.getValueType() == N2.getValueType() &&
2142 N1.getValueType() == VT && "Binary operator types must match!");
2143 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002144 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2145 assert(N1.getValueType() == VT &&
2146 MVT::isFloatingPoint(N1.getValueType()) &&
2147 MVT::isFloatingPoint(N2.getValueType()) &&
2148 "Invalid FCOPYSIGN!");
2149 break;
Chris Lattner76365122005-01-16 02:23:22 +00002150 case ISD::SHL:
2151 case ISD::SRA:
2152 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002153 case ISD::ROTL:
2154 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002155 assert(VT == N1.getValueType() &&
2156 "Shift operators return type must be the same as their first arg");
2157 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002158 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002159 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002160 case ISD::FP_ROUND_INREG: {
2161 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2162 assert(VT == N1.getValueType() && "Not an inreg round!");
2163 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2164 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002165 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2166 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002167 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002168 break;
2169 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002170 case ISD::FP_ROUND:
2171 assert(MVT::isFloatingPoint(VT) &&
2172 MVT::isFloatingPoint(N1.getValueType()) &&
2173 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2174 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002175 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002176 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002177 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002178 case ISD::AssertZext: {
2179 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2180 assert(VT == N1.getValueType() && "Not an inreg extend!");
2181 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2182 "Cannot *_EXTEND_INREG FP types");
2183 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2184 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002185 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002186 break;
2187 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002188 case ISD::SIGN_EXTEND_INREG: {
2189 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2190 assert(VT == N1.getValueType() && "Not an inreg extend!");
2191 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2192 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002193 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2194 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002195 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002196
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002197 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002198 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002199 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002200 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002201 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002202 return getConstant(Val, VT);
2203 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002204 break;
2205 }
2206 case ISD::EXTRACT_VECTOR_ELT:
2207 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2208
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002209 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2210 if (N1.getOpcode() == ISD::UNDEF)
2211 return getNode(ISD::UNDEF, VT);
2212
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002213 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2214 // expanding copies of large vectors from registers.
2215 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2216 N1.getNumOperands() > 0) {
2217 unsigned Factor =
2218 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2219 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2220 N1.getOperand(N2C->getValue() / Factor),
2221 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2222 }
2223
2224 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2225 // expanding large vector constants.
2226 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2227 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002228
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002229 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2230 // operations are lowered to scalars.
2231 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2232 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2233 if (IEC == N2C)
2234 return N1.getOperand(1);
2235 else
2236 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2237 }
2238 break;
2239 case ISD::EXTRACT_ELEMENT:
2240 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002241 assert(!MVT::isVector(N1.getValueType()) &&
2242 MVT::isInteger(N1.getValueType()) &&
2243 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2244 "EXTRACT_ELEMENT only applies to integers!");
2245
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002246 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2247 // 64-bit integers into 32-bit parts. Instead of building the extract of
2248 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2249 if (N1.getOpcode() == ISD::BUILD_PAIR)
2250 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002251
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002252 // EXTRACT_ELEMENT of a constant int is also very common.
2253 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002254 unsigned ElementSize = MVT::getSizeInBits(VT);
2255 unsigned Shift = ElementSize * N2C->getValue();
2256 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2257 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002258 }
2259 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002260 case ISD::EXTRACT_SUBVECTOR:
2261 if (N1.getValueType() == VT) // Trivial extraction.
2262 return N1;
2263 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002264 }
2265
2266 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002267 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002268 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002269 switch (Opcode) {
2270 case ISD::ADD: return getConstant(C1 + C2, VT);
2271 case ISD::SUB: return getConstant(C1 - C2, VT);
2272 case ISD::MUL: return getConstant(C1 * C2, VT);
2273 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002274 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002275 break;
2276 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002277 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002278 break;
2279 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002280 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002281 break;
2282 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002283 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002284 break;
2285 case ISD::AND : return getConstant(C1 & C2, VT);
2286 case ISD::OR : return getConstant(C1 | C2, VT);
2287 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002288 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002289 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2290 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2291 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2292 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002293 default: break;
2294 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002295 } else { // Cannonicalize constant to RHS if commutative
2296 if (isCommutativeBinOp(Opcode)) {
2297 std::swap(N1C, N2C);
2298 std::swap(N1, N2);
2299 }
2300 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002301 }
2302
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002303 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002304 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2305 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002306 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002307 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2308 // Cannonicalize constant to RHS if commutative
2309 std::swap(N1CFP, N2CFP);
2310 std::swap(N1, N2);
2311 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002312 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2313 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002314 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002315 case ISD::FADD:
2316 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002317 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002318 return getConstantFP(V1, VT);
2319 break;
2320 case ISD::FSUB:
2321 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2322 if (s!=APFloat::opInvalidOp)
2323 return getConstantFP(V1, VT);
2324 break;
2325 case ISD::FMUL:
2326 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2327 if (s!=APFloat::opInvalidOp)
2328 return getConstantFP(V1, VT);
2329 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002330 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002331 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2332 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2333 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002335 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002336 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2337 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2338 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002339 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002340 case ISD::FCOPYSIGN:
2341 V1.copySign(V2);
2342 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002343 default: break;
2344 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002345 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002346 }
Chris Lattner62b57722006-04-20 05:39:12 +00002347
2348 // Canonicalize an UNDEF to the RHS, even over a constant.
2349 if (N1.getOpcode() == ISD::UNDEF) {
2350 if (isCommutativeBinOp(Opcode)) {
2351 std::swap(N1, N2);
2352 } else {
2353 switch (Opcode) {
2354 case ISD::FP_ROUND_INREG:
2355 case ISD::SIGN_EXTEND_INREG:
2356 case ISD::SUB:
2357 case ISD::FSUB:
2358 case ISD::FDIV:
2359 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002360 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002361 return N1; // fold op(undef, arg2) -> undef
2362 case ISD::UDIV:
2363 case ISD::SDIV:
2364 case ISD::UREM:
2365 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002366 case ISD::SRL:
2367 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002368 if (!MVT::isVector(VT))
2369 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2370 // For vectors, we can't easily build an all zero vector, just return
2371 // the LHS.
2372 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002373 }
2374 }
2375 }
2376
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002377 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002378 if (N2.getOpcode() == ISD::UNDEF) {
2379 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002380 case ISD::XOR:
2381 if (N1.getOpcode() == ISD::UNDEF)
2382 // Handle undef ^ undef -> 0 special case. This is a common
2383 // idiom (misuse).
2384 return getConstant(0, VT);
2385 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002386 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002387 case ISD::ADDC:
2388 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002389 case ISD::SUB:
2390 case ISD::FADD:
2391 case ISD::FSUB:
2392 case ISD::FMUL:
2393 case ISD::FDIV:
2394 case ISD::FREM:
2395 case ISD::UDIV:
2396 case ISD::SDIV:
2397 case ISD::UREM:
2398 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002399 return N2; // fold op(arg1, undef) -> undef
2400 case ISD::MUL:
2401 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002402 case ISD::SRL:
2403 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002404 if (!MVT::isVector(VT))
2405 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2406 // For vectors, we can't easily build an all zero vector, just return
2407 // the LHS.
2408 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002409 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002410 if (!MVT::isVector(VT))
2411 return getConstant(MVT::getIntVTBitMask(VT), VT);
2412 // For vectors, we can't easily build an all one vector, just return
2413 // the LHS.
2414 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002415 case ISD::SRA:
2416 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002417 }
2418 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002419
Chris Lattner27e9b412005-05-11 18:57:39 +00002420 // Memoize this node if possible.
2421 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002422 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002423 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002424 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002425 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002426 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002427 void *IP = 0;
2428 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2429 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002430 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002431 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002432 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002433 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002434 }
2435
Chris Lattnerc3aae252005-01-07 07:46:32 +00002436 AllNodes.push_back(N);
2437 return SDOperand(N, 0);
2438}
2439
Chris Lattnerc3aae252005-01-07 07:46:32 +00002440SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2441 SDOperand N1, SDOperand N2, SDOperand N3) {
2442 // Perform various simplifications.
2443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2444 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002445 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002446 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002447 // Use FoldSetCC to simplify SETCC's.
2448 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002449 if (Simp.Val) return Simp;
2450 break;
2451 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002452 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002453 if (N1C) {
2454 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002455 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002456 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002457 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002458 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002459
2460 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002461 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002462 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002463 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002464 if (N2C->getValue()) // Unconditional branch
2465 return getNode(ISD::BR, MVT::Other, N1, N3);
2466 else
2467 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002468 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002469 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002470 case ISD::VECTOR_SHUFFLE:
2471 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2472 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2473 N3.getOpcode() == ISD::BUILD_VECTOR &&
2474 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2475 "Illegal VECTOR_SHUFFLE node!");
2476 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002477 case ISD::BIT_CONVERT:
2478 // Fold bit_convert nodes from a type to themselves.
2479 if (N1.getValueType() == VT)
2480 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002481 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002482 }
2483
Chris Lattner43247a12005-08-25 19:12:10 +00002484 // Memoize node if it doesn't produce a flag.
2485 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002486 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002487 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002488 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002489 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002490 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002491 void *IP = 0;
2492 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2493 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002494 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002495 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002496 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002497 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002498 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002499 AllNodes.push_back(N);
2500 return SDOperand(N, 0);
2501}
2502
2503SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002504 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002505 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002506 SDOperand Ops[] = { N1, N2, N3, N4 };
2507 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002508}
2509
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002510SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2511 SDOperand N1, SDOperand N2, SDOperand N3,
2512 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002513 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2514 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002515}
2516
Dan Gohman707e0182008-04-12 04:36:06 +00002517/// getMemsetValue - Vectorized representation of the memset value
2518/// operand.
2519static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
2520 SelectionDAG &DAG) {
Evan Chengf0df0312008-05-15 08:39:06 +00002521 unsigned NumBits = MVT::isVector(VT) ?
2522 MVT::getSizeInBits(MVT::getVectorElementType(VT)) : MVT::getSizeInBits(VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002524 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002525 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002526 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002527 Val = (Val << Shift) | Val;
2528 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002529 }
Evan Chengf0df0312008-05-15 08:39:06 +00002530 if (MVT::isInteger(VT))
2531 return DAG.getConstant(Val, VT);
2532 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002533 }
Evan Chengf0df0312008-05-15 08:39:06 +00002534
2535 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2536 unsigned Shift = 8;
2537 for (unsigned i = NumBits; i > 8; i >>= 1) {
2538 Value = DAG.getNode(ISD::OR, VT,
2539 DAG.getNode(ISD::SHL, VT, Value,
2540 DAG.getConstant(Shift, MVT::i8)), Value);
2541 Shift <<= 1;
2542 }
2543
2544 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002545}
2546
Dan Gohman707e0182008-04-12 04:36:06 +00002547/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2548/// used when a memcpy is turned into a memset when the source is a constant
2549/// string ptr.
Evan Chengf0df0312008-05-15 08:39:06 +00002550static SDOperand getMemsetStringVal(MVT::ValueType VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002551 const TargetLowering &TLI,
2552 std::string &Str, unsigned Offset) {
Evan Chengf0df0312008-05-15 08:39:06 +00002553 assert(!MVT::isVector(VT) && "Can't handle vector type here!");
2554 unsigned NumBits = MVT::getSizeInBits(VT);
2555 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002556 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002557 if (TLI.isLittleEndian())
2558 Offset = Offset + MSB - 1;
2559 for (unsigned i = 0; i != MSB; ++i) {
2560 Val = (Val << 8) | (unsigned char)Str[Offset];
2561 Offset += TLI.isLittleEndian() ? -1 : 1;
2562 }
2563 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002564}
2565
Dan Gohman707e0182008-04-12 04:36:06 +00002566/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002567///
Dan Gohman707e0182008-04-12 04:36:06 +00002568static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2569 SelectionDAG &DAG) {
2570 MVT::ValueType VT = Base.getValueType();
2571 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2572}
2573
Evan Chengf0df0312008-05-15 08:39:06 +00002574/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2575///
2576static bool isMemSrcFromString(SDOperand Src, std::string &Str,
2577 uint64_t &SrcOff) {
2578 unsigned SrcDelta = 0;
2579 GlobalAddressSDNode *G = NULL;
2580 if (Src.getOpcode() == ISD::GlobalAddress)
2581 G = cast<GlobalAddressSDNode>(Src);
2582 else if (Src.getOpcode() == ISD::ADD &&
2583 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2584 Src.getOperand(1).getOpcode() == ISD::Constant) {
2585 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2586 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2587 }
2588 if (!G)
2589 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002590
Evan Chengf0df0312008-05-15 08:39:06 +00002591 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2592 if (GV && GV->isConstant()) {
2593 Str = GV->getStringValue(false);
2594 if (!Str.empty()) {
2595 SrcOff += SrcDelta;
2596 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002597 }
2598 }
2599
Evan Chengf0df0312008-05-15 08:39:06 +00002600 return false;
2601}
Dan Gohman707e0182008-04-12 04:36:06 +00002602
Evan Chengf0df0312008-05-15 08:39:06 +00002603/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2604/// to replace the memset / memcpy is below the threshold. It also returns the
2605/// types of the sequence of memory ops to perform memset / memcpy.
2606static
2607bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2608 SDOperand Dst, SDOperand Src,
2609 unsigned Limit, uint64_t Size, unsigned &Align,
2610 SelectionDAG &DAG,
2611 const TargetLowering &TLI) {
2612 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
2613
2614 std::string Str;
2615 uint64_t SrcOff = 0;
2616 bool isSrcStr = isMemSrcFromString(Src, Str, SrcOff);
2617 bool isSrcConst = isa<ConstantSDNode>(Src);
2618 MVT::ValueType VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
2619 if (VT != MVT::iAny) {
2620 unsigned NewAlign = (unsigned)
2621 TLI.getTargetData()->getABITypeAlignment(MVT::getTypeForValueType(VT));
2622 // If source is a string constant, this will require an unaligned load.
2623 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2624 if (Dst.getOpcode() != ISD::FrameIndex) {
2625 // Can't change destination alignment. It requires a unaligned store.
2626 if (AllowUnalign)
2627 VT = MVT::iAny;
2628 } else {
2629 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2630 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2631 if (MFI->isFixedObjectIndex(FI)) {
2632 // Can't change destination alignment. It requires a unaligned store.
2633 if (AllowUnalign)
2634 VT = MVT::iAny;
2635 } else {
2636 // Give the stack frame object a larger alignment.
2637 MFI->setObjectAlignment(FI, NewAlign);
2638 Align = NewAlign;
2639 }
2640 }
2641 }
2642 }
2643
2644 if (VT == MVT::iAny) {
2645 if (AllowUnalign) {
2646 VT = MVT::i64;
2647 } else {
2648 switch (Align & 7) {
2649 case 0: VT = MVT::i64; break;
2650 case 4: VT = MVT::i32; break;
2651 case 2: VT = MVT::i16; break;
2652 default: VT = MVT::i8; break;
2653 }
2654 }
2655
2656 MVT::ValueType LVT = MVT::i64;
2657 while (!TLI.isTypeLegal(LVT))
2658 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2659 assert(MVT::isInteger(LVT));
2660
2661 if (VT > LVT)
2662 VT = LVT;
2663 }
Dan Gohman707e0182008-04-12 04:36:06 +00002664
2665 unsigned NumMemOps = 0;
2666 while (Size != 0) {
2667 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2668 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002669 // For now, only use non-vector load / store's for the left-over pieces.
2670 if (MVT::isVector(VT)) {
2671 VT = MVT::i64;
2672 while (!TLI.isTypeLegal(VT))
2673 VT = (MVT::ValueType)((unsigned)VT - 1);
2674 VTSize = MVT::getSizeInBits(VT) / 8;
2675 } else {
2676 VT = (MVT::ValueType)((unsigned)VT - 1);
2677 VTSize >>= 1;
2678 }
Dan Gohman707e0182008-04-12 04:36:06 +00002679 }
Dan Gohman707e0182008-04-12 04:36:06 +00002680
2681 if (++NumMemOps > Limit)
2682 return false;
2683 MemOps.push_back(VT);
2684 Size -= VTSize;
2685 }
2686
2687 return true;
2688}
2689
2690static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2691 SDOperand Chain, SDOperand Dst,
2692 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002693 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002694 const Value *DstSV, uint64_t DstSVOff,
2695 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2697
Dan Gohman21323f32008-05-29 19:42:22 +00002698 // Expand memcpy to a series of load and store ops if the size operand falls
2699 // below a certain threshold.
Dan Gohman707e0182008-04-12 04:36:06 +00002700 std::vector<MVT::ValueType> MemOps;
2701 uint64_t Limit = -1;
2702 if (!AlwaysInline)
2703 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002704 unsigned DstAlign = Align; // Destination alignment can change.
2705 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2706 DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002707 return SDOperand();
2708
Dan Gohman707e0182008-04-12 04:36:06 +00002709 std::string Str;
Dan Gohman1f13c682008-04-28 17:15:20 +00002710 uint64_t SrcOff = 0, DstOff = 0;
Evan Chengf0df0312008-05-15 08:39:06 +00002711 bool CopyFromStr = isMemSrcFromString(Src, Str, SrcOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002712
Evan Chengf0df0312008-05-15 08:39:06 +00002713 SmallVector<SDOperand, 8> OutChains;
2714 unsigned NumMemOps = MemOps.size();
Dan Gohman707e0182008-04-12 04:36:06 +00002715 for (unsigned i = 0; i < NumMemOps; i++) {
2716 MVT::ValueType VT = MemOps[i];
2717 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2718 SDOperand Value, Store;
2719
Evan Chengf0df0312008-05-15 08:39:06 +00002720 if (CopyFromStr && !MVT::isVector(VT)) {
2721 // It's unlikely a store of a vector immediate can be done in a single
2722 // instruction. It would require a load from a constantpool first.
2723 // FIXME: Handle cases where store of vector immediate is done in a
2724 // single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002725 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002726 Store = DAG.getStore(Chain, Value,
2727 getMemBasePlusOffset(Dst, DstOff, DAG),
2728 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002729 } else {
2730 Value = DAG.getLoad(VT, Chain,
2731 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002732 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002733 Store = DAG.getStore(Chain, Value,
2734 getMemBasePlusOffset(Dst, DstOff, DAG),
2735 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002736 }
2737 OutChains.push_back(Store);
2738 SrcOff += VTSize;
2739 DstOff += VTSize;
2740 }
2741
2742 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2743 &OutChains[0], OutChains.size());
2744}
2745
Dan Gohman21323f32008-05-29 19:42:22 +00002746static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2747 SDOperand Chain, SDOperand Dst,
2748 SDOperand Src, uint64_t Size,
2749 unsigned Align, bool AlwaysInline,
2750 const Value *DstSV, uint64_t DstSVOff,
2751 const Value *SrcSV, uint64_t SrcSVOff){
2752 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2753
2754 // Expand memmove to a series of load and store ops if the size operand falls
2755 // below a certain threshold.
2756 std::vector<MVT::ValueType> MemOps;
2757 uint64_t Limit = -1;
2758 if (!AlwaysInline)
2759 Limit = TLI.getMaxStoresPerMemmove();
2760 unsigned DstAlign = Align; // Destination alignment can change.
2761 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2762 DAG, TLI))
2763 return SDOperand();
2764
Dan Gohman21323f32008-05-29 19:42:22 +00002765 uint64_t SrcOff = 0, DstOff = 0;
2766
2767 SmallVector<SDOperand, 8> LoadValues;
2768 SmallVector<SDOperand, 8> LoadChains;
2769 SmallVector<SDOperand, 8> OutChains;
2770 unsigned NumMemOps = MemOps.size();
2771 for (unsigned i = 0; i < NumMemOps; i++) {
2772 MVT::ValueType VT = MemOps[i];
2773 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2774 SDOperand Value, Store;
2775
2776 Value = DAG.getLoad(VT, Chain,
2777 getMemBasePlusOffset(Src, SrcOff, DAG),
2778 SrcSV, SrcSVOff + SrcOff, false, Align);
2779 LoadValues.push_back(Value);
2780 LoadChains.push_back(Value.getValue(1));
2781 SrcOff += VTSize;
2782 }
2783 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2784 &LoadChains[0], LoadChains.size());
2785 OutChains.clear();
2786 for (unsigned i = 0; i < NumMemOps; i++) {
2787 MVT::ValueType VT = MemOps[i];
2788 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2789 SDOperand Value, Store;
2790
2791 Store = DAG.getStore(Chain, LoadValues[i],
2792 getMemBasePlusOffset(Dst, DstOff, DAG),
2793 DstSV, DstSVOff + DstOff, false, DstAlign);
2794 OutChains.push_back(Store);
2795 DstOff += VTSize;
2796 }
2797
2798 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2799 &OutChains[0], OutChains.size());
2800}
2801
Dan Gohman707e0182008-04-12 04:36:06 +00002802static SDOperand getMemsetStores(SelectionDAG &DAG,
2803 SDOperand Chain, SDOperand Dst,
2804 SDOperand Src, uint64_t Size,
2805 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002806 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002807 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2808
2809 // Expand memset to a series of load/store ops if the size operand
2810 // falls below a certain threshold.
2811 std::vector<MVT::ValueType> MemOps;
Evan Chengf0df0312008-05-15 08:39:06 +00002812 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
2813 Size, Align, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002814 return SDOperand();
2815
2816 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002817 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002818
2819 unsigned NumMemOps = MemOps.size();
2820 for (unsigned i = 0; i < NumMemOps; i++) {
2821 MVT::ValueType VT = MemOps[i];
2822 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2823 SDOperand Value = getMemsetValue(Src, VT, DAG);
2824 SDOperand Store = DAG.getStore(Chain, Value,
2825 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002826 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002827 OutChains.push_back(Store);
2828 DstOff += VTSize;
2829 }
2830
2831 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2832 &OutChains[0], OutChains.size());
2833}
2834
2835SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002836 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002837 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002838 const Value *DstSV, uint64_t DstSVOff,
2839 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002840
2841 // Check to see if we should lower the memcpy to loads and stores first.
2842 // For cases within the target-specified limits, this is the best choice.
2843 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2844 if (ConstantSize) {
2845 // Memcpy with size zero? Just return the original chain.
2846 if (ConstantSize->isNullValue())
2847 return Chain;
2848
2849 SDOperand Result =
2850 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002851 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002852 if (Result.Val)
2853 return Result;
2854 }
2855
2856 // Then check to see if we should lower the memcpy with target-specific
2857 // code. If the target chooses to do this, this is the next best.
2858 SDOperand Result =
2859 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2860 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002861 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002862 if (Result.Val)
2863 return Result;
2864
2865 // If we really need inline code and the target declined to provide it,
2866 // use a (potentially long) sequence of loads and stores.
2867 if (AlwaysInline) {
2868 assert(ConstantSize && "AlwaysInline requires a constant size!");
2869 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2870 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002871 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002872 }
2873
2874 // Emit a library call.
2875 TargetLowering::ArgListTy Args;
2876 TargetLowering::ArgListEntry Entry;
2877 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2878 Entry.Node = Dst; Args.push_back(Entry);
2879 Entry.Node = Src; Args.push_back(Entry);
2880 Entry.Node = Size; Args.push_back(Entry);
2881 std::pair<SDOperand,SDOperand> CallResult =
2882 TLI.LowerCallTo(Chain, Type::VoidTy,
2883 false, false, false, CallingConv::C, false,
2884 getExternalSymbol("memcpy", TLI.getPointerTy()),
2885 Args, *this);
2886 return CallResult.second;
2887}
2888
2889SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2890 SDOperand Src, SDOperand Size,
2891 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002892 const Value *DstSV, uint64_t DstSVOff,
2893 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002894
Dan Gohman21323f32008-05-29 19:42:22 +00002895 // Check to see if we should lower the memmove to loads and stores first.
2896 // For cases within the target-specified limits, this is the best choice.
2897 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2898 if (ConstantSize) {
2899 // Memmove with size zero? Just return the original chain.
2900 if (ConstantSize->isNullValue())
2901 return Chain;
2902
2903 SDOperand Result =
2904 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2905 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2906 if (Result.Val)
2907 return Result;
2908 }
Dan Gohman707e0182008-04-12 04:36:06 +00002909
2910 // Then check to see if we should lower the memmove with target-specific
2911 // code. If the target chooses to do this, this is the next best.
2912 SDOperand Result =
2913 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002914 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002915 if (Result.Val)
2916 return Result;
2917
2918 // Emit a library call.
2919 TargetLowering::ArgListTy Args;
2920 TargetLowering::ArgListEntry Entry;
2921 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2922 Entry.Node = Dst; Args.push_back(Entry);
2923 Entry.Node = Src; Args.push_back(Entry);
2924 Entry.Node = Size; Args.push_back(Entry);
2925 std::pair<SDOperand,SDOperand> CallResult =
2926 TLI.LowerCallTo(Chain, Type::VoidTy,
2927 false, false, false, CallingConv::C, false,
2928 getExternalSymbol("memmove", TLI.getPointerTy()),
2929 Args, *this);
2930 return CallResult.second;
2931}
2932
2933SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2934 SDOperand Src, SDOperand Size,
2935 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002936 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002937
2938 // Check to see if we should lower the memset to stores first.
2939 // For cases within the target-specified limits, this is the best choice.
2940 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2941 if (ConstantSize) {
2942 // Memset with size zero? Just return the original chain.
2943 if (ConstantSize->isNullValue())
2944 return Chain;
2945
2946 SDOperand Result =
2947 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002948 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002949 if (Result.Val)
2950 return Result;
2951 }
2952
2953 // Then check to see if we should lower the memset with target-specific
2954 // code. If the target chooses to do this, this is the next best.
2955 SDOperand Result =
2956 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002957 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002958 if (Result.Val)
2959 return Result;
2960
2961 // Emit a library call.
2962 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2963 TargetLowering::ArgListTy Args;
2964 TargetLowering::ArgListEntry Entry;
2965 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2966 Args.push_back(Entry);
2967 // Extend or truncate the argument to be an i32 value for the call.
2968 if (Src.getValueType() > MVT::i32)
2969 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2970 else
2971 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2972 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2973 Args.push_back(Entry);
2974 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2975 Args.push_back(Entry);
2976 std::pair<SDOperand,SDOperand> CallResult =
2977 TLI.LowerCallTo(Chain, Type::VoidTy,
2978 false, false, false, CallingConv::C, false,
2979 getExternalSymbol("memset", TLI.getPointerTy()),
2980 Args, *this);
2981 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002982}
2983
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002984SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002985 SDOperand Ptr, SDOperand Cmp,
2986 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002987 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002988 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2989 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002990 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002991 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002992 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2993 ID.AddInteger((unsigned int)VT);
2994 void* IP = 0;
2995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2996 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002997 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002998 CSEMap.InsertNode(N, IP);
2999 AllNodes.push_back(N);
3000 return SDOperand(N, 0);
3001}
3002
3003SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003004 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003005 MVT::ValueType VT) {
Mon P Wang63307c32008-05-05 19:05:59 +00003006 assert(( Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_LSS
3007 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3008 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
3009 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3010 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003011 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003012 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003013 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003014 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003015 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3016 ID.AddInteger((unsigned int)VT);
3017 void* IP = 0;
3018 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3019 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003020 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003021 CSEMap.InsertNode(N, IP);
3022 AllNodes.push_back(N);
3023 return SDOperand(N, 0);
3024}
3025
Duncan Sands14ea39c2008-03-27 20:23:40 +00003026SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003027SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
3028 MVT::ValueType VT, SDOperand Chain,
3029 SDOperand Ptr, SDOperand Offset,
3030 const Value *SV, int SVOffset, MVT::ValueType EVT,
3031 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003032 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3033 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003034 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003035 Ty = MVT::getTypeForValueType(VT);
3036 } else if (SV) {
3037 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3038 assert(PT && "Value for load must be a pointer");
3039 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003040 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003041 assert(Ty && "Could not get type information for load");
3042 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3043 }
Evan Cheng466685d2006-10-09 20:57:25 +00003044
Duncan Sands14ea39c2008-03-27 20:23:40 +00003045 if (VT == EVT) {
3046 ExtType = ISD::NON_EXTLOAD;
3047 } else if (ExtType == ISD::NON_EXTLOAD) {
3048 assert(VT == EVT && "Non-extending load from different memory type!");
3049 } else {
3050 // Extending load.
3051 if (MVT::isVector(VT))
3052 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
3053 else
3054 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
3055 "Should only be an extending load, not truncating!");
3056 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
3057 "Cannot sign/zero extend a FP/Vector load!");
3058 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
3059 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003060 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003061
3062 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003063 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003064 "Unindexed load with an offset!");
3065
3066 SDVTList VTs = Indexed ?
3067 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3068 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003069 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003070 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003071 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003072 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00003073 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00003074 ID.AddInteger(Alignment);
3075 ID.AddInteger(isVolatile);
3076 void *IP = 0;
3077 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3078 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003079 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3080 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003081 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003082 AllNodes.push_back(N);
3083 return SDOperand(N, 0);
3084}
3085
Duncan Sands14ea39c2008-03-27 20:23:40 +00003086SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
3087 SDOperand Chain, SDOperand Ptr,
3088 const Value *SV, int SVOffset,
3089 bool isVolatile, unsigned Alignment) {
3090 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003091 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3092 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003093}
3094
3095SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
3096 SDOperand Chain, SDOperand Ptr,
3097 const Value *SV,
3098 int SVOffset, MVT::ValueType EVT,
3099 bool isVolatile, unsigned Alignment) {
3100 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003101 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3102 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003103}
3104
Evan Cheng144d8f02006-11-09 17:55:04 +00003105SDOperand
3106SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3107 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003108 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003109 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3110 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003111 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3112 LD->getChain(), Base, Offset, LD->getSrcValue(),
3113 LD->getSrcValueOffset(), LD->getMemoryVT(),
3114 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003115}
3116
Jeff Cohend41b30d2006-11-05 19:31:28 +00003117SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003118 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003119 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00003120 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003121
Dan Gohman575e2f42007-06-04 15:49:41 +00003122 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3123 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003124 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003125 Ty = MVT::getTypeForValueType(VT);
3126 } else if (SV) {
3127 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3128 assert(PT && "Value for store must be a pointer");
3129 Ty = PT->getElementType();
3130 }
3131 assert(Ty && "Could not get type information for store");
3132 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3133 }
Evan Chengad071e12006-10-05 22:57:11 +00003134 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003135 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003136 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003137 FoldingSetNodeID ID;
3138 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003139 ID.AddInteger(ISD::UNINDEXED);
3140 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00003141 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003142 ID.AddInteger(Alignment);
3143 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003144 void *IP = 0;
3145 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3146 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003147 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003148 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003149 CSEMap.InsertNode(N, IP);
3150 AllNodes.push_back(N);
3151 return SDOperand(N, 0);
3152}
3153
Jeff Cohend41b30d2006-11-05 19:31:28 +00003154SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003155 SDOperand Ptr, const Value *SV,
3156 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003157 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00003158 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003159
3160 if (VT == SVT)
3161 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003162
Duncan Sandsaf47b112007-10-16 09:56:48 +00003163 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
3164 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003165 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
3166 "Can't do FP-INT conversion!");
3167
Dan Gohman575e2f42007-06-04 15:49:41 +00003168 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3169 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003170 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003171 Ty = MVT::getTypeForValueType(VT);
3172 } else if (SV) {
3173 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3174 assert(PT && "Value for store must be a pointer");
3175 Ty = PT->getElementType();
3176 }
3177 assert(Ty && "Could not get type information for store");
3178 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3179 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003180 SDVTList VTs = getVTList(MVT::Other);
3181 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003182 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003183 FoldingSetNodeID ID;
3184 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003185 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003186 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00003187 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003188 ID.AddInteger(Alignment);
3189 ID.AddInteger(isVolatile);
3190 void *IP = 0;
3191 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3192 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003193 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003194 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003195 CSEMap.InsertNode(N, IP);
3196 AllNodes.push_back(N);
3197 return SDOperand(N, 0);
3198}
3199
Evan Cheng144d8f02006-11-09 17:55:04 +00003200SDOperand
3201SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3202 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003203 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3204 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3205 "Store is already a indexed store!");
3206 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3207 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3208 FoldingSetNodeID ID;
3209 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3210 ID.AddInteger(AM);
3211 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003212 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00003213 ID.AddInteger(ST->getAlignment());
3214 ID.AddInteger(ST->isVolatile());
3215 void *IP = 0;
3216 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3217 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003218 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003219 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003220 ST->getSrcValue(), ST->getSrcValueOffset(),
3221 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003222 CSEMap.InsertNode(N, IP);
3223 AllNodes.push_back(N);
3224 return SDOperand(N, 0);
3225}
3226
Nate Begemanacc398c2006-01-25 18:21:52 +00003227SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
3228 SDOperand Chain, SDOperand Ptr,
3229 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003230 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003231 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003232}
3233
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003234SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003235 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003236 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003237 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003238 case 1: return getNode(Opcode, VT, Ops[0]);
3239 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3240 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003241 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003242 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003243
Chris Lattneref847df2005-04-09 03:27:28 +00003244 switch (Opcode) {
3245 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003246 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003247 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003248 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3249 "LHS and RHS of condition must have same type!");
3250 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3251 "True and False arms of SelectCC must have same type!");
3252 assert(Ops[2].getValueType() == VT &&
3253 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003254 break;
3255 }
3256 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003257 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003258 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3259 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003260 break;
3261 }
Chris Lattneref847df2005-04-09 03:27:28 +00003262 }
3263
Chris Lattner385328c2005-05-14 07:42:29 +00003264 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003265 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003266 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003267 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003268 FoldingSetNodeID ID;
3269 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003270 void *IP = 0;
3271 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3272 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003273 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003274 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003275 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003276 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003277 }
Chris Lattneref847df2005-04-09 03:27:28 +00003278 AllNodes.push_back(N);
3279 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003280}
3281
Chris Lattner89c34632005-05-14 06:20:26 +00003282SDOperand SelectionDAG::getNode(unsigned Opcode,
3283 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003284 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003285 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3286 Ops, NumOps);
3287}
3288
3289SDOperand SelectionDAG::getNode(unsigned Opcode,
3290 const MVT::ValueType *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003291 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003292 if (NumVTs == 1)
3293 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003294 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3295}
3296
3297SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003298 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003299 if (VTList.NumVTs == 1)
3300 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003301
Chris Lattner5f056bf2005-07-10 01:55:33 +00003302 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003303 // FIXME: figure out how to safely handle things like
3304 // int foo(int x) { return 1 << (x & 255); }
3305 // int bar() { return foo(256); }
3306#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003307 case ISD::SRA_PARTS:
3308 case ISD::SRL_PARTS:
3309 case ISD::SHL_PARTS:
3310 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003311 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003312 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3313 else if (N3.getOpcode() == ISD::AND)
3314 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3315 // If the and is only masking out bits that cannot effect the shift,
3316 // eliminate the and.
3317 unsigned NumBits = MVT::getSizeInBits(VT)*2;
3318 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3319 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3320 }
3321 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003322#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003323 }
Chris Lattner89c34632005-05-14 06:20:26 +00003324
Chris Lattner43247a12005-08-25 19:12:10 +00003325 // Memoize the node unless it returns a flag.
3326 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003327 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003328 FoldingSetNodeID ID;
3329 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003330 void *IP = 0;
3331 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3332 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003333 if (NumOps == 1)
3334 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3335 else if (NumOps == 2)
3336 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3337 else if (NumOps == 3)
3338 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3339 else
3340 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003341 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003342 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003343 if (NumOps == 1)
3344 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3345 else if (NumOps == 2)
3346 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3347 else if (NumOps == 3)
3348 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3349 else
3350 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003351 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003352 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003353 return SDOperand(N, 0);
3354}
3355
Dan Gohman08ce9762007-10-08 15:49:58 +00003356SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003357 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003358}
3359
3360SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3361 SDOperand N1) {
3362 SDOperand Ops[] = { N1 };
3363 return getNode(Opcode, VTList, Ops, 1);
3364}
3365
3366SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3367 SDOperand N1, SDOperand N2) {
3368 SDOperand Ops[] = { N1, N2 };
3369 return getNode(Opcode, VTList, Ops, 2);
3370}
3371
3372SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3373 SDOperand N1, SDOperand N2, SDOperand N3) {
3374 SDOperand Ops[] = { N1, N2, N3 };
3375 return getNode(Opcode, VTList, Ops, 3);
3376}
3377
3378SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3379 SDOperand N1, SDOperand N2, SDOperand N3,
3380 SDOperand N4) {
3381 SDOperand Ops[] = { N1, N2, N3, N4 };
3382 return getNode(Opcode, VTList, Ops, 4);
3383}
3384
3385SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3386 SDOperand N1, SDOperand N2, SDOperand N3,
3387 SDOperand N4, SDOperand N5) {
3388 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3389 return getNode(Opcode, VTList, Ops, 5);
3390}
3391
Chris Lattner70046e92006-08-15 17:46:01 +00003392SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003393 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003394}
3395
Chris Lattner70046e92006-08-15 17:46:01 +00003396SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00003397 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3398 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003399 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003400 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003401 }
3402 std::vector<MVT::ValueType> V;
3403 V.push_back(VT1);
3404 V.push_back(VT2);
3405 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003406 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003407}
Chris Lattner70046e92006-08-15 17:46:01 +00003408SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
3409 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003410 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003411 E = VTList.end(); I != E; ++I) {
3412 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3413 (*I)[2] == VT3)
3414 return makeVTList(&(*I)[0], 3);
3415 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003416 std::vector<MVT::ValueType> V;
3417 V.push_back(VT1);
3418 V.push_back(VT2);
3419 V.push_back(VT3);
3420 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003421 return makeVTList(&(*VTList.begin())[0], 3);
3422}
3423
3424SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
3425 switch (NumVTs) {
3426 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003427 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003428 case 2: return getVTList(VTs[0], VTs[1]);
3429 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3430 default: break;
3431 }
3432
3433 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3434 E = VTList.end(); I != E; ++I) {
3435 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3436
3437 bool NoMatch = false;
3438 for (unsigned i = 2; i != NumVTs; ++i)
3439 if (VTs[i] != (*I)[i]) {
3440 NoMatch = true;
3441 break;
3442 }
3443 if (!NoMatch)
3444 return makeVTList(&*I->begin(), NumVTs);
3445 }
3446
3447 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
3448 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003449}
3450
3451
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003452/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3453/// specified operands. If the resultant node already exists in the DAG,
3454/// this does not modify the specified node, instead it returns the node that
3455/// already exists. If the resultant node does not exist in the DAG, the
3456/// input node is returned. As a degenerate case, if you specify the same
3457/// input operands as the node already has, the input node is returned.
3458SDOperand SelectionDAG::
3459UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3460 SDNode *N = InN.Val;
3461 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3462
3463 // Check to see if there is no change.
3464 if (Op == N->getOperand(0)) return InN;
3465
3466 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003467 void *InsertPos = 0;
3468 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3469 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003470
3471 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003472 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003473 RemoveNodeFromCSEMaps(N);
3474
3475 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003476 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003477 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003478 N->OperandList[0].setUser(N);
3479 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003480
3481 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003482 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003483 return InN;
3484}
3485
3486SDOperand SelectionDAG::
3487UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3488 SDNode *N = InN.Val;
3489 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3490
3491 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003492 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3493 return InN; // No operands changed, just return the input node.
3494
3495 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003496 void *InsertPos = 0;
3497 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3498 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003499
3500 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003501 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003502 RemoveNodeFromCSEMaps(N);
3503
3504 // Now we update the operands.
3505 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003506 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003507 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003508 N->OperandList[0].setUser(N);
3509 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003510 }
3511 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003512 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003513 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003514 N->OperandList[1].setUser(N);
3515 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003516 }
3517
3518 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003519 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003520 return InN;
3521}
3522
3523SDOperand SelectionDAG::
3524UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003525 SDOperand Ops[] = { Op1, Op2, Op3 };
3526 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003527}
3528
3529SDOperand SelectionDAG::
3530UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3531 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003532 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3533 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003534}
3535
3536SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003537UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3538 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003539 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3540 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003541}
3542
Chris Lattner809ec112006-01-28 10:09:25 +00003543SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003544UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003545 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003546 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003547 "Update with wrong number of operands");
3548
3549 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003550 bool AnyChange = false;
3551 for (unsigned i = 0; i != NumOps; ++i) {
3552 if (Ops[i] != N->getOperand(i)) {
3553 AnyChange = true;
3554 break;
3555 }
3556 }
3557
3558 // No operands changed, just return the input node.
3559 if (!AnyChange) return InN;
3560
3561 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003562 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003563 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003564 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003565
Dan Gohman7ceda162008-05-02 00:05:03 +00003566 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003567 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003568 RemoveNodeFromCSEMaps(N);
3569
3570 // Now we update the operands.
3571 for (unsigned i = 0; i != NumOps; ++i) {
3572 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003573 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003574 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003575 N->OperandList[i].setUser(N);
3576 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003577 }
3578 }
3579
3580 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003581 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003582 return InN;
3583}
3584
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003585/// MorphNodeTo - This frees the operands of the current node, resets the
3586/// opcode, types, and operands to the specified value. This should only be
3587/// used by the SelectionDAG class.
3588void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003589 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003590 NodeType = Opc;
3591 ValueList = L.VTs;
3592 NumValues = L.NumVTs;
3593
3594 // Clear the operands list, updating used nodes to remove this from their
3595 // use list.
3596 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003597 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003598
3599 // If NumOps is larger than the # of operands we currently have, reallocate
3600 // the operand list.
3601 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003602 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003603 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003604 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003605 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003606 OperandsNeedDelete = true;
3607 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003608
3609 // Assign the new operands.
3610 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003611
3612 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3613 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003614 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003615 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003616 N->addUser(i, this);
3617 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003618 }
3619}
Chris Lattner149c58c2005-08-16 18:17:10 +00003620
3621/// SelectNodeTo - These are used for target selectors to *mutate* the
3622/// specified node to have the specified return type, Target opcode, and
3623/// operands. Note that target opcodes are stored as
3624/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003625///
3626/// Note that SelectNodeTo returns the resultant node. If there is already a
3627/// node of the specified opcode and operands, it returns that node instead of
3628/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003629SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3630 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003631 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003632 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003633 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003634 void *IP = 0;
3635 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003636 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003637
Chris Lattner7651fa42005-08-24 23:00:29 +00003638 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003639
Dan Gohman35b31be2008-04-17 23:02:12 +00003640 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003641
Chris Lattner4a283e92006-08-11 18:38:11 +00003642 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003643 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003644}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003645
Evan Cheng95514ba2006-08-26 08:00:10 +00003646SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3647 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003648 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003649 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003650 SDOperand Ops[] = { Op1 };
3651
Jim Laskey583bd472006-10-27 23:46:08 +00003652 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003653 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003654 void *IP = 0;
3655 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003656 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003657
Chris Lattner149c58c2005-08-16 18:17:10 +00003658 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003659 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003660 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003661 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003662}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003663
Evan Cheng95514ba2006-08-26 08:00:10 +00003664SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3665 MVT::ValueType VT, SDOperand Op1,
3666 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003667 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003668 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003669 SDOperand Ops[] = { Op1, Op2 };
3670
Jim Laskey583bd472006-10-27 23:46:08 +00003671 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003672 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003673 void *IP = 0;
3674 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003675 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003676
Chris Lattner149c58c2005-08-16 18:17:10 +00003677 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003678
Chris Lattner63e3f142007-02-04 07:28:00 +00003679 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003680
Chris Lattnera5682852006-08-07 23:03:03 +00003681 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003682 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003683}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003684
Evan Cheng95514ba2006-08-26 08:00:10 +00003685SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3686 MVT::ValueType VT, SDOperand Op1,
3687 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003688 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003689 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003690 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003691 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003692 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003693 void *IP = 0;
3694 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003695 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003696
Chris Lattner149c58c2005-08-16 18:17:10 +00003697 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003698
Chris Lattner63e3f142007-02-04 07:28:00 +00003699 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003700
Chris Lattnera5682852006-08-07 23:03:03 +00003701 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003702 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003703}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003704
Evan Cheng95514ba2006-08-26 08:00:10 +00003705SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003706 MVT::ValueType VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003707 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003708 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003709 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003710 FoldingSetNodeID ID;
3711 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003712 void *IP = 0;
3713 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003714 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003715
Chris Lattner6b09a292005-08-21 18:49:33 +00003716 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003717 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003718
Chris Lattnera5682852006-08-07 23:03:03 +00003719 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003720 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003721}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003722
Evan Cheng95514ba2006-08-26 08:00:10 +00003723SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3724 MVT::ValueType VT1, MVT::ValueType VT2,
3725 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003726 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003727 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003728 SDOperand Ops[] = { Op1, Op2 };
3729 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003730 void *IP = 0;
3731 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003732 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003733
Chris Lattner0fb094f2005-11-19 01:44:53 +00003734 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003735 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003736 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003737 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003738}
3739
Evan Cheng95514ba2006-08-26 08:00:10 +00003740SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3741 MVT::ValueType VT1, MVT::ValueType VT2,
3742 SDOperand Op1, SDOperand Op2,
3743 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003744 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003745 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003746 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003747 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003748 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003749 void *IP = 0;
3750 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003751 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003752
Chris Lattner0fb094f2005-11-19 01:44:53 +00003753 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003754
Chris Lattner63e3f142007-02-04 07:28:00 +00003755 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003756 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003757 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003758}
3759
Chris Lattner0fb094f2005-11-19 01:44:53 +00003760
Evan Cheng6ae46c42006-02-09 07:15:23 +00003761/// getTargetNode - These are used for target selectors to create a new node
3762/// with specified return type(s), target opcode, and operands.
3763///
3764/// Note that getTargetNode returns the resultant node. If there is already a
3765/// node of the specified opcode and operands, it returns that node instead of
3766/// the current one.
3767SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3768 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3769}
3770SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3771 SDOperand Op1) {
3772 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3773}
3774SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3775 SDOperand Op1, SDOperand Op2) {
3776 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3777}
3778SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003779 SDOperand Op1, SDOperand Op2,
3780 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003781 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3782}
3783SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003784 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003785 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003786}
3787SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003788 MVT::ValueType VT2) {
3789 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3790 SDOperand Op;
3791 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3792}
3793SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003794 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003795 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003796 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003797}
3798SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003799 MVT::ValueType VT2, SDOperand Op1,
3800 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003801 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003802 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003803 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003804}
3805SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003806 MVT::ValueType VT2, SDOperand Op1,
3807 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003808 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003809 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003810 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003811}
Evan Cheng694481e2006-08-27 08:08:54 +00003812SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3813 MVT::ValueType VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003814 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003815 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003816 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003817}
3818SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3819 MVT::ValueType VT2, MVT::ValueType VT3,
3820 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003821 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003822 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003823 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003824}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003825SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3826 MVT::ValueType VT2, MVT::ValueType VT3,
3827 SDOperand Op1, SDOperand Op2,
3828 SDOperand Op3) {
3829 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3830 SDOperand Ops[] = { Op1, Op2, Op3 };
3831 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3832}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003833SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003834 MVT::ValueType VT2, MVT::ValueType VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003835 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003836 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3837 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003838}
Evan Cheng05e69c12007-09-12 23:39:49 +00003839SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3840 MVT::ValueType VT2, MVT::ValueType VT3,
3841 MVT::ValueType VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003842 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng05e69c12007-09-12 23:39:49 +00003843 std::vector<MVT::ValueType> VTList;
3844 VTList.push_back(VT1);
3845 VTList.push_back(VT2);
3846 VTList.push_back(VT3);
3847 VTList.push_back(VT4);
3848 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3849 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3850}
Evan Cheng39305cf2007-10-05 01:10:49 +00003851SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3852 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003853 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng39305cf2007-10-05 01:10:49 +00003854 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3855 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3856 Ops, NumOps).Val;
3857}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003858
Evan Cheng08b11732008-03-22 01:55:50 +00003859/// getNodeIfExists - Get the specified node if it's already available, or
3860/// else return NULL.
3861SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003862 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003863 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3864 FoldingSetNodeID ID;
3865 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3866 void *IP = 0;
3867 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3868 return E;
3869 }
3870 return NULL;
3871}
3872
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003873
Evan Cheng99157a02006-08-07 22:13:29 +00003874/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003875/// This can cause recursive merging of nodes in the DAG.
3876///
Chris Lattner11d049c2008-02-03 03:35:22 +00003877/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003878///
Chris Lattner11d049c2008-02-03 03:35:22 +00003879void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003880 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003881 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003882 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003883 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003884 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003885
Chris Lattner8b8749f2005-08-17 19:00:20 +00003886 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003887 SDNode::use_iterator UI = From->use_begin();
3888 SDNode *U = UI->getUser();
3889
Chris Lattner8b8749f2005-08-17 19:00:20 +00003890 // This node is about to morph, remove its old self from the CSE maps.
3891 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003892 int operandNum = 0;
3893 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3894 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003895 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003896 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003897 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003898 I->setUser(U);
3899 To.Val->addUser(operandNum, U);
3900 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003901
3902 // Now that we have modified U, add it back to the CSE maps. If it already
3903 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003904 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003905 ReplaceAllUsesWith(U, Existing, UpdateListener);
3906 // U is now dead. Inform the listener if it exists and delete it.
3907 if (UpdateListener)
3908 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003909 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003910 } else {
3911 // If the node doesn't already exist, we updated it. Inform a listener if
3912 // it exists.
3913 if (UpdateListener)
3914 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003915 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003916 }
3917}
3918
3919/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3920/// This can cause recursive merging of nodes in the DAG.
3921///
3922/// This version assumes From/To have matching types and numbers of result
3923/// values.
3924///
Chris Lattner1e111c72005-09-07 05:37:01 +00003925void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003926 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003927 assert(From != To && "Cannot replace uses of with self");
3928 assert(From->getNumValues() == To->getNumValues() &&
3929 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003930 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003931 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3932 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003933
3934 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003935 SDNode::use_iterator UI = From->use_begin();
3936 SDNode *U = UI->getUser();
3937
Chris Lattner8b52f212005-08-26 18:36:28 +00003938 // This node is about to morph, remove its old self from the CSE maps.
3939 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003940 int operandNum = 0;
3941 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3942 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003943 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003944 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003945 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003946 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003947 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003948
Chris Lattner8b8749f2005-08-17 19:00:20 +00003949 // Now that we have modified U, add it back to the CSE maps. If it already
3950 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003951 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003952 ReplaceAllUsesWith(U, Existing, UpdateListener);
3953 // U is now dead. Inform the listener if it exists and delete it.
3954 if (UpdateListener)
3955 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003956 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003957 } else {
3958 // If the node doesn't already exist, we updated it. Inform a listener if
3959 // it exists.
3960 if (UpdateListener)
3961 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003962 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003963 }
3964}
3965
Chris Lattner8b52f212005-08-26 18:36:28 +00003966/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3967/// This can cause recursive merging of nodes in the DAG.
3968///
3969/// This version can replace From with any result values. To must match the
3970/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003971void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003972 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003973 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003974 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003975 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003976
3977 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003978 SDNode::use_iterator UI = From->use_begin();
3979 SDNode *U = UI->getUser();
3980
Chris Lattner7b2880c2005-08-24 22:44:39 +00003981 // This node is about to morph, remove its old self from the CSE maps.
3982 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003983 int operandNum = 0;
3984 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3985 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003986 if (I->getVal() == From) {
3987 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003988 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003989 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003990 I->setUser(U);
3991 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003992 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003993
Chris Lattner7b2880c2005-08-24 22:44:39 +00003994 // Now that we have modified U, add it back to the CSE maps. If it already
3995 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003996 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003997 ReplaceAllUsesWith(U, Existing, UpdateListener);
3998 // U is now dead. Inform the listener if it exists and delete it.
3999 if (UpdateListener)
4000 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004001 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004002 } else {
4003 // If the node doesn't already exist, we updated it. Inform a listener if
4004 // it exists.
4005 if (UpdateListener)
4006 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004007 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004008 }
4009}
4010
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004011namespace {
4012 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4013 /// any deleted nodes from the set passed into its constructor and recursively
4014 /// notifies another update listener if specified.
4015 class ChainedSetUpdaterListener :
4016 public SelectionDAG::DAGUpdateListener {
4017 SmallSetVector<SDNode*, 16> &Set;
4018 SelectionDAG::DAGUpdateListener *Chain;
4019 public:
4020 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4021 SelectionDAG::DAGUpdateListener *chain)
4022 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004023
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004024 virtual void NodeDeleted(SDNode *N) {
4025 Set.remove(N);
4026 if (Chain) Chain->NodeDeleted(N);
4027 }
4028 virtual void NodeUpdated(SDNode *N) {
4029 if (Chain) Chain->NodeUpdated(N);
4030 }
4031 };
4032}
4033
Chris Lattner012f2412006-02-17 21:58:01 +00004034/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4035/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004036/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004037void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004038 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004039 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004040
Chris Lattner012f2412006-02-17 21:58:01 +00004041 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004042 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004043 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004044 return;
4045 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004046
4047 if (From.use_empty()) return;
4048
Chris Lattnercf5640b2007-02-04 00:14:31 +00004049 // Get all of the users of From.Val. We want these in a nice,
4050 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004051 SmallSetVector<SDNode*, 16> Users;
4052 for (SDNode::use_iterator UI = From.Val->use_begin(),
4053 E = From.Val->use_end(); UI != E; ++UI) {
4054 SDNode *User = UI->getUser();
4055 if (!Users.count(User))
4056 Users.insert(User);
4057 }
Chris Lattner012f2412006-02-17 21:58:01 +00004058
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004059 // When one of the recursive merges deletes nodes from the graph, we need to
4060 // make sure that UpdateListener is notified *and* that the node is removed
4061 // from Users if present. CSUL does this.
4062 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004063
Chris Lattner012f2412006-02-17 21:58:01 +00004064 while (!Users.empty()) {
4065 // We know that this user uses some value of From. If it is the right
4066 // value, update it.
4067 SDNode *User = Users.back();
4068 Users.pop_back();
4069
Chris Lattner01d029b2007-10-15 06:10:22 +00004070 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004071 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004072 for (; Op != E; ++Op)
4073 if (*Op == From) break;
4074
4075 // If there are no matches, the user must use some other result of From.
4076 if (Op == E) continue;
4077
4078 // Okay, we know this user needs to be updated. Remove its old self
4079 // from the CSE maps.
4080 RemoveNodeFromCSEMaps(User);
4081
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004083 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004084 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004085 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004086 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004087 Op->setUser(User);
4088 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004089 }
4090 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004091
4092 // Now that we have modified User, add it back to the CSE maps. If it
4093 // already exists there, recursively merge the results together.
4094 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004095 if (!Existing) {
4096 if (UpdateListener) UpdateListener->NodeUpdated(User);
4097 continue; // Continue on to next user.
4098 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004099
4100 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004101 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004102 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004103 // can cause deletion of nodes that used the old value. To handle this, we
4104 // use CSUL to remove them from the Users set.
4105 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004106
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004107 // User is now dead. Notify a listener if present.
4108 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00004109 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004110 }
4111}
4112
Evan Chenge6f35d82006-08-01 08:20:41 +00004113/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4114/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004115unsigned SelectionDAG::AssignNodeIds() {
4116 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004117 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4118 SDNode *N = I;
4119 N->setNodeId(Id++);
4120 }
4121 return Id;
4122}
4123
Evan Chenge6f35d82006-08-01 08:20:41 +00004124/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004125/// based on their topological order. It returns the maximum id and a vector
4126/// of the SDNodes* in assigned order by reference.
4127unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004128 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004129 std::vector<unsigned> InDegree(DAGSize);
4130 std::vector<SDNode*> Sources;
4131
4132 // Use a two pass approach to avoid using a std::map which is slow.
4133 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004134 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4135 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004136 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004137 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004138 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004139 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004140 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004141 }
4142
Evan Cheng99157a02006-08-07 22:13:29 +00004143 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004144 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004145 SDNode *N = Sources.back();
4146 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004147 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004148 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004149 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004150 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004151 if (Degree == 0)
4152 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004153 }
4154 }
4155
Evan Chengc384d6c2006-08-02 22:00:34 +00004156 // Second pass, assign the actual topological order as node ids.
4157 Id = 0;
4158 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4159 TI != TE; ++TI)
4160 (*TI)->setNodeId(Id++);
4161
4162 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004163}
4164
4165
Evan Cheng091cba12006-07-27 06:39:06 +00004166
Jim Laskey58b968b2005-08-17 20:08:02 +00004167//===----------------------------------------------------------------------===//
4168// SDNode Class
4169//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004170
Chris Lattner917d2c92006-07-19 00:00:37 +00004171// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004172void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004173void UnarySDNode::ANCHOR() {}
4174void BinarySDNode::ANCHOR() {}
4175void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004176void HandleSDNode::ANCHOR() {}
4177void StringSDNode::ANCHOR() {}
4178void ConstantSDNode::ANCHOR() {}
4179void ConstantFPSDNode::ANCHOR() {}
4180void GlobalAddressSDNode::ANCHOR() {}
4181void FrameIndexSDNode::ANCHOR() {}
4182void JumpTableSDNode::ANCHOR() {}
4183void ConstantPoolSDNode::ANCHOR() {}
4184void BasicBlockSDNode::ANCHOR() {}
4185void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004186void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004187void RegisterSDNode::ANCHOR() {}
4188void ExternalSymbolSDNode::ANCHOR() {}
4189void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004190void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004191void VTSDNode::ANCHOR() {}
4192void LoadSDNode::ANCHOR() {}
4193void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004194void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004195
Chris Lattner48b85922007-02-04 02:41:42 +00004196HandleSDNode::~HandleSDNode() {
4197 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004198 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004199}
4200
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004201GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
4202 MVT::ValueType VT, int o)
4203 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004204 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004205 // Thread Local
4206 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4207 // Non Thread Local
4208 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4209 getSDVTList(VT)), Offset(o) {
4210 TheGlobal = const_cast<GlobalValue*>(GA);
4211}
Chris Lattner48b85922007-02-04 02:41:42 +00004212
Dan Gohman36b5c132008-04-07 19:35:22 +00004213/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004214/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004215MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman69de1932008-02-06 22:27:42 +00004216 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
4217 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004218 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4219 MachineMemOperand::MOStore;
4220 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004221
4222 // Check if the load references a frame index, and does not have
4223 // an SV attached.
4224 const FrameIndexSDNode *FI =
4225 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4226 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004227 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4228 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00004229 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004230 return MachineMemOperand(getSrcValue(), Flags,
4231 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00004232}
4233
Jim Laskey583bd472006-10-27 23:46:08 +00004234/// Profile - Gather unique data for the node.
4235///
4236void SDNode::Profile(FoldingSetNodeID &ID) {
4237 AddNodeIDNode(ID, this);
4238}
4239
Chris Lattnera3255112005-11-08 23:30:28 +00004240/// getValueTypeList - Return a pointer to the specified value type.
4241///
Dan Gohman547ca532008-02-08 03:26:46 +00004242const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004243 if (MVT::isExtendedVT(VT)) {
4244 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004245 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004246 } else {
4247 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
4248 VTs[VT] = VT;
4249 return &VTs[VT];
4250 }
Chris Lattnera3255112005-11-08 23:30:28 +00004251}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004252
Chris Lattner5c884562005-01-12 18:37:47 +00004253/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4254/// indicated value. This method ignores uses of other values defined by this
4255/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004256bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004257 assert(Value < getNumValues() && "Bad value!");
4258
4259 // If there is only one value, this is easy.
4260 if (getNumValues() == 1)
4261 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004262 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004263
Evan Cheng4ee62112006-02-05 06:29:23 +00004264 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004265
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004266 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004267
Roman Levensteindc1adac2008-04-07 10:06:32 +00004268 // TODO: Only iterate over uses of a given value of the node
4269 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4270 if (*UI == TheValue) {
4271 if (NUses == 0)
4272 return false;
4273 --NUses;
4274 }
Chris Lattner5c884562005-01-12 18:37:47 +00004275 }
4276
4277 // Found exactly the right number of uses?
4278 return NUses == 0;
4279}
4280
4281
Evan Cheng33d55952007-08-02 05:29:38 +00004282/// hasAnyUseOfValue - Return true if there are any use of the indicated
4283/// value. This method ignores uses of other values defined by this operation.
4284bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4285 assert(Value < getNumValues() && "Bad value!");
4286
Dan Gohman30359592008-01-29 13:02:09 +00004287 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004288
4289 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4290
4291 SmallPtrSet<SDNode*, 32> UsersHandled;
4292
Roman Levensteindc1adac2008-04-07 10:06:32 +00004293 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4294 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004295 if (User->getNumOperands() == 1 ||
4296 UsersHandled.insert(User)) // First time we've seen this?
4297 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4298 if (User->getOperand(i) == TheValue) {
4299 return true;
4300 }
4301 }
4302
4303 return false;
4304}
4305
4306
Evan Cheng917be682008-03-04 00:41:45 +00004307/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004308///
Evan Cheng917be682008-03-04 00:41:45 +00004309bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004310 bool Seen = false;
4311 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004312 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004313 if (User == this)
4314 Seen = true;
4315 else
4316 return false;
4317 }
4318
4319 return Seen;
4320}
4321
Evan Chenge6e97e62006-11-03 07:31:32 +00004322/// isOperand - Return true if this node is an operand of N.
4323///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004324bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4326 if (*this == N->getOperand(i))
4327 return true;
4328 return false;
4329}
4330
Evan Cheng917be682008-03-04 00:41:45 +00004331bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004332 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004333 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004334 return true;
4335 return false;
4336}
Evan Cheng4ee62112006-02-05 06:29:23 +00004337
Chris Lattner572dee72008-01-16 05:49:24 +00004338/// reachesChainWithoutSideEffects - Return true if this operand (which must
4339/// be a chain) reaches the specified operand without crossing any
4340/// side-effecting instructions. In practice, this looks through token
4341/// factors and non-volatile loads. In order to remain efficient, this only
4342/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004343bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004344 unsigned Depth) const {
4345 if (*this == Dest) return true;
4346
4347 // Don't search too deeply, we just want to be able to see through
4348 // TokenFactor's etc.
4349 if (Depth == 0) return false;
4350
4351 // If this is a token factor, all inputs to the TF happen in parallel. If any
4352 // of the operands of the TF reach dest, then we can do the xform.
4353 if (getOpcode() == ISD::TokenFactor) {
4354 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4355 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4356 return true;
4357 return false;
4358 }
4359
4360 // Loads don't have side effects, look through them.
4361 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4362 if (!Ld->isVolatile())
4363 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4364 }
4365 return false;
4366}
4367
4368
Evan Chengc5fc57d2006-11-03 03:05:24 +00004369static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004370 SmallPtrSet<SDNode *, 32> &Visited) {
4371 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004372 return;
4373
4374 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4375 SDNode *Op = N->getOperand(i).Val;
4376 if (Op == P) {
4377 found = true;
4378 return;
4379 }
4380 findPredecessor(Op, P, found, Visited);
4381 }
4382}
4383
Evan Cheng917be682008-03-04 00:41:45 +00004384/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004385/// is either an operand of N or it can be reached by recursively traversing
4386/// up the operands.
4387/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004388bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004389 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004390 bool found = false;
4391 findPredecessor(N, this, found, Visited);
4392 return found;
4393}
4394
Evan Chengc5484282006-10-04 00:56:09 +00004395uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4396 assert(Num < NumOperands && "Invalid child # of SDNode!");
4397 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4398}
4399
Reid Spencer577cc322007-04-01 07:32:19 +00004400std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004401 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004402 default:
4403 if (getOpcode() < ISD::BUILTIN_OP_END)
4404 return "<<Unknown DAG Node>>";
4405 else {
Evan Cheng72261582005-12-20 06:22:03 +00004406 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004407 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004408 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004409 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004410
Evan Cheng72261582005-12-20 06:22:03 +00004411 TargetLowering &TLI = G->getTargetLoweringInfo();
4412 const char *Name =
4413 TLI.getTargetNodeName(getOpcode());
4414 if (Name) return Name;
4415 }
4416
4417 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004418 }
4419
Evan Cheng27b7db52008-03-08 00:58:38 +00004420 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004421 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004422 case ISD::ATOMIC_LCS: return "AtomicLCS";
4423 case ISD::ATOMIC_LAS: return "AtomicLAS";
Mon P Wang63307c32008-05-05 19:05:59 +00004424 case ISD::ATOMIC_LSS: return "AtomicLSS";
4425 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4426 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4427 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
4428 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4429 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4430 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4431 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4432 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004433 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004434 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004435 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004436 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004437 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004438 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004439 case ISD::AssertSext: return "AssertSext";
4440 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004441
4442 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004443 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004444 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004445 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004446 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004447
4448 case ISD::Constant: return "Constant";
4449 case ISD::ConstantFP: return "ConstantFP";
4450 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004451 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004452 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004453 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004454 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004455 case ISD::RETURNADDR: return "RETURNADDR";
4456 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004457 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004458 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4459 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004460 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004461 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004462 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004463 case ISD::INTRINSIC_WO_CHAIN: {
4464 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4465 return Intrinsic::getName((Intrinsic::ID)IID);
4466 }
4467 case ISD::INTRINSIC_VOID:
4468 case ISD::INTRINSIC_W_CHAIN: {
4469 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004470 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004471 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004472
Chris Lattnerb2827b02006-03-19 00:52:58 +00004473 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004474 case ISD::TargetConstant: return "TargetConstant";
4475 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004476 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004477 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004478 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004479 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004480 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004481 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004482
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004483 case ISD::CopyToReg: return "CopyToReg";
4484 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004485 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004486 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004487 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004488 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004489 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004490 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004491 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004492 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004493
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004494 // Unary operators
4495 case ISD::FABS: return "fabs";
4496 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004497 case ISD::FSQRT: return "fsqrt";
4498 case ISD::FSIN: return "fsin";
4499 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004500 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004501 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004502
4503 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004504 case ISD::ADD: return "add";
4505 case ISD::SUB: return "sub";
4506 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004507 case ISD::MULHU: return "mulhu";
4508 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004509 case ISD::SDIV: return "sdiv";
4510 case ISD::UDIV: return "udiv";
4511 case ISD::SREM: return "srem";
4512 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004513 case ISD::SMUL_LOHI: return "smul_lohi";
4514 case ISD::UMUL_LOHI: return "umul_lohi";
4515 case ISD::SDIVREM: return "sdivrem";
4516 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004517 case ISD::AND: return "and";
4518 case ISD::OR: return "or";
4519 case ISD::XOR: return "xor";
4520 case ISD::SHL: return "shl";
4521 case ISD::SRA: return "sra";
4522 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004523 case ISD::ROTL: return "rotl";
4524 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004525 case ISD::FADD: return "fadd";
4526 case ISD::FSUB: return "fsub";
4527 case ISD::FMUL: return "fmul";
4528 case ISD::FDIV: return "fdiv";
4529 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004530 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004531 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004532
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004533 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004534 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004535 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004536 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004537 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004538 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004539 case ISD::CONCAT_VECTORS: return "concat_vectors";
4540 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004541 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004542 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004543 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004544 case ISD::ADDC: return "addc";
4545 case ISD::ADDE: return "adde";
4546 case ISD::SUBC: return "subc";
4547 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004548 case ISD::SHL_PARTS: return "shl_parts";
4549 case ISD::SRA_PARTS: return "sra_parts";
4550 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004551
4552 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4553 case ISD::INSERT_SUBREG: return "insert_subreg";
4554
Chris Lattner7f644642005-04-28 21:44:03 +00004555 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004556 case ISD::SIGN_EXTEND: return "sign_extend";
4557 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004558 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004559 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004560 case ISD::TRUNCATE: return "truncate";
4561 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004562 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004563 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004564 case ISD::FP_EXTEND: return "fp_extend";
4565
4566 case ISD::SINT_TO_FP: return "sint_to_fp";
4567 case ISD::UINT_TO_FP: return "uint_to_fp";
4568 case ISD::FP_TO_SINT: return "fp_to_sint";
4569 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004570 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004571
4572 // Control flow instructions
4573 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004574 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004575 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004576 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004577 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004578 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004579 case ISD::CALLSEQ_START: return "callseq_start";
4580 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004581
4582 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004583 case ISD::LOAD: return "load";
4584 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004585 case ISD::VAARG: return "vaarg";
4586 case ISD::VACOPY: return "vacopy";
4587 case ISD::VAEND: return "vaend";
4588 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004589 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004590 case ISD::EXTRACT_ELEMENT: return "extract_element";
4591 case ISD::BUILD_PAIR: return "build_pair";
4592 case ISD::STACKSAVE: return "stacksave";
4593 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004594 case ISD::TRAP: return "trap";
4595
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004596 // Bit manipulation
4597 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004598 case ISD::CTPOP: return "ctpop";
4599 case ISD::CTTZ: return "cttz";
4600 case ISD::CTLZ: return "ctlz";
4601
Chris Lattner36ce6912005-11-29 06:21:05 +00004602 // Debug info
4603 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004604 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004605
Duncan Sands36397f52007-07-27 12:58:54 +00004606 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004607 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004608
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004609 case ISD::CONDCODE:
4610 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004611 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004612 case ISD::SETOEQ: return "setoeq";
4613 case ISD::SETOGT: return "setogt";
4614 case ISD::SETOGE: return "setoge";
4615 case ISD::SETOLT: return "setolt";
4616 case ISD::SETOLE: return "setole";
4617 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004618
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004619 case ISD::SETO: return "seto";
4620 case ISD::SETUO: return "setuo";
4621 case ISD::SETUEQ: return "setue";
4622 case ISD::SETUGT: return "setugt";
4623 case ISD::SETUGE: return "setuge";
4624 case ISD::SETULT: return "setult";
4625 case ISD::SETULE: return "setule";
4626 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004627
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004628 case ISD::SETEQ: return "seteq";
4629 case ISD::SETGT: return "setgt";
4630 case ISD::SETGE: return "setge";
4631 case ISD::SETLT: return "setlt";
4632 case ISD::SETLE: return "setle";
4633 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004634 }
4635 }
4636}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004637
Evan Cheng144d8f02006-11-09 17:55:04 +00004638const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004639 switch (AM) {
4640 default:
4641 return "";
4642 case ISD::PRE_INC:
4643 return "<pre-inc>";
4644 case ISD::PRE_DEC:
4645 return "<pre-dec>";
4646 case ISD::POST_INC:
4647 return "<post-inc>";
4648 case ISD::POST_DEC:
4649 return "<post-dec>";
4650 }
4651}
4652
Duncan Sands276dcbd2008-03-21 09:14:45 +00004653std::string ISD::ArgFlagsTy::getArgFlagsString() {
4654 std::string S = "< ";
4655
4656 if (isZExt())
4657 S += "zext ";
4658 if (isSExt())
4659 S += "sext ";
4660 if (isInReg())
4661 S += "inreg ";
4662 if (isSRet())
4663 S += "sret ";
4664 if (isByVal())
4665 S += "byval ";
4666 if (isNest())
4667 S += "nest ";
4668 if (getByValAlign())
4669 S += "byval-align:" + utostr(getByValAlign()) + " ";
4670 if (getOrigAlign())
4671 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4672 if (getByValSize())
4673 S += "byval-size:" + utostr(getByValSize()) + " ";
4674 return S + ">";
4675}
4676
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004677void SDNode::dump() const { dump(0); }
4678void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004679 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004680
4681 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004682 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004683 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004684 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004685 else
Bill Wendling832171c2006-12-07 20:04:42 +00004686 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004687 }
Bill Wendling832171c2006-12-07 20:04:42 +00004688 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004689
Bill Wendling832171c2006-12-07 20:04:42 +00004690 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004691 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004692 if (i) cerr << ", ";
4693 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004694 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004695 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004696 }
4697
Evan Chengce254432007-12-11 02:08:35 +00004698 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4699 SDNode *Mask = getOperand(2).Val;
4700 cerr << "<";
4701 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4702 if (i) cerr << ",";
4703 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4704 cerr << "u";
4705 else
4706 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4707 }
4708 cerr << ">";
4709 }
4710
Chris Lattnerc3aae252005-01-07 07:46:32 +00004711 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004712 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004713 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004714 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4715 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4716 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4717 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4718 else {
4719 cerr << "<APFloat(";
4720 CSDN->getValueAPF().convertToAPInt().dump();
4721 cerr << ")>";
4722 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004723 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004724 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004725 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004726 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004727 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004728 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004729 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004730 else
Bill Wendling832171c2006-12-07 20:04:42 +00004731 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004732 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004733 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004734 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004735 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004736 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004737 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004738 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004739 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004740 else
Bill Wendling832171c2006-12-07 20:04:42 +00004741 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004742 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004743 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004744 else
Bill Wendling832171c2006-12-07 20:04:42 +00004745 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004746 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004747 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004748 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4749 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004750 cerr << LBB->getName() << " ";
4751 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004752 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004753 if (G && R->getReg() &&
4754 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004755 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004756 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004757 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004758 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004759 } else if (const ExternalSymbolSDNode *ES =
4760 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004761 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004762 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4763 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004764 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004765 else
Dan Gohman69de1932008-02-06 22:27:42 +00004766 cerr << "<null>";
4767 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4768 if (M->MO.getValue())
4769 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4770 else
4771 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004772 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4773 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004774 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004775 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004776 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004777 const Value *SrcValue = LD->getSrcValue();
4778 int SrcOffset = LD->getSrcValueOffset();
4779 cerr << " <";
4780 if (SrcValue)
4781 cerr << SrcValue;
4782 else
4783 cerr << "null";
4784 cerr << ":" << SrcOffset << ">";
4785
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004786 bool doExt = true;
4787 switch (LD->getExtensionType()) {
4788 default: doExt = false; break;
4789 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004790 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004791 break;
4792 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004793 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004794 break;
4795 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004796 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004797 break;
4798 }
4799 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004800 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004801
Evan Cheng144d8f02006-11-09 17:55:04 +00004802 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004803 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004804 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004805 if (LD->isVolatile())
4806 cerr << " <volatile>";
4807 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004808 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004809 const Value *SrcValue = ST->getSrcValue();
4810 int SrcOffset = ST->getSrcValueOffset();
4811 cerr << " <";
4812 if (SrcValue)
4813 cerr << SrcValue;
4814 else
4815 cerr << "null";
4816 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004817
4818 if (ST->isTruncatingStore())
4819 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004820 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004821
4822 const char *AM = getIndexedModeName(ST->getAddressingMode());
4823 if (*AM)
4824 cerr << " " << AM;
4825 if (ST->isVolatile())
4826 cerr << " <volatile>";
4827 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004828 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004829}
4830
Chris Lattnerde202b32005-11-09 23:47:37 +00004831static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004832 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4833 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004834 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004835 else
Bill Wendling832171c2006-12-07 20:04:42 +00004836 cerr << "\n" << std::string(indent+2, ' ')
4837 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004838
Chris Lattnerea946cd2005-01-09 20:38:33 +00004839
Bill Wendling832171c2006-12-07 20:04:42 +00004840 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004841 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004842}
4843
Chris Lattnerc3aae252005-01-07 07:46:32 +00004844void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004845 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004846 std::vector<const SDNode*> Nodes;
4847 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4848 I != E; ++I)
4849 Nodes.push_back(I);
4850
Chris Lattner49d24712005-01-09 20:26:36 +00004851 std::sort(Nodes.begin(), Nodes.end());
4852
4853 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004854 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004855 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004856 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004857
Jim Laskey26f7fa72006-10-17 19:33:52 +00004858 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004859
Bill Wendling832171c2006-12-07 20:04:42 +00004860 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004861}
4862
Evan Chengd6594ae2006-09-12 21:00:35 +00004863const Type *ConstantPoolSDNode::getType() const {
4864 if (isMachineConstantPoolEntry())
4865 return Val.MachineCPVal->getType();
4866 return Val.ConstVal->getType();
4867}